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. 1 2
output
  1. 2
input
  1. 2 2
output
  1. 8
input
  1. 24 20
output
  1. 35996330

思路:

所以本题直接是变种括号序列问题,可以直接套公式,注意除法取模等同于乘以分母的乘法逆元取模。

代码实现:

  1. #include<iostream>
  2. using namespace std;
  3. #define int long long
  4. const int p=1e9+7;
  5. int quick(int a,int b,int p){
  6. int res=1;
  7. while(b){
  8. if(b&1)res=res*a%p;
  9. a=a*a%p;
  10. b>>=1;
  11. }
  12. return res;
  13. }
  14. int c(int a,int b,int p){
  15. if(a<b)return 0;
  16. int res=1;
  17. for(int i=1,j=a;i<=b;i++,j--){
  18. res=res*j%p;
  19. res=res*quick(i,p-2,p)%p;
  20. }
  21. return res;
  22. }
  23. int lucus(int a,int b,int p){
  24. if(a<p&&b<p)return c(a,b,p);
  25. else return c(a%p,b%p,p)*lucus(a/p,b/p,p)%p;
  26. }
  27. signed main(){
  28. int a,b;
  29. cin>>a>>b;
  30. int res=lucus(2*a,a,p)%p;
  31. res=res*quick(a+1,p-2,p)%p;
  32. for(int i=0;i<a;i++)res=(res*b)%p;
  33. cout<<res<<endl;
  34. return 0;
  35. }

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笔记--通过MediaStore查询图片

    相关描述 已经完成发送彩信功能之后,就来继续向前走一步,来到MediaStore查询图片界面啦! 具体步骤实现 1.简简单单地一个界面 <?xml version="1.0" ...

  2. Linux & 标准C语言学习 <DAY7>

    一.windows下获取方向键:     1.导入#include <conio.h>     2.通过getch()获取键盘上的键值         上:72  下:80  左:75   ...

  3. 如果您喜欢我的博客可以进行RSS订阅

    如果喜欢我的博客,你也可以订阅我的博客 http://www.cnblogs.com/yhm138/rss 有时间的话我会写一篇菜鸟玩转RSS的介绍,或者你看这篇推送 2020-12-15填坑 我理解 ...

  4. 使用cmd命令行安装 windows系统

    条件:Microsoft WindowsPE 或其他第三方 WindowsPE 1. 使用 diskpart 分区: list disk:列出所有磁盘 select disk  编号:选择某块磁盘 c ...

  5. Linux耳机音响独立输出

    记得之前在用Ubuntu系统的时候,耳机和音响(线缆输出和模拟耳机输出)只能单独插一个,如果两个设备同时插入主机,将会导致只能耳机输出,即使选择后置音响(线缆输出)也不会有声音.在Windows下,即 ...

  6. NLP 开源形近字算法之相似字列表(番外篇)

    创作目的 国内对于文本的相似度计算,开源的工具是比较丰富的. 但是对于两个汉字之间的相似度计算,国内基本一片空白.国内的参考的资料少的可怜,国外相关文档也是如此. 本项目旨在抛砖引玉,实现一个基本的相 ...

  7. KMP算法的研究

    前脚学后脚忘,是时候给自己通俗易懂的总结一下了 KMP是什么 在计算机科学中,Knuth-Morris-Pratt字符串查找算法(简称为KMP算法)可在一个字符串S内查找一个词W的出现位置.一个词在不 ...

  8. 修复kube-proxy证书权限过大问题

    修复kube-proxy证书权限过大问题 之前kube-proxy服务都是用admin集群证书,造成权限过大不安全,后续该问题,将在文档中修复 请关注 https://github.com/cby-c ...

  9. pandas之时间操作

    顾名思义,时间序列(time series),就是由时间构成的序列,它指的是在一定时间内按照时间顺序测量的某个变量的取值序列,比如一天内的温度会随时间而发生变化,或者股票的价格会随着时间不断的波动,这 ...

  10. mysql迁移:ibd表空间迁移库表

    问题描述:将一个库中的表迁移到另一个数据库或实例下,利用ibd文件物理迁移,可适用情况为数据库起不来,强制迁移数据文件恢复 将数据库中的zabbix数据迁移到另一个库中 frm:存储表的列信息 ibd ...