Run
#include <bits/stdc++.h>
using namespace std;
int dp[1005][1005];
// cur means current coin index
// changeLeft means amount we want to change
int f(int coin[], int cur, int changeLeft)
{
// If no change left
if (changeLeft == 0)
return 1;
// If change left is negative
if (changeLeft < 0)
return 0;
// If no coin is left and we have positive value of money left.
if (cur < 0 && changeLeft >= 1)
return 0;
if (dp[cur][changeLeft] != -1)
return dp[cur][changeLeft];
return dp[cur][changeLeft] = f(coin, cur - 1, changeLeft) + f(coin, cur, changeLeft - coin[cur]);
}
int main()
{
memset(dp, -1, sizeof dp);
int n;
cin >> n;
int m;
cin >> m;
int coin[m];
for (int i = 0; i < m; i++)
{
cin >> coin[i];
}
cout << "Number of ways " << f(coin, m - 1, n) << endl;
return 0;
}
Login/Signup to comment