Problem Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
3 10 1 2 4 2 1 1 2 5 1 4 2 1 0 0
Sample Output
84
强行使用所有硬币加和数目作为dp循环,是恰好装满背包问题,除了dp[0]以外全部初始化为一个极大值,加和所能达到的数值都会被赋予所需最小硬币数目,而不能通过加和得到的仍为极大值,最后判断不为极大值的数都是我们可以支付的数
#include #include #include #define ma 110 #define maxn 100010 int dp[maxn]; int a[ma]; int m[ma]; using namespace std; //到达当前金额所需要的z最小金币数目 //恰好装满问题 //达不到这个值,这个值只会是最初初始化的值 int main() { int n, M; while(~scanf("%d%d", &n, &M)) { if(n == 0 && M == 0) break; for(int i = 0; i < n; i++) { scanf("%d", &a[i]); } int sum = 0; for(int i = 0; i < n; i++) { scanf("%d", &m[i]); if(sum < M) { for(int j = 0; j < m[i]; j++) { //所给硬币所能加和得到的最大的值 sum += a[i]; if(sum > M) { sum = M; break; } } } } fill(dp, dp + sum + 1, maxn); dp[0] = 0; //printf("%lld", sum); //二进制优化(HDU1114) for(int i = 0; i < n; i++) { int t = 1; int flag; while(t <= m[i]) { flag= 1; //哈哈哈,逆序 for(int k = sum; k >= t * a[i]; k--) { if(dp[k] > dp[k - t * a[i]] + t) { dp[k] = dp[k - t * a[i]] + t; flag = 0; } } if(flag) break; m[i] -= t; t = t * 2; } if(flag) continue; if(m[i]) { for(int k = sum; k >= m[i] * a[i]; k--) { if(dp[k] > dp[k - m[i] * a[i]] + m[i]) { dp[k] = dp[k - m[i] * a[i]] + m[i]; } } } } int ans = 0; for(int k = 1; k <= sum; k++) { if(dp[k] != maxn) { ans += 1; } } printf("%d\n", ans); } return 0; }