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. nacos实现Java和.NetCore的服务注册和调用

    用nacos作为服务注册中心,如何注册.NetCore服务,如何在Java中调用.NetCore服务呢?可以分为下面几个步骤: 0.运行nacos 1.开发.net core服务,然后调用nacos提 ...

  2. 笔精墨妙,妙手丹青,微软开源可视化版本的ChatGPT:Visual ChatGPT,人工智能AI聊天发图片,Python3.10实现

    说时迟那时快,微软第一时间发布开源库Visual ChatGPT,把 ChatGPT 的人工智能AI能力和Stable Diffusion以及ControlNet进行了整合.常常被互联网人挂在嘴边的& ...

  3. #HDU2255#奔小康赚大钱(KM模板题)

    Problem Description传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓,考虑 ...

  4. ElasticSearch 实现分词全文检索 - 高亮查询

    目录 ElasticSearch 实现分词全文检索 - 概述 ElasticSearch 实现分词全文检索 - ES.Kibana.IK安装 ElasticSearch 实现分词全文检索 - Rest ...

  5. flutter widget---->Spacer

    如果你想灵活控制Flex容器(Row, Column)中子组件中的间隔,可以考虑使用Spacer.下面以Row为例子,来为它的子组件添加间距. use Spacer import 'package:f ...

  6. 西瓜视频的li绑定容器 踏坑之旅

    一定要绑定key,不然会出现一个li里面渲染出两个video标签

  7. 使用 diffusers 训练你自己的 ControlNet 🧨

    简介 ControlNet 这个神经网络模型使得用户可以通过施加额外条件,细粒度地控制扩散模型的生成过程.这一技术最初由 Adding Conditional Control to Text-to-I ...

  8. 随机服务系统模拟—R实现(一)

    排队论--随机服务系统 日常生活中存在大量有形和无形的排队或拥挤现象,如旅客购票排队,市内电话占线等现象.排队论的基本思想是 1909 年丹麦数学家.科学家,工程师 A. K. 埃尔朗在解决自动电话设 ...

  9. k8s加入新的master节点出现etcd检查失败

        背景:     昨天在建立好新的集群后,出现了新的问题,其中的一台master节点无法正常工作.虽然可以正常使用,但是就出现了单点故障,今天在修复时出现了etcd健康检查自检没通过.      ...

  10. 异步注解@Async使用及其部分调优

    对于spring异步注解@Async的使用: 对于异步方法调用,从Spring3开始提供了@Async注解,该注解可以被标注在方法上,以便异步地调用该方法.调用者将在调用时立即返回,方法的实际执行将提 ...