HDU 2844 coins (motion gauge)

Source: Internet
Author: User
Coins Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 6904 accepted submission (s): 2809



Problem descriptionwhuacmers use coins. they have coins of value A1, A2, a3... an silverland dollar. one day hibix opened purse 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.
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 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.
Outputfor 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
 
Source2009 multi-university training contest 3-host by whu
Recommendgaojie | we have carefully selected several similar problems for you: 2845 2191 2870 2577

This is a good question! At the beginning, writing with the original multiple backpacks always times out. Later I heard that binary optimization can be used. I just copied the information to get rid of it !~
Question:
Give you a silver coin's nominal value and corresponding number, then give a number M and ask how many coins from 1 to m can be expressed with existing coins.

A simple multi-bag, using binary to optimize the face value of a coin. Then, determine the number of packages that meet B [I] = I;

A [I] indicates the value of the coin, and B [I] indicates the number of coins.

# Include <iostream> # include <algorithm>Using namespaceSTD;# Define M 100005IntDP[M],Vis[M]; // If you want to use a 01 backpack, the system will time out... IntA[105],B[105]; IntN,TOT,M; IntMax(IntX, IntY) {ReturnX>Y?X:Y;} // Baidu income, worship. VoidCompletepack(IntCost, IntWeight) // If you cannot finish it, it is a complete backpack. {For (intI=Cost;I<=M;I++)DP[I] =Max(DP[I],DP[I-Cost] +Weight);} VoidZeroonepack(IntCost, IntWeight) // If it can be obtained, it is the 01 backpack. {For (intI=M;I> =Cost;I--)DP[I] =Max(DP[I],DP[I-Cost] +Weight);} VoidMultiplypack(IntCost, IntWeight, IntAmount) // Multiple backpacks. {If (Cost*Amount> =M)// Not complete. Completepack(Cost,Weight); Else {intK=1; While (K<Amount){Zeroonepack(K*Cost,K*Weight);// The legendary binary optimization, indicating that it is not clear. I think it is a time to take 1, 2, 4, 8... Coin, multiply thoughts. Amount-=K;K<=1;}Zeroonepack(Amount*Cost,Amount*Weight); // Calculate the remaining items here. } Int main (intI, IntJ, IntK) {While (Scanf("% D",&N,&M)! =EOF&&N&&M){TOT=K=0; (I=1;I<=N;I++)Scanf("% D",&A[I]); (I=1;I<=N;I++)Scanf("% D",&B[I]);Memset(DP,0, Sizeof (DP); (I=1;I<=N;I++)Multiplypack(A[I],A[I],B[I]); (I=1;I<=M;I++) If (DP[I] =I)TOT++;Printf("% D \ n",TOT);} Return0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.