题目描述
小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共 m 盆。通过调查顾客的喜好,小明列出了顾客最喜欢的 nn种花,从 1 到 n 标号。为了在门口展出更多种花,规定第 i 种花不能超过 ai 盆,摆花时同一种花放在一起,且不同种类的花需按标号的从小到大的顺序依次摆列。
试编程计算,一共有多少种不同的摆花方案。
多重背包+求总方案数(小变种)
首先这是道动态规划问题,f[i][j]表示用前i种花摆j盆的方案数。
很显然f[i][j]+=f[i-1][j-k] ,k从0遍历到该种花总共的盆数。
注意f[0][0]=1,j从0开始。
//#include<bits/stdc++.h>
#include<iostream>
//#include<iomanip>
//#include<alogrithm>
using namespace std;
#define endl '\n'
const int M=1e6+7;
int n,m;
int a[200];
int f[200][200];
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
f[0][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
f[i][j]=f[i-1][j];
for(int k=1;k<=a[i] && k<=j;k++)
{
f[i][j]+=f[i-1][j-k]%M;
f[i][j]%=M;
}
}
}
cout<<f[n][m];
return 0;
}