HDU 5673 Robot ——(卡特兰数)
先推荐一个关于卡特兰数的博客:http://blog.csdn.net/hackbuteer1/article/details/7450250。
卡特兰数一个应用就是,卡特兰数的第n项表示,现在进栈和出栈的次数都是n次,问最后栈空的合法序列的个数。其他例子见上面这个博客。
那么关于这个题目,我们先选出i次右移的(相当于进栈)次数,i次左移的(相当于出栈)次数,那么当前对答案做出的贡献就是C(n,2*i)*cat[i],枚举所有的i计算出答案即可。
代码如下:
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
const int N = + ;
const int mod = (int)1e9 + ; int inv[N],cat[N],C[N]; void init()
{
inv[] = ;
for(int i=;i<N;i++) inv[i] = (mod-mod/i) * 1LL * inv[mod % i] % mod; cat[] = cat[] = ;
for(int i=;i<=N/;i++) cat[i] = 1LL* cat[i-] * (*i-) % mod * inv[i+] % mod;
} int main()
{
init();
int T;scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int ans = ;
C[] = ; // C(n,0) = 1;
for(int i=;i<=n;i++) C[i] = (ll)C[i-] * (n-(i-)) % mod * inv[i] % mod; // O(n)递推组合数C(n,i)
for(int i=;i<=n/;i++) ans = ((ans + (ll)C[i*] * cat[i] % mod) % mod + mod) % mod;
printf("%d\n",ans);
}
return ;
}
HDU 5673 Robot ——(卡特兰数)的更多相关文章
- hdu 5673 Robot 卡特兰数+逆元
Robot Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- HDU 5673 Robot【卡特兰数】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题意: 有一个机器人位于坐标原点上.每秒钟机器人都可以向右移到一个单位距离,或者在原地不动.如 ...
- hdu5673 Robot 卡特兰数+组合数学+线性筛逆元
Robot Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- hdu5673 Robot 卡特兰数 / 默慈金数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5673 分析: 这道题是一道裸的默慈金数,比较容易想到的是用卡特兰数来做.不了解的可以先学习一下. 卡特 ...
- hdu 4828 Grids 卡特兰数+逆元
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Problem D ...
- hdu 5184 类卡特兰数+逆元
BC # 32 1003 题意:定义了括号的合法排列方式,给出一个排列的前一段,问能组成多少种合法的排列. 这道题和鹏神研究卡特兰数的推导和在这题中的结论式的推导: 首先就是如何理解从题意演变到卡特兰 ...
- hdu 5184(数学-卡特兰数)
Brackets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5673 Robot 数学
Robot 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 Description There is a robot on the origi ...
- hdu 4828 Grids(拓展欧几里得+卡特兰数)
题目链接:hdu 4828 Grids 题目大意:略. 解题思路:将上一行看成是入栈,下一行看成是出栈,那么执着的方案就是卡特兰数,用递推的方式求解. #include <cstdio> ...
随机推荐
- [翻译]Understanding Weak References(理解弱引用)
原文 Understanding Weak References Posted by enicholas on May 4, 2006 at 5:06 PM PDT 译文 我面试的这几个人怎么这么渣啊 ...
- IIS 发布网站 ashx无法访问
IIS6 问题 1.是否安装相应的.net版本 2.查看.net版本是否一致 3.查看web 服务扩展中.net版本是否允许. 4.添加相应的MIME类型文件 在IIS中右键网站→属性→主目录→配置→ ...
- Linux下手动获取当前调用栈
被问到如何手动获取当前的调用栈,之前碰到过一时没记起来,现在回头整理一下. 其原理是:使用backtrace()从栈中获取当前调用各层函数调用的返回地址,backtrace_symbols()将对应地 ...
- YHLMR009 交货单查询
*********************************************************************** * Title : YHLMR009 * * Appli ...
- .vimrc常用
vim 的環境設定參數 :set nu:set nonu 行号 :set tabstop=4 :set softtabstop=4 :set shiftwidth=4 tab :set hlsea ...
- mysql存储过程详解(入门)
delimiter // #修改结束符号为// create procedure pro_wyx() /*创建存储过程*/ begin declare i int ; #定义变量 set i=1 ...
- 完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能
#!/bin/usr/env python#coding=utf-8'''完成一段简单的Python程序,用于实现一个简单的加减乘除计算器功能'''try: a=int(raw_input(" ...
- Durid(一): 原理架构
Durid是在2013年底开源出来的,当前最新版本0.9.2, 主要解决的是对实时数据以及较近时间的历史数据的多维查询提供高并发(多用户),低延时,高可靠性的问题.对比Druid与其他解决方案,Kyl ...
- 入口点函数的19种消息,AcRxArxApp只处理16种。
AcRx::AppMsgCode一共有19种消息. 但由IMPLEMENT_ARX_ENTRYPOINT宏实现的App类,只处理了16种消息. 缺: kSuspendMsg = 16, kIni ...
- linux下socket编程-进程间通信
一.什么是Socket Socket接口是TCP/IP网络通信的API,Socket接口定义了许多函数或例程,可以用它们来开发TCP/IP网络上的应用程序. Socket类型有两种:流式Socket ...