CoinsTime Limit: 3000 MS Memory Limit: 30000 KTotal Submissions: 25827 Accepted: 8741 DescriptionPeople 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 h E 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. inputThe 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 c Ontains 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. outputFor each test case output the answer on a single line. sample Input3 101 2 4 2 1 12 51 4 2 10 0 Sample Output84 question: give several coins of the nominal value and the corresponding number to see if each face value in 1-m can be obtained. Method 1: Multiple backpacks
Import java. io. *; import java. util. *;/*** author: deng_hui_long * Date: 2013-8-31 **/public class Main {int n, m; int dp [] = new int [1000001]; public static void main (String [] args) {new Main (). work ();} void work () {vertex SC = new vertex (new BufferedInputStream (System. in); while (SC. hasNext () {n = SC. nextInt (); m = SC. nextInt (); if (n = 0 & m = 0) break; Node node = new Node (); Arrays. fill (dp, 0); for (int I = 0; I <n; I ++) {node. a [I] = SC. nextInt () ;}for (int I = 0; I <n; I ++) {node. c [I] = SC. nextInt () ;}for (int I = 0; I <n; I ++) {multiplePack (node. a [I], node. a [I], node. c [I]);} int ans = 0; for (int I = 1; I <= m; I ++) {if (dp [I] = I) ans ++;} System. out. println (ans) ;}}// void multiplePack (int cost, int weight, int amount) {if (cost * amount> = m) // greater than the maximum value, completePack (cost, weight) by full backpack; else {// smaller than maximum value, int k = 1 by 01; while (k <amount) {zeroOnePack (k * cost, k * weight); amount-= k; k <= 1; // The right one, multiply by 2} zeroOnePack (amount * cost, amount * weight) ;}}// void completePack (int cost, int weight) {for (int I = cost; I <= m; I ++) {dp [I] = Math. max (dp [I], dp [I-cost] + weight) ;}// 01 backpack void zeroOnePack (int cost, int weight) {for (int I = m; i> = cost; I --) {dp [I] = Math. max (dp [I], dp [I-cost] + weight) ;}} class Node {int a [] = new int [n]; int c [] = new int [n];}
Method 2: Scroll Array
Import java. io. *; import java. util. *;/*** author: deng_hui_long * Date: 2013-8-31 **/public class Main {int n, m, MAX = 100001; boolean dp [] = new boolean [MAX]; public static void main (String [] args) {new Main (). work ();} void work () {vertex SC = new vertex (new BufferedInputStream (System. in); while (SC. hasNext () {n = SC. nextInt (); m = SC. nextInt (); if (n = 0 & m = 0) break; Node node = new Node (); for (in T I = 0; I <n; I ++) {node. a [I] = SC. nextInt () ;}for (int I = 0; I <n; I ++) {node. c [I] = SC. nextInt ();} Arrays. fill (dp, false); dp [0] = true; int ans = 0; // The scrolling array for (int I = 0; I <n; I ++) {int u [] = new int [MAX]; for (int j = node. a [I]; j <= m; j ++) {if (! Dp [j] & dp [j-node.a [I] & node. c [I]> u [j-node.a [I]) {dp [j] = true; u [j] = u [j-node.a [I] + 1; ans ++ ;}} System. out. println (ans) ;}} class Node {int a [] = new int [n]; int c [] = new int [n] ;}}