QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#67042 | #5169. 夹娃娃 | Dual | 10 | 2838ms | 196692kb | C++ | 5.8kb | 2022-12-09 20:54:27 | 2022-12-09 20:54:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
struct fastmod {
typedef unsigned long long u64;
typedef __uint128_t u128;
int m;
u64 b;
fastmod(int m) : m(m), b(((u128)1 << 64) / m) {}
int operator ()(u64 a) {
u64 q = ((u128)a * b) >> 64;
int r = a - q * m;
return r < m ? r : r - m;
}
} Red(2);
const int N = 15,M = 520,Lim = 5000;
const int B = 3,C = 5,S = 1 << B; //L : 块数,B : 块长
int n,Q,P;
inline int Add(const int &a,const int &b) { return (a + b >= P) ? (a + b - P) : (a + b);}
inline int Sub(const int &a,const int &b) { return (a < b) ? (a - b + P) : (a - b);}
inline void Plus(int &a,const int &b) { a += b;if(a >= P) a -= P;}
inline void Minus(int &a,const int &b) { a -= b;if(a < 0) a += P;}
inline int qpow(int a,int b) { int res = 1;while(b) {if(b&1) res = 1ll * res * a % P;a = 1ll * a * a % P;b >>= 1;} return res;}
typedef std::vector<int> vint;
vint f[C][S][M + 5]; // 块数,集合,k
vint g[C][S]; // 无限制
int fac[Lim + 5],ifac[Lim + 5],inv[Lim + 5];
int Poly[Lim + 5][M + 5];
void initfac(int n)
{
fac[0] = 1;
for(int i = 1;i <= n;i++) fac[i] = Red(1ll * fac[i - 1] * i);
inv[1] = 1;
for(int i = 2;i <= n;i++) inv[i] = Red(1ll * inv[P % i] * (P - P / i));
ifac[0] = 1;
for(int i = 1;i <= n;i++) ifac[i] = Red(1ll * ifac[i - 1] * inv[i]);
}
// 点值范围:0-Lim
void init_Lagrange(int n) // 系数表示法只需要保留 520 项,但是点值需要保留 2600/1560 项
{
for(int i = 0;i <= n;i++)
for(int j = 0;j <= min(n,M);j++)
Poly[i][j] = 0;
static int f[Lim + 5],tmp[Lim + 5];
initfac(n + 1);
memset(f,0,sizeof f);
f[0] = 1;
for(int i = 1;i <= n;i++)
{
for(int j = min(n,M) + 1;j >= 1;j--)
f[j] = Red(1ll * f[j] * (P - i)),Plus(f[j],f[j - 1]);
f[0] = Red(1ll * f[0] * (P - i));
}
for(int j = 0;j <= min(n,M);j++)
{
Poly[0][j] = Red(1ll * f[j] * ifac[n]);
if(n & 1) Poly[0][j] = P - Poly[0][j];
}
for(int j = min(n,M);j;j--) f[j] = f[j - 1];
f[0] = 0;
for(int i = 1;i <= n;i++)
{
memcpy(tmp,f,sizeof f);
for(int j = 0;j <= min(n,M);j++)
tmp[j] = P - Red(1ll * tmp[j] * inv[i]),Minus(tmp[j + 1],tmp[j]);
int res = Red(1ll * ifac[i] * ifac[n - i]);
if((n - i) & 1) res = P - res;
for(int j = 0;j <= min(n,M);j++)
Poly[i][j] = Red(1ll * res * tmp[j]);
}
}
inline int Solve(int S,int k,int m)
{
vector<int> tmp(M * (C + 1) + 1,1);
for(int p = 0;p <= (n - 1) / B;S >>= B,++p)
{
int now = S & ((1 << B) - 1);
for(int x = 0;x <= M * (C + 1);++x)
tmp[x] = Red(1ll * tmp[x] * f[p][now][k][x]);
}
long long ans = 0;
for(int x = 0;x <= M * (C + 1);++x)
ans += Red(1ll * tmp[x] * Poly[x][m]);
return ans % P;
}
int Cnt[S];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> Q >> P;
Red = fastmod(P);
for(int i = 0;i < n;i++)
{
int a;
cin >> a;
static int t[M + 5];
memset(t,0,sizeof t);
t[0] = 1;
while(a--)
{
int b,c;
cin >> b >> c;
for(int j = b;j <= M;j++) Plus(t[j],t[j - b]);
int tmp = b * (c + 1);
if(tmp > M) continue;
for(int j = M;j >= tmp;j--) Minus(t[j],t[j - tmp]);
}
// 没有限制的那个 g
g[i / B][1 << (i % B)].resize(M * B + 1);
for(int x = 0;x <= M * B;x++)
{
int res = 0;
for(int j = M;j >= 0;j--)
res = Red(1ll * res * x + t[j]);
// printf("gres:%d\n",res)
g[i / B][1 << (i % B)][x] = res;
}
for(int k = 0;k <= M;k++) f[i / B][1 << (i % B)][k].resize(M * (C + 1) + 1);
for(int x = 0;x <= M * (C + 1);x++)
{
int powx = 1,res = 0;
for(int k = 0;k <= M;k++)
{
res = Red(res + 1ll * t[k] * powx);
f[i / B][1 << (i % B)][k][x] = P - res; // 容斥系数 -1
powx = Red(1ll * powx * x);
}
}
}
for(int i = 1;i < S;i++) Cnt[i] = Cnt[i >> 1] + (i & 1);
init_Lagrange(M * B); // 到时候要把点值个数由 M * B 变为 M * (C + 1) (由块内变为块间)
for(int p = 0;p <= (n - 1) / B;++p)
{
int siz = min(B,n - p * B);
g[p][0].assign(M * B + 1,1); // 空集的权都赋为 1
for(int k = 0;k <= M;k++) f[p][0][k].assign(M * (C + 1) + 1,1);
for(int S = 1;S < (1 << siz);++S)
if(Cnt[S] > 1)
{
g[p][S].resize(M * B + 1);
for(int x = 0;x <= M * B;++x)
g[p][S][x] = Red(1ll * g[p][S & (S - 1)][x] * g[p][S & (-S)][x]);
for(int k = 0;k <= M && (k + 1) * Cnt[S] <= M;k++)
{
f[p][S][k].resize(M * (C + 1) + 1);
for(int x = 0;x <= M * (C + 1);++x)
f[p][S][k][x] = Red(1ll * f[p][S & (S - 1)][k][x] * f[p][S & (-S)][k][x]);
}
}
// return 0;
// 只保留 M * (C + 1) + 1 个点值
for(int S = 0;S < (1 << siz);++S)
{
vector<int> tmp(M + 1,0);
for(int i = 0;i <= M;i++)
for(int j = 0;j <= M * B;j++)
Plus(tmp[i],Red(1ll * g[p][S][j] * Poly[j][i]));
g[p][S].resize(M * (C + 1) + 1);
for(int x = 0;x <= M * (C + 1);++x)
{
int res = 0;
for(int j = M;j >= 0;j--)
res = Red(1ll * res * x + tmp[j]);
g[p][S][x] = res;
}
}
// return 0;
for(int S = 0;S < (1 << siz);++S)
for(int k = 0;k <= M && (k + 1) * Cnt[S] <= M;k++)
for(int x = 0;x <= M * (C + 1);++x)
f[p][S][k][x] = Red(1ll * f[p][S][k][x] * g[p][((1 << siz) - 1) ^ S][x]);
// return 0;
for(int u = 0;u < siz;++u)
for(int S = 0;S < (1 << siz);++S)
if((S >> u) & 1)
for(int k = 0;k <= M && (k + 1) * Cnt[S] <= M;k++)
for(int x = 0;x <= M * (C + 1);++x)
Plus(f[p][S][k][x],f[p][S ^ (1 << u)][k][x]);
}
init_Lagrange(M * (C + 1));
for(int i = 0;i <= M * (C + 1);i++)
for(int j = 1;j <= M;j++)
Plus(Poly[i][j],Poly[i][j - 1]);
while(Q--)
{
string s;
int k,m;
cin >> s >> m >> k;
--k;
int S = 0;
for(int i = n - 1;i >= 0;i--)
S = S << 1 | (s[i] - '0');
cout << Solve(S,k,m) << endl;
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 3
Accepted
time: 86ms
memory: 24116kb
input:
1 521 998244353 39 520 520 11 22 414 8 95 18 229 356 26 407 316 10 24 26 19 61 11 130 482 476 420 15 192 193 208 24 19 233 494 217 275 294 26 28 439 20 272 277 28 198 5 335 22 8 28 17 154 78 6 13 175 17 2 5 477 256 200 4 1 36 427 371 439 23 10 65 426 25 24 27 121 29 28 13 12 453 0 520 1 1 519 1 1 51...
output:
38813347 922143638 98254957 38813343 922143633 38813338 98254946 922143620 98254933 922143604 38813302 38813288 922143562 38813247 38813220 38813188 38813150 98254715 38813047 922143273 98254516 38812814 922142999 98254191 922142723 38812257 38812058 98253436 922141847 38811240 922141173 38810463 38...
result:
ok 521 lines
Test #2:
score: -3
Runtime Error
input:
2 1561 998244353 151 520 520 511 30 121 396 25 16 113 11 6 175 242 20 8 5 61 13 518 447 404 8 220 177 4 19 18 15 70 233 9 14 26 512 17 9 9 19 30 8 495 20 13 27 277 22 396 14 4 29 345 442 19 25 14 5 16 295 19 65 134 10 10 296 245 6 7 30 253 15 187 26 482 454 28 414 170 404 11 27 27 25 13 509 1 5 291 ...
output:
883965618 144348435 762074635 112296779 385763651 821718611 673974966 879750066 927942969 136450507 436584627 612945970 768262217 613885343 39304132 852224740 215596261 151746110 965953558 969833936 664053020 458247365 881060255 878484499 781573019 616944059 850325449 296113117 674829177 887392623 6...
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 10
Accepted
Test #9:
score: 10
Accepted
time: 2638ms
memory: 196692kb
input:
15 52099 998244353 1 9 3 1 9 4 1 9 2 1 8 10 1 4 4 1 3 1 1 2 5 1 4 9 1 1 4 1 9 4 1 7 6 1 1 6 1 2 5 1 5 2 1 3 5 101000000001010 516 1 010001001010101 520 2 000000101000001 519 2 101011111100011 518 1 010110001000111 520 2 000110111100111 516 1 000100101001011 519 3 000111001010011 518 1 00001110010111...
output:
993379058 496689529 866368587 797687294 481245176 481245176 39022588 269889529 552778235 769822588 331666941 99789529 903956470 112750588 756797435 519045176 870912000 361582588 594280447 494747647 597778941 178845176 435456000 493445999 461733882 308912117 271186941 496689529 919511294 85533882 894...
result:
ok 52099 lines
Test #10:
score: 0
Accepted
time: 2838ms
memory: 196504kb
input:
15 52099 998244353 1 444 1 1 184 2 1 427 1 1 23 1 1 513 2 1 413 4 1 304 2 1 214 5 1 108 2 1 304 1 1 283 5 1 465 1 1 277 1 1 57 2 1 160 4 111010111100101 520 25 010010011100110 520 3 010000011011000 519 36 110011011100110 516 21 010110001101101 516 42 100001101000111 520 31 110001100010001 519 10 110...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 52099 lines
Test #11:
score: 0
Accepted
time: 2785ms
memory: 196500kb
input:
15 52099 998244353 1 5 5 1 43 5 1 25 5 1 30 3 1 34 4 1 32 3 1 45 4 1 27 4 1 30 5 1 22 2 1 26 4 1 5 4 1 23 2 1 13 1 1 3 3 010111011010001 519 2 101000001100000 518 66 001011100110000 518 52 001100000000010 520 42 000110000110100 518 40 100011100010011 518 34 101000111100000 520 9 101010011100100 519 ...
output:
11101681 0 0 0 1142004 0 36064398 16061763 2645108 0 0 10591435 746157 1803184 0 0 16192453 0 0 0 0 20456968 0 3582238 805462 14505464 0 0 0 0 0 0 0 26667881 0 0 0 0 0 0 4674341 0 0 0 0 0 0 0 0 0 0 0 21963213 8597667 4334676 43539633 0 0 0 0 0 2382160 5712673 2788111 1352473 0 0 0 0 0 0 0 0 47306134...
result:
ok 52099 lines
Test #12:
score: 0
Accepted
time: 2192ms
memory: 136384kb
input:
10 52099 998244353 1 5 1 1 23 1 1 27 4 1 19 4 1 12 2 1 30 5 1 28 2 1 16 4 1 30 3 1 6 5 0011010111 519 23 0000001001 517 49 0010110011 517 60 0001111100 520 44 1101101010 518 37 1010011010 519 33 0010010001 519 43 0110001001 517 29 1110101110 518 38 1011010010 520 49 0000000011 520 29 0011111011 518 ...
output:
37275 0 0 0 0 0 0 0 0 0 79991 0 0 0 15342 0 0 0 20344 0 56202 0 0 0 50043 0 0 0 181532 0 0 0 0 52937 0 0 0 0 0 55478 56473 0 255272 0 22226 0 0 18671 236899 0 16685 0 0 31629 0 0 0 0 0 0 87684 0 0 84149 0 0 0 127178 0 0 0 0 0 0 0 0 0 0 0 159291 0 0 0 41794 70377 83979 0 0 56097 0 106432 0 0 0 0 1131...
result:
ok 52099 lines
Test #13:
score: 0
Accepted
time: 1849ms
memory: 122680kb
input:
9 52099 998244353 1 18 1 1 8 3 1 36 5 1 47 2 1 47 3 1 34 3 1 38 5 1 35 5 1 45 5 110110111 520 8 001110101 519 42 010101000 516 37 101001001 516 9 110111111 518 33 100111010 519 41 000010000 520 116 010110110 519 25 001011011 517 17 001100000 517 104 000010101 518 79 111100110 517 34 011100111 516 1 ...
output:
5039 4726 0 24816 0 0 24712 0 19695 0 3683 0 13040 0 10500 23330 0 0 52779 0 3146 3023 8773 9949 0 0 0 11047 0 30494 0 6016 7291 0 6836 16605 0 0 0 0 6209 14419 0 0 0 14987 0 0 0 0 0 13850 0 0 0 0 4210 11509 0 43740 0 21261 0 74429 0 0 15553 0 5876 33640 0 0 0 12734 0 14637 0 39701 2277 0 0 0 0 0 0 ...
result:
ok 52099 lines
Test #14:
score: 0
Accepted
time: 102ms
memory: 24588kb
input:
1 1000 998244353 1 5 1 0 516 28 1 517 106 1 517 22 0 519 4 0 516 37 0 518 10 1 517 62 0 519 32 1 518 58 1 519 72 1 516 82 1 520 56 1 516 59 1 520 79 0 517 122 1 517 2 1 517 53 0 518 93 0 520 149 1 520 86 0 520 53 1 517 120 1 518 41 1 517 94 0 519 76 0 517 46 1 520 48 0 519 31 0 519 4 0 516 24 0 516 ...
output:
2 0 0 2 2 2 0 2 0 0 0 0 0 0 2 1 0 2 2 0 2 0 0 0 2 2 0 2 2 2 2 0 2 2 2 2 0 0 0 2 2 0 0 2 2 2 2 0 0 2 0 2 0 0 2 2 0 2 2 0 0 2 2 2 0 0 2 0 0 2 0 2 0 0 2 0 0 0 0 2 2 0 0 0 1 2 2 2 2 2 2 2 2 0 0 2 2 2 0 2 2 0 0 2 0 2 0 2 0 2 2 0 0 0 0 0 2 2 2 2 2 0 2 0 0 0 0 0 2 2 2 0 2 0 2 0 0 2 2 0 0 2 2 0 2 0 2 2 0 0 ...
result:
ok 1000 lines
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Skipped
Dependency #4:
100%
Accepted
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #6:
0%