1042: [HAOI2008]硬币购物
Description
硬币购物一共有4种硬币。面值分别为c1,c2,c3,c4。某人去商店买东西,去了tot次。每次带di枚ci硬币,买si的价值的东西。请问每次有多少种付款方法。
Input
第一行 c1,c2,c3,c4,tot 下面tot行 d1,d2,d3,d4,s
Output
每次的方法数
Sample Input
1 2 5 10 2
3 2 3 1 10
1000 2 2 2 900
Sample Output
4
27
数据规模
di,s<=100000
tot<=1000
容斥原理+背包。
byvoid:
#include
#include
#include
#include
#define maxn 100010
using namespace std;
typedef long long ll;
ll ans, dp[maxn];
int c[4], d[4];
inline void read(int& num){
char ch = getchar();num = 0;
for(; ch < '!'; ch = getchar());
for(; ch > '!'; ch = getchar())
num = (num << 3) + (num << 1) + (ch ^ 48);
}
void solve(int num, int dep, ll s){
if(s < 0)return;
if(dep == 4){
if(num & 1)ans -= dp[s];
else ans += dp[s];
return;
}
solve(num + 1, dep + 1, s - (d[dep] + 1) * c[dep]);
solve(num, dep + 1, s);
}
int main(){
int test;
for(int i = 0; i < 4; i ++)
read(c[i]);
read(test);
dp[0] = 1;
for(int i = 0; i < 4; i ++)
for(int j = c[i]; j <= 100000; j ++)
dp[j] += dp[j - c[i]];
int x;
while(test --){
for(int i = 0; i < 4; i ++)
read(d[i]);
read(x);
ans = 0;
solve(0, 0, x);
printf("%lld\n", ans);
}
return 0;
}