原题链接:https://www.luogu.com.cn/problem/P1077
题意解读:n种花选m个的选法,每种花数量为ai。
解题思路:
设dp[i][j]表示前i种花选j个的选法
对于第i种花,可以选0,1,2...min(ai, j)个
则有递推式:dp[i][j] = ∑dp[i-1][j-k],k取0,1,2...min(ai, j)
初始化dp[0][0] = 1
100分代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 105, MOD = 1e6 + 7;
int n, m;
int a[N];
int dp[N][N]; //dp[i][j]表示i种花摆j盆的摆法
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
dp[0][0] = 1;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= m; j++)
{
for(int k = 0; k <= a[i] && k <= j; k++)
{
dp[i][j] = (dp[i][j] + dp[i-1][j-k]) % MOD;
}
}
}
cout << dp[n][m];
return 0;
}
标签:摆花,NOIP2012,int,洛谷题,选法,ai,种花,P1077,dp From: https://www.cnblogs.com/jcwy/p/18145952