可以想到这是一个背包DP,但是由于数据规模的原因,不能每次都进行一次DP。
于是从「硬币数量只有
4
」这个条件上入手。由容斥原理得到:
总方案数
于是可以跑一遍完全背包DP之后,在常数(
16
)时间内解决一组询问。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
typedef long long ll;
const int MaxN = 1e5, N = MaxN + 5;
int T, c[6], d[6], S; ll ans, f[N];
void dfs(int dep, int cnt, int s_now) {
if (dep == 5) {
if (cnt & 1) ans -= f[s_now];
else ans += f[s_now];
return;
}
dfs(dep + 1, cnt, s_now);
if (s_now >= 1ll * c[dep] * (d[dep] + 1))
dfs(dep + 1, cnt + 1, s_now - c[dep] * (d[dep] + 1));
}
int main() {
int i, j; cin >> c[1] >> c[2] >> c[3] >> c[4] >> T; f[0] = 1;
for (i = 1; i <= 4; i++) for (j = c[i]; j <= MaxN; j++)
f[j] += f[j - c[i]];
while (T--) {
for (i = 1; i <= 4; i++) d[i] = read(); S = read();
ans = 0; dfs(1, 0, S); printf("%lld\n", ans);
}
return 0;
}