[HDU-5184]Brackets

题目

传送门

题解

考虑将 ( 理解为向右走,而 ) 理解为向左走,那么这道题就转化为:

指定走前几步,问此时能走到右上角且不经过对角线的方案数是多少.

这是卡特兰数的其中一个意义,我们可以利用分析卡特兰数递推式的相似的思路分析这个问题.

记我们还需填 \(a\)(\(b\)),那么不考虑限制就是 \({a+b}\choose a\),但是如果超过对角线,考虑从刚刚开始超过的那里沿对角线翻折,终点一定会落在右上角之上,也就是 \({a+b}\choose b+1\),那么答案就是 \({{a+b}\choose a}-{{a+b}\choose b+1}\).

代码

#include<cstdio>
#include<cstring>

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define fi first
#define se second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x<y?y:x;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int maxn=1000000;
const int mod=1000000007;

inline int getmod(const int x){
    return (x%mod+mod)%mod;
}

char s[maxn+5];
int n,len;

/** @brief 还需要填入多少左括号*/
int a;
/** @brief 还需要填入多少右括号*/
int b;

int fac[maxn+5],invfac[maxn+5];
inline int qkpow(int a,int n){
    int ret=1;
    for(;n>0;n>>=1,a=1ll*a*a%mod)if(n&1)ret=1ll*ret*a%mod;
    return ret;
}

inline void Init(){
    fac[0]=1;
    rep(i,1,maxn)fac[i]=1ll*fac[i-1]*i%mod;
    invfac[maxn]=qkpow(fac[maxn],mod-2);
    fep(i,maxn-1,1)invfac[i]=1ll*invfac[i+1]*(i+1)%mod;
    invfac[0]=1;
}

inline int C(const int n,const int m){
    if(n<m)return 0;
    if(n==0 || n==m)return 1;
    return 1ll*fac[n]*invfac[m]%mod*invfac[n-m]%mod;
}

signed main(){
    Init();
Begin:
    while(~scanf("%d",&n)){
        scanf("%s",s+1);len=strlen(s+1);
        if(n&1){puts("0");goto Begin;}
        a=b=0;
        rep(i,1,len){
            if(s[i]=='(')++a;
            else if(s[i]==')')++b;
            if(a<b){puts("0");goto Begin;}
        }
        a=(n>>1)-a,b=(n>>1)-b;
        if(a<0 || b<0){puts("0");goto Begin;}
        writc(getmod(C(a+b,a)-C(a+b,b+1)),'\n');
    }
    return 0;
}
posted @ 2020-09-09 09:18  Arextre  阅读(131)  评论(0编辑  收藏  举报