题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5673

题意:

有一个机器人位于坐标原点上。每秒钟机器人都可以向右移到一个单位距离,或者在原地不动。如果机器人的当前位置在原点右侧,它同样可以向左移动单位距离。一系列的移动(左移,右移,原地不动)定义为一个路径。问有多少种不同的路径,使得n秒后机器人仍然位于坐标原点?答案可能很大,只需输出答案对1,000,000,007取模。

分析:

最终回到原点说明向左和向右走的步数相同,假设一共走了i步,那么左右各走了i/2步,剩下n−i步则原地等待。

因为只有在原点右边的时候才能向左走,也就是说在走的过程中右边的步数必须大于等于左边的步数,这样我们只需要对于不同的i(i<=n/2)求出其卡特兰数即可。

代码:

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll ;
const int maxn = 1000000 + 5, mod = 1e9 + 7;
ll f[maxn], rf[maxn], inv[maxn];
inline ll C(int n, int m)
{
if(m < 0||n < m)return 0;
return f[n] * rf[m] % mod * rf[n - m] % mod;
}
void init()
{
f[0] = f[1] = rf[0] = rf[1] = inv[0] = inv[1] = 1;
for(int i = 2; i < maxn; i++){
f[i] = f[i - 1] * i % mod;
inv[i] = inv[mod % i] *(mod - mod / i) % mod;
rf[i] = rf[i - 1] * inv[i] % mod;
}
}
int main (void)
{
int T;scanf("%d", &T);
init();
while(T--){
int n;
scanf("%d", &n);
ll ans = 0;
for(int i = 0; i <= n / 2; i++){
ans += (C(2 * i, i) - C(2 * i, i - 1) + mod )* C(n, 2 * i) % mod;
ans %= mod;
}
printf("%I64d\n", ans % mod);
}
return 0;
}

HDU 5673 Robot【卡特兰数】的更多相关文章

  1. hdu 5673 Robot 卡特兰数+逆元

    Robot Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  2. HDU 5673 Robot ——(卡特兰数)

    先推荐一个关于卡特兰数的博客:http://blog.csdn.net/hackbuteer1/article/details/7450250. 卡特兰数一个应用就是,卡特兰数的第n项表示,现在进栈和 ...

  3. hdu5673 Robot 卡特兰数+组合数学+线性筛逆元

    Robot Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  4. hdu5673 Robot 卡特兰数 / 默慈金数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5673 分析: 这道题是一道裸的默慈金数,比较容易想到的是用卡特兰数来做.不了解的可以先学习一下. 卡特 ...

  5. hdu 4828 Grids 卡特兰数+逆元

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem D ...

  6. hdu 5184 类卡特兰数+逆元

    BC # 32 1003 题意:定义了括号的合法排列方式,给出一个排列的前一段,问能组成多少种合法的排列. 这道题和鹏神研究卡特兰数的推导和在这题中的结论式的推导: 首先就是如何理解从题意演变到卡特兰 ...

  7. hdu 5184(数学-卡特兰数)

    Brackets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  8. HDU 5673 Robot 数学

    Robot 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 Description There is a robot on the origi ...

  9. hdu 4828 Grids(拓展欧几里得+卡特兰数)

    题目链接:hdu 4828 Grids 题目大意:略. 解题思路:将上一行看成是入栈,下一行看成是出栈,那么执着的方案就是卡特兰数,用递推的方式求解. #include <cstdio> ...

随机推荐

  1. lc287 Game of Live

    lc287 Game of Live 难点在于不能使用额外的空间. 观察到数组中每一个元素要么为1要么为0,32位int只用了一位,可以利用bit操作,将第二次state存储到int变量的倒数第二位中 ...

  2. Eclipse:Eclipse插件开发全套教程

    分享是美德,作者为Eclipse核心工程师之一,全英文版,有不明白的地方欢迎探讨和咨询. http://www.vogella.com/tutorials/eclipse.html

  3. BigDecimal的四则运算及小数位数格式

    一.加法 BigDecimal b1 = new BigDecimal("20");BigDecimal b2 = new BigDecimal("30");B ...

  4. TZ_11_Spring-Boot的属性注入方式(jdbc为例)

    1.以上一篇文档为基础 2.创建jdbc外部属性文件 application.properties此名字为默认文件名在使用是不需要使用 @Propertysource("classpath: ...

  5. SSM-5zookeeper在LINUX上自启

    把zookeeper做成服务 1.进入到/etc/rc.d/init.d目录下,新建一个zookeeper脚本 [root@zookeeper ~]# cd /etc/rc.d/init.d/ [ro ...

  6. R330 打印机连供墨水红灯常量处理

    墨水灯红灯常量,表示墨盒没墨水 1.按红灯,将墨盒移动到右侧空处 2.按住连供顶部的重置小按钮 15秒以上,复位(这个应该是让连供墨盒产生一个另外的墨盒序号,骗打印机换了个新墨盒) 3.按打印机红灯, ...

  7. Eclipse 遇到的问题和快捷键记录

    一.the user operation is waiting: 选择菜单栏的"Project",然后把菜单栏中"Build Automatically"前面的 ...

  8. linux一些配置的记录

    ssh 登录设置主机的别名 这样不用写那么多东西了 在/etc/ssh/ 下的ssh_config 和 sshd_config 两个文件 前一个是配置客户端的配置文件 另一个是服务器端的配置文件 主要 ...

  9. 2018-8-10-如何移动-nuget-缓存文件夹

    title author date CreateTime categories 如何移动 nuget 缓存文件夹 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...

  10. Java IO:为什么InputStream只能读一次

    http://zhangbo-peipei-163-com.iteye.com/blog/2021879 InputStream的接口规范就是这么设计的. /** * Reads the next b ...