P5853 [USACO19DEC] Tree Depth P
拆贡献,点
研究一下,
考虑从逆序对数为
若
现在枚举一下这个
除掉之后,最小值可以添加在左右端点,因此要对应乘上
算
负项默认为
滑动窗口维护,取出
#include<bits/stdc++.h>
#define endl '\n'
//#define MSOD
using namespace std;
using ll = long long;
const int N = 3e2 + 5;
int n, k, M;
int ans[N], f[2][N * N], g[N * N], h[N * N];
void add(int &x, int y) {
x += y;
if(x >= M) x -= M;
return;
}
void TEST() {
cin>>n>>k>>M;
f[1][0] = 1;
fill(g, g + n * n + 1, 1);
for(int i = 2 ; i <= n ; i ++) {
int p = i & 1, q = (i & 1) ^ 1;
fill(f[p], f[p] + n * n + 1, 0);
for(int j = 0 ; j <= n * n ; j ++) {
f[p][j] = g[j];
if(j - i + 1 > 0) add(f[p][j], M - g[j - i]);
}
g[0] = f[p][0];
for(int j = 1 ; j <= n * n ; j ++) {
g[j] = g[j - 1] + f[p][j];
if(g[j] >= M) g[j] -= M;
}
}
for(int t = 1 ; t < n ; t ++) {
fill(h, h + n * n + 1, 0);
h[0] = 1;
for(int i = 1, hd = 0, tl = 0, sm = 1 ; i <= n * n ; i ++) {
if(tl < i - 1) add(sm, h[++ tl]);
if(hd < i - t) add(sm, M - h[hd ++]);
h[i] = f[n & 1][i] + M - sm;
if(h[i] >= M) h[i] -= M;
}
add(ans[t + 1], h[k]);
if(k >= t) add(ans[1], h[k - t]), add(ans[n - t + 1], M - h[k - t]);
}
for(int i = 1 ; i <= n ; i ++) {
add(ans[i], ans[i - 1]);
int x = ans[i] + f[n & 1][k];
if(x >= M) x -= M;
cout<<x<<" ";
}
return;
}
signed main() {
ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
int TC = 1;
#ifdef MSOD
cin>>TC;
#endif
while(TC --) TEST();
return 0;
}