HUD 2844 coins

Source: Internet
Author: User
Coins

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 4196 accepted submission (s): 1669


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



Statistic | submit | discuss | note

A [I] coin, each c [I], solves how many different denominations smaller than m can be formed.

It is equivalent to a [I]. For each type of C [I], we can solve the number of different backpacks that can be filled with less than M.

Do not cycle the 1-M backpack. In this way, only the TLE and poj will not be able to get stuck for 3 seconds.

You can consider the types of items. Consider which backpacks can be filled with each item.

Use the f array to represent the backpack. 0 indicates that the package is not full, and 1 indicates that the package is full.

If a [I] * C [I]> = m, it can be converted into multiple backpack problems. Otherwise, the problem can be solved using the 01 backpack and optimized using binary.

AC code (including parsing ):

# Include <stdio. h>
# Include <math. h>
# Define n 100001
Int A [n], C [N], B [1001]; // array B is used to store space occupied by binary optimized items.
Int M, F [N];
Void zeropack (int h)
{

int i,j,k;

For (k = 0; C [H]-(INT) Pow (2, K) + 1> 0; k ++ );
K --;
For (I = 0; I <K; I ++)
B [I] = A [H] * (INT) Pow (2, I );
B [k] = A [H] * (C [H]-(INT) Pow (2, K) + 1); // binary Optimization
For (I = K; I> = 0; I --) // select the k + 1 converted item
{
For (j = m; j> = B [I]; j --) // 01 backpack problem, only consider loading or not loading
F [J] = f [J] | f [J-B [I]; // the code can be simplified by using a single bit or operator.
}
}
Void compeletpack (int h)
{
Int J;
For (j = A [H]; j <= m; j ++) // since there are infinite numbers, then, 1st packages worth a [H] will certainly be full, and you only need to consider whether to install another package.
F [J] = f [J-A [H] | f [J];
}
Int main ()
{
Int I, num, N;
Scanf ("% d", & N, & M );
While (N | m)
{
Num = 0;
F [0] = 1;
For (I = 1; I <= N; I ++)
Scanf ("% d", & A [I]);
For (I = 1; I <= N; I ++)
Scanf ("% d", & C [I]);
For (I = 1; I <= m; I ++) // Initialization
F [I] = 0;
For (I = 1; I <= N; I ++) // select n items
{
If (A [I] * C [I]> = m)
Compeletpack (I); // for coins whose total value exceeds m, it can be considered as an unlimited number of coins
Else
Zeropack (I );
}
For (I = 1; I <= m; I ++)
Num + = f [I];
Printf ("% d \ n", num );
Scanf ("% d", & N, & M );
}
Return 0;
}

There is also a more concise method for the above binary optimization, you do not need to go to array B:

For (k = 1; k <= A [H]/2; k = (k <1 ))

{

For (j = m; j> = K * A [H]; j --)

F [J] = f [J] | f [J-K * A [H];

}

K = A [H] + 1-k;

For (j = m; j> = K * A [H]; j --)

{

F [J] = f [J] | f [J-K * A [H];

}

A has learned a lot from this question and is easy to digest.

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.