bzoj 1042(容斥原理+背包dp)

来源:互联网 发布:福建星云大数据公司 编辑:程序博客网 时间:2024/06/03 17:59

传送门

题解:

先预处理出完全背包(无限制)的方案数。

ans=(f(s)-d1超限-d2…+d1d2+d2d3+…-d1d2d3….+d1d2d3d4)

P.S.d1超限即使用了d1+1个物品,于是剩下S-(d1+1)*c1都可以随意分配,于是d1超限的方案数就是f(S-(d1+1)*c1)

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const int MAXN=1e5+4;ll f[MAXN]={1},ans=0;int c[4],d[5],S,kase;inline int read() {int x=0;char c=getchar();while (c<'0'||c>'9') c=getchar();while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();return x;}void dfs(int step,bool t,int S) {if (S<0) return ;if (step==4) {t?ans-=f[S]:ans+=f[S];return ;}dfs(step+1,t,S);dfs(step+1,t^1,S-(d[step]+1)*c[step]);}int main() {//freopen("bzoj 1042.in","r",stdin);c[0]=read(),c[1]=read(),c[2]=read(),c[3]=read(),kase=read();for (int i=0;i<4;++i)for (int j=c[i];j<=100000;++j)f[j]+=f[j-c[i]];while (kase--) {d[0]=read(),d[1]=read(),d[2]=read(),d[3]=read(),S=read();ans=0;dfs(0,0,S);printf("%lld\n",ans);}return 0;}


原创粉丝点击