Luo Gu [P1450] Coins shopping

Title effect: given four kinds of coins and the number of the corresponding denomination, seeking to purchase goods element S is the number of the program number.

Solution:
Consider not limit the number of coins, then buy S $ Product number is the number of programs, this problem can be completely backpack pretreatment.
Consider inclusion and exclusion, namely: the total number of programs may be used - sum (A coin invalid program number) + sum (two kinds) - sum (three kinds) ...

code show as below

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
typedef long long LL;

LL dp[maxn];
int c[5],tot,s,d[5];

void prework(){
    dp[0]=1;
    for(int i=1;i<=4;i++)
        for(int j=c[i];j<=1e5;j++)
            dp[j]+=dp[j-c[i]];
}
int main(){
    for(int i=1;i<=4;i++)scanf("%d",&c[i]);
    scanf("%d",&tot);
    prework();
    while(tot--){
        for(int i=1;i<=4;i++)scanf("%d",&d[i]);
        scanf("%d",&s);
        LL ans=dp[s];
        for(int i=1;i<1<<4;i++){
            int cnt=1; LL sum=0;
            for(int j=1;j<=4;j++){
                if(i>>(j-1)&1){
                    cnt=-cnt,sum+=(LL)(d[j]+1)*c[j];
                }
            }
            if(sum>s)continue;
            ans+=cnt*dp[s-sum];
        }
        printf("%lld\n",ans);
    }
    return 0;
} 

Guess you like

Origin www.cnblogs.com/wzj-xhjbk/p/10990473.html