牛客多校(7) J题 Melborp Elcissalc & 杭电多校(7)1003 Counting Stickmen
这两场多校的两道计数dp
参考博客:
蔚来杯2022牛客暑期多校训练营7 CFGJ
“蔚来杯“2022牛客暑期多校训练营7 J题: Melborp Elcissalc
2022 杭电多校7 个人题解
#include<bits/stdc++.h>
using namespace std;
#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const int INF = 0X3f3f3f3f, N = 70, MOD = 998244353;
const double eps = 1e-7, pi = acos(-1);
int f[N][N][N * N], C[N][N];
inline void qadd(int&a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
void Init() {
C[0][0] = 1;
rep (i, 1, N - 1) {
C[i][0] = 1;
rep (j, 1, i) {
C[i][j] = C[i - 1][j - 1];
qadd(C[i][j], C[i - 1][j]);
}
}
}
void work() {
int n, k, t;
cin >> n >> k >> t;
f[0][0][0] = 1;
rep (i, 1, k) {
for (int j = 0; j <= n; j++) {
rep (u, 0, t) {
if (!f[i - 1][j][u]) continue;
for (int v = 0; v + j <= n; v++) {
if (i == 1) {
qadd(f[i][v + j][u + C[v + 1][2]], (LL)C[v + j][v] * f[i - 1][j][u] % MOD);
}
else {
qadd(f[i][v + j][u + C[v][2]], (LL)C[v + j][v] * f[i - 1][j][u] % MOD);
}
}
}
}
}
cout << f[k][n][t] << endl;
}
signed main() {
IO
Init();
int test = 1;
// cin >> test;
while (test--) {
work();
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define fr first
#define se second
#define et0 exit(0);
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i ++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i --)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const int INF = 0X3f3f3f3f, N = 5e5 + 10, MOD = 998244353;
const double eps = 1e-7, pi = acos(-1);
LL n, ans;
LL d[N];
LL hand[N], body[N];
vector<int> g[N];
LL qmi(LL a, LL b, LL p) {
LL base = a, res = 1;
while (b) {
if (b & 1) res = res * base % p;
base = base * base % p;
b >>= 1;
}
return res % p;
}
int C2(LL x) {
return x * (x - 1) / 2 % MOD;
}
void dfs(int u, int fa) {
LL b = 0, h = 0;
for (auto v : g[u]) {
b += body[v];
h += hand[v];
b %= MOD, h %= MOD;
if (v == fa) continue;
dfs(v, u);
}
if (d[u] < 4) return;
for (auto v : g[u]) {
LL res = (body[v] * (d[u] - 3) % MOD * ((C2((h - hand[v] + MOD) % MOD) - ((b - body[v] + MOD) % MOD) + MOD) % MOD)) % MOD;
ans = (ans + res) % MOD;
}
}
void work() {
cin >> n;
rep (i, 1, n) g[i].clear(), body[i] = hand[i] = d[i] = 0;
rep (i, 2, n) {
int x, y;
cin >> x >> y;
g[x].push_back(y), g[y].push_back(x);
d[x]++, d[y]++;
}
rep (i, 1, n) {
if (d[i] >= 2) hand[i] = d[i] - 1;
if (d[i] >= 3) body[i] = C2(d[i] - 1);
}
ans = 0;
dfs(1, 0);
cout << ans << endl;
}
signed main() {
IO
int test = 1;
cin >> test;
while (test--) {
work();
}
return 0;
}

浙公网安备 33010602011771号