HDU 4828 Grids

思路:能够转化为卡特兰数,先把前n个人标为0。后n个人标为1。然后去全排列,全排列的数列。假设每一个1的前面相应的0大于等于1,那么就是满足的序列,假设把0看成入栈,1看成出栈。那么就等价于n个元素入栈出栈,求符合条件的出栈序列,这个就是卡特兰数了。

然后去递推一下解,过程中须要求逆元去计算

代码:

#include <stdio.h>
#include <string.h> const int N = 1000005;
const long long MOD = 1000000007; long long extend_gcd(long long a,long long b,long long &x,long long &y)
{
if(a == 0 && b == 0) return -1;
if(b == 0){x = 1; y = 0; return a;}
long long d = extend_gcd(b, a % b, y, x);
y -= a / b * x;
return d;
} long long mod_reverse(long long a, long long n)
{
long long x,y;
long long d = extend_gcd(a, n, x, y);
if(d == 1) return (x % n + n) % n;
else return -1;
} int t, n;
long long Catalan[N]; int main() {
Catalan[1] = Catalan[2] = 1;
for (int i = 3; i < N; i++) {
long long tmp = mod_reverse((long long) i, MOD);
Catalan[i] = Catalan[i - 1] * (4 * i - 6) % MOD * tmp % MOD;
}
int cas = 0;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
printf("Case #%d:\n", ++cas);
printf("%lld\n", Catalan[n + 1]); }
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 4828 (卡特兰数+逆)的更多相关文章

  1. HDU 4828 (卡特兰数+逆元)

    HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0,后n个人标为1.然后去全排列,全排列的数列,假设每一个1的前面相应的0大于等于1,那么就是满足的序列.假设把0看成入栈,1看 ...

  2. hdu 1130,hdu 1131(卡特兰数,大数)

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. hdu 1023 卡特兰数+高精度

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

    题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...

  5. HDU 1023(卡特兰数 数学)

    题意是求一列连续升序的数经过一个栈之后能变成的不同顺序的数目. 开始时依然摸不着头脑,借鉴了别人的博客之后,才知道这是卡特兰数,卡特兰数的计算公式是:a( n )  =  ( ( 4*n-2 ) / ...

  6. Buy the Ticket HDU 1133 卡特兰数应用+Java大数

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  7. hdoj 4828 卡特兰数取模

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

  8. Train Problem II HDU 1023 卡特兰数

    Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...

  9. hdu 1133(卡特兰数变形)

      题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1133   题意:排队买50块一张的票,初始票台没有零钱可找,有m个人持有50元,n人持有100元, ...

随机推荐

  1. [Android学习笔记]EditText的使用

    EditText就是我们最常用的文本输入框 常用属性见官方文档 主要是以下几个问题: 1.取消默认获取焦点 Activity启动时候会把焦点默认停留在第一个EditText控件上 一般的解决方法是在此 ...

  2. Android自己定义组件系列【2】——Scroller类

    在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友能够先看<自己定义View及ViewGroup> scrollTo和scrollBy尽管实 ...

  3. js使用栈来实现10进制转8进制 js取除数 余数

    function ten2eight(x){ var s=[]; var r=''; while(x>0){ s.push(x%8); x=parseInt(x/8); } while(s.le ...

  4. POJ 2002 点hash

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15489   Accepted: 5864 Descript ...

  5. 读 Working with forms 一些心得

    Working with forms About this document This document provides an introduction to Django’s form handl ...

  6. dell N5010

    Inspiron N5010Microsoft Windows 10 企业版 (64位) (英特尔)Intel(R) Core(TM) i3 CPU       M 370  @ 2.40GHz(24 ...

  7. zoj3201(树形dp)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3201 题意:给一棵树, n结点<=1000, 和K < ...

  8. C++习题 虚函数-计算图形面积

    C++习题 虚函数-计算图形面积 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 86 [cid=1143&pid=6 ...

  9. Linux C语言写的超级简单port扫描器

    这个本来曾经也写过的,今天无聊复习下 再写一遍.简单的一塌糊涂,写的不咋地大家见谅哦!有空再加强 嘿嘿! #include <stdio.h> #include <stdlib.h& ...

  10. 《Android内核剖析》读书笔记 第13章 View工作原理【View树遍历】

    View状态分类 在View视图中定义了多种和界面效果相关的状态,比如拥有焦点Focused.按下Pressed等,不同的状态一般会显示不同的界面效果,而且视图状态会随着用户的操作而改变,一般通过xm ...