Coins
Time Limit:3000 MS |
|
Memory Limit:30000 K |
Total Submissions:25745 |
|
Accepted:8717 |
Description
People in Silverland use coins. they have coins of value A1, A2, a3... an Silverland dollar. one day Tony opened his money-box 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 wocould 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.
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
Two zeros.
Output
For each test case output the answer on a single line.
Sample Input
3 101 2 4 2 1 12 51 4 2 10 0
Sample Output
84
Source
LouTiancheng @ POJ is another question about multiple parts and problems in the book. Use dp [I] [j] to obtain j with the first I, and the remaining I items are the most. In this way, if j can be obtained by adding the number of front I-1, the number of I can be left c [I. In addition, if k (k> 0) is left for the first I type when j-a [I] is added, with this I-type number plus j, the I-type number can be left with the K-1. For details about the recurrence formula, see the code. Because the memory for this question is not large, we need to use a rolling array.
#include<cstdio>#include<iostream>#include<algorithm>#include<vector>#include<cstring>#include<queue>#include<stack>using namespace std;const int maxn = 100 + 5;const int maxm = 100000 + 5;const int INF = 2000000000;const int Mod = 1000000000;typedef pair<int,int> P;typedef long long LL;int dp[2][maxm];int a[maxn],c[maxn];int vis[maxm];int main(){ int n,m; while(scanf("%d%d",&n,&m)){ if(n == 0 && m == 0) break; for(int i = 1;i <= n;i++) scanf("%d",&a[i]); for(int i = 1;i <= n;i++) scanf("%d",&c[i]); memset(dp,-1,sizeof(dp)); memset(vis,0,sizeof(vis)); dp[0][0] = 0; for(int i = 1;i <= n;i++){ for(int j = 0;j <= m;j++){ if(dp[(i+1)&1][j] >= 0) dp[i&1][j] = c[i]; else if(j < a[i] || dp[i&1][j-a[i]] <= 0) dp[i&1][j] = -1; else dp[i&1][j] = dp[i&1][j-a[i]]-1; if(dp[i&1][j] != -1) vis[j] = 1; } } int ans = 0; for(int i = 1;i <= m;i++){ if(vis[i] == 1) ans++; } printf("%d\n",ans); } return 0;}