If coin Order matters, that's, each sequence are unique, the DP function is a simple enough-to-make it 1D DP. But the key is this order doesn ' T matter, so we need to add one more state:ending coin. And for each DP advance step, we only put >= coins.
#include <iostream>#include<vector>#include<algorithm>#include<numeric>using namespacestd;typedef unsignedLong LongULL;intMain () {unsigned n, m; CIN>> N >>m; Vector<ULL>V (m); for(Unsigned i =0; I < m; i++) Cin>>V[i]; Std::sort (V.begin (), V.end ()); //dp[val][ending Coin]Vector<vector<ull>> DP (n +1, Vector<ull> (M,0)); dp[0][0] =1; for(Unsigned i =0; I <= N; i++) for(Unsigned j =0; J < M; J + +) { for(unsigned k = j; k < m; k + +) { if((ULL (i) + v[k]) <=ULL (n)) dp[ull (i)+ V[k]][k] + =Dp[i][j]; }} ULL ret=0; for(Unsigned i =0; I < m; i + +) ret + =Dp[n][i]; cout<< ret <<Endl; return 0;}
Hackerrank-"The Coin Change Problem"