POJ 1742
Coins
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 27580 | Accepted: 9335 |
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 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
8 4
Source
定义dp[i]为目前为止装满i容量最多剩余多少个当前面值的
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 const int MAX_N = 105; 9 const int MAX_M = 1e5 + 7; 10 int N, M; 11 int v[MAX_N], c[MAX_N]; 12 int dp[MAX_M]; 13 14 void solve() { 15 dp[0] = 0; 16 for(int i = 1; i <= N; ++i) { 17 for(int j = 0; j <= M; ++j) { 18 if(dp[j] >= 0) { 19 dp[j] = c[i]; 20 } 21 else if(j < v[i] || dp[j - v[i]] <= 0) { 22 dp[j] = -1; 23 } else { 24 dp[j] = dp[j - v[i]] - 1; 25 } 26 27 } 28 } 29 30 31 int ans = 0; 32 for(int i = 1; i <= M; ++i) { 33 ans += dp[i] >= 0; 34 //printf("%d ",dp[i]); 35 } 36 37 printf("%d\n", ans); 38 } 39 int main() 40 { 41 // freopen("sw.in", "r", stdin); 42 while(~scanf("%d%d", &N, &M) && (N + M)) { 43 memset(dp, -1, sizeof(dp)); 44 for(int i = 1; i <= N; ++i) { 45 scanf("%d", &v[i]); 46 } 47 for(int i = 1; i <= N; ++i) { 48 scanf("%d", &c[i]); 49 } 50 51 solve(); 52 } 53 //cout << "Hello world!" << endl; 54 return 0; 55 }