QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#142617 | #6187. Digit Sum Problem | SanguineChameleon | AC ✓ | 1301ms | 387904kb | C++20 | 4.1kb | 2023-08-19 14:15:41 | 2023-08-19 14:15:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxK = 22;
namespace NTT {
const long long mod = (119 << 23) + 1;
long long bin_pow(long long x, long long y) {
long long res = 1;
while (y) {
if (y & 1) {
res = res * x % mod;
}
y >>= 1;
x = x * x % mod;
}
return res;
}
long long mod_inv(long long x) {
return bin_pow(x, mod - 2);
}
int maxL;
int maxN;
long long root;
void fft(long long A[], bool inv) {
for (int i = 0; i < maxN; i++) {
int rev = 0;
for (int j = 0; j < maxL; j++) {
rev |= ((i >> j) & 1) << (maxL - 1 - j);
}
if (i < rev) {
swap(A[i], A[rev]);
}
}
for (int len = 2; len <= maxN; len <<= 1) {
long long mul = (inv ? mod_inv(root) : root);
for (int tmp = len; tmp < maxN; tmp <<= 1) {
mul = mul * mul % mod;
}
for (int i = 0; i < maxN; i += len) {
long long cur = 1;
for (int j = 0; j < (len >> 1); j++) {
long long X = A[i + j];
long long Y = A[i + j + (len >> 1)];
A[i + j] = (X + cur * Y) % mod;
A[i + j + (len >> 1)] = (X + (mod - cur) * Y) % mod;
cur = cur * mul % mod;
}
}
}
if (inv) {
long long mul = mod_inv(maxN);
for (int i = 0; i < maxN; i++) {
A[i] = A[i] * mul % mod;
}
}
}
long long A[1 << maxK];
long long B[1 << maxK];
void mul(long long _A[], long long _B[], long long C[], int _maxL) {
maxL = _maxL;
maxN = (1 << maxL);
root = bin_pow(bin_pow(3, 119), 1 << (23 - maxL));
for (int i = 0; i < maxN; i++) {
A[i] = _A[i];
B[i] = _B[i];
}
fft(A, false);
fft(B, false);
for (int i = 0; i < maxN; i++) {
C[i] = A[i] * B[i] % mod;
}
fft(C, true);
}
};
const long long mod = 998244353;
const long long maxN = 1e13 + 20;
const int maxL = 50;
const int block_size = 531441;
const int max_block_cnt = max(block_size, (int)(maxN / block_size)) + 20;
long long pwA_small[block_size];
long long pwA_big[max_block_cnt];
long long ternary[max_block_cnt];
long long pwB[maxL];
long long big[2][1 << maxK];
long long small[1 << maxK];
long long prod[2][1 << maxK];
void just_do_it() {
long long N, A, B, C;
cin >> N >> A >> B >> C;
pwB[0] = 1;
for (int i = 1; i < maxL; i++) {
pwB[i] = pwB[i - 1] * B % mod;
}
pwA_small[0] = 1;
for (int i = 1; i < block_size; i++) {
pwA_small[i] = pwA_small[i - 1] * A % mod;
}
pwA_big[0] = 1;
pwA_big[1] = pwA_small[block_size - 1] * A % mod;
for (int i = 2; i < max_block_cnt; i++) {
pwA_big[i] = pwA_big[i - 1] * pwA_big[1] % mod;
}
ternary[0] = 1;
for (int i = 1; i < max_block_cnt; i++) {
ternary[i] = ternary[i / 3];
for (int j = 0; j < i % 3; j++) {
ternary[i] = ternary[i] * C % mod;
}
}
int block_cnt = (N + 1) / block_size;
int K = 0;
while ((1 << K) < block_size) {
K++;
}
for (int i = 0; i < block_cnt; i++) {
long long pref = 1LL * i * block_size;
int low = (pref & ((1 << K) - 1));
long long high = (pref >> K);
big[0][low] = (big[0][low] + pwA_big[i] * ternary[i] % mod * pwB[__builtin_popcountll(high)]) % mod;
big[1][low] = (big[1][low] + pwA_big[i] * ternary[i] % mod * pwB[__builtin_popcountll(high + 1)]) % mod;
}
for (int i = 0; i < block_size; i++) {
small[i] = (small[i] + pwA_small[i] * ternary[i]) % mod;
}
NTT::mul(big[0], small, prod[0], K + 1);
NTT::mul(big[1], small, prod[1], K + 1);
long long res = 0;
for (int i = 0; i < (1 << K); i++) {
res = (res + pwB[__builtin_popcount(i)] * prod[0][i]) % mod;
res = (res + pwB[__builtin_popcount(i)] * prod[1][i + (1 << K)]) % mod;
}
for (long long i = 1LL * block_cnt * block_size; i <= N; i++) {
res = (res + pwA_big[block_cnt] * pwA_small[i % block_size] % mod * pwB[__builtin_popcountll(i)] % mod * ternary[block_cnt] % mod * ternary[i % block_size]) % mod;
}
res = (res + mod - 1) % mod;
cout << res;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 943ms
memory: 371508kb
input:
123456 12345 234567 3456789
output:
664963464
result:
ok 1 number(s): "664963464"
Test #2:
score: 0
Accepted
time: 1282ms
memory: 387872kb
input:
9876543210987 12816 837595 128478
output:
7972694
result:
ok 1 number(s): "7972694"
Test #3:
score: 0
Accepted
time: 1213ms
memory: 387692kb
input:
9196665971964 727160879 966835565 746444639
output:
949890012
result:
ok 1 number(s): "949890012"
Test #4:
score: 0
Accepted
time: 1242ms
memory: 387756kb
input:
9361549073598 749653880 965489817 371100607
output:
949904373
result:
ok 1 number(s): "949904373"
Test #5:
score: 0
Accepted
time: 1301ms
memory: 387904kb
input:
9567572694963 193332930 544713669 390021151
output:
878781872
result:
ok 1 number(s): "878781872"
Test #6:
score: 0
Accepted
time: 1220ms
memory: 387852kb
input:
9769301349033 215825931 425927410 408941695
output:
351574791
result:
ok 1 number(s): "351574791"
Test #7:
score: 0
Accepted
time: 1257ms
memory: 387732kb
input:
9975324970399 657749333 5151262 729852127
output:
400022780
result:
ok 1 number(s): "400022780"
Test #8:
score: 0
Accepted
time: 948ms
memory: 371240kb
input:
1 149762920 266126484 107367523
output:
910371791
result:
ok 1 number(s): "910371791"
Test #9:
score: 0
Accepted
time: 1015ms
memory: 387804kb
input:
903900971479 969144397 356713678 36786741
output:
414279957
result:
ok 1 number(s): "414279957"
Test #10:
score: 0
Accepted
time: 1064ms
memory: 387692kb
input:
1940757501452 72683414 106545701 263512239
output:
786323834
result:
ok 1 number(s): "786323834"
Test #11:
score: 0
Accepted
time: 1083ms
memory: 387900kb
input:
2914414844884 174466783 133201789 792227626
output:
187534312
result:
ok 1 number(s): "187534312"
Test #12:
score: 0
Accepted
time: 1098ms
memory: 387852kb
input:
3851250038782 553074217 881278164 297532837
output:
847958862
result:
ok 1 number(s): "847958862"
Test #13:
score: 0
Accepted
time: 1158ms
memory: 387728kb
input:
4692374803740 352867698 211679787 826248223
output:
426334178
result:
ok 1 number(s): "426334178"
Test #14:
score: 0
Accepted
time: 1144ms
memory: 387760kb
input:
5566041306806 454651067 959756163 633543322
output:
842296050
result:
ok 1 number(s): "842296050"
Test #15:
score: 0
Accepted
time: 1153ms
memory: 387732kb
input:
6902869060611 556434437 709588186 860268821
output:
897681255
result:
ok 1 number(s): "897681255"
Test #16:
score: 0
Accepted
time: 1203ms
memory: 387648kb
input:
7239695301792 356227918 736244273 667563920
output:
726280051
result:
ok 1 number(s): "726280051"
Test #17:
score: 0
Accepted
time: 1209ms
memory: 387688kb
input:
8217660029470 458011287 486076297 198034954
output:
967159922
result:
ok 1 number(s): "967159922"