QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#121932 | #6187. Digit Sum Problem | UndertrainedOverpressure# | AC ✓ | 1841ms | 66736kb | C++23 | 5.2kb | 2023-07-09 00:53:48 | 2023-07-09 00:53:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define TIME (clock() * 1.0 / CLOCKS_PER_SEC)
// если модуль подается на вход, убрать все <> и раскомментировать нужные строки
using uint = unsigned int;
using ull = unsigned long long;
template <uint MD> struct ModInt {
using M = ModInt;
// static int MD;
uint v;
ModInt(ll _v = 0) { set_v(uint(_v % MD + MD)); }
M& set_v(uint _v) {
v = (_v < MD) ? _v : _v - MD;
return *this;
}
explicit operator bool() const { return v != 0; }
M operator-() const { return M() - *this; }
M operator+(const M& r) const { return M().set_v(v + r.v); }
M operator-(const M& r) const { return M().set_v(v + MD - r.v); }
M operator*(const M& r) const { return M().set_v(uint((ull)v * r.v % MD)); }
M operator/(const M& r) const { return *this * r.inv(); }
M& operator+=(const M& r) { return *this = *this + r; }
M& operator-=(const M& r) { return *this = *this - r; }
M& operator*=(const M& r) { return *this = *this * r; }
M& operator/=(const M& r) { return *this = *this / r; }
bool operator==(const M& r) const { return v == r.v; }
bool operator!=(const M& r) const { return v != r.v; }
M inv() const;
friend istream& operator>>(istream& is, M& r) { ll x; is >> x; r = M(x); return is; }
friend ostream& operator<<(ostream& os, const M& r) { return os << r.v; }
};
template<uint MD>
ModInt<MD> pow(ModInt<MD> x, ll n) {
ModInt<MD> r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
template<uint MD>
ModInt<MD> ModInt<MD>::inv() const { return pow(*this, MD - 2); }
// or copy egcd and {return egcd(MD, v, 1).second;}
// if MD is from input
// this line is necessary, read later as you wish
// int ModInt::MD;
using Mint = ModInt<998244353>;
// using Mint = double;
#define V vector
void nft(bool type, V<Mint>& a) {
Mint G = 3;
int n = int(a.size()), s = 0;
while ((1 << s) < n) s++;
assert(1 << s == n);
static V<Mint> ep, iep;
while (int(ep.size()) <= s) {
ep.push_back(pow(G, Mint(-1).v / (1 << ep.size())));
iep.push_back(ep.back().inv());
}
V<Mint> b(n);
for (int i = 1; i <= s; i++) {
int w = 1 << (s - i);
Mint base = type ? iep[i] : ep[i], now = 1;
for (int y = 0; y < n / 2; y += w) {
for (int x = 0; x < w; x++) {
auto l = a[y << 1 | x];
auto r = now * a[y << 1 | x | w];
b[y | x] = l + r;
b[y | x | n >> 1] = l - r;
}
now *= base;
}
swap(a, b);
}
}
V<Mint> multiply_nft(const V<Mint>& a, const V<Mint>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
if (min(n, m) <= 50) {
V<Mint> ans(n + m - 1);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) ans[i + j] += a[i] * b[j];
return ans;
}
int lg = 0;
while ((1 << lg) < n + m - 1) lg++;
int z = 1 << lg;
auto a2 = a, b2 = b;
a2.resize(z);
b2.resize(z);
nft(false, a2);
nft(false, b2);
for (int i = 0; i < z; i++) a2[i] *= b2[i];
nft(true, a2);
a2.resize(n + m - 1);
Mint iz = Mint(z).inv();
for (int i = 0; i < n + m - 1; i++) a2[i] *= iz;
return a2;
}
int f2(ll x) {
int ans = 0;
while (x) {
ans += x % 2;
x /= 2;
}
return ans;
}
int f3(ll x) {
int ans = 0;
while (x) {
ans += x % 3;
x /= 3;
}
return ans;
}
void solve() {
ll n;
Mint a, b, c;
cin >> n >> a >> b >> c;
n++;
Mint ans = -1;
const int K = 21;
int k3 = 0;
int pw3 = 1;
while (3 * pw3 <= (1 << K)) {
k3++;
pw3 *= 3;
}
int cnt = n / (ll)(1 << K);
Mint pwa = pow(a, ((ll)cnt << (ll)K));
for (ll x = ((ll)cnt << (ll)K); x < n; x++) {
ans += pwa * pow(b, f2(x)) * pow(c, f3(x));
pwa *= a;
}
vector<Mint> p1((1 << K), 0);
pwa = 1;
for (int y = 0; y < (1 << K); y++) {
p1[y] = pwa * pow(b, f2(y));
pwa *= a;
}
vector<Mint> p2(pw3, 0);
for (int y = 0; y < pw3; y++) {
p2[y] = pow(c, f3(y));
}
reverse(p1.begin(), p1.end());
auto sm = multiply_nft(p1, p2);
Mint ml = pow(a, (1 << K));
pwa = 1;
for (int x = 0; x < cnt; x++) {
ll L = (ll)x << (ll)K;
ll R = L + (1 << K);
Mint cur = 0;
for (int i = (L / pw3); (ll)i * pw3 < R; i++) {
ll Lc = (ll)i * pw3;
int delta = L - Lc + (int)p1.size() - 1;
cur += sm[delta] * pow(c, f3(i));
}
ans += cur * pwa * pow(b, f2(x));
pwa *= ml;
}
cout << ans << "\n";
}
int main() {
#ifdef ONPC
freopen("input", "r", stdin);
#endif
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
cerr << TIME << "\n";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1142ms
memory: 66508kb
input:
123456 12345 234567 3456789
output:
664963464
result:
ok 1 number(s): "664963464"
Test #2:
score: 0
Accepted
time: 1841ms
memory: 66556kb
input:
9876543210987 12816 837595 128478
output:
7972694
result:
ok 1 number(s): "7972694"
Test #3:
score: 0
Accepted
time: 1613ms
memory: 66568kb
input:
9196665971964 727160879 966835565 746444639
output:
949890012
result:
ok 1 number(s): "949890012"
Test #4:
score: 0
Accepted
time: 1710ms
memory: 66532kb
input:
9361549073598 749653880 965489817 371100607
output:
949904373
result:
ok 1 number(s): "949904373"
Test #5:
score: 0
Accepted
time: 1649ms
memory: 66736kb
input:
9567572694963 193332930 544713669 390021151
output:
878781872
result:
ok 1 number(s): "878781872"
Test #6:
score: 0
Accepted
time: 1835ms
memory: 66560kb
input:
9769301349033 215825931 425927410 408941695
output:
351574791
result:
ok 1 number(s): "351574791"
Test #7:
score: 0
Accepted
time: 1790ms
memory: 66536kb
input:
9975324970399 657749333 5151262 729852127
output:
400022780
result:
ok 1 number(s): "400022780"
Test #8:
score: 0
Accepted
time: 1142ms
memory: 66512kb
input:
1 149762920 266126484 107367523
output:
910371791
result:
ok 1 number(s): "910371791"
Test #9:
score: 0
Accepted
time: 1319ms
memory: 66560kb
input:
903900971479 969144397 356713678 36786741
output:
414279957
result:
ok 1 number(s): "414279957"
Test #10:
score: 0
Accepted
time: 1300ms
memory: 66492kb
input:
1940757501452 72683414 106545701 263512239
output:
786323834
result:
ok 1 number(s): "786323834"
Test #11:
score: 0
Accepted
time: 1355ms
memory: 66512kb
input:
2914414844884 174466783 133201789 792227626
output:
187534312
result:
ok 1 number(s): "187534312"
Test #12:
score: 0
Accepted
time: 1353ms
memory: 66540kb
input:
3851250038782 553074217 881278164 297532837
output:
847958862
result:
ok 1 number(s): "847958862"
Test #13:
score: 0
Accepted
time: 1526ms
memory: 66580kb
input:
4692374803740 352867698 211679787 826248223
output:
426334178
result:
ok 1 number(s): "426334178"
Test #14:
score: 0
Accepted
time: 1506ms
memory: 66556kb
input:
5566041306806 454651067 959756163 633543322
output:
842296050
result:
ok 1 number(s): "842296050"
Test #15:
score: 0
Accepted
time: 1611ms
memory: 66576kb
input:
6902869060611 556434437 709588186 860268821
output:
897681255
result:
ok 1 number(s): "897681255"
Test #16:
score: 0
Accepted
time: 1683ms
memory: 66488kb
input:
7239695301792 356227918 736244273 667563920
output:
726280051
result:
ok 1 number(s): "726280051"
Test #17:
score: 0
Accepted
time: 1711ms
memory: 66512kb
input:
8217660029470 458011287 486076297 198034954
output:
967159922
result:
ok 1 number(s): "967159922"