Simran is running up a staircase with N
steps, and can hop(jump) either 1 step, 2 steps or 3 steps at a time. You have to count how many possible ways Simran can run up to the stairs.
Input is N
and output is the number of possible ways.
Why this code is not working with N>21
?
#include<bits/stdc++.h>
using namespace std;
long long fact(int n)
{
return (n==0) || (n==1) ? 1 : n* fact(n-1);
}
int main()
{
int n;
cin>>n;
long long count=0;
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
for(int k=0;k<=n;k++)
{
if((i*1+j*2+k*3)==n)
{
count+=fact(i+j+k)/(fact(i)*fact(j)*fact(k));
}
}
}
}
cout<<count;
return 0;
}