HDU 1023(卡特兰数 数学)】的更多相关文章

题意是求一列连续升序的数经过一个栈之后能变成的不同顺序的数目. 开始时依然摸不着头脑,借鉴了别人的博客之后,才知道这是卡特兰数,卡特兰数的计算公式是:a( n )  =  ( ( 4*n-2 ) / ( n+1 ) * a( n-1 ) ): 用一个二维数组,a[ i ][ 0 ] 表示第 i 个卡特兰数的位数,a[ i ][ j ] ( j != 0) 中存第 i 个卡特兰数从低位到高位的第 j 个数,也就是说数是倒过来存的,输出时要倒着输出. 代码如下: #include<bits/stdc…
Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasi…
Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.   Input The input contains…
Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5372    Accepted Submission(s): 2911 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Stati…
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…
How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3382    Accepted Submission(s): 1960 Problem Description A binary search tree is a binary tree with root k such that any node v re…
链接:HDU 1023 /**************************************** * author : Grant Yuan * time : 2014/10/19 15:51 * source : HDU 1023 * algorithm : Catalan数+高精度 * ***************************************/ import java.io.*; import java.math.*; import java.util.*;…
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…
题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展gcd, 不是用逆元吗.. 网上还有别人的解释,没看懂,贴一下: (a / b) % m = ( a % (m*b)) / b 笔者注:鉴于ACM题目特别喜欢M=1000000007,为质数: 当gcd(b,m) = 1, 有性质: (a/b)%m = (a*b^-1)%m, 其中b^-1是b模m的逆…
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next few days. As a crazy fan of Harry Potter, you will go to the cinema and have the first sight, won’t you? Suppose the cinema only has one ticket-office and…