# 【bzoj1042】[HAOI2008]硬币购物

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;

#define MAXN 100010

typedef long long LL;

int n;
int x;
int a[7],b[7];

LL ans;

LL f[MAXN];

void dfs(int x,int k,int d)
{
if (d<0)
return ;
if (x==5)
{
if (k & 1)
ans-=f[d];
else
ans+=f[d];
return ;
}
dfs(x+1,k+1,d-(a[x]+1)*b[x]);
dfs(x+1,k,d);
}

int main()
{
for (int i=1;i<=4;i++)
scanf("%d",&b[i]);
scanf("%d",&n);
f[0]=1;
for (int i=1;i<=4;i++)
for (int j=b[i];j<=MAXN;j++)
f[j]+=f[j-b[i]];
for (int i=1;i<=n;i++)
{
for (int j=1;j<=4;j++)
scanf("%d",&a[j]);
scanf("%d",&x);
ans=0;
dfs(1,0,x);
printf("%lld\n",ans);
}
return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int c[5];
long long F[110000];
struct{long long operator[](int pos){return pos<0?0:F[pos];}}f;
int main(int argc, char *argv[])
{
int T;scanf("%d%d%d%d%d",&c[1],&c[2],&c[3],&c[4],&T);
F[0]=1;
for(int i=1;i<=4;i++)
for(int j=0;j<=100000;j++)
if(j+c[i]<=100000)F[j+c[i]]+=F[j];
while(T--)
{
int d[5],s;scanf("%d%d%d%d%d",&d[1],&d[2],&d[3],&d[4],&s);
long long ans=f[s];
ans-=f[s-(d[1]+1)*c[1]];
ans-=f[s-(d[2]+1)*c[2]];
ans-=f[s-(d[3]+1)*c[3]];
ans-=f[s-(d[4]+1)*c[4]];
ans+=f[s-(d[1]+1)*c[1]-(d[2]+1)*c[2]];
ans+=f[s-(d[1]+1)*c[1]-(d[3]+1)*c[3]];
ans+=f[s-(d[1]+1)*c[1]-(d[4]+1)*c[4]];
ans+=f[s-(d[2]+1)*c[2]-(d[3]+1)*c[3]];
ans+=f[s-(d[2]+1)*c[2]-(d[4]+1)*c[4]];
ans+=f[s-(d[3]+1)*c[3]-(d[4]+1)*c[4]];
ans-=f[s-(d[1]+1)*c[1]-(d[2]+1)*c[2]-(d[3]+1)*c[3]];
ans-=f[s-(d[1]+1)*c[1]-(d[2]+1)*c[2]-(d[4]+1)*c[4]];
ans-=f[s-(d[1]+1)*c[1]-(d[3]+1)*c[3]-(d[4]+1)*c[4]];
ans-=f[s-(d[2]+1)*c[2]-(d[3]+1)*c[3]-(d[4]+1)*c[4]];
ans+=f[s-(d[1]+1)*c[1]-(d[2]+1)*c[2]-(d[3]+1)*c[3]-(d[4]+1)*c[4]];
#ifdef ONLINE_JUDGE
printf("%lld\n",ans);
#else
printf("%I64d\n",ans);
#endif
}
return 0;
}

posted @ 2016-03-25 21:04  Yangjiyuan  阅读(...)  评论(...编辑  收藏