这题的前置知识我竟然不会\ll
发现题目求 ,相当于 ,于是 。
对于 ,可以使用 容斥:
然后套个期望,变成扩展 容斥:
然后就可以愉快的写 暴力了。
考虑 DP,发现 只和 有关。
而 只和 与合法的 的数量有关。
我们将它们加到状态里,设 表示决策到第 个数字, 且 的合法 的数量。
转移为:。
然后就可以愉快的写 暴力了。
考虑如何优化,发现 这一维可以忽略。
即 表示 乘上 的数量的值。
首先 是好处理的,乘 即可。
然后是 ,而组合数是可以递推的,由于是乘法,可以将组合式与其他值一起递推。
当然,组合值还要维护选的数量,所以加上组合数系数这一维,转移为:
然后就是快乐代码时间O(∩_∩)O
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
}