-
Notifications
You must be signed in to change notification settings - Fork 5
/
GFG_ReachAGivenScore.cpp
75 lines (58 loc) · 1.26 KB
/
GFG_ReachAGivenScore.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
https://practice.geeksforgeeks.org/problems/reach-a-given-score-1587115621/1#
Reach a given score
*/
// { Driver Code Starts
// Driver Code
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
// } Driver Code Ends
// Complete this function
long long int solve(int , long long int);
vector<int> combinations = {3,5,10};
vector<vector<long long int>> memo(4, vector<long long int>(1001, -1));
long long int count(long long int n)
{
long long int table[n+1],i;
memset(table, 0, sizeof(table));
table[0]=1; // If 0 sum is required number of ways is 1.
// Your code here
for(int ci: combinations)
{
for(int sum=ci; sum<=n; sum++)
{
table[sum] += table[sum-ci];
}
}
return table[n];
// return solve(0, n);
}
long long int solve(int idx, long long int sum)
{
if(sum == 0)
return 1;
if(sum < 0)
return 0;
if(memo[idx][sum] !=-1)
return memo[idx][sum];
long long int cnt = 0;
for(int i=idx; i<3; i++)
{
cnt+= solve(i, sum-combinations[i]);
}
return memo[idx][sum] = cnt;
}
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
cout<<count(n)<<endl;
}
return 0;
} // } Driver Code Ends