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 ...
随机推荐
- js直接操作数据库会怎么样
这几天刷脉脉的时候看到一个话题初看觉得可笑,再看陷入沉思,最后还是决定花点时间想清楚,写下来. 确实没见人这么干过,为什么呢? 技术限制 被技术限制了?据我所知目前没有面向js的数据库驱动,但反观现在 ...
- 如何快速弄懂Java线程池
Java线程池是一种高效的多线程编程技术,它可以帮助程序员有效地控制多线程的并发执行.它可以提高应用程序的性能.降低内存消耗和减少延迟. 线程池的原理是,程序员可以将每个任务放入线程池中,然后由线程池 ...
- Vue中使用axios发起POST请求成功,却被挂起
服务器能接收请求并处理,控制台没有报错,axios().catch也没有捕获异常.随后查看控制台网络页,发现被挂起 在Stack搜到同问题,上面说将axios()函数返回用.then查看被挂起信息.n ...
- SpringBoot集成海康网络设备SDK
目录 SDK介绍 概述 功能 下载 对接指南 集成 初始化项目 初始化SDK 初始化SDK概述 新建AppRunner 新建SdkInitService 新建InitSdkTask 新建 HCNetS ...
- Leetcode Practice -- 字符串
目录 14. 最长公共前缀 思路解析 151. 反转字符串中的单词 思路解析 125. 验证回文串 思路解析 415. 字符串相加 思路解析 3. 无重复字符的最长子串 思路解析 8. 字符串转换整数 ...
- 算法学习笔记(20): AC自动机
AC自动机 前置知识: 字典树:可以参考我的另一篇文章 算法学习笔记(15): Trie(字典树) KMP:可以参考 KMP - Ricky2007,但是不理解KMP算法并不会对这个算法的理解产生影响 ...
- Trie树结构
PrefixTree 208. 实现 Trie (前缀树) Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键.这一数据结构 ...
- RochyLinux 8.6安装Oracle19c Client
一.环境准备 1.1 软件下载 下载地址:https://www.oracle.com/database/technologies/oracle19c-linux-downloads.html 其中c ...
- Comic Life - 超棒的漫画制作工具,拥有多种动画模版,创作属于自己的漫画
Comic Life是一个照片编辑器,能够添加各种效果,并基于它们创建漫画.该工具包包括各种各样的模板,可以很容易地将照片放置在工作表上,还有大量各种形状的标注.除了拼贴画上的标注之外,您还可以添加带 ...
- 【ACM算法竞赛日常训练】DAY5题解与分析【储物点的距离】【糖糖别胡说,我真的不是签到题目】| 前缀和 | 思维
DAY5共2题: 储物点的距离(前缀和) 糖糖别胡说,我真的不是签到题目(multiset,思维) 作者:Eriktse 简介:19岁,211计算机在读,现役ACM银牌选手力争以通俗易懂的方式讲解算法 ...