P1077 [NOIP2012 普及组] 摆花
题目:
链接:https://www.luogu.com.cn/problem/P1077
总的来说就是和上题差不多?
记dp[i][j]为前i种花塞进了j的背包的种类,那么状态转移方程:
就是:dp[i][j] = dp[i-1][j] + dp[i-1]j-k
贴代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef unsigned long long ll;
using namespace std;
ll n, m;
ll ans;
const int NP = 1e6 + 7;
const int N = 1e3;
ll dp[N][N];
ll num[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++)cin >> num[i];
dp[0][0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= m; j++)
for (int k = 0; k <= min(num[i], (ll)j); k++)//这里注意
dp[i][j] = (dp[i][j] + dp[i - 1][j - k])%NP;//然后由于过大,每次都要求余,不影响结果
}
cout << dp[n][m] ;
return 0;
}