单点时限: 2.0 sec
内存限制: 256 MB
有 n 种面额不同的邮票,面额分别为 C1,C2,C3……Cn。面额 Ci 的邮票最多可以取 Mi 张。请问,用这些邮票,可以贴出多少面额不同的邮资 (包括 0)。贴邮票时,邮票不必全部使用。
输入格式
本题有多组数据,第一行为一个整数 t,表示有 t 组测试数据。
每组测试数据的第一行为一个整数 n,表示有多少种不同面额的邮票。(1<=N<=10)
第二行有 n 个整数,分别表示第 C1,C2….Cn 种不同面额。(1<=Ci<=20)
第三行有 n 个整数,分别表示面额为 Ci 的邮票有 Mi 张。(1<=Mi<=20)
输出格式
每组数据输出一行,每行一个整数,表示可以贴出多少种不同的面额。
样例
input
2
2
1 2
2 1
1
2
1
output
5
2
/*
思路:dp
dp[i]为1时则选择后面邮票面值c也将为1,注意从后往前遍历dp;
*/
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int c[21];
int m[21];
int dp[5000];
int main() {
int t;
cin>>t;
for(int i = 0; i < t; i++) {
cin>>n;
for(int j = 0; j < n; j++)
cin>>c[j];
for(int j = 0; j < n; j++)
cin>>m[j];
fill(dp,dp+5000,0);
for(int k = 0; k <= m[0]; k++)
dp[k*c[0]]=1;
for(int j = 1; j < n; j++) {
for(int k = 1; k <= m[j]; k++) {
for(int z=4999; z>=0; z--) {
if(dp[z]) {
dp[z+c[j]]=1;
}
}
}
}
int ans=0;
for(int j = 0; j < 5000; j++)
if(dp[j])
ans++;
cout<<ans<<endl;
}
return 0;
}