P14254 分割(divide)题解
ArisakaMashiro · · 题解
题目中的这个式子:
是我们破局的关键。
我们不妨记
我们已知一棵子树中最小的深度不可能比根节点还小,也就是说对于一棵以
那么集合
那么所有的
那么题面就已经被我们简化许多了,我们可以枚举每一个可能的
由于
假定
那么据此,我们就可以写出一段区间的贡献:
设
特殊地,在
对上式求和即可。
:::info[代码如下]
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 998244353, N = 1100000;
int A(int a, int b){
if(a < b) return 0;
ll rtv = 1;
for(int i = a, cnt = 0; cnt < b; cnt++, i--) rtv = rtv * i % mod;
return rtv;
}
vector<int> nodes[N], ech_max_depth[N];
int n, k, f[N], depth[N], global_max_depth, a, sum_a, here_max_depth, ans;
int dfs(int p, int fa){
depth[p] = depth[fa] + 1;
int max_depth = depth[p];
for(int i : nodes[p]){
if(i == fa) continue;
max_depth = max(max_depth, dfs(i, p));
}
ech_max_depth[depth[p]].push_back(max_depth);
global_max_depth = max(max_depth, global_max_depth);
return max_depth;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for(int i = 2; i <= n; i++) cin >> f[i], nodes[f[i]].push_back(i);
dfs(1, 0);
for(int i = 1; i <= global_max_depth; i++) sort(ech_max_depth[i].begin(), ech_max_depth[i].end()), ech_max_depth[i].push_back(0);
for(int i = 1; i <= global_max_depth; i++){
int max_size = ech_max_depth[i].size() - 1;
if(max_size <= k) continue;
sum_a = a = 0, here_max_depth = ech_max_depth[i][0];
for(int j = 0; sum_a + k < max_size; j++){
if(ech_max_depth[i][j] == here_max_depth) { a++; continue; }
int b = max_size - sum_a - a;
ans += a * ((A(a + b - 1, k - 1) - (b == k - 1 ? 0 : A(b, k - 1)) + mod) % mod) % mod;
ans %= mod;
sum_a += a, a = 1, here_max_depth = ech_max_depth[i][j];
}
}
cout << (ans + mod) % mod;
}
:::