BZOJ1042: coin shopping (backpacks & allowances)

Source: Internet
Author: User
Tags time limit

1042: [HAOI2008] Coin shopping Time Limit:10 Sec Memory limit:162 MB
submit:2953 solved:1822
[Submit] [Status] [Discuss] Description

Coin shopping A total of 4 kinds of coins. The face value is C1,C2,C3,C4 respectively. Someone went to the store to buy something and went to the tot time. Take di ci coins at a time and buy s
I have something of value. How many methods of payment are available each time. Input

The first line C1,c2,c3,c4,tot below tot row d1,d2,d3,d4,s, where di,s<=100000,tot<=1000 Output

Number of methods per each Sample Input 1 2 5 10 2
3 2 3 1 10
1000 2 2 2 900 Sample Output 4
HINT

Source

idea: A very ingenious topic, in general, we are directly complete backpack that number of solutions, but this will time out, taking into account the type of coins, we should first calculate the number of unlimited numbers of options, and then use the principle of tolerance to eliminate illegal scheme number, b[i]+1 is the first type of coins illegal state.

# include <iostream>
# include <cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 1E5;
LL dp[maxn+30]={1}, A[5], b[5], S, ans;
void Dfs (int pos, LL sum, int cnt)
{
    if (sum < 0) return;
    if (pos = = 5)
    {
        if (cnt&1) ans-= dp[sum];
        else ans + + dp[sum];
        return;
    }
    DFS (pos+1, sum, CNT);
    DFS (pos+1, sum-(b[pos]+1) *a[pos], cnt+1);
int main ()
{
    int t;
    for (int i=1; i<=4; ++i) scanf ("%lld", &a[i));
    for (int i=1; i<=4; ++i) for
        (int j=0; j+a[i]<=maxn; ++j)
            dp[j+a[i]] = = Dp[j];
    scanf ("%d", &t);
    while (t--)
    {
        for (int i=1; i<=4; ++i) scanf ("%lld", &b[i]);
        scanf ("%lld", &s);
        Ans = 0;
        DFS (1, s, 0);
        printf ("%lld\n", ans);
    }
    return 0;
}


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.