Friday, December 1, 2017

Count ways to reach the n’th stair

There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter).

Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, an integer N will be given.

Output:
Print number of possible ways to reach Nth stair. Answer your output % 10^9+7.

Constraints:
1<=T<=1000
1<=N<=100

Example:
Input:
3
4
10
24
Output:
5
89
75025

Solution:
#include <iostream>
using namespace std;
int main() {
          int n;
          cin>>n;
          while(n--)
          {
              unsigned long int i,a;
              cin>>a;
              unsigned long int dp[a+1];
              dp[0]=0;
              dp[1]=1;
              dp[2]=2;
              for(i=3;i<a+1;i++)
               dp[i]=(dp[i-1]+dp[i-2])%1000000007;
              cout<<dp[a]%1000000007<<endl;
          }
          return 0;
}
Share: