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. Android进度表示

    在连接上数据库之后,一切都变得简单了呢! 开心,很轻松地就能够将APP里面的相关内容写完啦! 尝试了好久的连接Mysql数据库,最后还是没有成功: 虽然Android studio里面自带的SQLit ...

  2. python爬虫基础教程

    爬虫介绍 爬虫就是程序,是从互联网中,各个网站上爬取数据(能浏览到的网页才可以爬),做数据清洗,入库 爬虫本质: 模拟http请求,获取数据,入库 网站/app > 抓包 我们日常使用的baid ...

  3. 分享我通过 API 赚钱的思路

    写在最前 我们经常看到非常多的 API 推荐,但又经常收藏到收藏夹里吃灰,仿佛收藏了就是用了. 很多时候没有用起来,可能是因为想不到某类 API 可以用来做什么或者能应用在哪里. 下面我将我思考的一些 ...

  4. LinkedBlockingQueue出入队实现原理

    类图概述 由类图可以看出,L是单向链表实现的,有两个ReentrantLock实例用来控制元素入队和出队的原子性,takeLock用来控制只有一个线程可以从队头获取元素,putLock控制只有一个线程 ...

  5. 解决class path resource [applicationContext.xml] cannot be opened because it does not exist

    在学习spring的过程出现class path resource [applicationContext.xml] cannot be opened because it does not exis ...

  6. 一文总结你需要的OpenCV操作

    目录 一.OpenCV简介 1.1 OpenCV是什么 1.2 安装及使用 二.图像的基础 2.1 成像原理 2.2 图像格式 2.3 颜色空间 三.OpenCV基础操作 3.1 图像的读取.显示.保 ...

  7. 四月十三号java基础知识

    1.双层for循环外层要写,但是内层一定要写,不然容易报错2.Exception in thread "main" java.lang.ArrayIndexOutOfBoundsE ...

  8. 服务器实现端口转发的N种方式

    简介 在一些实际的场景里,我们需要通过利用一些端口转发工具,比如系统自带的命令行工具或第三方小软件,来绕过网络访问限制触及目标系统.下文为大家总结了linux系统和windows系统端口转发常用的一些 ...

  9. [GAUSS-50201]:The /opt/software/openGauss/xxxx-RedHat-64bit.tar.bz2 does not exist

    问题描述:使用redhat7.9来安装opengauss集群,预安装过不去.opengauss官方只支持centos版本,最好是centos7.6. [root@db01 script]# ./gs_ ...

  10. Golang一日一库之 日志库 zap

    简介 在开发过程中 会使用到日志库去记录错误的日志,尤其是golang中 有无穷无尽的error 如果不记录,当你的代码出错,就无从排错了. zap 是开源的 Go 高性能日志库 主要有以下特点: 支 ...