http://www.lydsy.com/JudgeOnline/problem.php?id=1042 (题目链接)

题意

  共有4种硬币,面值分别为c1,c2,c3,c4。某人去商店买东西,去了tot次。每次带di枚ci硬币,买s的价值的东西。请问每次有多少种付款方法。

Solution

  容斥原理。

  设F[i]为不考虑每种硬币的数量限制的情况下,得到面值i的方案数。则状态转移方程为 F[i]=Sum{F[i-C[k]] | i-C[k]>=0 且 k=1..4} ,边界条件F[0]=0。

  接下来对于每次询问,奇妙的解法如下:根据容斥原理,答案为 得到面值S的超过限制的方案数 – 第1种硬币超过限制的方案数 – 第2种硬币超过限制的方案数 – 第3种硬币超过限制的方案数 – 第4种硬币超过限制的方案数 + 第1,2种硬币同时超过限制的方案数 + 第1,3种硬币同时超过限制的方案数 + …… + 第1,2,3,4种硬币全部同时超过限制的方案数。

  当第1种硬币超过限制时,只要要用到D[1]+1枚硬币,剩余的硬币可以任意分配,所以方案数为 F[ S – (D[1]+1)×C[1] ],当且仅当(S – (D[1]+1)×C[1])>=0,否则方案数为0。其余情况类似,每次询问只用问16次,所以询问的时间复杂度为O(1)。

  hzwer终于写了次题解,于是我就直接蒯了。

代码

// bzoj1042
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<map>
#define inf 2147483640
#define LL long long
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
LL x=0,f=1;char ch=getchar();
while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} int c[5],d[5],s,tot;
LL ans,f[100010]; void dfs(int x,int k,int sum) {
if (sum<0) return;
if (x==5) {
if (k&1) ans-=f[sum];
else ans+=f[sum];
return;
}
dfs(x+1,k+1,sum-(d[x]+1)*c[x]);
dfs(x+1,k,sum);
}
int main() {
scanf("%d%d%d%d%d",&c[1],&c[2],&c[3],&c[4],&tot);
f[0]=1;
for (int i=1;i<=4;i++)
for (int j=c[i];j<=100000;j++) f[j]+=f[j-c[i]];
for (int i=1;i<=tot;i++) {
scanf("%d%d%d%d%d",&d[1],&d[2],&d[3],&d[4],&s);
ans=0;
dfs(1,0,s);
printf("%lld\n",ans);
}
return 0;
}

  

最新文章

  1. Qt安装配置
  2. 修正 XE6 TListView 上方 SearchBok 右边的清除钮显示
  3. mysql中的高级查询
  4. mouseenter和mouseout中间的时间控制
  5. 用JS查看修改CSS样式(cssText,attribute(&#39;style&#39;),currentStyle,getComputedStyle)
  6. C# : 操作Word文件的API - (将C# source中的xml注释转换成word文档)
  7. linux中配置maven环境
  8. Android开发之Adapter
  9. [Javascript] Advanced Reduce: Common Mistakes
  10. light 1012 Guilty Prince
  11. poj2411(状压dp)
  12. idea编译时JDK版本变化
  13. js中对cookie的操作及json数据与cookie结合的用法
  14. linux学习笔记-shell-script相关知识
  15. [java核心篇02]__内部类
  16. 一、VScode构建.NET应用程序
  17. (原创)odoo解决方案---接收以及回复外部邮件
  18. wpf 使用Font-Awesome图标字体
  19. linux运维发展路线
  20. ./configure: error: the HTTP rewrite module requires the PCRE library解决

热门文章

  1. fastDFS 一二事 - 简易服务器搭建(单linux)
  2. android图片缩小和放大Matrix
  3. Android优化——UI优化(五) Listview 重用convertView
  4. C++ Set &amp; MultiSet
  5. perl 简单学习,安装perl模块
  6. listview向下滑动过程中背景色变成黑色和一些奇怪问题
  7. C#本质论读书笔记:第一章 C#概述|第二章 数据类型
  8. GDB代码调试与使用
  9. ViewController与outlet绑定
  10. 【JS笔记】私有变量