P4707 重返现世 题解
Californium_qwq · · 题解
这题的前置知识我竟然不会\ll
发现题目求
对于
然后套个期望,变成扩展
然后就可以愉快的写
考虑 DP,发现
而
我们将它们加到状态里,设
转移为:
然后就可以愉快的写
考虑如何优化,发现
即
首先
然后是
当然,组合值还要维护选的数量,所以加上组合数系数这一维,转移为:
然后就是快乐代码时间O(∩_∩)O ::::success[Loading……]
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 10;
const int M = 1e4 + 10;
const int K = 1e1 + 10;
const int P = 998244353;
int n, m, d, p[N], inv[M], dp[M][K], ans;
void add(int &x, int y) {
x += y;
if(x >= P) x -= P;
}
int qpow(int x, int y) {
int res = 1;
for(; y; x = x * x % P, y >>= 1)
if(y & 1) res = res * x % P;
return res;
}
signed main(void) {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> d >> m; d = n - d + 1;
for(int i = 1; i <= n; i++) cin >> p[i];
for(int i = 1; i <= m; i++) inv[i] = qpow(i, P - 2);
inv[0] = dp[0][0] = 1; // 这里预处理扩展了组合数的定义
for(int i = 1; i <= n; i++) {
for(int j = m; j >= p[i]; j--) {
for(int k = d; k; k--) {
int tmp = dp[j - p[i]][k - 1] - dp[j - p[i]][k];
add(dp[j][k], (tmp + P) % P);
}
}
}
for(int i = 0; i <= m; i++)
add(ans, dp[i][d] * inv[i] % P);
cout << ans * m % P; // 最终期望要乘 m
}
::::