BZOJ1042: [HAOI2008]硬币购物

Description
硬币购物一共有4种硬币。面值分别为c1,c2,c3,c4。某人去商店买东西,去了tot次。每次带di枚ci硬币,买si的价值的东西。请问每次有多少种付款方法。
Input
第一行 c1,c2,c3,c4,tot
下面tot行 d1,d2,d3,d4,s
Output
每次的方法数
Sample Input
1 2 5 10 2
3 2 3 1 10
1000 2 2 2 900
Sample Output
4
27
HINT
数据规模
di,s<=100000
tot<=1000
Source

基本思路:先预处理出来没有限制时的方案数(完全背包),然后利用容斥原理 ans= 价值为 s 时的方案数(超过限制)(一种硬币超过限制) + (两种硬币超过限制) (三种硬币超过限制)+ (四种硬币超过限制)。
这里可以用位运算(共 24 种)来枚举所有方案。
(注意:一定要开 longlong

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int S=100010;
long long f[S];
int c[10],d[10],s,tot;
void in(int &x)
{
    int f=1;x=0;char t=getchar();
    while((t<48)or(t>57)){if(t=='-')f=-1;t=getchar();}
    while((t>=48)and(t<=57)){x=x*10+t-48;t=getchar();}
    x*=f;
}
void work()
{
    f[0]=1;
    for (int i=1;i<=4;++i)
        for (int j=0;j<S;++j)
        if (j>=c[i]) f[j]+=f[j-c[i]];
}
int main()
{
    for (int i=1;i<=4;++i) in(c[i]);
    in(tot);work();
    while(tot)
    {
        for (int i=1;i<=4;++i) in(d[i]);
        in(s);long long ans=0;
        for (int i=0;i<(1<<4);++i)
        {
            int num=0,x=s;
            for (int j=1;j<=4;++j)
            if ((i>>(j-1))&1)//取出从后往前数第j位
            {
                x-=(d[j]+1)*c[j];
                ++num;
            }
            if (x>=0)
                if (num&1) ans-=f[x];
                else ans+=f[x];
        }
        printf("%lld\n",ans);
        --tot;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值