Bracket Sequence
F. Bracket Sequence
0.5 seconds
256 megabytes
standard input
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.
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 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.
1 2
2
2 2
8
24 20
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的更多相关文章
- UESTC 1546 Bracket Sequence
Bracket Sequence Time Limit: 3000MS Memory Limit: 65536KB 64 ...
- CF#138 div 1 A. Bracket Sequence
[#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...
- CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...
- 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 ...
- 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 ...
- (中等) UESTC 94 Bracket Sequence,线段树+括号。
There is a sequence of brackets, which supports two kinds of operations. we can choose a interval [l ...
- Replace To Make Regular Bracket Sequence
Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...
- CF1095E Almost Regular Bracket Sequence
题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...
- D - Replace To Make Regular Bracket Sequence
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...
- 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 ...
随机推荐
- Android笔记--发送彩信
发送彩信 界面还是那个界面,图片也实现了从相册进行选择图片,选择完成之后,就会回到界面里面的功能: 下面接着实现发送彩信的完整版: 具体实现如下: 界面代码不再展示,主要着重于后台的代码编写啦! 1. ...
- Javacv 音视频小工具 - 下载抖音视频
一.前言 大家好,俗话说的好,学习新的知识后要学以致用,在学习音视频的过程中,你有没有疑问,不知道音视频可以用来做什么.下面举几个例子,比较耳熟能详,被吹到风口的一些场景有:AI 视觉计算, AI 人 ...
- .Net 6.0全局异常捕获
背景: 全局异常捕获是我们开发当中不可获取的一个环节,顾名思义就是全局的捕获异常,并响应出去,今天就带着各位童鞋们在.Net 6.0当中如何做到全局异常捕获. 思路: 我们可以通过请求管道达到我们全局 ...
- SpringBoot——MVC原理
更多内容,前往 IT-BLOG 一.SpringMVC自动配置 SpringMVC auto-configuration:SpringBoot 自动配置好了SpringMVC.以下是 SpringBo ...
- Three.js 进阶之旅:全景漫游-初阶移动相机版
声明:本文涉及图文和模型素材仅用于个人学习.研究和欣赏,请勿二次修改.非法传播.转载.出版.商用.及进行其他获利行为. 摘要 3D 全景技术可以实现日常生活中的很多功能需求,比如地图的街景全景模式.数 ...
- blender资源库 【自用】
1 https://www.threedscans.com A Website with a lot of photo-scanned sculptures which are free to use ...
- 电商AARRR模型分析(一)——R语言
在2010年,互联网创业者增长黑客之父肖恩·埃利斯(Sean Ellis)就创造了增长黑客(Growth hacker)这样一个概念.2015年,范冰撰写的一本新书<增长黑客>确立了Gro ...
- 和 chatgpt 聊了一会儿分布式锁 redis/zookeeper distributed lock
前言 最近的 chatGPT 很火爆,听说取代程序员指日可待. 于是和 TA 聊了一会儿分布式锁,我的感受是,超过大部分程序员的水平. Q1: 谈一谈 java 通过 redis 实现分布式 锁 ch ...
- CVE-2022-21454:漏洞整改mysql5.7.37升级至5.7.38 tar包升级
问题描述:对数据库服务器进行漏扫,发现一些中高位漏洞需要整改,有些数据库需要升级到最新版 漏洞修改指导链接:https://www.oracle.com/security-alerts/cpuapr2 ...
- BISS-C 8通道采集renishaw传感器及其CRC校验
背景 BISS-C 是常见的位置编码器传输协议,相对于传统的协议,支持更快的传输速度,电器接口为电压差分RS422或者485,抗干扰能力较强,在精密位置传输中应用广泛. 下述信息源自雷尼绍 典型的请求 ...