HDU 1023 Catalan数+高精度

链接: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.*;
public class Main{
	public static void main(String args[])
	{
		Scanner cin=new Scanner(System.in);
		BigInteger []a = new BigInteger [105];
		BigInteger t;
		a[0]=a[1]=BigInteger.ONE;
		for(int  i=2;i<=102;i++)
		{
			a[i]=a[i-1].multiply(BigInteger.valueOf(4*i-2)).divide(BigInteger.valueOf(i+1));
		}
		while(cin.hasNext()){
			int n=cin.nextInt();
			if(n==-1) break;
			String str;
			str=a[n].toString();
			System.out.println(str);
		}
	}
}
时间: 2024-10-08 12:27:41

HDU 1023 Catalan数+高精度的相关文章

C - Train Problem II——(HDU 1023 Catalan 数)

传送门 Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7616    Accepted Submission(s): 4101 Problem Description As we all know the Train Problem I, the boss of the Ignatius Train

hdu 1023 卡特兰数+高精度

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

POJ 2084 Catalan数+高精度

POJ 2084 /**************************************** * author : Grant Yuan * time : 2014/10/19 15:42 * source : POJ 2084 * algorithm: Catalan数+高精度 * ***************************************/ import java.io.*; import java.math.*; import java.util.*; publ

Train Problem II HDU 1023 卡特兰数

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 se

hdu 1023 卡特兰数

import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner in = new Scanner(System.in); BigInteger[] a = new BigInteger[105]; for(int i = 0;i < 105;i++){ a[i] = new BigInteger("1"

HDU 1023 Train Problem II 大数打表Catalan数

一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n,n) / (n+1); 递推公式 C(n ) = C(n-1)*(4*n-2) / (n+1) 高精度乘以一个整数和高精度除以一个整数的知识.这样还是使用整数数组比較好计算,假设使用string那么就不太好计算了,由于整数也可能是多位的. const int MAX_N = 101; short

HDU 4828 - Grids (Catalan数)

题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4828 Catalan数的公式为 C[n+1] = C[n] * (4 * n + 2) / (n + 2) 题目要求对M = 1e9+7 取模 利用乘法逆元将原式中除以(n+2)取模变为对(n+2)逆元的乘法取模 C[n+1] = C[n] * (4 * n + 2) * Pow(n+2, MOD-2) % MOD 其中Pow用快速幂解决 #include <cstdio> #include

HDU1023 Train Problem II【Catalan数】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1023 题目大意: 一列N节的火车以严格的顺序到一个站里.问出来的时候有多少种顺序. 解题思路: 典型的求Catalan数的题目,可是结果会非常大,所以须要用大数来解决. Catalan公式为 h(n) = h(n-1) * (4*n-2) / (n + 1),h(0) = h(1) = 1. AC代码: #include<iostream> #include<algorithm> #

UVA10303 - How Many Trees?(java大数+catalan数)

UVA10303 - How Many Trees?(java大数+catalan数) 题目链接 题目大意:给你1-N N个数,然后要求由这些数构成二叉搜索树,问有多少种这样的二叉搜索树. 解题思路:把前5项理出来,正好是1 2 5 14 42..就猜想是catalan数,结果也是对了.公式f(i + 1) = (4?i - 6)/ i; (i >= 2).结果很大,要用高精度. 代码: import java.util.*; import java.math.*; import java.io