Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300 points
Problem Statement
How many integer sequences of length N, A=(A1,…,AN), satisfy all of the conditions below?
-
1≤Ai≤M (1≤i≤N)
-
∑Ai≤K
Since the count can get enormous, find it modulo 998244353998244353.
Constraints
- 1≤N,M≤50
- N≤K≤NM
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K
Output
Print the answer.
Sample Input 1 Copy
Copy
2 3 4
Sample Output 1 Copy
Copy
6
The following six sequences satisfy the conditions.
- (1,1)(1,1)
- (1,2)(1,2)
- (1,3)(1,3)
- (2,1)(2,1)
- (2,2)(2,2)
- (3,1)(3,1)
Sample Input 2 Copy
Copy
31 41 592
Sample Output 2 Copy
Copy
798416518
Be sure to print the count modulo 998244353.
思路:非常经典的背包问题。
状态表示:dp[i][j]
表示当前背包的物品个数不超过i,且物品总体积为j的所有集合
属性:最大值(集合中数的最大值)
状态计算:考虑到dp[i][j]是由dp[i - 1][j - u]转移过来的,只有当j >= u 的时候,才有这个物品。
01背包的思考方式是:先不要最后一个物品,考虑当前状态和上一个状态加上物品价值 谁更大
这个题的思路是:我们枚举所有可以选择的上一种状态,累加到这个状态
见代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110, M = N * N, mod = 998244353;
int dp[N][M];//当前物品个数为i,总体积不超过j
int n, m, k;
int main()
{
cin >> n >> m >> k;
dp[0][0] = 1;//当前的物品个数是0,总价值是0,是一种方案
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= k; j ++ )
for (int u = 1; u <= m; u ++ )//每一个数是不能超过m
if (j >= u)
dp[i][j] = (dp[i][j] + dp[i - 1][j - u]) % mod;
int ans = 0;
for (int i = 1; i <= k; i ++ )//枚举每一种体积
ans = (ans + dp[n][i]) % mod;
cout << ans << endl;
return 0;
}