To solve this problem, we need to determine the number of valid parenthesis strings of length 2n that are k-valid. A string is considered k-valid if adding k opening parentheses '(' at the beginning and k closing parentheses ')' at the end makes the entire string a valid parenthesis sequence.
Understanding the Problem
-
Valid Parenthesis String: A string of parentheses is valid if every opening parenthesis '(' has a corresponding closing parenthesis ')', and the pairs are properly nested.
-
k-Valid String: A string s is k-valid if by adding k '(' at the beginning and k ')' at the end, the resulting string is valid.
Approach
To count the number of k-valid strings of length 2n:
-
Combination Count: The total number of strings with exactly n '(' and n ')' is given by the binomial coefficient C(2n, n), where C(2n, n) represents the number of ways to choose n positions out of 2n for '('.
-
Invalid Strings: Not all such combinations will be k-valid. Some strings will cause the balance (number of '(' minus ')') to dip below -k at some point, making the augmented string invalid.
-
Using Reflection Principle: We subtract the number of invalid strings C(2n, n - k - 1) from the total combinations C(2n, n) to get the number of valid k-valid strings.
- Note: If
n - k - 1 is negative, it means no invalid strings exist for those parameters, so we only consider C(2n, n).
-
Modular Arithmetic: Since the numbers can be very large, all computations are done modulo 10^9 + 7.
-
Precomputation: To efficiently compute binomial coefficients for large n and k, we precompute factorials and their modular inverses up to 2 * 10^6.
Implementation Details
- Factorials and Inverses: Precompute factorials (
fac) and inverse factorials (inv_fac) modulo 10^9 + 7.
- Modular Exponentiation: Use fast exponentiation to compute modular inverses.
- Handling Large Inputs: Optimize input/output operations by unsyncing C++ streams with C streams and untie
cin and cout.
C++ Implementation
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAX = 2000000;
// Function to compute x^y mod MOD
ll power_mod(ll x, ll y, ll mod) {
ll res = 1;
x %= mod;
while(y > 0){
if(y & 1LL){
res = res * x % mod;
}
x = x * x % mod;
y >>=1LL;
}
return res;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
// Precompute factorial and inverse factorial
vector<long long> fac(MAX+1, 1);
for(int i=1;i<=MAX;i++) fac[i] = fac[i-1] * i % MOD;
vector<long long> inv_fac(MAX+1,1);
inv_fac[MAX] = power_mod(fac[MAX], MOD-2, MOD);
for(int i=MAX-1;i>=0;i--) inv_fac[i] = inv_fac[i+1] * (i+1) % MOD;
// Function to compute C(n,k)
auto comb = [&](int n, int k) -> ll{
if(k <0 || k >n) return 0;
return fac[n] * inv_fac[k] % MOD * inv_fac[n -k] % MOD;
};
int t;
cin >> t;
while(t--){
int n;
ll k;
cin >> n >> k;
ll c1 = comb(2*n, n);
ll r = n - (ll)k -1;
ll c2 = 0;
if(r >=0){
c2 = comb(2*n, r);
}
ll ans = (c1 - c2 + MOD) % MOD;
cout << ans << "\n";
}
}
Explanation of the Code
-
Precomputation:
- We precompute the factorial (
fac) and inverse factorial (inv_fac) for all numbers up to 2 * 10^6 to efficiently calculate binomial coefficients.
-
Combination Function:
- A lambda function
comb calculates the binomial coefficient C(n, k) using the precomputed factorials and inverse factorials. If k is out of bounds, it returns 0.
-
Processing Test Cases:
- For each test case, we read
n and k.
- Calculate
C(2n, n) which is the total number of strings with exactly n '(' and n ')'.
- Calculate
C(2n, n - k - 1) which represents the number of invalid strings.
- The answer is
C(2n, n) - C(2n, n - k - 1), adjusted modulo 10^9 + 7 to handle negative results.
-
Output:
- The computed answer for each test case is printed on a new line.
Complexity Analysis
-
Time Complexity:
- Precomputation: O(MAX) where
MAX = 2 * 10^6.
- Each test case: O(1).
- Total: O(2 * 10^6 + t).
-
Space Complexity:
- O(MAX) for storing factorials and inverse factorials.
Sample Input and Output
Input
3
2 1
4 2
5 3
Output
5
62
242
This matches the sample provided, ensuring the correctness of the implementation.