C# Coding Test Course, I Will Become the President of the Residents’ Association

Problem Description

The given problem is as follows. You need to write an algorithm to become the head of the residents’ association for the number of floors in an apartment and the number of residents on each floor. The goal is to implement an algorithm that calculates the number of residents on a specific floor.

The apartment has floors from 0 to N (0 ≤ N ≤ 14), and there are k residents living in apartment k on the n-th floor. For example, there are 3 residents living in apartment 3 on the 3rd floor. The number of residents on each floor is calculated as follows.

– 1st floor: 1 resident in apartment 1, 2 residents in apartment 2, 3 residents in apartment 3…
– 2nd floor: 1 resident in apartment 1, 3 residents in apartment 2, 6 residents in apartment 3…
– The number of residents in apartment k on the n-th floor is stored as the sum of the number of residents in apartment k-1 on the n-th floor and the number of residents in apartment k on the n-th floor.

Input Format

The first line contains the number of test cases T (1 ≤ T ≤ 100). In the following T lines, each test case provides integers N (0 ≤ N ≤ 14) and K (1 ≤ K ≤ 14). Here, N represents the floor number, and K represents the apartment number.

Output Format

For each test case, you should output the number of residents in apartment k on the n-th floor.

Example Input


2
1 3
2 3

Example Output


3
6

Problem Solving Process

To solve this problem, we will use Dynamic Programming (DP) techniques. We can use the following relationship to find the number of residents in apartment k on the n-th floor.


residents(n, k) = residents(n-1, 1) + residents(n, k-1)

Here, we initialize residents(0, k) = k and residents(n, 0) = 0. Below are the basic steps for storing the number of residents in a table.

Step 1: Initialization

First, declare a 2-dimensional array to store the number of residents and set the initial values.


int[,] dp = new int[15, 15]; // Array for 15 floors and 15 apartments
for (int i = 0; i <= 14; i++) {
dp[0, i] = i; // k residents live in apartment k on the 0th floor.
}

Step 2: Fill the DP Table

We fill in the dp table using a nested loop. We consider all cases for n-th floor and k-th apartment as follows.


for (int n = 1; n <= 14; n++) {
for (int k = 1; k <= 14; k++) {
dp[n, k] = dp[n - 1, k] + dp[n, k - 1];
}
}

Step 3: Output Results

Calculate and output the number of residents for all test cases. Below is an example of the final implementation.


using System;
class Program {
static void Main(string[] args) {
int[,] dp = new int[15, 15];
for (int i = 0; i <= 14; i++) {
dp[0, i] = i;
}
for (int n = 1; n <= 14; n++) {
for (int k = 1; k <= 14; k++) {
dp[n, k] = dp[n - 1, k] + dp[n, k - 1];
}
}
int T = int.Parse(Console.ReadLine());
for (int t = 0; t < T; t++) {
string[] input = Console.ReadLine().Split();
int n = int.Parse(input[0]);
int k = int.Parse(input[1]);
Console.WriteLine(dp[n, k]);
}
}
}

Conclusion

Through this problem, we were able to understand the basic concepts of dynamic programming and enhance our problem-solving skills. This problem is one of the types frequently asked in algorithm tests, and there are various variations. To apply this in actual coding tests, it is important to practice DP techniques repeatedly.