程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDOJ 5184 Brackets 卡特蘭數擴展

HDOJ 5184 Brackets 卡特蘭數擴展

編輯:關於C++

 

既求從點(0,0)只能向上或者向右並且不穿越y=x到達點(a,b)有多少總走法...

有公式: 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 than n.
Output For each case,output answer % 1000000007 in a single line.
Sample Input
4
()
4
(
6
()

Sample Output
1
2
2

HintFor 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        :CKboss
Created Time  :2015年03月18日 星期三 20時10分21秒
File Name     :HDOJ5184.cpp
************************************************ */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include

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;in) 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 left
		int b=n/2-right; /// remain right

		if(b>a) swap(a,b);
		LL ans = (COMB(a+b,b)-COMB(a+b,b-1)+mod)%mod;
		cout<

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved