QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#591712 | #9317. Rivals | OIer_kzc | TL | 64ms | 48120kb | C++14 | 3.1kb | 2024-09-26 17:19:14 | 2024-09-26 17:19:14 |
Judging History
answer
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <vector>
#include <algorithm>
#define LOG(FMT...) fprintf(stderr, FMT)
#define eb emplace_back
#define em emplace
using namespace std;
typedef long long LL;
constexpr int N = 32, M = 11 * N;
constexpr int mod = 998244353;
int n, m, a[N], suma;
int f[M][M][N], g[M][M][N], bac[M][M][N];
int va[M], vb[M], fact[M], infact[M];
int b[M][N];
int res[M];
constexpr int inv(int x, int k = mod - 2) {
int r = 1;
while (k) {
if (k & 1) {
r = x * (LL)r % mod;
}
x = x * (LL)x % mod;
k >>= 1;
}
return r;
}
void Fav(int &x, int y, int z) {
x = (x + y * (LL)z) % mod;
}
void Add(int &x, int y) {
if ((x += y) >= mod) {
x -= mod;
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
suma += a[i];
}
*vb = *va = 1, *fact = 1, *infact = 1;
for (int i = 1; i < M; ++i) {
va[i] = va[i - 1] * (LL)inv(n) % mod * inv(i) % mod;
fact[i] = fact[i - 1] * (LL)i % mod;
infact[i] = infact[i - 1] * (LL)inv(i) % mod;
vb[i] = vb[i - 1] * (LL)inv(n) % mod;
}
for (int j = 0; j < M; ++j) {
for (int k = 0; k < n; ++k) {
b[j][k] = inv(inv(mod + 1 - k * (LL)inv(n) % mod, j + 1)) * (LL)fact[j] % mod;
}
}
f[0][0][0] = 1;
int sz = 0, sx = 0, se = 0;
for (int l = m + 1; l <= n; ++l) {
for (int i = 0; i <= sz; ++i) {
for (int j = 0; j <= sx; ++j) {
for (int k = 0; k <= se; ++k) {
const int &v = f[i][j][k];
if (!v) {
continue;
}
Add(g[i + a[l]][j][k + 1], v);
for (int t = 0; t < a[l]; ++t) {
Fav(g[i + a[l]][j + t][k], mod - va[t], v);
Fav(g[i + t][j + t][k], va[t], v);
}
}
}
}
sz += a[l], sx += a[l], se += 1;
memcpy(f, g, sizeof f);
memset(g, 0, sizeof g);
}
memcpy(bac, f, sizeof f);
for (int w = 1; w <= m; ++w) {
memcpy(f, bac, sizeof f);
for (int l = 1; l <= m; ++l) {
if (w == l) {
continue;
}
for (int i = 0; i <= sz; ++i) {
for (int j = 0; j <= sx; ++j) {
for (int k = 0; k <= se; ++k) {
const int &v = f[i][j][k];
if (!v) {
continue;
}
Add(g[i + a[l]][j][k + 1], v);
for (int t = 0; t < a[l]; ++t) {
Fav(g[i + a[l]][j + t][k], mod - va[t], v);
}
}
}
}
sz += a[l], sx += a[l], se += 1;
memcpy(f, g, sizeof f);
memset(g, 0, sizeof g);
}
memcpy(g, f, sizeof g);
int l = w;
for (int i = 0; i <= sz; ++i) {
for (int j = 0; j <= sx; ++j) {
for (int k = 0; k <= se; ++k) {
const int &v = g[i][j][k];
if (!v) {
continue;
}
if (k >= n) {
LOG("ERR\n");
while (true);
}
/* LOG("%d %d %d %d %d\n", l, i + a[l], j + a[l] - 1, k, v);
LOG("%d\n", b[j + a[l] - 1][k]); */
res[i + a[l]] = (res[i + a[l]] + b[j + a[l] - 1][k] * (LL)v % mod * vb[a[l]] % mod * infact[a[l] - 1]) % mod;
}
}
}
memset(g, 0, sizeof g);
}
for (int i = 1; i <= suma; ++i) {
Add(res[i], res[i - 1]);
printf("%d ", res[i]);
}
puts("");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 28ms
memory: 48120kb
input:
5 3 1 1 1 1 1
output:
0 0 299473306 199648871 1
result:
ok 5 tokens
Test #2:
score: 0
Accepted
time: 64ms
memory: 48044kb
input:
8 5 3 5 3 2 2 5 4 4
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 851829480 293319617 603094447 451112091 433952646 112377604 425219038 332689344 62257787 407546627 163509571 467949711 235335868 1
result:
ok 28 tokens
Test #3:
score: -100
Time Limit Exceeded
input:
30 17 1 8 9 3 2 6 6 9 5 9 1 2 1 3 3 1 1 5 7 1 2 5 5 7 3 3 4 7 5 6