Hdoj 5184 Brackets Cattleya number Extension

Source: Internet
Author: User


both the point (0,0) can only be up or to the right and do not cross the y=x arrival point (A, B) How many total go ...

have a formula: C (a+b,min (b))-C (A+b,min (A, B)-1)// /

Origami method proves Cattleya number: http://blog.sina.com.cn/s/blog_6917f47301010cno.html


BracketsTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 506 Accepted Submission (s): 120


Problem Descriptionwe 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) is 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 is regular brackets sequences:
(), (()), ()(), ()(())
While the following character sequences is 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 is given already.

Inputmulti Test Cases (about - ), every case occupies, 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 the length of STR is larger than 0 and no more than n .
Outputfor 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 is (()) and () (). For the third case regular sequences is () () () and () (()).



/* ***********************************************author:ckbosscreated time:2015 March 18 Wednesday 20:10 21 seconds file Name : hdoj5184.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;typedef Long long int ll;const int maxn=1001000;const LL mod=1000000007ll;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<maxn;i++) {inv[i]= inv[mod%i]* (mod-mod/i)%mod;jc[i]= (jc[i-1]*i)%mod;jcv[i]= (jcv[i-1]*inv[i])%mod;} ll Comb (ll n,ll m) {if (m<0| | M>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<len&&flag;i++) {if (str[i]== ' (') left++;else if (str[i]== ') ') right++;if (left>=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<<ans<<endl;} return 0;}


Hdoj 5184 Brackets Cattleya number Extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.