Problem Description
Problem Analysis
Although the number of T test cases is 10000, the n values are actually only up to 30, which also suggests that it is best to use precomputation to construct the data.
It can be easily found that if $ dp[i] $ is used to represent the number of ways to climb to the $i$-th floor of stairs, then the following recurrence relation can be obtained:
$$ dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3] $$That is, the number of ways to climb to the $i$-th floor is equal to the number of ways to climb to the $i-1$ floor plus the number of ways to climb to the $i-2$ floor plus the number of ways to climb to the $i-3$ floor.
Note that the sample indicates that when n = 1, 0 should be output, but in the recurrence it is obvious that reaching the first floor also needs to be treated as one way, so $dp[1]$ needs to be initialized to $1$, and a special case is needed when outputting.
AC Code
| |
Problem address: Thanksgiving KK Special — Climbing Stairs - Problem Details - Problem - DidaOJ