[2019 CSP-S before training] [P1450] [Tacca Xx_queue Science DP] 2. Coins cart

Topic links: https://www.luogu.org/problem/P1450

Title Description

There are four kinds of shopping coin coin. Denominations of c1, c2, c3, c4. Someone go to the store to buy things, go tot times. Every time we take di ci gold coins to buy something of value si. Every time I ask how many kinds of payment methods.

Analysis: junior partner and I am sure you, like me, at first glance this question: this is not a multi-board backpack questions you ??

But, look at the range of data, so the practice of violence will not work ah, T fly away;

So what clever way to solve this question it?

If we say that for every inquiry, there is no limit to the number of coins that a backpack is completely right, I believe we must be able to look out for;

So we can not think about it, take the backpack completely lose that part does not meet the restrictions, it is not that the answer?

This is quite easy to handle, ah, not a legitimate solution is to \ (f [S-(D_J + 1) * C_J] \) (J for the first several coins), think about why?

I call this the first fixed coin to spend \ (d_j + 1 \) months, the results obtained in this way is certainly not legitimate, illegitimate nature is the number of programs: the sum deducted \ (d_j + 1 \) a \ ( C_J \) completely after several kinds of coins method backpack;

It is not very clever? (Yeah, I also saw a solution to a problem)

Therefore, the final answer is \ (F [S] - \ sum_ {J}. 1 ^ = {F}. 4 [S- (D_J + 1'd) * C_J] \) ;

So the answer to how to find it here there is a little bit small problem:? You use illegal scheme 1 coin there is an overlap with the coins you use illegal scheme 2, how to solve?

Inclusion-Exclusion look like; how inclusion-exclusion Refer to code?.

Cautions: 1 open long long 50 minutes is not only open pro-test;!

2. Pretreatment \ (F [0] =. 1 \) (0 argument will only put together a scheme: nothing at all), or program output is 0;

#include <bits/stdc++.h>
#define int long long
#define N (100000+5)
using namespace std;
int tot,ans,ss;
int c[5],d[5],dp[N];
void dfs(int k,int s,int p){//当前第k种硬币,剩余s元要凑,p为符号判断;
    if(s<0) return;//凑够了(多了),return
    if(k==5){//硬币种类已经选完
        ans+=dp[s]*p;//ans加一下,容斥
        return;
    }
    dfs(k+1,s,p);//符合限制的方案
    dfs(k+1,s-(d[k]+1)*c[k],-p);//不符合限制的方案,注意变号
}
signed main(){
     for(int i=1;i<=4;i++)scanf("%lld",&c[i]);
    scanf("%lld",&tot);
    dp[0]=1;
    for(int i=1;i<=4;i++){
        for(int j=c[i];j<=100000;j++){
            dp[j]=dp[j]+dp[j-c[i]];
        }
    }//完全背包
    for(int i=1;i<=tot;i++){
        ans=0;
        for(int j=1;j<=4;j++)scanf("%lld",&d[j]);
        scanf("%lld",&ss);
        dfs(1,ss,1);
        printf("%lld\n",ans);
    }
    return 0;
}

I hope everyone can understand it ......

Anyway, some of our big brothers to write a blog engine room is really obscure, full length is "...... can", "obviously ......";

Dalao really keep up with their thinking, ah, it seems I have to refuel ah!

Guess you like

Origin www.cnblogs.com/Xx-queue/p/11715604.html