Poj 1742 Coins (Backpack)

Source: Internet
Author: User

The number of coins with n denominations and the number of each of these coins must be calculated (less than or equal to m)

Ps: the method of using multiple backpacks timed out on poj (line hdoj), and then I saw a method on the Internet, use f [j] to indicate the sum of money that can constitute the value of j (0 and 1 ), use used [j] to indicate how many times A [I] is used when the current face value is j.
Code:
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Int I = 0, j = 0, n = 0, m = 0, ans = 0, A [102], C [102], f [100002], used [100002];
While (scanf ("% d", & n, & m), n + m)
{
Memset (f, 0, sizeof (f ));
For (I = 0; I <n; I ++)
Scanf ("% d", & A [I]);
For (I = 0; I <n; I ++)
Scanf ("% d", & C [I]);
F [0] = 1;
For (I = 0; I <n; I ++)
{
For (j = 0; j <= m; j ++) // all initialization, indicating that A [I] 0 times is used when the current face value is j.
Used [j] = 0;
For (j = A [I]; j <= m; j ++)
{
If (! F [j] & f [j-A [I] & used [j-A [I] <C [I]) // when f [j] is 1, used [j] = 0
 
{
F [j] = 1;
Used [j] = used [j-A [I] + 1;
}
}
}
Ans = 0;
For (I = 1; I <= m; I ++)
If (f [I])
Ans ++;
Printf ("% d \ n", ans );
}
Return 0;
}


Multiple backpacks
Code:
# Include <stdio. h>
# Include <string. h>
Int n = 0, m = 0, A [102], C [102], dp [100002];
Void zero_one (int weight, int value)
{
Int j = 0;
For (j = m; j> = weight; j --)
Dp [j] = dp [j]> dp [j-weight] + value? Dp [j]: dp [j-weight] + value;
}
Int main ()
{
Int I = 0, j = 0, k = 0, sum = 0, ans = 0;
While (scanf ("% d", & n, & m), n + m)
{
Memset (dp, 0, sizeof (dp ));
Ans = 0;
For (I = 0; I <n; I ++)
Scanf ("% d", & A [I]);
For (I = 0; I <n; I ++)
Scanf ("% d", & C [I]);
For (I = 0; I <n; I ++)
{
Sum = A [I] * C [I];
If (sum> m) // full backpack
For (j = A [I]; j <= m; j ++)
Dp [j] = dp [j]> dp [j-A [I] + A [I]? Dp [j]: dp [j-A [I] + A [I];
Else
{
K = 1;
While (k <C [I]) // binary splitting
{
Zero_one (k * A [I], k * A [I]);
C [I]-= k;
K * = 2;
}
Zero_one (C [I] * A [I], C [I] * A [I]);
}
}
For (I = 1; I <= m; I ++)
If (dp [I] = I)
Ans ++;
Printf ("% d \ n", ans );
}
Return 0; www.2cto.com
}


Author: ulquiorra0cifer

Related Article

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.