Gopal is climbing the stairs. He can jump 1 or 2 or 3 steps at a time. He wants to climb…
- User
Gopal is climbing the stairs. He can jump 1 or 2 or 3 steps at a time. He wants to climb N steps. In how many ways can he reach the Nth step? As the answer can be too large Output it modulo 10^9+7. Input: First line of the input contains an integer T denoting the number of test cases. Then T lines follow each line containing an integer N.
Output: Output the required answer in a new line for each test case.
Constraints: 1 ≤ N ≤ 10^5
Sample Input 2 3 4
Sample Output 4 7
Explanation: Test case: 2 There 7 ways of reaching 4^th step in the stairs:-
1+1+1+1 1+1+2 1+2+1 2+1+1 1+3 3+1 2+2SAMPLE INPUT 2 3 4
SAMPLE OUTPUT 4 7
Assistant
pythonMOD = 10**9 + 7 max_n = 10**5 # Precompute the dp array up to max_n dp = [0] * (max_n + 1) dp[0] = 1 if max_n >= 1: dp[1] = 1 if max_n >= 2: dp[2] = 2 for i in range(3, max_n + 1): dp[i] = (dp[i-1] + dp[i-2] + dp[i-3]) % MOD # Read input and output results T = int(input()) for _ in range(T): N = int(input()) print(dp[N])
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.