[BZOJ1042][DP][容斥原理]HAOI2018:硬币购物

版权声明:虽然博主很菜,但是还是请注明出处(我觉得应该没人偷我的博客) https://blog.csdn.net/qq_43346903/article/details/88902731

BZOJ1042

先考虑没有个数限制时有多少方案,一个递推出来
然后有限制时方案就是没有限制的方案数-第一种超过限制的方案数-第二种。。。+第一,二种都超过。。。容斥一下就做完了

Code:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline ll read(){
	ll res=0,f=1;char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') f=-f;ch=getchar();}
	while(isdigit(ch)) {res=(res<<1)+(res<<3)+(ch^48);ch=getchar();}
	return res*f;
}
const int S=1e5+5;
ll s,ans=0,dp[S],c[5],d[5];
inline void init(){
	for(int i=1;i<=4;i++) c[i]=read();
	dp[0]=1;
	for(int j=1;j<=4;j++)
		for(int i=c[j];i<=S-5;i++)
			dp[i]+=dp[i-c[j]];
}
inline void check(int t,ll sum){ans+=((t&1)?-dp[sum]:dp[sum]);return;}
void dfs(int v,int t,ll now){
	if(now<0) return;
	if(v==5) return check(t,now);
	dfs(v+1,t,now);
	dfs(v+1,t+1,now-(d[v]+1)*c[v]);
}
int main(){
	init();
	int t=read();
	while(t--){
		for(int i=1;i<=4;i++) d[i]=read();
		s=read();ans=0;
		dfs(1,0,s);
		cout<<ans<<"\n";
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43346903/article/details/88902731