QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#93890 | #6187. Digit Sum Problem | whatever# | AC ✓ | 796ms | 217828kb | C++17 | 3.5kb | 2023-04-03 16:18:52 | 2023-04-03 16:18:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
inline void add(int &x, int y) {
(x += y) >= P && (x -= P);
}
inline int Add(int x, int y) {
return add(x, y), x;
}
inline void sub(int &x, int y) {
(x -= y) < 0 && (x += P);
}
inline int Sub(int x, int y) {
return sub(x, y), x;
}
inline void mul(int &x, int y) {
x = 1ull * x * y % P;
}
inline int Mul(int x, int y) {
return 1ull * x * y % P;
}
int power(int a, int b) {
int res = 1;
for (; b; b >>= 1) {
if (b & 1) {
mul(res, a);
}
mul(a, a);
}
return res;
}
using ull = unsigned long long;
using poly = vector<int>;
const int L = 1 << 21;
int Omgs[L], rev[L];
void set_up() {
int Omg = power(3, (P - 1) / L);
Omgs[L >> 1] = 1;
for (int i = (L >> 1) + 1; i < L; ++i) {
Omgs[i] = Mul(Omgs[i - 1], Omg);
}
for (int i = (L >> 1) - 1; i; --i) {
Omgs[i] = Omgs[i << 1];
}
}
int extend(int len) {
int n = 1;
while (n < len) {
n <<= 1;
}
for (int i = 0; i < n; ++i) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? (n >> 1) : 0);
}
return n;
}
void dft(ull *a, int n) {
for (int i = 0; i < n; ++i) {
if (i < rev[i]) {
swap(a[i], a[rev[i]]);
}
}
for (int k = 1; k < n; k <<= 1) {
if (k == 1 << 18) {
for (int i = 0; i < n; ++i) {
a[i] %= P;
}
}
for (int i = 0; i < n; i += k << 1) {
ull *p = a + i, *q = a + i + k;
int *w = Omgs + k;
for (int j = 0; j < k; ++j, ++p, ++q, ++w) {
ull tmp = *q * *w % P;
*q = *p + P - tmp;
*p += tmp;
}
}
}
for (int i = 0; i < n; ++i) {
a[i] %= P;
}
}
poly operator*(const poly &a, const poly &b) {
static ull f[L], g[L];
int len = a.size() + b.size() - 1;
int n = extend(len);
for (int i = 0; i < n; ++i) {
f[i] = i < (int) a.size() ? a[i] : 0;
g[i] = i < (int) b.size() ? b[i] : 0;
}
dft(f, n);
dft(g, n);
for (int i = 0; i < n; ++i) {
f[i] = Mul(f[i], g[i]);
}
reverse(f + 1, f + n);
dft(f, n);
int coef = power(n, P - 2);
poly c(len);
for (int i = 0; i < len; ++i) {
c[i] = Mul(f[i], coef);
}
return c;
}
const int N = 531441, M = 18816780;
long long n;
int x, y, z, px[M], py[70], coef[M];
int main() {
set_up();
cin >> n >> x >> y >> z;
int m = n / N;
int x1 = power(x, N), z1 = Mul(z, z);
px[0] = 1;
for (int i = 1; i < m; ++i) {
px[i] = Mul(px[i - 1], x1);
}
py[0] = 1;
for (int i = 1; i < 70; ++i) {
py[i] = Mul(py[i - 1], y);
}
coef[0] = 1;
for (int i = 1; i < M; ++i) {
if (i % 3 == 0) {
coef[i] = coef[i / 3];
} else {
coef[i] = Mul(coef[i / 3], (i % 3 == 1) ? z : z1);
}
}
poly f0(1 << 20), f1(1 << 20);
for (int i = 0; i < m; ++i) {
long long x = 1ll * i * N;
int val = Mul(px[i], coef[i]);
int high = x >> 20, low = x & ((1 << 20) - 1);
add(f0[low], Mul(val, py[__builtin_popcount(high)]));
add(f1[low], Mul(val, py[__builtin_popcount(high + 1)]));
}
poly g(N);
int pw = 1;
for (int i = 0; i < N; ++i) {
g[i] = Mul(pw, coef[i]);
mul(pw, x);
}
f0 = f0 * g;
f1 = f1 * g;
int ans = 0;
for (int i = 0; i < 1 << 20; ++i) {
int coef = py[__builtin_popcount(i)];
add(ans, Mul(f0[i], coef));
if (i < N - 1) {
add(ans, Mul(f1[i + (1 << 20)], coef));
}
}
long long cur = 1ll * m * N;
int p1 = power(x1, m);
for (int i = 0; cur <= n; ++cur, ++i) {
int p2 = py[__builtin_popcountll(cur)] % P;
int p3 = Mul(coef[m], coef[i]);
add(ans, Mul(p1, Mul(p2, p3)));
mul(p1, x);
}
sub(ans, 1);
cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 443ms
memory: 148052kb
input:
123456 12345 234567 3456789
output:
664963464
result:
ok 1 number(s): "664963464"
Test #2:
score: 0
Accepted
time: 796ms
memory: 217736kb
input:
9876543210987 12816 837595 128478
output:
7972694
result:
ok 1 number(s): "7972694"
Test #3:
score: 0
Accepted
time: 741ms
memory: 213532kb
input:
9196665971964 727160879 966835565 746444639
output:
949890012
result:
ok 1 number(s): "949890012"
Test #4:
score: 0
Accepted
time: 758ms
memory: 213624kb
input:
9361549073598 749653880 965489817 371100607
output:
949904373
result:
ok 1 number(s): "949904373"
Test #5:
score: 0
Accepted
time: 729ms
memory: 217824kb
input:
9567572694963 193332930 544713669 390021151
output:
878781872
result:
ok 1 number(s): "878781872"
Test #6:
score: 0
Accepted
time: 756ms
memory: 217828kb
input:
9769301349033 215825931 425927410 408941695
output:
351574791
result:
ok 1 number(s): "351574791"
Test #7:
score: 0
Accepted
time: 756ms
memory: 217760kb
input:
9975324970399 657749333 5151262 729852127
output:
400022780
result:
ok 1 number(s): "400022780"
Test #8:
score: 0
Accepted
time: 443ms
memory: 147944kb
input:
1 149762920 266126484 107367523
output:
910371791
result:
ok 1 number(s): "910371791"
Test #9:
score: 0
Accepted
time: 466ms
memory: 154200kb
input:
903900971479 969144397 356713678 36786741
output:
414279957
result:
ok 1 number(s): "414279957"
Test #10:
score: 0
Accepted
time: 503ms
memory: 160272kb
input:
1940757501452 72683414 106545701 263512239
output:
786323834
result:
ok 1 number(s): "786323834"
Test #11:
score: 0
Accepted
time: 535ms
memory: 166632kb
input:
2914414844884 174466783 133201789 792227626
output:
187534312
result:
ok 1 number(s): "187534312"
Test #12:
score: 0
Accepted
time: 552ms
memory: 174672kb
input:
3851250038782 553074217 881278164 297532837
output:
847958862
result:
ok 1 number(s): "847958862"
Test #13:
score: 0
Accepted
time: 572ms
memory: 180900kb
input:
4692374803740 352867698 211679787 826248223
output:
426334178
result:
ok 1 number(s): "426334178"
Test #14:
score: 0
Accepted
time: 674ms
memory: 187092kb
input:
5566041306806 454651067 959756163 633543322
output:
842296050
result:
ok 1 number(s): "842296050"
Test #15:
score: 0
Accepted
time: 787ms
memory: 197240kb
input:
6902869060611 556434437 709588186 860268821
output:
897681255
result:
ok 1 number(s): "897681255"
Test #16:
score: 0
Accepted
time: 717ms
memory: 199392kb
input:
7239695301792 356227918 736244273 667563920
output:
726280051
result:
ok 1 number(s): "726280051"
Test #17:
score: 0
Accepted
time: 745ms
memory: 205456kb
input:
8217660029470 458011287 486076297 198034954
output:
967159922
result:
ok 1 number(s): "967159922"