Alan threw a party and invited all her close friend to it. Let us suppose there were 2n…
- User
Alan threw a party and invited all her close friend to it. Let us suppose there were 2n people in the party and all of them were sitting across a round table. The host asked them to shake hands with the condition that no two hands should cross each other i.e if 4 people are sitting then 1st person cannot shake hands with 3rd as that will cross with the hands of 2nd and 4th person. The host wants to know the no of ways in which her friends can shake hands with each other. So your task is to help Alan in finding this. Input First line of input will contain no of test cases t, followed by t lines each containing an integer N(<1000).
Output For each test case, output a single integer which is the no of ways in which persons can shake hands for that particular N. output the answer %100003.
Example Input: 2 2 4 Output: 2 14
Assistant
To solve this problem, we need to determine the number of ways in which 2N people sitting around a round table can shake hands without any crossings. The solution involves using the Catalan numbers, which count the number of non-crossing partitions into pairs.
Approach
The problem can be solved by computing the Nth Catalan number for each test case. The Catalan numbers can be efficiently computed using dynamic programming due to their recurrence relation:
- C(0) = 1
- For n >= 1, C(n) = sum_{i=0 to n-1} C(i) * C(n-1-i)
This approach ensures that we precompute the Catalan numbers up to the maximum possible N (999) using dynamic programming, which allows for efficient query responses.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.