QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#66901 | #5169. 夹娃娃 | yehaodee | 3 | 2365ms | 11004kb | C++14 | 4.0kb | 2022-12-09 12:28:39 | 2022-12-09 12:28:47 |
Judging History
answer
#include <bits/stdc++.h>
namespace IO {
#define iL (1 << 20)
char ibuf[iL], *iS = ibuf + iL, *iT = ibuf + iL;
#define gc() ((iS == iT) ? (iT = (iS = ibuf) + fread(ibuf, 1, iL, stdin), iS == iT ? EOF : *iS ++) : *iS ++)
template<class T> inline void read(T &x) {
x = 0;int f = 0;char ch = gc();
for (; !isdigit(ch); f |= ch == '-', ch = gc());
for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc());
x = (f == 1 ? ~ x + 1 : x);
}
template<class T, class... Args> inline void read(T &x, Args&... args) { read(x), read(args...); }
template<class T> inline void readch(T &x) { char ch = gc(); for (; !isdigit(ch); ch = gc()); x = ch; }
char Out[iL], *iter = Out;
#define flush() fwrite(Out, 1, iter - Out, stdout), iter = Out
template<class T> inline void write(T x, char ch = '\n') {
T l, c[35];
if (x < 0) *iter ++ = '-', x = ~ x + 1;
for (l = 0; !l || x; c[l] = x % 10, l++, x /= 10);
for (; l; -- l, *iter ++ = c[l] + '0');*iter ++ = ch;
flush();
}
} // IO
using namespace IO;
#define N 20
#define M 525
#define ll long long
#define DEBUG
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
using namespace std;
int n, q, p, answ[M * 100]; vi f[N], g[N][M], sf[M], sg[M]; vi ansf[M][(1 << 6)];
vector<pair<pii, int> > qry[M];
namespace poly {
int l, rev[M * 10];
int qpow(int a, int b) { int r = 1; while (b) { if (b & 1) r = 1ll * r * a % p; a = 1ll * a * a % p, b >>= 1; } return r; }
void init(int n) {
l = 1; int s = 0; while (l < n) l <<= 1, s ++;
for (int i = 0; i < l; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << s - 1);
}
inline void ntt(vi &f, int l, int on) {
f.resize(l);
for (int i = 0; i < l; i++) if (rev[i] > i) swap(f[i], f[rev[i]]);
for (int h = 2; h <= l; h <<= 1) {
int wn = qpow(3, (p - 1) / h); if (on == -1) wn = qpow(wn, p - 2);
for (int i = 0; i < l; i += h) for (int j = 0, w = 1; j < h / 2; j++, w = 1ll * w * wn % p) {
int u = f[i + j], t = 1ll * w * f[i + j + h / 2] % p;
f[i + j] = (u + t > p) ? u + t - p : u + t;
f[i + j + h / 2] = (u - t < 0) ? u - t + p : u - t;
}
}
if (on == -1) { int in = qpow(l, p - 2);
for (int i = 0; i < l; i++) f[i] = 1ll * f[i] * in % p; }
}
inline vi conv(vi a, vi b) {
int n = a.size(), m = b.size();
if (n == 0 || m == 0) return vi{};
if (n < 30 || m < 30) {
vi r = {}; if (n > m) swap(a, b), swap(n, m); r.resize(n + m - 1);
for (int i = 0; i < n + m - 1; i++) {
for (int j = 0; j <= i && j < n; j++) r[i] = 1ll * (r[i] + 1ll * a[j] * b[i - j] % p) % p;
}
r.resize(M);
return r;
}
int sz = n + m - 1; init(sz);
ntt(a, l, 1), ntt(b, l, 1); for (int i = 0; i < l; i++) a[i] = 1ll * a[i] * b[i] % p;
ntt(a, l, -1); vi r; r.resize(M); for (int i = 0; i < min(sz, M); i++) r[i] = a[i];
return r;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin); freopen("test.out", "w", stdout);
#endif
read(n, q, p);
for (int i = 1, len; i <= n; i++) {
read(len); f[i] = {1};
for (int j = 1, b, c; j <= len; j++) { read(b, c);
int l = 1; vi prod; prod.resize(min(M, c * b + 1));
for (int k = 0; k < min(M, c * b + 1); k += b) prod[k] = 1;
f[i] = poly::conv(f[i], prod);
}
}
for (int k = 1; k < M; k++) {
for (int i = 1; i <= n; i++) {
g[i][k] = f[i];
if (g[i][k].size() < k) vi().swap(g[i][k]);
else { for (int j = 0; j < k; j++) g[i][k][j] = 0; }
}
}
for (int k = 1; k < M; k++) {
for (int s = 0; s < (1 << n); s++) {
ansf[k][s] = {1};
for (int i = 1; i <= n; i++) {
if ((s >> i - 1) & 1) ansf[k][s] = poly::conv(ansf[k][s], g[i][k]);
else ansf[k][s] = poly::conv(ansf[k][s], f[i]);
ansf[k][s].resize(M);
}
}
}
for (int _ = 1; _ <= q; _++) {
int s = 0, m, k;
for (int i = 1, l = 1, x; i <= n; i++) readch(x), s += l * (x - '0'), l <<= 1;
read(m, k); ll res = 0; ansf[k][s].resize(M);
for (int i = 0; i <= m; i++) res = (res + ansf[k][s][i]) % p;
write(res);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 3
Accepted
Test #1:
score: 3
Accepted
time: 49ms
memory: 7792kb
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: 0
Accepted
time: 2365ms
memory: 11004kb
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:
ok 1561 lines
Subtask #2:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #3:
score: 0
Time Limit Exceeded
input:
3 4160 998244353 444 520 520 26 332 29 183 25 479 175 14 13 16 1 447 2 293 4 20 64 472 491 11 21 259 75 22 390 401 8 508 405 3 137 4 15 154 164 1 484 13 257 14 44 20 7 13 26 15 26 432 14 9 478 24 18 10 22 28 8 21 260 25 431 22 7 6 20 26 8 27 239 19 1 134 2 322 16 225 6 42 517 6 197 407 268 500 433 5...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Time Limit Exceeded
Test #9:
score: 0
Time Limit Exceeded
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:
result:
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Skipped
Dependency #4:
0%
Subtask #7:
score: 0
Skipped
Dependency #6:
0%