[HAOI????] 硬币购物

 硬币购物

 
题目描述 Description

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

输入描述 Input Description

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

输出描述 Output Description

每次的方法数

样例输入 Sample Input

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

样例输出 Sample Output

4
27

数据范围及提示 Data Size & Hint


di,s<=100000
tot<=1000

 

背包+容斥即可。

 

#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
using namespace std;
ll f[maxn],ans;
int a[10],ci[20],T,S;
int n,m,tmp[105],tot[105];

inline void dp(){
	f[0]=1;
	for(int i=0;i<4;i++)
	    for(int j=a[i];j<=100000;j++) f[j]+=f[j-a[i]];
	
	tmp[0]=1;
	for(int i=1;i<ci[4];i++) tmp[i]=-tmp[i^(i&-i)];
}

int main(){
	ci[0]=1;
	for(int i=1;i<=10;i++) ci[i]=ci[i-1]<<1;
	for(int i=0;i<4;i++) scanf("%d",a+i);
	dp();
	
	scanf("%d",&T);
	while(T--){
		for(int i=0;i<4;i++) scanf("%d",&tot[ci[i]]),tot[ci[i]]=((tot[ci[i]]+1)*a[i]);
		scanf("%d",&S);
		ans=0;
		
		for(int i=0;i<ci[4];i++){
			if((i&-i)!=i) tot[i]=tot[i&-i]+tot[i^(i&-i)];
			if(S>=tot[i]) ans+=tmp[i]*f[S-tot[i]];
		}
		
		printf("%lld\n",ans);
	}
	
	return 0;
}

  

 

转载于:https://www.cnblogs.com/JYYHH/p/8540159.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值