[BZOJ1042][HAOI2008]硬币购物-容斥原理

硬币购物

Description

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

Input

  第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s,其中di,s<=100000,tot<=1000

Output

  每次的方法数

Sample Input

1 2 5 10 2
3 2 3 1 10
1000 2 2 2 900

Sample Output

4
27


哇还有这种操作
新的容斥姿势get


思路:
考虑没有限制的情况下,令 f[i] s=i 时的答案。
则很显然 f[i]=f[icj] (j=1,2,3,4)

考虑有限制的情况下,则答案可以表示为:所有方案-不合法方案。
考虑容斥,则答案=所有方案-至少一个不合法的方案+至少两个不合法的方案-…
可以发现,对于至少一个不合法,可以取 4j=1f[ic[j](d[j]+1)] 的值作为其方案,因为很明显这样做至少使 c[j] 的数量至少为 d[j]+1 ,即强制令j不合法,其他咱不管,后面会容斥掉的。
对于至少两个或以上的方案不合法,也可以用这种方法。
预处理 f <script type="math/tex" id="MathJax-Element-977">f</script>数组后直接算即可~

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

typedef long long ll;

inline int read()
{
    int x=0;char ch=getchar();
    while(ch<'0' || '9'<ch)ch=getchar();
    while('0'<=ch && ch<='9')x=x*10+(ch^48),ch=getchar();
    return x;
}

const int N=100009;

int c[9],d[9],s,tot;
ll f[N],ans;

inline void dfs(int dep,int fl,int sum)
{
    if(sum<0)return;
    if(dep==5)
    {
        ans+=fl*f[sum];
        return;
    }
    dfs(dep+1,fl,sum);
    dfs(dep+1,-fl,sum-c[dep]*(d[dep]+1));
}    

int main()
{
    for(int i=1;i<=4;i++)
        c[i]=read();
    tot=read();

    f[0]=1;

    for(int i=1;i<=4;i++)
        for(int j=c[i];j<N;j++)
            f[j]+=f[j-c[i]];

    for(int i=1;i<=tot;i++)
    {
        for(int j=1;j<=4;j++)
            d[j]=read();
        s=read();
        ans=0;
        dfs(1,1,s);
        printf("%lld\n",ans);
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值