题目描述
小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共mm盆。通过调查顾客的喜好,小明列出了顾客最喜欢的nn种花,从11到nn标号。为了在门口展出更多种花,规定第ii种花不能超过a_iai盆,摆花时同一种花放在一起,且不同种类的花需按标号的从小到大的顺序依次摆列。
试编程计算,一共有多少种不同的摆花方案。
输入格式
第一行包含两个正整数nn和mm,中间用一个空格隔开。
第二行有nn个整数,每两个整数之间用一个空格隔开,依次表示a_1,a_2,…,a_na1,a2,…,an。
输出格式
一个整数,表示有多少种方案。注意:因为方案数可能很多,请输出方案数对10000071000007取模的结果。
输入输出样例
输入 #1复制
2 4 3 2
输出 #1复制
2
说明/提示
【数据范围】
对于20%数据,有0<n≤8,0<m≤8,0≤a_i≤80<n≤8,0<m≤8,0≤ai≤8;
对于50%数据,有0<n≤20,0<m≤20,0≤a_i≤200<n≤20,0<m≤20,0≤ai≤20;
对于100%数据,有0<n≤100,0<m≤100,0≤a_i≤1000<n≤100,0<m≤100,0≤ai≤100。
NOIP 2012 普及组 第三题
#include <iostream>
#include <cstdio>
#include <pthread.h>
using namespace std;
// 解题思路:dp, O(n^3), O(m)
// 类似01背包,定义dp[i][j]表示前i个数总和为j的方案数
inline void read(int &x)
{
register int f = 1;
x = 0;
register char c = getchar();
while (c > '9' || c < '0')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
x *= f;
}
const int maxn = 1e2 + 5;
const int mod = 1000007;
int cnt[maxn], dp[maxn];
void init(const int &n)
{
// 读入数据
for (int i = 1; i <= n; i++)
{
read(cnt[i]);
}
// 处理边界
dp[0] = 1;
}
int main()
{
int n, m;
read(n); read(m);
init(n);
for (int i = 1; i <= n; i++)
{
// 滚动数组逆序求解,类01背包
for (int j = m; j >= 0; j--)
{
// 枚举第i个数的取值
for(int k = 1; k <= min(j, cnt[i]); k++)
{
// 状态转移方程:
dp[j] = (dp[j - k] % mod + dp[j]) % mod;
}
}
}
printf("%d\n", dp[m]);
return 0;
}