link of question https://hack.codingblocks.com/contests/c/547/1045
i have used the recursive approach: f(n,m)=f(n-1,m)+f(n-m,m)…i have took the base case that if n=0 return 1 and if n<0 return 0.
Sample input and only Test Case 4 is passing, Rest all test cases are giving time limit exceed… How to optimize? Please explain
#include
using namespace std;
int noofways(int,int);
int main() {
int t,n,m;
cin>>t;
while(t–)
{
cin>>n>>m;
int count=noofways(n,m);
cout<<count<<endl;
}
return 0;
}
int noofways(int n,int m)
{
if(n==0)
return 1;
if(n<0)
return 0;
int way1=noofways(n-1,m);
int way2=noofways(n-m,m);
return (way1+way2)%1000000007;
}