# BZOJ 1042 硬币购物(背包DP+容斥原理)

c[1]表示第一种硬币的币值。 剩下的同理。

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-9
# define MOD 12345678
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long LL;
int Scan() {
int res=0, flag=0;
char ch;
if((ch=getchar())=='-') flag=1;
else if(ch>='0'&&ch<='9') res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')  res=res*10+(ch-'0');
return flag?-res:res;
}
void Out(int a) {
if(a<0) {putchar('-'); a=-a;}
if(a>=10) Out(a/10);
putchar(a%10+'0');
}
const int N=100005;
//Code begin...

LL dp[N];
int c[5], d[5];

void init()
{
dp[0]=1;
FO(i,0,4) FO(j,c[i],N) dp[j]=dp[j]+dp[j-c[i]];
}
int main ()
{
scanf("%d%d%d%d%d",c,c+1,c+2,c+3,c+4);
init();
while (c[4]--) {
scanf("%d%d%d%d%d",d,d+1,d+2,d+3,d+4);
LL ans=dp[d[4]];
FO(i,1,16) {
int tot=0, tmp=0;
FO(j,0,4) if (i&(1<<j)) ++tot, tmp+=(d[j]+1)*c[j];
if (tmp<=d[4]) ans+=((tot&1)?-dp[d[4]-tmp]:dp[d[4]-tmp]);
}
printf("%lld\n",ans);
}
return 0;
}
View Code

posted @ 2017-03-05 18:42  free-loop  阅读(55)  评论(0编辑  收藏