QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#784390 | #9644. 药水 | le0n | 100 ✓ | 1169ms | 194880kb | C++14 | 8.1kb | 2024-11-26 14:53:21 | 2024-11-26 14:53:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 998244353;
int rev[1 << 23], omg[1 << 23], X[1 << 22], Y[1 << 22], Z[1 << 22], W[1 << 22];
int qpow(int x, int y)
{
int ans = 1;
while(y)
{
if(y & 1)
ans = (ll)ans * x % mod;
x = (ll)x * x % mod;
y >>= 1;
}
return ans;
}
void prep(int n)
{
int i, j, w;
for(i = 0; i <= n; i++)
{
for(j = 1; j < (1 << i); j++)
rev[(1 << i) + j] = rev[(1 << (i - 1)) + (j >> 1)] | ((j & 1) << (i - 1));
w = qpow(3, (mod - 1) >> i);
omg[1 << i] = 1;
for(j = 1; j < (1 << i); j++)
omg[(1 << i) + j] = (ll)omg[(1 << i) + j - 1] * w % mod;
}
}
ull Tmp[1 << 21];
void ntt(int *A, int n, bool t)
{
int i, j, k, w;
for(i = 0; i < (1 << n); i++)
Tmp[i] = A[i];
for(i = 0; i < (1 << n); i++)
if(i < rev[(1 << n) + i])
swap(Tmp[i], Tmp[rev[(1 << n) + i]]);
for(i = 0; i < n; i++)
{
for(j = 0; j < (1 << n); j += (2 << i))
for(k = 0; k < (1 << i); k++)
{
w = Tmp[(1 << i) + j + k] * omg[(2 << i) + k] % mod;
Tmp[(1 << i) + j + k] = Tmp[j + k] + mod - w;
Tmp[j + k] = Tmp[j + k] + w;
}
if(i == n - 1 || i == 16)
for(j = 0; j < (1 << n); j++)
Tmp[j] %= mod;
}
for(i = 0; i < (1 << n); i++)
A[i] = Tmp[i];
if(t)
{
for(i = 1; i < (1 << n); i++)
if(i < (1 << n) - i)
swap(A[i], A[(1 << n) - i]);
w = qpow(1 << n, mod - 2);
for(i = 0; i < (1 << n); i++)
A[i] = (ll)A[i] * w % mod;
}
}
void Inv(int *A, int *B, int n)
{
int i, j;
B[0] = qpow(A[0], mod - 2);
for(i = 1; i <= n; i++)
{
memset(X, 0, 8 << i);
memset(Y, 0, 8 << i);
memcpy(X, A, 4 << i);
memcpy(Y, B, 4 << i);
ntt(X, i + 1, 0);
ntt(Y, i + 1, 0);
for(j = 0; j < (2 << i); j++)
Y[j] = (ll)(mod + 2 - (ll)X[j] * Y[j] % mod) * Y[j] % mod;
ntt(Y, i + 1, 1);
memcpy(B, Y, 4 << i);
}
}
struct poly
{
vector<int> c;
poly()
{
c.clear();
}
poly operator + (poly p)
{
int i;
p.c.resize(max(p.c.size(), c.size()));
for(i = 0; i < p.c.size(); i++)
if(i < c.size())
p.c[i] = (p.c[i] + c[i]) % mod;
return p;
}
friend poly operator * (poly f, poly g)
{
if(f.c.empty() || g.c.empty())
return poly();
poly h;
int i, l = 0;
while((f.c.size() + g.c.size()) >> l)
l++;
memset(X, 0, 4 << l);
memset(Y, 0, 4 << l);
for(i = 0; i < f.c.size(); i++)
X[i] = f.c[i];
for(i = 0; i < g.c.size(); i++)
Y[i] = g.c[i];
ntt(X, l, 0);
ntt(Y, l, 0);
for(i = 0; i < (1 << l); i++)
X[i] = (ll)X[i] * Y[i] % mod;
ntt(X, l, 1);
h.c.resize(f.c.size() + g.c.size() - 1);
for(i = 0; i < h.c.size(); i++)
h.c[i] = X[i];
return h;
}
poly inv(int n)
{
int i, l = 0;
while(n >> l)
l++;
poly f;
if(!n)
return f;
memset(Z, 0, 4 << l);
memset(W, 0, 4 << l);
for(i = 0; i < (1 << l) && i < c.size(); i++)
Z[i] = c[i];
Inv(Z, W, l);
f.c.resize(n);
for(i = 0; i < n; i++)
f.c[i] = W[i];
return f;
}
};
int a[15], l, r;
int f[15], g[15], d, L, R, p; // A^p
int bs;
vector<int> F[3][15], pv[8][8], qwq[8];
poly mat[8][8], ans[8], tmp;
int U[8][1000005], V[8][1000005], T[1000005];
void next()
{
int i, tmp = 0;
++R;
if(R == (ll)l * p)
{
f[R - d] = qpow(a[0], p);
return ;
}
for(i = l + 1; i <= r; i++)
tmp = (tmp + (ll)a[i - l] * f[R + l - i - d] % mod * ((ll)p * i % mod - (R + l - i))) % mod;
f[R - d] = (ll)(tmp + mod) * qpow((ll)a[0] * (R - (ll)p * l % mod + mod) % mod, mod - 2) % mod;
}
void pre()
{
int i, tmp = 0;
--L;
if(L == (ll)r * p)
{
f[L - d] = qpow(a[r - l], p);
return ;
}
for(i = l; i < r; i++)
tmp = (tmp + (ll)a[i - l] * f[L + r - i - d] % mod * ((ll)p * i % mod - (L + r - i))) % mod;
f[L - d] = (ll)(tmp + mod) * qpow((ll)a[r - l] * (L - (ll)p * r % mod + mod) % mod, mod - 2) % mod;
}
const int LIM = 16;
void work(int l, int r)
{
int mid = (l + r) >> 1, i, j, k, o = 0;
if(l == r)
{
for(i = 0; i < d; i++)
ans[i].c[l] = (mat[i][d].c[l] + mod - qwq[i][l]) % mod;
return ;
}
work(l, mid);
while((r - l + 1) >> o)
o++;
if((r - l + 1) <= LIM)
{
int x, y;
for(i = 0; i < d; i++)
for(j = 0; j < d; j++)
for(x = l; x <= mid; x++)
for(y = mid + 1; y <= r; y++)
qwq[i][y] = (qwq[i][y] + (ll)ans[j].c[x] * mat[i][j].c[y - x]) % mod;
}
else
{
for(i = 0; i < d; i++)
{
memset(U[i], 0, 4 << o);
memset(V[i], 0, 4 << o);
for(j = l; j <= mid; j++)
U[i][j - l] = ans[i].c[j];
ntt(U[i], o, 0);
}
for(i = 0; i < d; i++)
for(j = 0; j < d; j++)
{
for(k = 0; k < (1 << o); k++)
T[k] = pv[i][j][k + (1 << o)];
for(k = 0; k < (1 << o); k++)
V[i][k] = (V[i][k] + (ll)T[k] * U[j][k]) % mod;
}
for(i = 0; i < d; i++)
{
ntt(V[i], o, 1);
for(j = mid + 1; j <= r; j++)
qwq[i][j] = (qwq[i][j] + V[i][j - l]) % mod;
}
}
work(mid + 1, r);
}
int main()
{
// freopen("water.in", "r", stdin);
// freopen("dnc.out", "w", stdout);
prep(21);
int n, m, k, v, i, j, s, t, o = 0;
scanf("%d%d%d%d%d", &n, &m, &k, &l, &r);
for(i = l; i <= r; i++)
scanf("%d", a + (i - l));
reverse(a, a + r - l + 1);
swap(l, r);
l = -l;
r = -r;
while((m + 1) >> o)
o++;
++k;
v = max(-l, r);
d = -2 * v;
L = -v;
R = v;
memset(f, 0, sizeof(f));
f[-d] = 1;
for(i = 0; i <= m; i++)
{
for(j = L; j <= R; j++)
F[0][j - L].emplace_back(f[j - d])/*, printf("!!%d %d %d\n", i, j, f[j - d])*/;
memset(g, 0, sizeof(g));
p = i;
for(j = 1; j <= v; j++)
{
next();
pre();
}
L += v;
R -= v;
for(j = L; j <= R; j++)
for(s = l; s <= r; s++)
g[j - d] = (g[j - d] + (ll)a[s - l] * f[j - s - d]) % mod;
memcpy(f, g, sizeof(g));
}
d = k - v;
L = k;
R = k + 2 * v;
memset(f, 0, sizeof(f));
if(!L)
f[-d] = 1;
for(i = 0; i <= m; i++)
{
for(j = L; j <= R; j++)
F[1][j - L].emplace_back(f[j - d])/*, printf("??%d %d %d\n", i, j, f[j - d])*/;
memset(g, 0, sizeof(g));
p = i;
for(j = 1; j <= v; j++)
{
next();
pre();
}
L += v;
R -= v;
for(j = L; j <= R; j++)
for(s = l; s <= r; s++)
g[j - d] = (g[j - d] + (ll)a[s - l] * f[j - s - d]) % mod;
memcpy(f, g, sizeof(g));
}
d = -k - 3 * v;
L = -k - 2 * v;
R = -k;
memset(f, 0, sizeof(f));
if(!R)
f[-d] = 1;
for(i = 0; i <= m; i++)
{
for(j = L; j <= R; j++)
F[2][j - L].emplace_back(f[j - d])/*, printf("..%d %d %d\n", i, j, f[j - d])*/;
memset(g, 0, sizeof(g));
p = i;
for(j = 1; j <= v; j++)
{
next();
pre();
}
L += v;
R -= v;
for(j = L; j <= R; j++)
for(s = l; s <= r; s++)
g[j - d] = (g[j - d] + (ll)a[s - l] * f[j - s - d]) % mod;
memcpy(f, g, sizeof(g));
}
for(i = l; i <= 0; i++)
{
mat[i - l][r - l + 1].c = F[0][i + v];
for(j = l; j <= 0; j++)
mat[i - l][j - l].c = F[0][i - j + v];
for(j = k; j < k + r; j++)
mat[i - l][j - k - l + 1].c = F[2][i - j + k + 2 * v];
}
mat[-l][r - l + 1].c[0] = 0;
for(i = k; i < k + r; i++)
{
mat[i - k - l + 1][r - l + 1].c = F[1][i - k];
for(j = l; j <= 0; j++)
mat[i - k - l + 1][j - l].c = F[1][i - j - k];
for(j = k; j < k + r; j++)
mat[i - k - l + 1][j - k - l + 1].c = F[0][i - j + v];
}
d = r - l + 1;
for(i = 0; i < d; i++)
{
ans[i].c.resize(m + 1);
qwq[i].resize(m + 1);
for(j = 0; j <= d; j++)
{
pv[i][j].resize(2 << o);
for(t = 0; t <= o; t++)
{
memset(X, 0, 4 << t);
for(s = 0; s < min(m + 1, 1 << t); s++)
X[s] = mat[i][j].c[s];
ntt(X, t, 0);
for(s = 0; s < (1 << t); s++)
pv[i][j][s + (1 << t)] = X[s];
}
}
}
bs = o;
work(0, m);
// for(i = 0; i < d; i++)
// {
// memset(X, 0, 4 << o);
// for(s = 0; s < (1 << o); s++)
// X[s] = pv[i][d][s];
// ntt(X, o, 1);
// for(s = 0; s <= m; s++)
// ans[i].c.emplace_back(X[s]);
// }
tmp.c.clear();
for(i = l; i <= 0; i++)
tmp = tmp + ans[i - l];
for(i = 0; i <= m; i++)
tmp.c[i] = (mod + (!i) - tmp.c[i]) % mod;
tmp = tmp.inv(n + 1);
printf("%d\n", tmp.c[n]);
return 0;
}
/*
6 2 3 -1 2
1 2 3 998244348
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 22ms
memory: 50852kb
input:
9 8 2 -3 3 978376014 534476433 82925309 123969914 464834705 142480510 667670175
output:
865328909
result:
ok single line: '865328909'
Test #2:
score: 10
Accepted
time: 20ms
memory: 50452kb
input:
9 5 21 -3 3 667211041 371819287 271110495 717869134 779977594 366592561 818397301
output:
584708415
result:
ok single line: '584708415'
Test #3:
score: 10
Accepted
time: 28ms
memory: 49936kb
input:
2 1 2 -3 3 389820258 245100065 892595587 651530510 730254290 432074702 651602001
output:
496986133
result:
ok single line: '496986133'
Test #4:
score: 10
Accepted
time: 24ms
memory: 50000kb
input:
10 5 3 -3 3 313158007 630233365 831165042 863498766 564299188 888052687 900814711
output:
965169282
result:
ok single line: '965169282'
Test #5:
score: 10
Accepted
time: 25ms
memory: 50976kb
input:
10 3 17 -3 3 66276843 258859685 725988178 37476143 921367279 97568130 887196802
output:
507638561
result:
ok single line: '507638561'
Subtask #2:
score: 10
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 10
Accepted
time: 16ms
memory: 66904kb
input:
91 87 54 -2 1 917754956 65696013 146792742 866244996
output:
28738959
result:
ok single line: '28738959'
Test #7:
score: 10
Accepted
time: 22ms
memory: 70256kb
input:
94 75 16 -3 1 852677705 370813440 222340218 818599820 730301877
output:
723018005
result:
ok single line: '723018005'
Test #8:
score: 10
Accepted
time: 29ms
memory: 50824kb
input:
98 15 14 -3 1 465962078 808631917 839559181 771314157 109265727
output:
747714330
result:
ok single line: '747714330'
Test #9:
score: 10
Accepted
time: 28ms
memory: 49116kb
input:
7 6 5 -3 3 408267021 158348217 115197532 71150315 628349077 531747008 83429537
output:
121060786
result:
ok single line: '121060786'
Test #10:
score: 10
Accepted
time: 12ms
memory: 67212kb
input:
30 27 8 -2 1 959777297 423706123 65112648 547892639
output:
112147442
result:
ok single line: '112147442'
Subtask #3:
score: 20
Accepted
Dependency #2:
100%
Accepted
Test #11:
score: 20
Accepted
time: 23ms
memory: 79604kb
input:
9791 846 7795 -3 3 609852827 124621801 378253739 421303592 163745355 175152354 123559039
output:
567131719
result:
ok single line: '567131719'
Test #12:
score: 20
Accepted
time: 84ms
memory: 85960kb
input:
9867 7766 24222 -3 3 162633694 205421574 361295532 466631050 173014631 153620454 473871772
output:
972664267
result:
ok single line: '972664267'
Test #13:
score: 20
Accepted
time: 32ms
memory: 80132kb
input:
2185 1775 3043 -3 3 407147191 182935255 188403511 116452081 176446804 372638044 552465821
output:
248031044
result:
ok single line: '248031044'
Test #14:
score: 20
Accepted
time: 78ms
memory: 85920kb
input:
8833 6297 14492 -3 3 814890583 559885007 172004804 788419034 666141268 925075 990711642
output:
824962211
result:
ok single line: '824962211'
Test #15:
score: 20
Accepted
time: 129ms
memory: 92880kb
input:
9751 9525 12744 -3 3 770794432 204974950 518482169 321658889 223803839 149671680 805347101
output:
778812135
result:
ok single line: '778812135'
Subtask #4:
score: 15
Accepted
Test #16:
score: 15
Accepted
time: 377ms
memory: 98116kb
input:
120000 117980 112081 -1 1 499122177 0 499122177
output:
193378538
result:
ok single line: '193378538'
Test #17:
score: 15
Accepted
time: 379ms
memory: 97720kb
input:
120000 116256 70515 -1 1 499122177 0 499122177
output:
613444885
result:
ok single line: '613444885'
Test #18:
score: 15
Accepted
time: 384ms
memory: 98160kb
input:
119847 116202 70490 -1 1 499122177 0 499122177
output:
650775770
result:
ok single line: '650775770'
Test #19:
score: 15
Accepted
time: 362ms
memory: 97704kb
input:
120000 119040 27196 -1 1 499122177 0 499122177
output:
198334971
result:
ok single line: '198334971'
Test #20:
score: 15
Accepted
time: 382ms
memory: 97768kb
input:
119977 119517 87801 -1 1 499122177 0 499122177
output:
911396650
result:
ok single line: '911396650'
Subtask #5:
score: 10
Accepted
Dependency #4:
100%
Accepted
Test #21:
score: 10
Accepted
time: 353ms
memory: 97584kb
input:
119543 118246 21936 -1 1 522726212 220279917 255238225
output:
720141873
result:
ok single line: '720141873'
Test #22:
score: 10
Accepted
time: 385ms
memory: 95832kb
input:
119627 118509 46868 -1 1 139898284 421648729 436697341
output:
707360530
result:
ok single line: '707360530'
Test #23:
score: 10
Accepted
time: 55ms
memory: 68912kb
input:
119732 3881 85231 -1 1 821442273 668064491 506981943
output:
62290543
result:
ok single line: '62290543'
Test #24:
score: 10
Accepted
time: 373ms
memory: 98000kb
input:
119383 115504 94825 -1 1 353911541 201817237 442515576
output:
233037070
result:
ok single line: '233037070'
Test #25:
score: 10
Accepted
time: 379ms
memory: 94944kb
input:
119970 119700 90422 -1 1 38681152 69780746 889782456
output:
67663678
result:
ok single line: '67663678'
Subtask #6:
score: 15
Accepted
Test #26:
score: 15
Accepted
time: 329ms
memory: 100296kb
input:
59196 51984 65786 -2 2 854679542 149682196 462175536 281379814 248571619
output:
436431710
result:
ok single line: '436431710'
Test #27:
score: 15
Accepted
time: 152ms
memory: 86180kb
input:
17498 16568 2290 -2 2 147984892 372916805 732121021 11903853 731562136
output:
184108028
result:
ok single line: '184108028'
Test #28:
score: 15
Accepted
time: 234ms
memory: 93736kb
input:
59377 32700 30501 -3 1 955519822 780899202 627724195 700280782 928553412
output:
18194952
result:
ok single line: '18194952'
Test #29:
score: 15
Accepted
time: 359ms
memory: 101332kb
input:
59161 44005 169519 -1 3 236185791 547845212 230625917 328613979 653217808
output:
934734720
result:
ok single line: '934734720'
Test #30:
score: 15
Accepted
time: 331ms
memory: 107056kb
input:
59810 58187 46591 -2 2 745258481 303300008 256421686 681246175 10262357
output:
94000430
result:
ok single line: '94000430'
Subtask #7:
score: 20
Accepted
Dependency #5:
100%
Accepted
Dependency #6:
100%
Accepted
Test #31:
score: 20
Accepted
time: 1153ms
memory: 194880kb
input:
120000 116424 25393 -3 3 741309042 606455820 367917828 827995272 665187318 628731548 155380585
output:
500865391
result:
ok single line: '500865391'
Test #32:
score: 20
Accepted
time: 1169ms
memory: 187004kb
input:
119670 118125 313324 -3 3 920642396 757974633 445817199 327185459 709606612 995760891 834234576
output:
310694083
result:
ok single line: '310694083'
Test #33:
score: 20
Accepted
time: 1138ms
memory: 193828kb
input:
120000 119162 138170 -3 3 612043854 963535753 21589849 194773645 238307580 934818639 29663740
output:
295409528
result:
ok single line: '295409528'
Test #34:
score: 20
Accepted
time: 1144ms
memory: 192400kb
input:
119997 117868 112039 -3 3 768738897 982614886 351190742 968990563 324943114 879380826 715362738
output:
637321264
result:
ok single line: '637321264'
Test #35:
score: 20
Accepted
time: 1158ms
memory: 187132kb
input:
119174 118782 68055 -3 3 561262663 716918073 704588480 361359134 402055681 843894426 402898956
output:
262581478
result:
ok single line: '262581478'