首页 > 其他分享 >Bracket Sequence

Bracket Sequence

时间:2023-04-15 09:46:18浏览次数:43  
标签:Sequence int res Bracket balanced output types bracket

#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;
}
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 1≤K,N≤10^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;
}

 

标签:Sequence,int,res,Bracket,balanced,output,types,bracket
From: https://www.cnblogs.com/hxss/p/17320541.html

相关文章

  • codeforces#FF DIV2C题DZY Loves Sequences(DP)
    题目地址:http://codeforces.com/contest/447/problem/CC.DZYLovesSequencestimelimitpertestmemorylimitpertestinputoutputa,consistingof nai, ai + 1, .........
  • 1811E Living Sequence 两种解法
    思维进制转换数位DP无前导0T3Problem-1811E-Codeforces题目大意从一个不含有数字4的递增序列中找第k个数并输出。如\(1,2,3,5,6,7,8,9,10,11,12\),\(k=4\)时输出\(5\)。思路1有一个巧妙的解法:考虑这个问题,从一个没有限制的从1开始的递增序列找出第k个数,......
  • C. Sequence Master
    题目链接挺有意思的一道题题意:给定一个\(2*n\)长度的数组\(p\),要求构造一个长度也为\(2*n\)的整数数组\(q\),使得\(q\)满足从\(q\)中任选\(n\)个数字的积等于\(q\)中剩下\(n\)个数的和,求出\(p\)与\(q\)的最短距离最短距离定义为对应元素差的绝对值之和由于\(q\)的要求有点严苛......
  • UVa 10049 Self-describing Sequence (自描述序列&二分递推)
    10049-Self-describingSequenceTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=990SolomonGolomb's self­describingsequence  istheonlynon­decreas......
  • (KMP 1.1)hdu 1711 Number Sequence(KMP的简单应用——求pattern在text中第一次出现的
    题目:NumberSequenceTimeLimit:10000/5000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):12902    AcceptedSubmission(s):5845ProblemDescriptionGiventwosequencesofnumbers:a[1],a[2],......,a[N],andb[1......
  • UML建模之时序图(Sequence Diagram)
     一、时序图简介(Briefintroduction) 二、时序图元素(SequenceDiagramElements)  角色(Actor)  对象(Object)  生命线(Lifeline)  控制焦点(FocusofControl)  消息(Message)  自关联消息(Self-Message)  CombinedFragments  三、时序图实例分析(SequeceDiagramExampleA......
  • ZOJ - 2421 Recaman's Sequence(打表水题)
    题目大意:A0=0Am=A(m-1)-m,如果Am小于0或者Am前面已经出现过了,那么Am=A(m-1)+m解题思路:打表水题我用的是map,纪录数是否出现过了#include<cstdio>#include<cstring>#include<map>usingnamespacestd;constintN=500010;typedeflonglongLL;map<LL,int>Ma......
  • CodeForces - 149D Coloring Brackets(区间DP)
    题目大意:给你一个符合括号规则的字符串,现在要求你将这些括号染色,染色规则如下1.一个括号要么被染成红色,要么被染成蓝色,要么不染2.相邻的括号的颜色不能相同(可以同为无色)3.成对的括号只能有一个被染色问染色的方案数解题思路:0表示不染,1表示红色,2表示蓝色那么成对的括号......
  • POJ - 2955 Brackets(区间dp)
    题目大意:给出一个括号字符串,问这个字符串中符合规则的最长子串的长度解题思路:区间dp,用dp[i][j]表示[i,j]这个区间内有符合规则的最长子串的长度如果str[i]和str[j]能构成()或者[],那么dp[i][j]=dp[i+1][j-1]+2剩下的情况就是dp[i][j]=max(dp[i][j],dp[i][k]+dp[k......
  • UVA - 10706 Number Sequence 子序列
    #include<cstdio>#include<cmath>#include<algorithm>usingnamespacestd;constintmaxn=32761;longlongS[maxn];//存放的是S1,S2,到SK的和,S[5]表示了S1到S4的和,当数字变化到K的时候,一共有多少个字数了intborder[9]={0,1,10,100,1000,10000,100000,1000000,10000000......