To solve this problem, we need to determine the number of valid parentheses sequences of length (2n), which is given by the (n)-th Catalan number. Catalan numbers are a sequence of natural numbers that have many applications in combinatorial mathematics, including counting valid parentheses sequences.
Approach
The (n)-th Catalan number can be computed using the formula: [ C(n) = \frac{1}{n+1} \binom{2n}{n} ] where (\binom{2n}{n}) is the binomial coefficient of (2n) choose (n). This formula works because the binomial coefficient counts all possible ways to arrange (n) opening and (n) closing parentheses, and dividing by (n+1) filters out the invalid sequences.
Solution Code
import math
n = int(input())
catalan = math.comb(2 * n, n) // (n + 1)
print(catalan)
Explanation
- Binomial Coefficient Calculation: The binomial coefficient (\binom{2n}{n}) is computed using
math.comb(available in Python 3.10+), which efficiently calculates the number of ways to choose (n) elements from (2n) elements. - Catalan Number: Divide the binomial coefficient by (n+1) to get the (n)-th Catalan number. Since Catalan numbers are integers, integer division (
//) is used to ensure the result is an integer.
This approach is efficient and leverages Python's ability to handle large integers, making it suitable for large values of (n). For example:
- When (n=3), the result is (5) (valid sequences:
()()(),(())(),()(()),((())),(()())). - When (n=4), the result is (14).
The time complexity is dominated by the binomial coefficient calculation, which is (O(n)) for math.comb (as it uses dynamic programming or multiplicative formulas under the hood). This ensures the solution is efficient even for large (n).
Answer: The code computes the (n)-th Catalan number, which is the number of valid parentheses sequences of length (2n). For the given input (n), the output is the desired result. For example, if input is 3, output is 5. If input is 4, output is14, etc. So the final answer is the computed Catalan number for the input (n).
(\boxed{C(n)}) where (C(n)) is the (n)-th Catalan number as computed above. For a specific input, replace (C(n)) with the numerical value. For example, if input is 3, the answer is (\boxed{5}). If input is 5, it would be (\boxed{42}), etc. However, since the input is variable, the general solution is the code provided, and the answer for any input (n) is the result of the code.
But if we need to present it as a formula-based answer, it's (\boxed{\frac{1}{n+1} \binom{2n}{n}}). However, in programming contests, the expected answer is the numerical value for the given input, so the code is the solution, and the answer is the output of the code. For example, if the input is 3, the answer is (\boxed{5}).
Assuming the input is a specific (n), let's say the problem expects the code to output the correct value, so the final answer is the computed Catalan number for the input (n). For example, if input is 5, the answer is (\boxed{42}).
But since the problem's input isn't given here, the general solution is the code, and the answer is the Catalan number for the input (n). So the answer box should contain the result of the code for the given input. For example, if the input is 3, then (\boxed{5}). If input is 2, (\boxed{2}), etc.
Given that the problem likely expects the code to compute the correct value, the answer is the numerical result of the code for the input (n). So, for any input (n), the answer is the (n)-th Catalan number, which the code computes correctly.
Thus, the final answer is the computed Catalan number for the input (n), e.g., (\boxed{5}) for (n=3). (\boxed{5}) (example for (n=3))

作者声明:本文包含人工智能生成内容。