1042: [HAOI2008] Coin shopping Time Limit:10 Sec Memory limit:162 MB
submit:2953 solved:1822
[Submit] [Status] [Discuss] Description
Coin shopping A total of 4 kinds of coins. The face value is C1,C2,C3,C4 respectively. Someone went to the store to buy something and went to the tot time. Take di ci coins at a time and buy s
I have something of value. How many methods of payment are available each time. Input
The first line C1,c2,c3,c4,tot below tot row d1,d2,d3,d4,s, where di,s<=100000,tot<=1000 Output
Number of methods per each Sample Input 1 2 5 10 2
3 2 3 1 10
1000 2 2 2 900 Sample Output 4
HINT
Source
idea: A very ingenious topic, in general, we are directly complete backpack that number of solutions, but this will time out, taking into account the type of coins, we should first calculate the number of unlimited numbers of options, and then use the principle of tolerance to eliminate illegal scheme number, b[i]+1 is the first type of coins illegal state.
# include <iostream>
# include <cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 1E5;
LL dp[maxn+30]={1}, A[5], b[5], S, ans;
void Dfs (int pos, LL sum, int cnt)
{
if (sum < 0) return;
if (pos = = 5)
{
if (cnt&1) ans-= dp[sum];
else ans + + dp[sum];
return;
}
DFS (pos+1, sum, CNT);
DFS (pos+1, sum-(b[pos]+1) *a[pos], cnt+1);
int main ()
{
int t;
for (int i=1; i<=4; ++i) scanf ("%lld", &a[i));
for (int i=1; i<=4; ++i) for
(int j=0; j+a[i]<=maxn; ++j)
dp[j+a[i]] = = Dp[j];
scanf ("%d", &t);
while (t--)
{
for (int i=1; i<=4; ++i) scanf ("%lld", &b[i]);
scanf ("%lld", &s);
Ans = 0;
DFS (1, s, 0);
printf ("%lld\n", ans);
}
return 0;
}