QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77225 | #4610. 小 Y 和恐怖的奴隶主 | alpha1022 | 100 ✓ | 283ms | 8960kb | C++17 | 3.9kb | 2023-02-13 15:38:13 | 2023-02-13 15:38:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 998244353;
int norm(int x) { return x >= mod ? x - mod : x; }
int reduce(int x) { return x < 0 ? x + mod : x; }
int neg(int x) { return x ? mod - x : 0; }
void add(int &x, int y) { if ((x += y - mod) < 0) x += mod; }
void sub(int &x, int y) { if ((x -= y) < 0) x += mod; }
void fam(int &x, int y, int z) { x = (x + (ll)y * z) % mod; }
int mpow(int a, int b) {
int ret = 1;
for (; b; b >>= 1) {
if (b & 1) ret = (ll)ret * a % mod;
a = (ll)a * a % mod;
}
return ret;
}
pair<vector<int>, vector<int>> berlekampMassey(int n, const vector<int> &base) {
vector<int> u(n * 2 + 1), v(n * 2), x, y(1, 1);
u[n * 2] = 1;
for (int i = 0; i < v.size(); ++i) v[i] = base[i];
while (1) {
while (!v.empty() && !v.back()) v.pop_back();
if (v.size() - 1 < n && y.size() - 1 <= n) break;
x.resize(max(x.size(), u.size() - v.size() + y.size()));
int t = neg(mpow(v.back(), mod - 2));
for (int i = u.size() - v.size(); i >= 0; --i) {
int c = (ll)t * u[i + v.size() - 1] % mod;
for (int j = 0; j < y.size(); ++j) fam(x[i + j], c, y[j]);
for (int j = 0; j < v.size(); ++j) fam(u[i + j], c, v[j]);
}
swap(u, v), swap(x, y);
}
int inv = mpow(y[0], mod - 2);
for (int i = 0; i < y.size(); ++i) y[i] = (ll)y[i] * inv % mod;
for (int i = 0; i < v.size(); ++i) v[i] = (ll)v[i] * inv % mod;
return make_pair(v, y);
}
vector<int> operator*(vector<int> a, vector<int> b) {
vector<int> ret(a.size() + b.size() - 1);
for (int i = 0; i < a.size(); ++i)
for (int j = 0; j < b.size(); ++j)
fam(ret[i + j], a[i], b[j]);
return ret;
}
vector<int> polyMod(vector<int> u, vector<int> v) {
int n = u.size() - 1, m = v.size() - 1;
for (; n && !u[n]; --n) u.pop_back(); for (; m && !v[m]; --m) v.pop_back();
if (!v[m]) return {};
int inv = mpow(v[m], mod - 2);
for (int i = 0; i <= m; ++i) v[i] = (ll)v[i] * inv % mod;
while (n >= m) {
int t = u[n];
for (int i = 0; i <= m; ++i) u[n - m + i] = (u[n - m + i] + (ll)(mod - t) * v[i]) % mod;
for (; n && !u[n]; --n) u.pop_back();
}
return u;
}
const int LG = 64;
const int K = 8;
const int B = 1e3;
const int W = 8;
int T, m, k;
ll n;
int inv[K + 2];
int f[B + 5][K + 1][K + 1][K + 1];
vector<int> base(B + 1), q;
vector<int> pw[LG / W + 1][(1 << W) + 1];
int query(ll n) {
vector<int> res(1, 1);
for (int p = 0; n; n >>= W, ++p) res = polyMod(res * pw[p][n & ((1 << W) - 1)], q);
int ret = 0;
for (int i = 0; i < res.size(); ++i) fam(ret, res[i], base[i]);
return ret;
}
int main() {
scanf("%d%d%d", &T, &m, &k);
inv[1] = 1;
for (int i = 2; i <= k + 1; ++i) inv[i] = (ll)(mod - mod / i) * inv[mod % i] % mod;
for (int i = 1; i <= B; ++i)
for (int a = 0; a <= k; ++a)
for (int b = 0; a + b <= k; ++b)
for (int c = 0; a + b + c <= k; ++c) {
f[i][a][b][c] = (f[i][a][b][c] + (ll)(f[i - 1][a][b][c] + 1) * inv[a + b + c + 1]) % mod;
if (a) f[i][a][b][c] = (f[i][a][b][c] + (ll)f[i - 1][a - 1][b][c] * a % mod * inv[a + b + c + 1]) % mod;
if (b) f[i][a][b][c] = (f[i][a][b][c] + (ll)f[i - 1][a + 1][b - 1 + (a + b + c < k && m == 2)][c + (a + b + c < k && m == 3)] * b % mod * inv[a + b + c + 1]) % mod;
if (c) f[i][a][b][c] = (f[i][a][b][c] + (ll)f[i - 1][a][b + 1][c - 1 + (a + b + c < k)] * c % mod * inv[a + b + c + 1]) % mod;
}
for (int i = 0; i <= B; ++i) base[i] = f[i][m == 1][m == 2][m == 3];
q = berlekampMassey(base.size() >> 1, base).second, reverse(q.begin(), q.end());
for (int p = 0; p * W <= LG; ++p) {
auto w = p ? pw[p - 1][1 << W] : vector<int>({0, 1});
pw[p][0] = {1};
for (int i = 1; i <= (1 << W); ++i) pw[p][i] = polyMod(pw[p][i - 1] * w, q);
}
for (; T; --T) scanf("%lld", &n), printf("%d\n", query(n));
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 3
Accepted
time: 0ms
memory: 6508kb
input:
10 1 1 3 5 10 6 10 4 5 2 1 7
output:
873463811 967049221 997269514 982646790 997269514 935854084 967049221 748683266 499122177 990445575
result:
ok 10 numbers
Test #2:
score: 8
Accepted
time: 4ms
memory: 6880kb
input:
10 2 8 9 7 3 1 8 10 4 6 8 4
output:
957797658 957988697 471393168 499122177 751114294 293461261 227146807 120778537 751114294 227146807
result:
ok 10 numbers
Test #3:
score: 7
Accepted
time: 0ms
memory: 6608kb
input:
10 2 3 738202531831392322 783619408699447923 95448256259589251 844448705450323564 624412958283331450 82254415491953115 161329806857404403 873406552855800221 579441694693039373 362343929997412473
output:
728204560 970397627 628767169 940576960 292842745 769165416 359318636 321055107 521151178 572158341
result:
ok 10 numbers
Test #4:
score: 12
Accepted
time: 1ms
memory: 6848kb
input:
10 2 7 302412104829477627 267175673759183341 180043432686593470 526090092686666014 505721208708278398 506294196055642495 389633992918104929 635697493290952378 757493234187573112 761239842356661668
output:
956147808 849708906 858020267 830680308 607733651 859659370 410667202 74957667 412240449 2988204
result:
ok 10 numbers
Test #5:
score: 30
Accepted
time: 15ms
memory: 7344kb
input:
20 3 5 852899626493760283 754069554675030848 773874428758880017 123375651672149292 556249242341531885 428470484260225591 612955822468784753 144411941571891154 869439856253804050 908146565477516347 814952754132360789 756092742572000841 820420180133712434 992274690451907092 846208580168608503 96150212...
output:
442017592 994454054 471018258 880700521 951831327 782278936 550520887 17878310 29620413 775413149 813993612 543356349 800788471 419038715 896629027 986912907 730534588 830045981 693902411 658725837
result:
ok 20 numbers
Test #6:
score: 10
Accepted
time: 72ms
memory: 7776kb
input:
500 3 6 738291794096542548 764358602831820281 563238804829926575 370603315432808748 100345531802350888 78859647115211243 811761470222990744 81127714898978520 402422318136684465 654308566563468036 169605695188757775 955311887954849077 150737338108335362 918211967525286737 351667577081940049 451234288...
output:
405831149 171783879 776079313 103798029 898275377 20932150 975107349 853149926 888583476 582990522 505595168 671098744 8292739 317527083 444671167 313608387 465763722 130368749 301863319 64029391 893498845 647170020 375946791 381167702 445055557 852714764 583144741 587653726 388605317 261031842 9436...
result:
ok 500 numbers
Test #7:
score: 10
Accepted
time: 94ms
memory: 8328kb
input:
200 3 7 672259869393347339 369327731126838699 64061787795877737 501816384000770596 690696325130441433 154127833833484890 576073302548020172 356224027574683716 360014439391885202 900035121039104331 620401957312455398 685627564948275461 257115887698249471 182590752506753132 795335245342655904 29229833...
output:
822803116 246007391 101854981 962791981 64436557 662297076 941478132 840251238 812289127 761081894 873173247 438918597 547800259 494535870 319490239 602671449 459646020 499523787 979522183 300512672 686999084 631298438 265075125 543461615 65764751 809426591 171508623 460409761 695067532 495969167 81...
result:
ok 200 numbers
Test #8:
score: 5
Accepted
time: 252ms
memory: 8352kb
input:
1000 3 7 927431038590684175 532845056241370848 981420276391761817 468489151751236651 660335172832504160 565758532825355787 164153593760812135 279434702460342896 358625155725778969 633338776100569232 211963237957802180 916221369234575530 779873510288248601 289233265840477315 537317800100747277 296733...
output:
581533337 854140161 990664899 520403265 166891093 299581858 76208179 551023557 764068860 97699696 990197247 340479754 729738323 167993645 413761832 6811126 967548892 426463088 204296280 942438465 670196053 491275194 355126211 567659383 934348609 497517329 776379621 941483102 768168420 250403018 9658...
result:
ok 1000 numbers
Test #9:
score: 10
Accepted
time: 146ms
memory: 8956kb
input:
100 3 8 293455384739357531 67612334385116908 709127840857554827 592692648624080073 669323382975105711 481820112205801832 213938967367348923 179085811004224321 25406212354373018 202909893880406081 150981737006062413 943764734403419344 962389709289625138 630945113981699018 159258773119290193 593552184...
output:
599797299 452625349 141842480 784318912 848233103 613481707 395997513 657986654 477321383 85573193 480737345 398643793 356946510 308675760 576535865 990122150 471928813 466277158 503920426 587614978 151779847 305622475 623301933 338578086 828288151 710126122 538710757 535209007 696280660 704016439 7...
result:
ok 100 numbers
Test #10:
score: 5
Accepted
time: 283ms
memory: 8960kb
input:
500 3 8 251326191428260710 145522620174033893 610704251244187963 885592345865036062 202160400718267461 99793413045684561 740002575418552853 4684983321229405 138776151283141117 170156142094361225 197199356826770314 660871465396172439 452817036656312156 912499034370116560 811398525499262192 7660511931...
output:
551989877 36808433 783659161 491489258 222922313 595265107 459220958 202796464 279377202 309963221 386525430 863771005 252573581 265858948 747475692 12473463 772992309 800249702 635246065 417682455 265785246 739410856 277378490 370780683 867176180 107389794 290732757 269814348 352257509 802457925 85...
result:
ok 500 numbers
Extra Test:
score: 0
Extra Test Passed