题意
给出两个正整数 \(N,K\),求有多少有理数集满足以下所有条件
- 集合有且只有 \(N\) 个元素,并且元素和为 \(K\);
- 每个元素须可表示为 \( \frac {1}{2^{i}}\) $(i\in N) $.
sol
考虑 dp,容易想到记 \(f_{i,j}\) 表示选 \(i\) 个数恰好和为 \(j\)
考虑到会出现诸如 \(\dfrac{1}{2^k}\) 的情况,此时,可以将前缀整体除 \(2\),由于无视顺序,因此操作等价
即 \(f_{i,j} = f_{i-1,j-1} + f_{i,2\cdot j}\)
代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 5005, mod = 998244353;
int f[N][N];
int n, k;
int main(){
scanf("%d%d", &n, &k);
f[0][0] = 1;
for (int i = 1; i <= n; i ++ ) {
for (int j = i; j; j -- ) {
f[i][j] = (f[i][j] + f[i - 1][j - 1]) % mod;
if (j * 2 <= i) f[i][j] = (f[i][j] + f[i][j * 2]) % mod;
}
}
printf("%d\n", f[n][k]);
return 0;
}
标签:arc107,Multisets,Number,int,lnsyoj2378,include
From: https://www.cnblogs.com/XiaoJuRuoUP/p/-/lnsyoj2378_luoguAT_arc107_d