QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#642119 | #4811. Be Careful | hhoppitree | RE | 0ms | 0kb | C++17 | 4.0kb | 2024-10-15 10:28:44 | 2024-10-15 10:28:48 |
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 205, P = 998244353;
struct MOD {
int operator ()(long long x) {
return x - (((__int128)x * 18479187002) >> 64) * P;
}
} mod;
vector<int> G[N];
int n, dp[N][N], sdp[N][N], f[2][1 << 18], g[1 << 18][N], C[N][N];
void dfs(int x, int fa = -1) {
int deg = 0, cnt = 0;
for (auto v : G[x]) {
if (v != fa) {
dfs(v, x);
for (int i = n; i >= 0; --i) {
sdp[v][i] = mod(sdp[v][i + 1] + dp[v][i]);
}
cnt += (G[v].size() == 1), ++deg;
}
}
if (!deg) {
fill(dp[x], dp[x] + n + 1, 1);
return;
}
vector< pair<int, int> > o;
for (auto v : G[x]) {
if (v != fa && G[v].size() != 1) {
int L = n;
while (!dp[v][L]) --L;
o.push_back({L, v});
}
}
sort(o.begin(), o.end());
int mn = o.size() + 1, ps = -1;
for (int i = 0; i < (int)o.size(); ++i) {
if (o[i].first + ((int)o.size() - i) < mn) {
mn = o[i].first + ((int)o.size() - i), ps = i;
}
}
int tz = (!~ps ? 0 : o[ps].first + 1);
for (int i = 0; i < 1 << tz; ++i) f[0][i] = !i;
for (int i = 0; i <= ps; ++i) {
for (int j = 0; j < 1 << tz; ++j) f[1][j] = f[0][j], f[0][j] = 0;
for (int j = (1 << (o[i].first + 1)) - 1; ~j; --j) {
for (int k = 0; k <= o[i].first; ++k) {
f[0][j | (1 << k)] = mod(f[0][j | (1 << k)] + 1ll * f[1][j] * dp[o[i].second][k]);
}
}
}
for (int i = 0; i < 1 << tz; ++i) if (f[0][i]) {
int z = o.size() - 1 - ps;
for (int j = 0; j < (1 << z); ++j) {
for (int k = 0; k <= cnt; ++k) g[j][k] = 0;
}
g[(1 << z) - 1][cnt] = f[0][i];
for (int j = 0; ; ++j) {
int ok = 0;
for (int k = 0; k < (1 << z); ++k) {
for (int l = 0; l <= cnt; ++l) {
if (g[k][l] %= P) ok = 1;
}
}
if (!ok) break;
for (int l = 0; l <= cnt; ++l) {
vector< pair<int*, int> > del;
for (int k = 0; k < (1 << z); ++k) if (g[k][l]) {
if (j >= 30 || !((i >> j) & 1)) {
del.push_back({&g[k][l], g[k][l]});
int mul = g[k][l];
for (int t = 1; t <= l; ++t) mul = mod(1ll * mul * (n - j));
for (int p = 0; p < z; ++p) {
if ((k >> p) & 1) {
mul = mod(1ll * mul * sdp[o[ps + 1 + p].second][j + 1]);
}
}
dp[x][j] = mod(dp[x][j] + mul);
}
}
for (int len = 2; len <= (1 << z); len <<= 1) {
int pos = o[ps + __builtin_ctz(len)].second;
for (int k = 0; k < (1 << z); k += len) {
for (int p = k; p < k + (len >> 1); ++p) {
g[p][l] = mod(g[p][l] + 1ll * g[p + (len >> 1)][l] * dp[pos][j]);
}
}
}
for (int k = 0; k < (1 << z); ++k) {
for (int L = 0; L <= l; ++L) {
g[k][L] = mod(g[k][L] + 1ll * g[k][l] * C[l][L]);
}
}
for (auto [x, y] : del) *x = mod(*x - y);
}
}
}
}
signed main() {
scanf("%d", &n);
for (int i = C[0][0] = 1; i <= n; ++i) {
for (int j = C[i][0] = 1; j <= i; ++j) {
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;
}
}
for (int i = 1, x, y; i < n; ++i) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1);
for (int i = 0; i <= n; ++i) printf("%d\n", (dp[1][i] % P + P) % P);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
5 1 2 1 3 2 4 2 5