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
题解:一开始我想的直接做,用dp,结果发现有点复杂,转移不过去。。感觉是dp没错,但是不知道姿势哪里错了。。
不得已膜了一发题解,感觉真的强。。
首先如果没有限制的话这是个经典的dp:f[i]+=fi-c[j]
然后我们可以用容斥的思想。
我们现在的总方案数是既有超过限制的也有没超过限制的,那么容斥一下就好了,
就把不合法的方案数算出来就行,这里的容斥系数是普通的容斥系数。。表示我反演做多了下意识带了个μ。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**************************************************************
Problem: 1042
User: OOO123
Language: C++
Result: Accepted
Time:80 ms
Memory:9104 kb
****************************************************************/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=2e5+5;
typedef long long ll;
const int mo=1e9+7;
int c[10],d[10],s,tot;
ll f[5][N],ans;
inline void dfs(int x,int ge,int sum)
{
if (sum>s)return;
if (x==5)
{
if (ge==0)return;
if (ge%2==1)ans-=f[4][s-sum];
else ans+=f[4][s-sum];
return;
}
dfs(x+1,ge,sum);
dfs(x+1,ge+1,sum+((d[x]+1)*c[x]));
}
int main()
{
scanf("%d%d%d%d%d",&c[1],&c[2],&c[3],&c[4],&tot);
f[1][0]=1;
for(int i=c[1];i<=100000;i+=c[1])f[1][i]=1;
for (int i=2;i<=4;++i)
for (int j=0;j<=100000;++j)
{
f[i][j]=f[i-1][j];
if (j>=c[i]) f[i][j]+=f[i][j-c[i]];
}
fo(cas,1,tot)
{
scanf("%d%d%d%d%d",&d[1],&d[2],&d[3],&d[4],&s);
ans=f[4][s];
dfs(1,0,0);
printf("%lld\n",ans);
}
}