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;
}

Bracket Sequence的更多相关文章

  1. UESTC 1546 Bracket Sequence

                                        Bracket Sequence Time Limit: 3000MS   Memory Limit: 65536KB   64 ...

  2. CF#138 div 1 A. Bracket Sequence

    [#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...

  3. CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)

    E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...

  4. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈

    C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...

  5. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  6. (中等) UESTC 94 Bracket Sequence,线段树+括号。

    There is a sequence of brackets, which supports two kinds of operations. we can choose a interval [l ...

  7. Replace To Make Regular Bracket Sequence

    Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...

  8. CF1095E Almost Regular Bracket Sequence

    题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...

  9. D - Replace To Make Regular Bracket Sequence

    You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...

  10. CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈

    C. Replace To Make Regular Bracket Sequence time limit per test 1 second memory limit per test 256 m ...

随机推荐

  1. ZOJ 3735 Josephina and RPG (概率dp)

    题意:给你一个n,然后给你C(n,3)个队伍, 给你每个队伍之间的胜率. 接下来给你m个队伍,让你依次跟他们比赛,开始你能选择任意的队伍,如果你打赢了一支队伍,你可以选择换成输给你的这个队伍或者不换, ...

  2. “==” 与 equals 区别 简化易懂版

    首先,我们只需要看Object中的equals 方法写的是啥 很显然,就是一句话,"==" 与 equals方法作用完全一致. 都是用来比较在内存的首地址,即用来比较两个引用变量是 ...

  3. K8S安全学习

    k8s安全学习 一.云 云的定义看似模糊,但本质上,它是一个用于描述全球服务器网络的术语,每个服务器都有一个独特的功能.云不是一个物理实体,而是一个庞大的全球远程服务器网络,它们连接在一起,旨在作为单 ...

  4. linux使用汇总

    linux使用汇总 Linux的目录结构 没有逻辑磁盘分区(C盘.D盘...) 是一棵树形结构,根目录是/ 根目录下边有几个文件夹,需要我们了解: /etc:配置文件所在的文件夹.比如:安装JDK,配 ...

  5. ThreadPool实现机制

    Android中阻塞队列的应用有哪些 阻塞队列在 Android 中有很多应用,比如: 线程池:线程池任务的执行就是基于一个阻塞队列,如果线程池任务已满,则任务需要等待阻塞队列中的其他任务完成. Ha ...

  6. C# 通过StreamWriter输出的TXT流文件,前缀带EF BB BF

    好久没有动笔写博客了,这个小天地被我闲置的放了好久好久,接下来要慢慢捡起来了. 备注:通过C#的StreamWriter类输出一个TXT流文件,供下位机工程师使用,发现打开的16进制文件中,默认添加了 ...

  7. 安装原版Windows自动安装已经备份的驱动

    安装完Windows10后联网会自动更新驱动,除了自动更新.如果不让新安装的Windows系统自动安装驱动以外,还有自己手动安装驱动的方式.如何在安装系统过程中就让系统自己安装好驱动? 重装系统首先要 ...

  8. win10_Dock安装设置

    1.安装:(在win10上安装) 桌面版:https://www.docker.com/products/docker-desktop, 安装后重启电脑 2.配置 打开DockerDesktop,(可 ...

  9. Let和Const区别,详细版本

    let:声明的是变量1.不存在变量提升 // var 的情况 console.log(foo); // 输出undefined var foo = 2; // let 的情况 console.log( ...

  10. kubernetes(k8s)中部署dashboard可视化面板

    Web 界面 (Dashboard) Dashboard 是基于网页的 Kubernetes 用户界面.你可以使用 Dashboard 将容器应用部署到 Kubernetes 集群中,也可以对容器应用 ...