HDOJ 5184 Brackets catlan number Extension
This method is used to calculate the total number of walk performed by the starting point (0, 0), which can only be up or down to the right without traversing y = x to the arriving point (a, B...
Formula: C (a + B, min (a, B)-C (a + B, min (a, B)-1 )///
Brackets
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 506 Accepted Submission (s): 120
Problem Description We give the following inductive definition of a "regular brackets" sequence:
● The empty sequence is a regular brackets sequence,
● If s is a regular brackets sequence, then (s) are regular brackets sequences, and
● If a and B are regular brackets sequences, then AB is a regular brackets sequence.
● No other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:
(),(()),()(),()(())
While the following character sequences are not:
(,),)(,((),((()
Now we want to construct a regular brackets sequence of length N , How many regular brackets sequences we can get when the front several brackets are given already.
Input Multi test cases (about 2000 ), Every case occupies two lines.
The first line contains an integer N .
Then second line contains a string str which indicates the front several brackets.
Please process to the end of file.
[Technical Specification]
1 ≤ n ≤1000000
Str contains only '(' and ')' and length of str is larger than 0 and no more N .
Output For each case, output answer % 1000000007 In a single line.
Sample Input
4()4(6()
Sample Output
122HintFor the first case the only regular sequence is ()().For the second case regular sequences are (()) and ()().For the third case regular sequences are ()()() and ()(()).
/*************************************** * ******** Author: CKbossCreated Time: October 16, Wednesday, March 18, 2015 21 seconds File Name: HDOJ5184.cpp *************************************** * *********/# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; typedef long int LL; const int maxn = 1001000; const LL mod = limit 7ll; int n, len; char str [maxn]; LL inv [maxn]; LL jc [maxn], jcv [maxn]; void init () {inv [1] = 1; jc [0] = 1; jcv [0] = 1; jc [1] = 1; jcv [1] = 1; for (int I = 2; I
N) return 0LL; if (m = 0 | m = n) return 1LL; LL ret = (jc [n] * jcv [n-m]) % mod * jcv [m]) % mod; return ret;} int main () {// freopen(in.txt, r, stdin); // freopen(out.txt, w, stdout ); init (); while (scanf (% d, & n )! = EOF) {scanf (% s, str); len = strlen (str); bool flag = true; if (n % 2 = 1) flag = false; int left = 0, right = 0; for (int I = 0; I
= Right) continue; else flag = false;} if (flag = false) {puts (0); continue;} int a = n/2-left; /// remain leftint B = n/2-right; // remain rightif (B> a) swap (a, B); LL ans = (COMB (a + B, b)-COMB (a + B, B-1) + mod) % mod; cout <