Subject
a natural idea is inclusion and exclusion.
If you buy only one coin case, the answer is no limit \ (s \) the number of program minus the mandatory use \ (d + 1 \) to buy gold under the circumstances \ (s \) of the program that is no limit to the number of Buy a case where \ (sc (d + 1) \) of the number of programs.
Now is the variety of the coin, so to add inclusion and exclusion.
Buy the case then we need look no pretreatment limits \ (i \) number of the program.
#include<cstdio>
#define ll long long
const int N=100001;
int c[5],d[5];ll f[N];
int read(){int x;scanf("%d",&x);return x;}
int main()
{
int i,j,s,t,flg;ll ans;f[0]=1;
for(i=1;i<=4;++i) c[i]=read();
for(i=1;i<=4;++i) for(j=c[i];j<=100000;++j) f[j]+=f[j-c[i]];
for(int T=read();T;--T)
{
ans=0;
for(i=1;i<=4;++i) d[i]=read();
s=read();
for(i=0;i<=15;++i)
{
t=s,flg=0;
for(j=0;j<4;++j) if(i&1<<j) t-=c[j+1]*(d[j+1]+1),flg^=1;
if(t<0) continue;
ans+=(flg? -1:1)*f[t];
}
printf("%lld\n",ans);
}
}