题解
一个合法的括号序列,满足
-
长度为偶数
-
前缀和处处不小于0
-
左括号等于右括号数量
code
#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) ((x)&(-x))
using namespace std;
const ll inf=1e18;
const ll mod=1e9+7;
ll qpow(ll a,ll n)
{
ll res=1;
while(n)
{
if(n&1) res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return res;
}
ll inv(ll x)
{
return qpow(x,mod-2);
}
ll C(ll n,ll m)
{
ll res=1;
for(ll i=1;i<=m;i++)
{
res=res*inv(i)%mod;
res=res*(n-i+1)%mod;
}
return res;
}
void solve()
{
ll n;
cin>>n;
string s;
cin>>s;
ll m=s.size();
ll cnt=n/2;
ll sum=0;
bool flag=1;
for(auto it:s)
{
if(it==')')
{
cnt--;
sum--;
}
else sum++;
if(sum<0) flag=0;
}
if(cnt<0||m-(n/2-cnt)>n/2||n&1) flag=0;
if(flag) cout<<(C(n-m,cnt)+mod+mod-C(n-m,(sum+2LL+n-m)/2)+mod)%mod;
else cout<<0;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll TT=1;
//cin>>TT;
while(TT--) solve();
return 0;
}
标签:return,--,res,ll,II,flag,Bracket,Sequences,mod
From: https://www.cnblogs.com/pure4knowledge/p/18331267