bzoj1042 硬幣購物

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

HINT

數據規模

di,s<=100000

tot<=1000

Source

dp+容斥原理

其實再就沒什麼寫的了。。

代碼短小精煉

#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline ll read()
{
	ll x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
ll ans,f[100005];
int T;
int c[5],d[5];
void dfs(int x, int y, int k)
{
	if(k < 0) return;
	if(x == 5)
	{
		if(y & 1) ans -= f[k];
		else ans += f[k];
		return;
	}
    dfs(x + 1, y + 1, k - (d[x] + 1) * c[x]);
	dfs(x + 1, y, k);
}

int main()
{
	for(int i = 1; i <= 4; i ++)
		c[i] = read();
	T = read();
	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 k = 1; k <= 4; k ++)
			d[k] = read();
		int x = read();
		ans = 0;
		dfs(1, 0, x);
		printf("%lld\n",ans);
	}
	return 0;
}


發佈了85 篇原創文章 · 獲贊 6 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章