QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#319684 | #4056. 进制转换 | hos_lyric | 100 ✓ | 605ms | 179744kb | C++14 | 6.8kb | 2024-02-03 02:31:27 | 2024-02-03 02:31:27 |
Judging History
answer
// virtual after solving this as ucup, sorry (this code is written during virtual)
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;
constexpr int E = 50;
constexpr int F = 30;
Int two[E + 1], thr[F + 1];
int calc2(Int n) {
int ret = 0;
for (; n; n >>= 1) ret += (n & 1);
return ret;
}
int calc3(Int n) {
int ret = 0;
for (; n; n /= 3) ret += n % 3;
return ret;
}
Int N;
Mint X, Y, Z;
int main() {
two[0] = thr[0] = 1;
for (int e = 1; e <= E; ++e) two[e] = two[e - 1] * 2;
for (int f = 1; f <= F; ++f) thr[f] = thr[f - 1] * 3;
for (; ~scanf("%lld%u%u%u", &N, &X.x, &Y.x, &Z.x); ) {
++N;
vector<Mint> YY(E + 1), ZZ(2 * F + 1);
YY[0] = ZZ[0] = 1;
for (int e = 1; e <= E; ++e) YY[e] = YY[e - 1] * Y;
for (int f = 1; f <= 2 * F; ++f) ZZ[f] = ZZ[f - 1] * Z;
int e = 0, f = 0;
vector<Int> ns{0, 1};
vector<Mint> ws{1};
for (; !(N < two[e] * thr[f]); ) {
const int len = ws.size();
vector<Int> nns{0};
vector<Mint> wws;
int ee = e, ff = f;
auto add = [&](Int l, Int r, Mint w) -> void {
// cerr<<" [add] "<<l<<" "<<r<<" "<<w<<endl;
assert(nns.back() == l);
if (l % two[ee] != 0 && l % thr[ff] != 0) {
nns.back() = r;
wws.back() += w;
} else {
nns.push_back(r);
wws.push_back(w);
}
};
if (two[e] <= thr[f]) {
++ee;
for (int a = 0; a < 2; ++a) {
const Int shift = two[e] * thr[f] * a;
const Mint x = X.pow(shift);
for (int i = 0; i < len; ++i) {
add(shift + ns[i], shift + ns[i + 1], x * YY[(shift + ns[i]) / two[e] % 2] * ws[i]);
}
}
} else {
++ff;
for (int b = 0; b < 3; ++b) {
const Int shift = two[e] * thr[f] * b;
const Mint x = X.pow(shift);
for (int i = 0; i < len; ++i) {
add(shift + ns[i], shift + ns[i + 1], x * ZZ[(shift + ns[i]) / thr[f] % 3] * ws[i]);
}
}
}
e = ee;
f = ff;
ns.swap(nns);
ws.swap(wws);
cerr<<"e="<<e<<" f="<<f<<" "<<two[e]<<" "<<thr[f]<<" "<<ws.size()<<endl;
// cerr<<ns<<endl<<ws<<endl;
}
const int len = ws.size();
Mint ans = 0;
for (int i = 0; i < len; ++i) {
if (ns[i + 1] <= N) {
ans += YY[calc2(ns[i] / two[e])] * ZZ[calc3(ns[i] / thr[f])] * ws[i];
} else {
Mint x = X.pow(ns[i]);
for (Int n = ns[i]; n < N; ++n) {
ans += x * YY[calc2(n)] * ZZ[calc3(n)];
x *= X;
}
break;
}
}
ans -= 1;
printf("%u\n", ans.x);
#ifdef LOCAL
if(N<=1'000'000){
Mint brt=0;
for(Int n=1;n<N;++n)brt+=X.pow(n)*Y.pow(calc2(n))*Z.pow(calc3(n));
if(brt!=ans){
cerr<<"FAIL N="<<N<<" "<<X<<" "<<Y<<" "<<Z<<": "<<brt<<" "<<ans<<endl;
if(N<=100)for(Int n=0;n<two[e]*thr[f];++n)cerr<<n<<": "<<X.pow(n)<<" "<<Y.pow(calc2(n))<<" "<<Z.pow(calc3(n))<<": "<<X.pow(n)*Y.pow(calc2(n))*Z.pow(calc3(n))<<endl;
}
assert(brt==ans);
}
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 0ms
memory: 4336kb
input:
9134097 778012792 263448561 839843856
output:
887680205
result:
ok 1 number(s): "887680205"
Test #2:
score: 5
Accepted
time: 1ms
memory: 4068kb
input:
9896386 2948513 263418583 271155379
output:
853292631
result:
ok 1 number(s): "853292631"
Test #3:
score: 5
Accepted
time: 1ms
memory: 4072kb
input:
9150910 827328107 842171962 39947937
output:
534921610
result:
ok 1 number(s): "534921610"
Test #4:
score: 5
Accepted
time: 564ms
memory: 179120kb
input:
9586674634211 1 1 58301262
output:
13306748
result:
ok 1 number(s): "13306748"
Test #5:
score: 5
Accepted
time: 593ms
memory: 178856kb
input:
9774917720998 1 1 609549524
output:
825025220
result:
ok 1 number(s): "825025220"
Test #6:
score: 5
Accepted
time: 605ms
memory: 178516kb
input:
9765239207265 422503033 1 719749187
output:
993518920
result:
ok 1 number(s): "993518920"
Test #7:
score: 5
Accepted
time: 513ms
memory: 178444kb
input:
9732354736444 277693641 1 501293609
output:
77844778
result:
ok 1 number(s): "77844778"
Test #8:
score: 5
Accepted
time: 17ms
memory: 9192kb
input:
9004409828 377918953 449219487 26422407
output:
110868569
result:
ok 1 number(s): "110868569"
Test #9:
score: 5
Accepted
time: 13ms
memory: 9312kb
input:
9579878149 820453354 218842704 133154415
output:
727248713
result:
ok 1 number(s): "727248713"
Test #10:
score: 5
Accepted
time: 14ms
memory: 9168kb
input:
9475807443 305433821 391589421 170059051
output:
372839725
result:
ok 1 number(s): "372839725"
Test #11:
score: 5
Accepted
time: 120ms
memory: 56644kb
input:
484758270277 372146623 410538257 35340632
output:
575284574
result:
ok 1 number(s): "575284574"
Test #12:
score: 5
Accepted
time: 107ms
memory: 53936kb
input:
473458173541 864158404 220259603 529747800
output:
610487662
result:
ok 1 number(s): "610487662"
Test #13:
score: 5
Accepted
time: 121ms
memory: 55100kb
input:
459992983903 359742981 983942229 552405867
output:
81366483
result:
ok 1 number(s): "81366483"
Test #14:
score: 5
Accepted
time: 112ms
memory: 56540kb
input:
462331701308 665849375 563297194 141092054
output:
774987426
result:
ok 1 number(s): "774987426"
Test #15:
score: 5
Accepted
time: 493ms
memory: 179744kb
input:
9061635042931 746632077 302662913 559990819
output:
274721229
result:
ok 1 number(s): "274721229"
Test #16:
score: 5
Accepted
time: 543ms
memory: 178760kb
input:
9653325901537 559549569 638292572 474780356
output:
418906016
result:
ok 1 number(s): "418906016"
Test #17:
score: 5
Accepted
time: 547ms
memory: 178440kb
input:
9640271229478 619740479 644097590 907038757
output:
936646026
result:
ok 1 number(s): "936646026"
Test #18:
score: 5
Accepted
time: 526ms
memory: 178364kb
input:
9781711161203 988850684 154464719 995932763
output:
166841144
result:
ok 1 number(s): "166841144"
Test #19:
score: 5
Accepted
time: 494ms
memory: 179160kb
input:
9156325004698 915375188 316066096 217969045
output:
306877956
result:
ok 1 number(s): "306877956"
Test #20:
score: 5
Accepted
time: 590ms
memory: 179708kb
input:
9042535293051 906265264 156788435 622201740
output:
397975092
result:
ok 1 number(s): "397975092"