We are asked: "The captain of TITANIC is a mathematics freak. He has recently been given a problem that he is unable to solve. And he has asked your help to solve it.
There are n numbers in a given expression.
X1 X2 X3 .... Xn
What is the number of ways to put parenthesis in the expression.
For n=4, the different ways are
(((x1.x2).x3).x4)
((x1.x2).(x3.x4))
(x1.((x2.x3).x4))
(x1.(x2.(x3.x4)))
((x1.(x2.x3)).x4)
Hence the required answer would be 5.
Input
The first line contains the number of test cases t ≤10.
Each of the next t lines contain single integers ≤1000 denoting n.
Output
Display t lines containg the number of ways to put parenthesis in the expression of length n modulo 10000.
Example
Input:
2
4
5
Output:
5
14"
So we need to compute the number of ways to fully parenthesize a product (or binary operation) on n symbols. The classic answer: number of ways to parenthesize a product of n factors is the (n-1)th Catalan number: C_{n-1} = (1/(n)) * binomial(2n-2, n-1). For n=1, there is 1 way (no parentheses needed, maybe considered as just x1). For n=2: one way: (x1.x2). For n=3: 2 ways: ((x1.x2).x3) and (x1.(x2.x3)). For n=4: 5 ways. For n=5: 14 ways. So indeed Catalan numbers: C_0=1, C_1=1, C_2=2, C_3=5, C_4=14, C_5=42, etc. So answer for n is Catalan(n-1). However we must output modulo 10000. n ≤ 1000, so Catalan numbers for n-1 up to 999 are huge. But we only need modulo 10000. t ≤10.
We can compute Catalan numbers modulo 10000 for up to 999. But careful: Catalan numbers can be computed via recurrence: C_0 = 1, C_{n+1} = sum_{i=0}^{n} C_i * C_{n-i}. That is O(n^2). For n up to 999, that is ~500k operations, which is trivial. But we need modulo 10000. However note that Catalan numbers can be computed using formula: C_n = binomial(2n, n)/(n+1). Since we are modulo 10000, we could also compute binomial modulo 10000 using multiplicative formulas but division mod non-prime (10000=2^4 *5^4) is tricky because modulus not prime. Better to compute via DP recurrence which uses only addition and multiplication modulo 10000. However the recurrence uses sum of products, which is fine.
But note: The recurrence is for Catalan numbers directly: C_0=1, C_{n+1}= sum_{i=0}^{n} C_i * C_{n-i}. So for n up to 999 (since Catalan index n-1), we need Catalan numbers up to 999. That's fine.
Alternatively, compute using formula with binomial and division: Since modulus 10000 is not prime, we can compute the actual Catalan number as integer (maybe huge) but we can compute modulo 10000 using integer arithmetic (big integers) as Python's big ints can handle up to 1000, but Catalan numbers for 999 are enormous (~ 10^597). That's okay, Python can handle that; we could compute exact Catalan using DP or using formula with binomial, then take %10000. But computing binomial(2n, n) for n=999 would be huge but Python big ints can manage. However computing all 10 test cases via exact integer could be simpler: compute Catalan numbers up to max_n using recurrence or using factorial and integer division. Since n ≤ 1000, the numbers are huge but Python can compute them quickly. We need modulo 10000; we can compute using recurrence with Python ints and take mod. Recurrence uses O(n^2) time and multiplication of moderate sized ints (they get big, but up to 1000 digits). The DP might be slower but still fine for 10 test cases and n=1000. But we can also compute using recurrence with modulo: since multiplication mod 10000 is okay, we can compute C[i] mod 10000. However caution: The recurrence uses addition of products; if we compute modulo 10000, the recurrence still holds modulo 10000 because it's linear over integers. So we can compute Catalan numbers modulo 10000 directly using DP and mod after each operation. That's the easiest.
But we need to answer for n (number of numbers). So if n = 1? Possibly n=1? Input n ≤1000, but not specified if n>=2? The problem statement says "There are n numbers in a given expression. X1 X2 X3 .... Xn". Example gives n=4,5. For n=1, there is 1 way? Usually for a single number, no parentheses needed, but the count is 1. But if n=1 appears, answer should be 1. Let's check typical Catalan: C_0 =1 corresponds to 1 number? Actually number of ways to parenthesize a product of n items is the (n-1)th Catalan number. So for n=1, C_0 =1. So we should handle n=1. So compute Catalan up to index max_n-1. If max_n = 1000, then need Catalan index up to 999.
We should precompute Catalan numbers mod 10000 for indices 0..999.
But careful: The recurrence: C[0] = 1. For k from 1 to max_index: C[k] = sum_{i=0}^{k-1} C[i] * C[k-1-i] (mod 10000). That's O(N^2). For N=999, that's about 500k terms, trivial.
Alternatively, we could compute using the formula C_n = binomial(2n, n) // (n+1). But we need modulo 10000; we can compute using Python's integer arithmetic, then mod. That's also possible. Since n=1000, computing binomial(2000,1000) is huge but Python's math.comb can compute it? Python 3.8+ has math.comb. But we need up to n=999, so binomial(1998, 999). That's fine. And division by n+1 is exact. So we can compute exact Catalan numbers for each n using math.comb and integer division. But note: math.comb returns an integer. That works. But to compute for many test cases, we can compute on the fly, but easier to precompute with DP.
Let's check time: DP O(N^2) with N=1000 is 0.5 million operations, fast. Using math.comb for each test case is also fine. However, we have up to 10 test cases, and each might require computing binomial; but computing large binomials up to 2000 choose 1000 each time might be okay (maybe each takes a few microseconds). But I'd go with DP precompute.
But there's a nuance: The problem may also consider other interpretations? The given expression: "X1 X2 X3 .... Xn" and we need to put parentheses. Usually it's binary operation, so it's Catalan numbers. The example matches.
Thus answer: For each n, output Catalan(n-1) mod 10000.
Now, we need to output modulo 10000. However, note that Catalan numbers can be huge and modulo 10000 is straightforward. But we must ensure correct handling of mod after sum of products.
Implementation plan:
- Read t.
- Read all n's, determine max_n.
- Compute max_index = max_n - 1, but if max_n = 0? Not possible, n>=1. So compute up to max_n - 1.
- Precompute Catalan_mod array of length max_index+1 (0-indexed) using recurrence:
cat[0] = 1
for i in range(1, max_index+1):
sum_ = 0
for j in range(i):
sum_ += cat[j] * cat[i-1-j]
cat[i] = sum_ % 10000
- For each n, output cat[n-1] % 10000 (though it's already mod 10000).
Edge Cases: n=1 -> cat[0] = 1 -> output 1. The problem might not test n=1 but it's good.
Double-check: For n=4, cat[3] = 5; for n=5, cat[4] = 14. Yes.
Now, consider modulo 10000. Since Catalan numbers mod 10000 can be computed correctly using recurrence modulo 10000. Because the recurrence is integer linear; taking modulo at each step doesn't affect final mod, as (a+b) mod m = ((a mod m)+(b mod m)) mod m, and product similarly. So DP is fine.
But we must be careful: The recurrence uses multiplication of two modded numbers, which yields the product mod m, but since we later sum and take mod, it's equivalent to mod of the true sum. However, careful: The sum of products in exact arithmetic may be huge, but we mod after each addition, still fine because (x mod m) + (y mod m) mod m = (x+y) mod m. And (a mod m)(b mod m) mod m = (ab) mod m. So we can compute cat[i] = sum_{j} (cat[j] mod m)*(cat[i-1-j] mod m) mod m. So correct.
Thus DP with modulo 10000 is correct.
Alternatively, we could compute using the closed form with factorial but need modular inverse not possible.
Thus implement DP.
But note: Catalan numbers can also be computed using the recurrence: cat[n] = (2*(2n-1) * cat[n-1]) // (n+1). But that involves division; if we compute modulo 10000, division is not straightforward because modulus not prime and division not integer modulo. So we can't use that recurrence mod 10000 directly unless we use integer arithmetic then mod. But we can compute exact Catalan numbers using that recurrence with integer arithmetic (since it's integer division). That recurrence is O(N). That might be simpler: compute exact Catalan numbers up to 1000 using big integers, then mod 10000. The recurrence: C_0 = 1; for n>=1: C_n = C_{n-1} * (4*n-2) // (n+1). This is more efficient O(N) and avoids O(N^2). For N=1000, it's fine. But careful: The division must be exact; using integer arithmetic in Python, it's fine. Then we can store C_n as Python int and take mod 10000 when needed. That might be even simpler.
Let's check: For n=1: C_1 = C_0 * (41-2)//(1+1) = 12//2=1. For n=2: C_2 = C_1 * (42-2)//(2+1) =16//3=2. For n=3: C_3 =2*(10)//4=20//4=5. Works.
So we can compute Catalan numbers up to index 999 (n from 0 to 999) using this recurrence. That is O(N) and avoids large nested loops.
But careful: The recurrence involves division; we need to ensure integer division. Since it's exact, we can compute as:
c = 1
for i in range(1, max_index+1):
c = c * (4*i - 2) // (i + 1)
store c.
But we must compute for each i, where i is the index (n). For n=1 (i=1), we compute C_1. Actually if we start with C0=1, then for n from 1 to N: C_n = C_{n-1} * (4*n - 2) // (n + 1). That's correct.
But note: The recurrence yields Catalan numbers for index n. For our problem, answer is C_{n-1}. So we need up to n-1 = 999, i.e., Catalan index 999. So compute up to index 999.
We can compute exact Catalan numbers as big integers and store mod 10000. However, storing the full big integer for 999 may be fine. Alternatively, we can compute modulo 10000 directly using the recurrence? Not directly because division by (n+1) modulo 10000 is not defined. So we need to compute exact integer then mod.
But computing exact Catalan numbers up to 999 using big integers is trivial; they have about 600 digits; Python can handle that quickly. The recurrence involves multiplication by (4n-2) and division by (n+1). These numbers are small (less than 4000). So we can compute sequentially. That's O(N) operations with big ints. For N=1000, that's extremely fast.
Thus we can:
- Precompute list cat_mod where cat_mod[k] = Catalan(k) % 10000, for k=0..max_n-1.
We'll compute exact Catalan numbers and take mod. But we need to compute up to max_n-1, where max_n can be up to 1000, so up to 999. So compute cat_exact[0]=1, cat_mod[0]=1. Then for i from 1 to max_index: cat_exact = cat_exact * (4*i - 2) // (i+1); then cat_mod[i] = cat_exact % 10000. That is simple and efficient.
Alternatively, we can compute using DP O(N^2) mod 10000, which is also fine but a bit more code. Since O(N) with big ints is simpler, I'll go with that.
But caution: For large i, cat_exact becomes huge; but Python's big ints multiplication and division by small ints are efficient for numbers of ~600 digits, and 1000 iterations is trivial.
Thus algorithm:
- Read t.
- Read all n values into list ns.
- Determine max_n = max(ns).
- Compute max_cat_index = max_n - 1.
- If max_cat_index < 0? Actually if max_n=0? Not possible. So proceed.
- Initialize array cat_mod of size max_cat_index+1, set cat_mod[0] = 1 (if max_cat_index >=0).
- If max_cat_index >=1, compute exact = 1 (for index 0), then for i from 1 to max_cat_index:
exact = exact * (4*i - 2) // (i + 1)
cat_mod[i] = exact % 10000
- Then for each n in ns: output cat_mod[n-1] (if n>=1) else maybe 0? n>=1.
Check: n=1 -> cat_mod[0] = 1. n=2 -> cat_mod[1] = C_1=1? Wait expected for n=2, ways = 1, yes.
But example: n=4 -> cat_mod[3] = C_3 =5; n=5 -> cat_mod[4] = C_4 =14. So works.
Thus solution.
But careful: The recurrence formula is for Catalan numbers: C_n = (2(2n-1)/(n+1)) * C_{n-1}. But my recurrence uses (4n-2) // (n+1). That's correct.
Let's test manually:
i=1: (41-2)=2, (1+1)=2 => exact=12//2=1. cat_mod[1]=1.
i=2: (42-2)=6, (2+1)=3 => exact=16//3=2. cat_mod[2]=2.
i=3: (43-2)=10, (3+1)=4 => exact=210//4=5. cat_mod[3]=5.
i=4: (44-2)=14, (4+1)=5 => exact=514//5=14. cat_mod[4]=14.
i=5: (45-2)=18, (5+1)=6 => exact=1418//6=42. cat_mod[5]=42.
Works.
Now, modular output: We need to output exactly as integer modulo 10000. For 14, output 14. For 42, output 42. For large numbers, just the modulo.
Now, what about n=0? Not in input.
Edge: n=1000 -> index=999, compute Catalan(999). That's fine.
Potential pitfalls: The division // is exact only if we compute in order; since Catalan numbers are integer, it's safe.
But there is also possible overflow? Python's big ints are fine.
Now, implement.
But wait: The problem statement: "X1 X2 X3 .... Xn" Maybe the operator is '.' (like multiplication). So it's binary. So yes.
Thus answer.
But there might be twist: The expression is "X1 X2 X3 .... Xn" without operator? Usually it's implied binary operator. In the example, they use dot: (x1.x2). So yes.
Now, need to output modulo 10000. Since Catalan numbers grow fast, for large n, Catalan mod 10000 might be 0? Possibly.
Now, test small: n=1 -> 1, n=2 ->1, n=3->2, n=4->5, n=5->14, n=6->42? Actually Catalan(5)=42, so n=6 gives 42, mod 10000=42.
But is Catalan(5)=42? Actually Catalan numbers: C0=1, C1=1, C2=2, C3=5, C4=14, C5=42, C6=132, C7=429, C8=1430, C9=4862, C10=16796, C11=58786, C12=208012, etc. For n=11, Catalan(10)=16796, mod 10000 = 6796? Actually 16796 mod 10000 = 6796. For n=12, Catalan(11)=58786 mod 10000=8786. For n=13, Catalan(12)=208012 mod 10000=8012. etc.
Now, we must ensure we compute mod for large n correctly.
One more thing: The problem states "modulo 10000". So output values between 0 and 9999 inclusive.
Now, implement solve() function.
Pseudo:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
t = int(data[0])
ns = list(map(int, data[1:1+t]))
max_n = max(ns) if ns else 0
max_idx = max_n - 1
if max_idx < 0:
# if no n? but t>=1 and n>=1? So just handle.
for n in ns:
print(1 if n==1 else 0?) Actually n=0 not allowed. But skip.
return
cat_mod = [0](max_idx+1)
cat_mod[0] = 1
if max_idx >= 1:
exact = 1
for i in range(1, max_idx+1):
exact = exact * (4i - 2) // (i + 1)
cat_mod[i] = exact % 10000
# output
out_lines = []
for n in ns:
out_lines.append(str(cat_mod[n-1]))
sys.stdout.write("\n".join(out_lines))
But note: For n=1, cat_mod[0] is 1. Good.
Now, potential off-by-one: For n=1, answer should be 1? Let's think: For one number, there is exactly one trivial way (no parentheses). Usually it's counted as 1. The problem doesn't specify but it's plausible. However, they gave examples for n=4 and 5; they didn't mention n=1. In many problems like "number of ways to parenthesize a product of n numbers" they give answer as Catalan(n-1), with base n=1 gives 1. So I'll include.
But just to be safe, check if any test case might be n=0? Probably not.
Thus solution.
However, before finalizing, I want to double-check the Catalan number for n=4 (index 3) we got 5. The list of parenthesizations given:
(((x1.x2).x3).x4)
((x1.x2).(x3.x4))
(x1.((x2.x3).x4))
(x1.(x2.(x3.x4)))
((x1.(x2.x3)).x4)
That's 5. Good.
Now, if n=2, there is (x1.x2) only, so 1.
Thus answer correct.
Now, about modulo: Since we compute exact Catalan then mod, we are safe.
Alternatively, we could compute using DP with modulo 10000, but O(N^2) is also fine. But the O(N) recurrence is more efficient for large N, and easier to code.
But there's a subtlety: The recurrence C_n = (4n-2)/(n+1) * C_{n-1} works only for n >=1. So we need to compute up to index max_n-1. Works.
Now, check if max_n could be 1000, then we compute up to index 999, which is fine.
Now, test: n=1000, compute Catalan(999) exact, but that number is massive, maybe takes some time? Let's approximate: Catalan number grows like ~ 4^n / (n^{3/2} sqrt(pi)). For n=999, 4^999 is about 10^602. So number of digits is about 602. Multiplying and dividing by numbers up to ~4000 (which are small) on a 602-digit number is cheap. 999 iterations of such operations is trivial. So it's fine.
Thus solution is good.
Now, consider mod 10000: In some languages, one might need to use DP to avoid big ints, but Python can handle.
Thus final answer.
But I'd also want to double-check if any possibility of misinterpretation: Could it be that the order of the numbers is fixed, and we are inserting parentheses around the expression? That is exactly the Catalan number. So correct.
Now, I'll produce final solution explanation.
But let's ensure we handle input format: first line t, then t lines each with integer n. Possibly there might be spaces, but we can read all.
Now, code.
Potential improvements: Use sys.stdin.
Now, sample:
Input:
2
4
5
Our code: max_n=5, max_idx=4, compute cat_mod[0..4]. cat_mod[3]=5, cat_mod[4]=14. Output: 5 and 14. Matches.
Thus answer.
But I'm going to think: Could there be a nuance about the expression not having any operator? The problem says "X1 X2 X3 .... Xn", but in the examples they use dot. So it's binary.
Thus final.
Now, I'll write a solution explanation.
However, I recall some problems have variations: "number of ways to parenthesize an expression of n numbers" is indeed the Catalan number. The recurrence is Catalan numbers. So answer.
Now, one more check: For n=1, some definitions might say there is no way because you need at least two numbers to put parentheses. But typical combinatorial interpretation counts the number of full binary trees with n leaves, which is Catalan_{n-1}. For n=1, there is one tree (a single leaf). So I think it's correct.
But if the problem expects only n≥2? It says n ≤1000, not specifying lower bound. Could be 1. But if they test n=1, output 1. I think it's fine.
Now, provide final answer.
Given the modulo 10000, we could also compute using the DP O(N^2) to be safe regarding division, but I'll stick with O(N) exact.
Implementation details:
- We'll read all lines.
- Compute max_n.
- If max_n <= 0, just output nothing? Not needed.
- Precompute Catalan mod values up to max_n.
Alternative: Instead of storing exact, we can compute and store mod only, but we need exact for next multiplication/division. So we keep a variable 'exact' as Python int.
Edge: We must be careful about integer division: Since (4*i-2) * exact is divisible by (i+1) exactly. In Python, // works.
Now, I'll produce final code.
But wait, the recurrence is for C_n where n is index. But we might get off-by-one: For n=1 (the first factor?), we want Catalan(0) = 1. So for computing cat_mod[0] = 1. Then for i from 1 to max_idx: cat_mod[i] = Catalan(i). So answer = cat_mod[n-1].
Thus fine.
Now, write solution explanation: Use Catalan numbers, recurrence etc.
Now, I think this is the solution.
But the problem may have twist: They ask "put parenthesis in the expression", and sometimes it's also known as number of ways to parenthesize a product, which is indeed the Catalan numbers.
Thus final. ```python
import sys
MOD = 10000
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
ns = list(map(int, data[1:1+t]))
if not ns:
return
max_n = max(ns)
max_idx = max_n - 1 # we need Catalan numbers up to index max_idx
# Precompute Catalan numbers modulo MOD
cat_mod = [0] * (max_idx + 1)
cat_mod[0] = 1 # C0 = 1
if max_idx >= 1:
exact = 1 # holds the exact Catalan number C_{i-1}
for i in range(1, max_idx + 1):
# exact = exact * (4*i - 2) // (i + 1) gives C_i
exact = exact * (4 * i - 2) // (i + 1)
cat_mod[i] = exact % MOD
out_lines = [str(cat_mod[n - 1]) for n in ns]
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()