1042: [HAOI2008]硬币购物
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2496 Solved: 1497
[ Submit][ Status][ Discuss]
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
3 2 3 1 10
1000 2 2 2 900
Sample Output
4
27
27
HINT
Source
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
ll ans,f[100005];
int T,d[5],c[5],s;
void dfs( int x, int k, int left ){
if( left < 0 ) return ;
if( x == 5 ){
if( k & 1 ) ans -= f[left];
else ans += f[left];
return ;
}
dfs(x+1,k^1,left-(d[x]+1)*c[x]);
dfs(x+1,k,left);
}
int main(){
for( int i = 1; i <= 4; i++ ) scanf("%d", &c[i]); scanf("%d", &T);
f[0] = 1;
for( int i = 1; i <= 4; i++ )
for( int j = c[i]; j <= 100000; j++ )
f[j] += f[j-c[i]];
for( int i = 1; i <= T; i++ ){
for( int j = 1; j <= 4; j++ ) scanf("%d", &d[j]); scanf("%d", &s);
ans = 0; dfs(1,0,s);
printf("%lld\n", ans);
}
return 0;
}