poj1742Coins (multiple backpack)

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.
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


When doing this problem, the first so written:
#include <the iostream>
#include <cstdio>
the using namespace STD;
BOOL DP [100] [100001];
int n-, m, A [100], SU [100];
main int () {
the while (CIN >> >> n-m) {
IF (n-m == 0 && == 0) return 0;
for (int I = 0; I <n-; I ++)
for (int. 1 = J; J <= m; J ++)
DP [I] [J] = to false;
for (int I = 0; I <n-; I ++)
Scanf ( "% D", & A [I]), DP [I] [0] = true; // dp [i] [ j] is true, showing j element may scrape out, a [i] coin denomination
for (int I = 0; I <n-; I ++)
Scanf ( "% D", & SU [ i]); // su [i ] represents the denomination of a [i] of the coin quantity
for (int i = 1; i <= su [0] && a [0] * i <= m; i ++) // NOTE a [0] * i <= m, or there will Runtime error
DP [0] [A [0] * I] = to true;
for (int I =. 1; I <n-; I ++) {
for (int J = 1; j <= m;j++){
for(int k=1;k<=su[i];k++){
IF (JK * A [I]> = 0 && DP [I-. 1] [JK * A [I]]) = to true DP [I] [J];
the else DP [I] [J] = DP [I-. 1] [J];
}
}
}
int S = 0;
for (int I =. 1; I <= m; I ++) {
IF (DP [n--. 1] [I]) / * the printf ( "% D", I) , * / S ++;
}
the printf ( "% D \ n-", S);
}
}
so written words will time out, although the dp well understood, but the complexity of the dp is O (m (su [i] and) )
now attach the second AC codes:
#include <the iostream>
#include <cstdio>
#include <CString>
the using namespace STD;
int DP [2] [100005];
int main () {
int n-, m, VA [ 102], SU [102];
the while (CIN >> >> n-m) {
IF (n-m == 0 && == 0) return 0;
Memset (DP, -1, the sizeof (DP));
for (int I = 0; I <n-; I ++)
Scanf ( "% D", & VA [I]);// coin denomination
for (int i = 0; i <n; i ++)
scanf ( "% d", & su [i]); // denomination va [i] of the coin quantity
for (int i = 0; i <= su [0] && va [0] * i <= m; i ++ )
DP [0] [VA [0] * I] = SU [0] -i;
for (int I =. 1; I <n-; I ++) {
for (int J = 0; J <= m; J ++) {
IF (DP [(I-. 1)% 2] [J]> = 0) DP [% 2 I] [J] = SU [I];
the else IF (J <VA [I] || DP [I 2% ] [J-VA [I]] <= 0) DP [I% 2] [J] = -. 1;
the else DP [I% 2] [J] = DP [I% 2] [J-VA [I] ] -1;
}
}
int S = 0;
for (int I =. 1; I <= m; I ++)
IF (DP [(n--. 1)% 2] [I]> = 0) / * the printf ( "% D ", I), * / S ++;
the printf ("% D \ n-", S);
}
}
this complexity is O (nm),
DP [I] [J] represents the front i + 1 coin sum obtained j dollars when the i + 1 the number of coins remaining species.

 

 

Guess you like

Origin www.cnblogs.com/sunjianzhao/p/11423164.html