【bzoj】1042: [HAOI2008]硬币购物
来源:互联网 发布:华讯网络和华讯网存 编辑:程序博客网 时间:2024/09/21 09:07
1042: [HAOI2008]硬币购物
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 2371 Solved: 1416
[Submit][Status][Discuss]
Description
硬币购物一共有4种硬币。面值分别为c1,c2,c3,c4。某人去商店买东西,去了tot次。每次带di枚ci硬币,买s
i的价值的东西。请问每次有多少种付款方法。
Input
第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s,其中di,s<=100000,tot<=1000
Output
每次的方法数
Sample Input
1 2 5 10 2
3 2 3 1 10
1000 2 2 2 900
Sample Output
4
题解:
设F[i]为不考虑每种硬币的数量限制的情况下,得到面值i的方案数。则状态转移方程为
F[i]=Sum{F[i-C[k]] | i-C[k]>=0 且 k=1..4}
对于某种硬币ci,超过数量限制的方案数为f(s−(di+1)×ci)
最终方案=总方案(无限制的方案)-1种硬币超限方案+2种硬币超限方案-3种硬币超限方案+4种硬币超限方案
#include <iostream>#include <iomanip>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef long long LL;LL ans,vis[100000+5];int c[4],d[4],tot,s;void dfs(int x,int k,int sum){ if(sum<0) return; if(x == 4) { if(k&1) ans -= vis[sum]; else ans += vis[sum]; return; } dfs(x+1,k+1,sum-(d[x]+1)*c[x]); dfs(x+1,k,sum);}inline int read(){ int x=0;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x;}int main(){ for(int i=0;i<4;i++) c[i]=read(); tot = read(); vis[0] = 1; for(int i=0;i<4;i++) for(int j=c[i];j<=100000;j++) vis[j] +=vis[j-c[i]]; while(tot--) { for(int i=0;i<4;i++) d[i]=read(); s = read(); ans =0; dfs(0,0,s); cout<<ans<<endl; } return 0;}
0 0
- BZOJ 1042: [HAOI2008]硬币购物
- BZOJ 1042: [HAOI2008]硬币购物
- BZOJ 1042: [HAOI2008]硬币购物
- 【BZOJ 1042】 [HAOI2008]硬币购物
- [BZOJ 1042][HAOI2008]硬币购物
- BZOJ 1042: [HAOI2008]硬币购物
- 【bzoj】1042: [HAOI2008]硬币购物
- [bzoj]1042: [HAOI2008]硬币购物
- bzoj 1042 [HAOI2008]硬币购物
- BZOJ 1042 [HAOI2008] 硬币购物
- bzoj 1042: [HAOI2008]硬币购物
- bzoj 1042: [HAOI2008]硬币购物
- BZOJ 1042 [HAOI2008]硬币购物
- bzoj P1042 [HAOI2008]硬币购物
- 1042: [HAOI2008]硬币购物
- 1042: [HAOI2008]硬币购物
- BZOJ 1042 HAOI2008 硬币购物 背包+容斥原理
- BZOJ 1042 [HAOI2008]硬币购物 容斥原理
- 使用国内镜像composer安装laravel
- 抽象类和接口的区别,使用场景
- 通过Unity3D制作天空盒
- pip install时报错InvalidSchema: Missing dependencies for SOCKS support.的解决方法
- tornado 概述
- 【bzoj】1042: [HAOI2008]硬币购物
- MySQL必知必会-20MySQL更新表
- linux--vim的一些简单便捷的操作
- Textvie实现左边图片和换行文字左对齐
- HiberNate框架搭建--1
- css3样式控制自定义行省略号显示
- 交换排序之快速排序
- php时间计算
- 程序员必须知道的10大基础实用算法及其讲解