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. 这篇文章汇聚33个BUG!来挑战一下,看看你能找出来几个?

    你好呀,我是歪歪. 前几天看到"Qunar技术沙龙"公众号推送了一篇关于他们举办了一场"Code Review大赛"的文章. 看到 Code Review 我很 ...

  2. SqlServer 高并发的情况下,如何利用锁保证数据的稳定性

    sql的锁机制,是时刻贯彻在每一次的sql事务中的,为了理解更透彻,介绍锁之前,我们得先了解,锁是为了干什么!! 1.数据库异常情况 1.1 先来聊聊数据可能发生个异常状况 1)脏读:读未提交,顾名思 ...

  3. Go 语言:如何利用好 TDD 学习指针并了解 Golang 中的 error 处理

    我们在上一节中学习了结构体(structs),Go语言:利用 TDD 驱动开发测试 学习结构体.方法和接口 它可以组合与一个概念相关的一系列值. 你有时可能想用结构体来管理状态,通过将方法暴露给用户的 ...

  4. Spring IOC——源码分析

    Spring 容器的 refresh() 创建容器 1 //下面每一个方法都会单独提出来进行分析 2 @Override 3 public void refresh() throws BeansExc ...

  5. Cannot read properties of undefined (reading 'toUpperCase')

    无法读取 JS 中未定义的属性"toUpperCase"|鲍比哈兹 (bobbyhadz.com) 根据其中的内容找到了答案:使用了未定义的属性去使用toUpperCase()函数 ...

  6. 当后端人员未提供接口,前端人员该怎么测试 --mock

    1.回顾 2.线上的mock http://rap2.taobao.org/ https://www.easy-mock.com/ 3.线上接口文档 Swagger https://swagger.i ...

  7. 超详细!新手如何创建一个Vue项目

    目录 一.在官网下载Vue.js 二.使用<script>标签直接引入本地的vue.js 三.使用CDN引入Vue.js 四.验证是否安装成功 五.安装Vue Devtools浏览器调试插 ...

  8. 四月二十二日java基础知识

    1.利用接口实现类的多重继承:java语言中接口的主要作用是可以帮助实现类似于类的多重继承功能.多重继承,是指一个子类可以有一个以上的直接父类,该子类可以直接继承它所有父类的非私有成员.2.一个类实现 ...

  9. Intellij_idea for循环 快捷键

    for循环四次.用 i 进行for循环 4.for fori 增强for循环 int [] arrays=new int[2]; arrays.for

  10. docker启动mysql注意事项

    1.编码问题 登录mysql伪终端 mysql查看编码 show variables like 'character%'; 宿主机在conf.d中添加配置my.cnf文件 [client] defau ...