Bracket Sequence

F. Bracket Sequence

time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A balanced bracket sequence is a string consisting of only brackets ("((" and "))").

One day, Carol asked Bella the number of balanced bracket sequences of length 2N (N pairs of brackets). As a mental arithmetic master, she calculated the answer immediately. So Carol asked a harder problem: the number of balanced bracket sequences of length 2N (N pairs of brackets) with K types of bracket. Bella can't answer it immediately, please help her.

Formally you can define balanced bracket sequence with K types of bracket with:

  • ϵ (the empty string) is a balanced bracket sequence;
  • If A is a balanced bracket sequence, then so is lAr, where l indicates the opening bracket and r indicates the closing bracket. lr must match with each other and forms one of the K types of bracket.
  • If A and B are balanced bracket sequences, then so is AB.

For example, if we have two types of bracket "()()" and "[][]", "()[]()[]", "[()][()]" and "([])([])" are balanced bracket sequences, and "[(])[(])" and "([)]([)]" are not. Because "(](]" and "[)[)" can't form can't match with each other.

Input

A line contains two integers N and K: indicating the number of pairs and the number of types.

It's guaranteed that 1K,N10^5.

Output

Output one line contains a integer: the number of balanced bracket sequences of length 2N (N pairs of brackets) with K types of bracket.

Because the answer may be too big, output it modulo 10^9+7.

Examples
input
1 2
output
2
input
2 2
output
8
input
24 20
output
35996330

思路:

所以本题直接是变种括号序列问题,可以直接套公式,注意除法取模等同于乘以分母的乘法逆元取模。

代码实现:

#include<iostream>
using namespace std;
#define int long long
const int p=1e9+7;
int quick(int a,int b,int p){
    int res=1;
    while(b){
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
int c(int a,int b,int p){
    if(a<b)return 0;
    int res=1;
    for(int i=1,j=a;i<=b;i++,j--){
        res=res*j%p;
        res=res*quick(i,p-2,p)%p;
    }
    return res;
}
int lucus(int a,int b,int p){
    if(a<p&&b<p)return c(a,b,p);
    else return c(a%p,b%p,p)*lucus(a/p,b/p,p)%p;
}
signed main(){
    int a,b;
    cin>>a>>b;
    int res=lucus(2*a,a,p)%p;
    res=res*quick(a+1,p-2,p)%p;
    for(int i=0;i<a;i++)res=(res*b)%p;
    cout<<res<<endl;
    return 0;
}

 

posted @ 2023-04-15 09:30  回忆、少年  阅读(24)  评论(0编辑  收藏  举报