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≤1000
Output
每次的方法数
首先DP出不受硬币个数限制的每种价格的方案数
dps
运用容斥原理求解
列出题目中的n个条件。
求满足这n个条件中每一个的方案的个数。
枚举这些条件的所有2^n个集合。
考虑一个集合x,令不满足x中所有条件的方案有A个。
如果x的大小是奇数,给答案减去A,不然给答案加上A。
——WJMZBMR
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 100100;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
inline void read(int &x) {
static char c; x = 0;
for (c = get(); c < '0' || c > '9'; c = get());
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
ll dp[N], ans;
int S, test;
int c[10], d[10];
void dfs(int pos, int k, int sum) {
if (sum < 0) return;
if (pos > 4) {
ans += dp[sum] * k; return;
}
dfs(pos + 1, k, sum);
dfs(pos + 1, -k, sum - (d[pos] + 1) * c[pos]);
}
int main(void) {
for (int i = 1; i <= 4; i++) read(c[i]);
dp[0] = 1;
for (int i = 1; i <= 4; i++)
for (int j = c[i]; j <= 100000; j++)
dp[j] += dp[j - c[i]];
read(test);
while (test--) {
for (int i = 1; i <= 4; i++) read(d[i]);
read(S); ans = 0;
dfs(1, 1, S);
printf("%lld\n", ans);
}
return 0;
}