BZOJ 1042:[HAOI2008]硬币购物 容斥原理 背包dp

1042: [HAOI2008]硬币购物

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2505  Solved: 1505
[Submit][Status][Discuss]

Description

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

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

把不需要限制硬币使用个数的搞出来

然后用容斥原理把超出限制的高出去


#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
inline ll read()
{
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return x*f;
}
const int N=100100;
ll c[5],d[5],f[N];
int main()
{
	ll n,T;
	for(int i=1;i<=4;i++)c[i]=read();T=read();
	f[0]=1;
	for(int j=1;j<=4;j++)for(int i=c[j];i<=N;i++)
	f[i]+=f[i-c[j]];
	while(T--)
	{
		for(int i=1;i<=4;i++)d[i]=read();n=read();
		ll ans=f[n];
		for(int i=1;i<=4;i++)if(n>=c[i]*(d[i]+1))
		ans-=f[n-c[i]*(d[i]+1)];
		for(int i=1;i<=4;i++)for(int j=i+1;j<=4;j++)
		if(n>=c[i]*(d[i]+1)+c[j]*(d[j]+1))
		ans+=f[n-c[i]*(d[i]+1)-c[j]*(d[j]+1)];
		for(int i=1;i<=4;i++)for(int j=i+1;j<=4;j++)for(int k=j+1;k<=4;k++)
		if(n>=c[i]*(d[i]+1)+c[j]*(d[j]+1)+c[k]*(d[k]+1))
		ans-=f[n-c[i]*(d[i]+1)-c[j]*(d[j]+1)-c[k]*(d[k]+1)];
		if(n>=c[1]*(d[1]+1)+c[2]*(d[2]+1)+c[3]*(d[3]+1)+c[4]*(d[4]+1))
		ans+=f[n-c[1]*(d[1]+1)-c[2]*(d[2]+1)-c[3]*(d[3]+1)-c[4]*(d[4]+1)];
		printf("%lld\n",ans);
	}
	return 0;
}
/*
1 2 5 10 2
3 2 3 1 10
1000 2 2 2 900

4
27
*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值