I'm given a number n
of order 105. I've to find number of ways to reach nth step from ground using steps of length 1 or 2 or 3 or.....or m
, here m<=n.
As the answer can be too large output it modulo 109+7.
#include<iostream.h>
using namespace std;
#define ll long long
#define MOD 1000000007
ll countWays_(ll n, ll m){
ll res[n];
res[0] = 1; res[1] = 1;
for (ll i=2; i<n; i++)
{
res[i] = 0;
for (ll j=1; j<=m && j<=i; j++)
res[i] =(res[i]%MOD+ res[i-j]%MOD)%MOD;
}
return res[n-1];
}
ll countWays(ll s, ll m){
return countWays_(s+1, m);
}
int main (){
scanf("%lld%lld",&s,&m);
printf("%lld\n",countWays(s,m));
return 0;
}
As the complexity O(m*n)
, I want to reduce it.