# [BZOJ 1042] [HAOI2008] 硬币购物 【DP + 容斥】

### 题目分析

f[0] = 1;
for (int i = 1; i <= 4; ++i) {
for (int j = B[i]; j <= MaxS; ++j) {
f[j] += f[j - B[i]];
}
}


### 代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>

using namespace std;

const int MaxN = 7, MaxS = 100000;

int n, S;
int A[MaxN], B[MaxN];

typedef long long LL;

LL Ans;
LL f[MaxS + 5];

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 - (A[x] + 1) * B[x]);
DFS(x + 1, k, Sum);
}

int main()
{
for (int i = 1; i <= 4; ++i) scanf("%d", &B[i]);
scanf("%d", &n);
f[0] = 1;
for (int i = 1; i <= 4; ++i) {
for (int j = B[i]; j <= MaxS; ++j) {
f[j] += f[j - B[i]];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= 4; ++j) scanf("%d", &A[j]);
scanf("%d", &S);
Ans = 0ll;
DFS(1, 0, S);
printf("%lld\n", Ans);
}
return 0;
}


posted @ 2014-12-24 20:04  JoeFan  阅读(235)  评论(0编辑  收藏