QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#120139 | #6187. Digit Sum Problem | hos_lyric | AC ✓ | 594ms | 79368kb | C++14 | 8.1kb | 2023-07-06 14:12:16 | 2023-07-06 14:12:17 |
Judging History
answer
#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 <map>
#include <numeric>
#include <queue>
#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; }
////////////////////////////////////////////////////////////////////////////////
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>;
int main() {
Int N;
Mint A, B[2];
for (; ~scanf("%lld%u%u%u", &N, &A.x, &B[0].x, &B[1].x); ) {
const Int sqrtN = sqrt((long double)N);
Int m[2] = {1, 1};
m[0] = m[1] = 1;
// [0][0], [1][1]: offset of the other
// [0][1], [1][0]: length
vector<Mint> f[2][2];
f[0][0] = f[1][1] = {1};
f[0][1] = f[1][0] = {0, 1};
for (; ; ) {
cerr<<"m = ";pv(m,m+2);
#ifdef LOCAL_
for(int s=0;s<2;++s)for(int t=0;t<2;++t)cerr<<"f["<<s<<"]["<<t<<"] = "<<f[s][t]<<endl;
auto brute=[&](Int r0,Int r1,Int len)->Mint{
Mint ret=0;
Mint a=1;
for(Int i=0;i<len;++i){
Mint prod=a;
for(Int x=(r0+i)%m[0];x;x/=2){if(x%2>=1)prod*=B[0];}
for(Int x=(r1+i)%m[1];x;x/=3){if(x%3>=1)prod*=B[1];if(x%3>=2)prod*=B[1];}
ret+=prod;
a*=A;
}
// cerr<<" brute "<<r0<<" "<<r1<<" "<<len<<" = "<<ret<<endl;
return ret;
};
for(int i=0;i<m[1];++i)assert(brute(0,i,m[0])==f[0][0][i]);
for(int i=0;i<m[0];++i)assert(brute(i,0,m[1])==f[1][1][i]);
for(int i=0;i<=min(m[0],m[1]);++i)assert(brute(0,m[1]-i,i)==f[0][1][i]);
for(int i=0;i<=min(m[0],m[1]);++i)assert(brute(m[0]-i,0,i)==f[1][0][i]);
#endif
int s, t;
Int mm;
if (m[0] <= m[1]) {
s = 0;
t = 1;
mm = 2 * m[0];
} else {
s = 1;
t = 0;
mm = 3 * m[1];
}
if (mm > sqrtN) {
break;
}
vector<Mint> as(min(mm, m[t]) + 1);
as[0] = 1;
for (int i = 0; i < min(mm, m[t]); ++i) as[i + 1] = as[i] * A;
const Mint bs[3] = {1, B[s], B[s] * B[s]};
auto calc = [&](Int Rs, Int rt, Int len) -> Mint {
Mint ret = 0;
Mint a = 1;
Int rs = Rs % m[s];
for (; len > 0; ) {
const Int d = min(m[s] - rs, m[t] - rt);
const Int rrs = ((rs + d == m[s]) ? 0 : (rs + d));
const Int rrt = ((rt + d == m[t]) ? 0 : (rt + d));
assert(rs == 0 || rt == 0);
assert(rrs == 0 || rrt == 0);
ret += a * bs[Rs / m[s]] * (
(rs == 0)
? ((rrs == 0) ? f[s][s][rt] : f[s][t][d])
: ((rrs == 0) ? f[t][s][d] : f[t][t][rs])
);
Rs = ((Rs + d) == mm) ? 0 : (Rs + d);
rs = rrs;
rt = rrt;
len -= d;
a *= as[d];
}
return ret;
};
vector<Mint> g[2][2];
g[s][s].assign(m[t], 0);
g[t][t].assign(mm, 0);
g[s][t].assign(min(mm, m[t]) + 1, 0);
g[t][s].assign(min(mm, m[t]) + 1, 0);
for (int i = 0; i < m[t]; ++i) g[s][s][i] = calc(0, i, mm);
for (int i = 0; i < mm; ++i) g[t][t][i] = calc(i, 0, m[t]);
for (int i = 0; i <= min(mm, m[t]); ++i) g[s][t][i] = calc(0, m[t] - i, i);
for (int i = 0; i <= min(mm, m[t]); ++i) g[t][s][i] = calc(mm - i, 0, i);
m[s] = mm;
for (int ss = 0; ss < 2; ++ss) for (int tt = 0; tt < 2; ++tt) {
f[ss][tt] = g[ss][tt];
}
}
vector<Mint> as(min(m[0], m[1]) + 1);
as[0] = 1;
for (int i = 0; i < min(m[0], m[1]); ++i) as[i + 1] = as[i] * A;
Int lim[2];
vector<Mint> bs[2];
for (int s = 0; s < 2; ++s) {
lim[s] = max(m[s], N / m[s] + 1);
bs[s].resize(lim[s]);
bs[s][0] = 1;
for (int i = 1; i < lim[s]; ++i) {
bs[s][i] = bs[s][i / ((s == 0) ? 2 : 3)];
if (s == 0 && i % 2 >= 1) bs[s][i] *= B[s];
if (s == 1 && i % 3 >= 1) bs[s][i] *= B[s];
if (s == 1 && i % 3 >= 2) bs[s][i] *= B[s];
}
}
Mint ans = 0;
Mint a = 1;
for (Int n = 0; n < N + 1; ) {
const Int d = min(m[0] - n % m[0], m[1] - n % m[1]);
if (n + d <= N + 1) {
Mint low;
if (n % m[0] == 0) {
if ((n + d) % m[0] == 0) {
low = f[0][0][n % m[1]];
} else {
low = f[0][1][d];
}
} else {
if ((n + d) % m[0] == 0) {
low = f[1][0][d];
} else {
low = f[1][1][n % m[0]];
}
}
ans += a * bs[0][n / m[0]] * bs[1][n / m[1]] * low;
n += d;
a *= as[d];
} else {
for (; n < N + 1; ++n) {
ans += a * bs[0][n / m[0]] * bs[0][n % m[0]] * bs[1][n / m[1]] * bs[1][n % m[1]];
a *= A;
}
}
}
// n = 0
ans -= 1;
printf("%u\n", ans.x);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3660kb
input:
123456 12345 234567 3456789
output:
664963464
result:
ok 1 number(s): "664963464"
Test #2:
score: 0
Accepted
time: 589ms
memory: 79204kb
input:
9876543210987 12816 837595 128478
output:
7972694
result:
ok 1 number(s): "7972694"
Test #3:
score: 0
Accepted
time: 551ms
memory: 77540kb
input:
9196665971964 727160879 966835565 746444639
output:
949890012
result:
ok 1 number(s): "949890012"
Test #4:
score: 0
Accepted
time: 556ms
memory: 77560kb
input:
9361549073598 749653880 965489817 371100607
output:
949904373
result:
ok 1 number(s): "949904373"
Test #5:
score: 0
Accepted
time: 560ms
memory: 77604kb
input:
9567572694963 193332930 544713669 390021151
output:
878781872
result:
ok 1 number(s): "878781872"
Test #6:
score: 0
Accepted
time: 585ms
memory: 78660kb
input:
9769301349033 215825931 425927410 408941695
output:
351574791
result:
ok 1 number(s): "351574791"
Test #7:
score: 0
Accepted
time: 594ms
memory: 79368kb
input:
9975324970399 657749333 5151262 729852127
output:
400022780
result:
ok 1 number(s): "400022780"
Test #8:
score: 0
Accepted
time: 1ms
memory: 3636kb
input:
1 149762920 266126484 107367523
output:
910371791
result:
ok 1 number(s): "910371791"
Test #9:
score: 0
Accepted
time: 156ms
memory: 34984kb
input:
903900971479 969144397 356713678 36786741
output:
414279957
result:
ok 1 number(s): "414279957"
Test #10:
score: 0
Accepted
time: 237ms
memory: 44376kb
input:
1940757501452 72683414 106545701 263512239
output:
786323834
result:
ok 1 number(s): "786323834"
Test #11:
score: 0
Accepted
time: 333ms
memory: 50584kb
input:
2914414844884 174466783 133201789 792227626
output:
187534312
result:
ok 1 number(s): "187534312"
Test #12:
score: 0
Accepted
time: 350ms
memory: 62080kb
input:
3851250038782 553074217 881278164 297532837
output:
847958862
result:
ok 1 number(s): "847958862"
Test #13:
score: 0
Accepted
time: 457ms
memory: 77572kb
input:
4692374803740 352867698 211679787 826248223
output:
426334178
result:
ok 1 number(s): "426334178"
Test #14:
score: 0
Accepted
time: 494ms
memory: 77548kb
input:
5566041306806 454651067 959756163 633543322
output:
842296050
result:
ok 1 number(s): "842296050"
Test #15:
score: 0
Accepted
time: 513ms
memory: 77568kb
input:
6902869060611 556434437 709588186 860268821
output:
897681255
result:
ok 1 number(s): "897681255"
Test #16:
score: 0
Accepted
time: 512ms
memory: 77604kb
input:
7239695301792 356227918 736244273 667563920
output:
726280051
result:
ok 1 number(s): "726280051"
Test #17:
score: 0
Accepted
time: 549ms
memory: 77448kb
input:
8217660029470 458011287 486076297 198034954
output:
967159922
result:
ok 1 number(s): "967159922"