altreodo's blog

By altreodo, history, 2 years ago, In English

Check this problem of CSES Coin Combination II

I have used memoization but still it is giving me TLE , I know CSES is tight in checking runtime but i have seen this type of solution in internet also but it is easily accepting but in mine it is showing TLE

CAN ANYBODY HELP ME?

#include <bits/stdc++.h>
 
using namespace std;
#define ll long long 
    ll MOD=1e9+7;
vector<vector<ll>>dp;
ll getter(vector<ll>&v,ll n,ll k){
    if(k==0){
        return (1);
    }
    if( k<0 || n>=v.size()){
        return 0;
    }
    if(dp[n][k-1]!=-1)return dp[n][k-1];
    return dp[n][k-1]=(getter(v,n+1,k)%MOD+getter(v,n,k-v[n])%MOD)%MOD;
}
int main() {
    ll n,k;
    cin>>n>>k;
    vector<ll>v(n);
    for(ll y=0;y<n;y++){
        cin>>v[y];
    }
    dp.resize(n+1,vector<ll>(k,-1));
    cout<<getter(v,0,k);
}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it