We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Mathematics
  3. Geometry
  4. Polygons

Polygons

Problem
Submissions
Leaderboard
Discussions
Editorial

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.

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

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

Constraints:
1 <= T <= 10000
4 <= N <= 10^9
1 <= K <= N 

Sample Input:
3
4 1
5 2
5 3

Sample Output:
2
5
0

Explanation:

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.

Author

HackerRank

Difficulty

Hard

Max Score

80

Submitted By

211

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website

An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com