I am currently trying to make a program which involves ascending sequences. N is for the size of the sequence and K is the maximum number, for example
Input: 2,3
Output: 6 (1,1 - 1,2 - 1,3 - 2,2 - 2,3 - 3,3)
My current code outputs the right answer however it takes way too much time to get to the right answer. What is the fastest solution of this and how do I do the dynamical solution? Here is the code with the slow solution :
#include<iostream>
using namespace std;
int n,k,cnt=0,mod=1000000007;
int seq(int n, int k, int first, int depth){
if(depth > n){
cnt = cnt + 1;
return cnt;
}
else {
for(int i=first;i<=k;i++){
seq(n,k,i,depth+1);
}
}
return cnt;
}
int main()
{
cin>>n>>k;
cout<<seq(n,k,1,1)%mod<<"\n";
return 0;
}