C# Coding Test Course, Counting Steps

1. Problem Description

The staircase number is a problem of counting numbers that meet specific conditions.
Given a number N, we will address the problem of finding N-digit staircase numbers.
A staircase number is a number where each digit must be either one more or one less than the next digit. For example, 1234 and 3210 are staircase numbers.
However, 1123 and 2222 are not staircase numbers.
This problem will help you understand the basic ideas and implementation methods of dynamic programming.

2. Input & Output Format

Input

N (1 ≤ N ≤ 100), the given natural number N represents the number of digits in the staircase number.

Output

Print the number of N-digit staircase numbers modulo 1,000,000,000.

3. Approach to the Problem

To count staircase numbers, we need to choose a dynamic programming method.
We can approach the problem in the following way.
To find N-digit staircase numbers, we calculate values using (N-1)-digit staircase numbers.
We can find the following rules:

  • If each digit is 0, the next digit can only be 1.
  • If each digit is 9, the next digit can only be 8.
  • If each digit is between 1 and 8, there are two options for the next digit: one less or one more.

Based on this, we can construct a DP table and keep track of staircase numbers for each digit,
ultimately allowing us to find the N-digit staircase numbers.
The approach involves initializing the DP array and gradually filling it out.

4. C# Code Implementation

Now we will implement the code to find staircase numbers in C#.
Below is the C# code to solve the problem.


using System;

class Program
{
    static void Main()
    {
        int N = int.Parse(Console.ReadLine());
        long[,] dp = new long[N + 1, 10];

        // Initialize 1-digit staircase numbers (1~9)
        for (int i = 1; i <= 9; i++)
        {
            dp[1, i] = 1;
        }

        // Use DP to find N-digit staircase numbers
        for (int i = 2; i <= N; i++)
        {
            for (int j = 0; j <= 9; j++)
            {
                if (j == 0)
                {
                    dp[i, j] = dp[i - 1, j + 1] % 1000000000; // 0 -> 1
                }
                else if (j == 9)
                {
                    dp[i, j] = dp[i - 1, j - 1] % 1000000000; // 9 -> 8
                }
                else
                {
                    dp[i, j] = (dp[i - 1, j - 1] + dp[i - 1, j + 1]) % 1000000000; // j-1, j+1
                }
            }
        }

        // Calculate the total number of N-digit staircase numbers
        long result = 0;
        for (int j = 0; j <= 9; j++)
        {
            result = (result + dp[N, j]) % 1000000000;
        }

        // Print the result
        Console.WriteLine(result);
    }
}
            

5. Code Explanation

I will explain the code step by step.
1. First, read the value of N from the user’s input.
2. Create a two-dimensional array dp, where dp[i, j] stores the number of i-digit staircase numbers ending with j.
3. Since 1-digit numbers can only use digits from 1 to 9, we initialize this.
4. Next, we calculate the number of digits from 2 up to N.
5. Each digit’s calculation is done according to the rules explained above.
6. Finally, sum all N-digit staircase numbers and print the result.

6. Complexity Analysis

The time complexity of this algorithm is O(N).
When N is 100, we check the possibilities for each digit through a double loop,
which results in O(N) * O(10) = O(N).
The space complexity is O(N * 10), indicating relatively low memory usage.

7. Example

Input Example


3
            

Output Example


32
            

In the above example, there are 32 three-digit staircase numbers.
This helps to understand the problem.

8. Conclusion

In this tutorial, we learned the basic concepts of dynamic programming
through the problem of finding staircase numbers.
The staircase number problem can serve as a good practice problem to improve algorithm skills.
I hope you gain more experience by solving various problems.
Continue to challenge yourself with algorithm problem-solving!