How to do this , please suggest.

Consider a regular polygon with N vertices labelled 1…N. In how many ways can you draw K diagonals such that no two diagonals intersect at a point strictly inside the polygon? A diagonal is a line segment joining two non adjacent vertices of the polygon.


The first line contains the number of test cases T. Each of the next T lines contain two integers N and K.


Output T lines, one corresponding to each test case. Since the answer can be really huge, output it modulo 1000003.


1 <= T <= 10000

4 <= N <= 10^9

1 <= K <= N

Sample Input:


4 1

5 2

5 3

Sample Output:





For the first case, there are clearly 2 ways to draw 1 diagonal - 1 to 3, or 2 to 4. (Assuming the vertices are labelled 1…N in cyclic order).

For the third case, at most 2 non-intersecting diagonals can be drawn in a 5-gon, and so the answer is 0.

1 Like

1.first take N
2.substract N by 2 ,then you will get possible points where you can place diagonal compare with K if K is greater the print answer as 0.
4.else if N-3=1 ans is N*1
5.else calculate combinations using formula (N-3)!/K(N-K)!
6.And multiply the answer by N/2