-
Notifications
You must be signed in to change notification settings - Fork 7
/
Lightoj-1231 - Coin Change (I) .cpp
50 lines (45 loc) · 1.21 KB
/
Lightoj-1231 - Coin Change (I) .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
const long long mod = 100000007;
/// there are many recursion ways to solve this ,but I like dp by bottom-up approach
/// bottom- up approach
int main()
{
int tc;
scanf("%d",&tc);
int caso=0;
while(tc--)
{
caso++;
int n,k;
scanf("%d%d",&n,&k);
int arr[n];
int amount[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(int j=0;j<n;j++)
{
scanf("%d",&amount[j]);
}
long long dp[n+1][k+1];
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=0;i<n;i++)/// we can use n types of coins
{
for(int j=0;j<=k;j++)/// build k , check by making sub-sum
{
for(int times=0;times<=amount[i];times++)
{
if(times*arr[i]<=j)/// is it possible to make j with times*arr[i]?
{
dp[i+1][j]=dp[i+1][j]+dp[i][j-(times*arr[i])]%mod;
}
}
}
}
printf("Case %d: %lld\n",caso,dp[n][k]);/// by using n types of coins how many ways to make k
}
return 0;
}