首页 技术 正文
技术 2022年11月8日
0 收藏 524 点赞 1,649 浏览 1217 个字

1042: [HAOI2008]硬币购物

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1811  Solved: 1057
[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
27

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=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 100010
int C[],tot,D[],S; long long f[maxn],ans;
void Work(int s,int dep,int x,int t)
{
if (dep==) {if (t%) ans-=f[s]; else ans+=f[s]; return;}
Work(s,dep+,x+,t);
if (C[x]*(D[x]+)<=s) Work(s-C[x]*(D[x]+),dep+,x+,t+);
}
int main()
{
for (int i=; i<=; i++) C[i]=read(); tot=read();
f[]=;
for (int i=; i<=; i++)
for (int j=C[i]; j<=maxn; j++)
f[j]+=f[j-C[i]];
for (int i=; i<=tot; i++)
{
for (int j=; j<=; j++) D[j]=read(); S=read();
ans=; Work(S,,,); printf("%lld\n",ans);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,992
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,767
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844