
The first line of input contains an integer ‘T’ denoting the number of test cases.
Then the T test cases follow.
The first line of each test case contains two space separated integers ‘N’ and ‘R’ denoting the total number of students in the class and number of students to choose respectively.
For each test case, the number of ways to choose R students out of N is printed.
The output of each test case is printed in a separate line.
1 <= T <= 10
1 <= N <= 200
0 <= R <= N
where ‘T’ is the number of test cases, ‘N’ is the total number of students in the class and ‘R’ is the number of students to choose.
The idea is to recursively find the value of C(N, R). Using the standard formula of calculating Binomial Coefficient.
C(N,R) = C(N-1, R-1) + C(N-1, R).
C(N, 0) = C(N, N) = 1
Starting form a total of N students there will be two cases of choosing R students :
Algorithm:
C => Binomial Coefficient
As you can see in the figure above, this problem leads to recomputation of the same data again and again.
The idea is the same as the previous approach, but in the previous approach many recursive calls were made again and again with the same parameters. This can be eliminated by storing the value obtained from a particular call in a 2d array, called the memo array. memo[i][j] stores the value of the Binomial Coefficient of C(i,j) that is the number of ways of choosing ‘j’ students from ‘i’ students.
Algorithm:
Rows of pascal triangle :
1==========> n = 0, C(0,0) = 1
1–1========> n = 1, C(1,0) = 1, C(1,1) = 1
1–2–1======> n = 2, C(2,0) = 1, C(2,1) = 2, C(2,2) = 1
1–3–3–1====> n = 3, C(3,0) = 1, C(3,1) = 3, C(3,2) = 3, C(3,3)=1
1–4–6–4–1==> n = 4, C(4,0) = 1, C(4,1) = 4, C(4,2) = 6, C(4,3)=4, C(4,4)=1
As seen above, every loop builds a row of pascal triangle using the previous row of the pascal triangle. The right-hand side represents the value coming from the previous iteration (A row of Pascal’s triangle depends on the previous row). The left-Hand side represents the value of the current iteration which will be obtained.
Algorithm: