QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#71804 | #4610. 小 Y 和恐怖的奴隶主 | He_Ren | 100 ✓ | 314ms | 10656kb | C++23 | 3.9kb | 2023-01-12 01:19:20 | 2023-01-12 01:20:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 998244353;
const ll inv2 = (mod + 1) / 2;
namespace FastIO {
const int SIZ1 = 1 << 23;
char buf1[SIZ1], *s1 = buf1, *t1 = buf1;
inline char gc(void) {
return s1 == t1 && (t1 = buf1 + fread(buf1, 1, SIZ1, stdin)) == (s1 = buf1) ? EOF : *s1++;
}
template<typename T>
void read(T &res) {
res = 0;
char c = gc();
while (c < '0' || c > '9')
c = gc();
while (c >= '0' && c <= '9')
res = res * 10 + c - '0', c = gc();
}
} using FastIO::read;
inline void add_mod(int &a, int b) {
a += b;
if (a >= mod)
a -= mod;
}
inline int mod_add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
inline ll pw(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
const int MX = 168;
struct Matrix {
static int N;
int a[MX][MX];
Matrix(void) {
memset(a, 0, sizeof(a));
}
int *operator [](int x) {
return a[x];
}
const int *operator [](int x) const {
return a[x];
}
#define FOR(i) for(int i=0; i<N; i+=8)
#define fi fj(0)fj(1)fj(2)fj(3)fj(4)fj(5)fj(6)fj(7)
Matrix operator * (const Matrix &b) const {
#define V(i,j,k) 1ll * a[ti+i][tk+k] * b[tk+k][tj+j]
#define fj(i) fk(i,0)fk(i,1)fk(i,2)fk(i,3)fk(i,4)fk(i,5)fk(i,6)fk(i,7)
#define fk(i,j) res[ti+i][tj+j]=(res[ti+i][tj+j]+V(i,j,0)+V(i,j,1)+V(i,j,2)+V(i,j,3)+V(i,j,4)+V(i,j,5)+V(i,j,6)+V(i,j,7))%mod;
Matrix res;
FOR(ti)FOR(tj)FOR(tk) {
fi
}
return res;
#undef V
#undef fj
#undef fk
}
void aply(int b[]) const {
#define V(i,j) 1ll * a[ti+i][tj+j] * b[tj+j]
#define fj(i) res[ti+i]=(res[ti+i]+V(i,0)+V(i,1)+V(i,2)+V(i,3)+V(i,4)+V(i,5)+V(i,6)+V(i,7))%mod;
int res[MX];
memset(res, 0, sizeof(res));
FOR(ti)FOR(tj) {
fi
}
memcpy(b, res, (N + 1) << 2);
#undef V
#undef fj
}
#undef FOR
#undef fi
};
int Matrix::N;
int main(void) {
int T, m, d;
read(T);
read(m);
read(d);
if (m == 1) {
while (T--) {
ll n;
read(n);
printf("%d\n", mod_add(n - 1, pw(inv2, n)));
}
return 0;
}
static int id[9][9][9], pcnt = 0;
static tuple<int, int, int> deco[MX];
for (int i = 0; i <= d; ++i)
for (int j = 0; i + j <= d; ++j)
for (int k = 0; i + j + k <= d; ++k) {
if (m == 2 && k)
continue;
deco[pcnt] = tie(i, j, k);
id[i][j][k] = pcnt++;
}
int beg = m == 2 ? id[0][1][0] : id[0][0][1];
Matrix::N = pcnt + 1;
Matrix a;
for (int u = 0; u < pcnt; ++u) {
int i, j, k;
tie(i, j, k) = deco[u];
ll iv = pw(i + j + k + 1, mod - 2);
int flag2 = m == 2, flag3 = m == 3;
if (i + j + k >= d)
flag2 = flag3 = 0;;
if (i)
add_mod(a[ id[i - 1][j][k]][u], iv * i % mod);
if (j)
add_mod(a[ id[i + 1][j - 1 + flag2][k + flag3]][u], iv * j % mod);
if (k)
add_mod(a[ id[i ][j + 1 + flag2][k - 1 + flag3]][u], iv * k % mod);
add_mod(a[u][u], iv);
add_mod(a[pcnt][u], iv);
}
add_mod(a[pcnt][pcnt], 1);
static const int lb = 60;
static Matrix pwa[lb];
pwa[0] = a;
for (int i = 1; i < lb; ++i)
pwa[i] = pwa[i - 1] * pwa[i - 1];
while (T--) {
ll n;
read(n);
static int res[MX];
memset(res, 0, sizeof(res));
res[beg] = 1;
for (int i = 0; n; ++i, n >>= 1)
if (n & 1)
pwa[i].aply(res);
printf("%d\n", res[pcnt]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 3
Accepted
time: 2ms
memory: 3988kb
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: 10ms
memory: 10384kb
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: 3ms
memory: 10592kb
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: 9ms
memory: 10656kb
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: 10ms
memory: 10440kb
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: 76ms
memory: 10464kb
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: 102ms
memory: 10380kb
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: 287ms
memory: 10428kb
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: 163ms
memory: 10560kb
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: 314ms
memory: 10336kb
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