Source: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1276
Solution report:
If f [k] = 1, it indicates that the amount of money can be K.
Starting from the first D1, F [1 * N1] = 1, F [2 * N1] = 1,..., F [D1 * N1] = 1
Then DK should add 1 * NK, 2 * NK ,.. DK * NK to get a new amount that can be made up of. In addition, to distinguish the new amount added in this round from the amount that can be made up in the previous round, set f [New amount] to 2. After a round ends, set it to 1 again (because I forgot this, WA once ).
# Include <iostream> <br/> using namespace STD; </P> <p> int main () <br/>{< br/> int cash; <br/> while (scanf ("% d", & cash )! = EOF) <br/>{< br/> int N; <br/> CIN> N; <br/> int * n = new int [N]; <br/> int * D = new int [N]; <br/> for (INT I = 0; I <n; I ++) <br/> CIN> N [I]> d [I]; <br/> int * f = new int [cash + 1]; <br/> F [0] = 1; <br/> for (INT I = 0; I <n; I ++) <br/> {<br/> for (int K = 0; k <cash + 1; k ++) <br/>{< br/> If (F [k] = 1) <br/>{< br/> for (Int J = 1; j <= N [I]; j ++) <br/>{< br/> int temp = J * d [I] + K; <br/> If (temp> cash) <br/> break; <br/> else if (F [temp]! = 1) <br/> F [temp] = 2; <br/>}< br/> for (int K = 0; k <cash + 1; k ++) <br/> If (F [k] = 2) <br/> F [k] = 1; <br/>}< br/> for (INT I = cash; I> = 0; I --) <br/>{< br/> If (F [I] = 1) <br/>{< br/> cout <I <Endl; <br/> break; <br/>}< br/>}
Appendix:
Cash machine
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:12018 |
|
Accepted:4078 |
Description
A bank plans to install a machine for cash withdrawal. the machine is able to deliver appropriate @ bills for a requested cash amount. the machine uses exactly n distinct Bill denominations, say DK, k = 1, n, and for each denomination DK the machine has a supply of NK bills. for example,
N = 3, n1 = 10, d1 = 100, n2 = 4, D2 = 50, N3 = 5, D3 = 10
Means the machine has a supply of 10 bills of @ 100 each, 4 bills of @ 50 each, and 5 bills of @ 10 each.
Call cash the requested amount of cash the machine shocould deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be specified tively delivered according to the available bill supply the machine.
Notes:
@ Is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc.
Input
The program input is from standard input. Each data set in the input stands for a participating transaction and has the format:
Cash n N1 D1 N2 D2... nn DN
Where 0 <= cash <= 100000 is the amount of cash requested, 0 <= n <= 10 is the number of Bill denominations and 0 <= NK <= 1000 is the number of available bills for the DK denomination, 1 <= dk <= 1000, k = 1, N. white spaces can occur freely between the numbers in the input. the input data are correct.
Output
For each set of data the program prints the result to the standard output on a separate line as shown in the examples below.
Sample Input
735 3 4 125 6 5 3 350633 4 500 30 6 100 1 5 0 1735 00 3 10 100 10 50 10 10
Sample output
73563000
Hint
The first data set designates a transaction where the amount of cash requested is @ 735. the machine contains 3 Bill denominations: 4 bills of @ 125, 6 Bills of @ 5, and 3 bills of @ 350. the machine can deliver the exact amount of requested cash.
In the second case the bill supply of the machine does not fit the exact amount of cash requested. the maximum cash that can be delivered is @ 630. notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash.
In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @ 0 and, therefore, the machine delivers no cash.