【BZOJ-1042】硬币购物 容斥原理 + 完全背包
2016-05-09 09:48
441 查看
1042: [HAOI2008]硬币购物
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1811 Solved: 1057
[Submit][Status][Discuss]
Description
硬币购物一共有4种硬币。面值分别为c1,c2,c3,c4。某人去商店买东西,去了tot次。每次带di枚ci硬币,买si的价值的东西。请问每次有多少种付款方法。
Input
第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s,其中di,s<=100000,tot<=1000Output
每次的方法数Sample Input
1 2 5 10 23 2 3 1 10
1000 2 2 2 900
Sample Output
427
HINT
Source
Solution
容斥这东西,说是会了,但还不是很会用..最暴力的就是跑完全背包啊,很明显不行,所以考虑容斥
先用完全背包做出方案数,然后容斥一下,也就是减去不满足的方案
最终方案=总方案(无限制的方案)-1种硬币超限方案+2种硬币超限方案-3种硬币超限方案+4种硬币超限方案
对于一个数,限制条件为$D[i]$那么超限至少为$D[i]+1$,所以这种方案数为$f[S-(D[i]+1)*C[i]]$,其余同理
Code
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; int read() { int x=0,f=1; char ch=getchar(); while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f; } #define maxn 100010 int C[5],tot,D[5],S; long long f[maxn],ans; void Work(int s,int dep,int x,int t) { if (dep==5) {if (t%2) ans-=f[s]; else ans+=f[s]; return;} Work(s,dep+1,x+1,t); if (C[x]*(D[x]+1)<=s) Work(s-C[x]*(D[x]+1),dep+1,x+1,t+1); } int main() { for (int i=1; i<=4; i++) C[i]=read(); tot=read(); f[0]=1; for (int i=1; i<=4; i++) for (int j=C[i]; j<=maxn; j++) f[j]+=f[j-C[i]]; for (int i=1; i<=tot; i++) { for (int j=1; j<=4; j++) D[j]=read(); S=read(); ans=0; Work(S,1,1,0); printf("%lld\n",ans); } return 0; }
相关文章推荐
- JAV基础语法之---数据类型转换
- 今天开始记录自己苹果开发博客旅程!~
- Ex2010-02 How to Reconnect a Disconnected Mailbox in 2010
- spring+mybatis+druid无法读取jdbc.properties属性导致datasource加载失败
- oracle误删数据文件以后无法启动实例
- UNITY3D有限状态机
- 自定义圆形ImageView-简单
- OpenCV 序列CvSeq
- Jackson 框架,轻易转换JSON
- 后缀数组
- 使用gdb调试Python进程
- Tomcat长出现的内存溢出问题
- mysql三张表关联查询
- Kafka - SQL 代码实现
- 今日BBC
- VC/windowsx.h 和windows.h的区别
- 豆腐衣和冶金新工艺
- mysql中文无法输入或读取时乱码
- 散列表学习总结
- javaweb性能优化的系统思考