题解:AT_arc107_d [ARC107D] Number of Multisets

· · 题解

Solution

这题其实很好想到用 DP。设 DP_{i,j} 表示选 i 个数和为 j 的情况。由于题目中到只能放入值 2^{-x} 的数,那么 i、j 一定满足 i \geq j。

现在考虑如何转移。

注意 j 要从大到小操作,因为要先求出 DP_{i,j \times 2}。

Code

#include<bits/stdc++.h>
using namespace std;
int N, K;
int DP[5005][5005];
signed main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cin >> N >> K, DP[0][0] = 1;
    for (register int i = 1; i <= N; ++i) for (register int j = i; j >= 1; j--) {
            DP[i][j] = DP[i - 1][j - 1];
            if ((j << 1) <= i) DP[i][j] = (DP[i][j] + DP[i][j << 1] > 998244353 ? DP[i][j] + DP[i][j << 1] - 998244353 : DP[i][j] + DP[i][j << 1]);
        }
    cout << DP[N][K] << endl;
    return 0;
}