[HAOI2008]硬币购物

[HAOI2008]硬币购物

题目描述

硬币购物一共有4种硬币。面值分别为c1,c2,c3,c4。某人去商店买东西,去了tot次。每次带di枚ci硬币,买si的价值的东西。请问每次有多少种付款方法。

输入输出格式

输入格式:

第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s

输出格式:

每次的方法数

输入输出样例

输入样例#1:
1 2 5 10 2
3 2 3 1 10
1000 2 2 2 900
输出样例#1:
4
27

说明

di,s<=100000

tot<=1000

[HAOI2008]

题解:
DP预处理+容斥原理

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long lol;
int c[5],d[5],tot;
lol ans,f[100005];
void dfs(int a,int s,int k)
{
    if(a==5)
    {
        k%=2;
        ans+=f[s]*((k)?(-1):(1));
        return;
    }
    if(c[a]*(d[a]+1)<=s)
        dfs(a+1,s-c[a]*(d[a]+1),k+1);
    dfs(a+1,s,k);
}
int main()
{
    int i,j;
    for(i=1;i<=4;i++)scanf("%d",&c[i]);scanf("%d",&tot);
    f[0]=1;
    for(i=1;i<=4;i++)
    {
        for(j=c[i];j<=100000;j++)
        f[j]+=f[j-c[i]];
    }
    while(tot--)
    {
        int s;
        for(i=1;i<=4;i++)scanf("%d",&d[i]);scanf("%d",&s);
        ans=0;
        dfs(1,s,0);
        printf("%lld
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/huangdalaofighting/p/7355127.html