HDU 1023 Traning Problem (2) 高精度卡特兰数
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
Sample Output
Hint
The result will be very large, so you may not process it by 32-bit integers.
Source
求高精度的卡特兰数。
1.java代码,套公式就可以了。
import java.io.*;
import java.util.*;
import java.math.BigInteger; public class Main
{
public static void main(String args[])
{
BigInteger[] a = new BigInteger[101];
a[0] = BigInteger.ZERO;
a[1] = BigInteger.valueOf(1);
for(int i = 2; i <= 100; ++i)
a[i] = a[i - 1].multiply(BigInteger.valueOf(4 * i - 2)).divide(BigInteger.valueOf(i+1));
Scanner in = new Scanner(System.in);
int n;
while(in.hasNext())
{
n = in.nextInt();
System.out.println(a[n]);
}
}
}
2.C++代码,kuangbin模板
//h( n ) = ( ( 4*n-2 )/( n+1 )*h( n-1 ) ); #include<stdio.h> //*******************************
//打表卡特兰数
//第 n个 卡特兰数存在a[n]中,a[n][0]表示长度;
//注意数是倒着存的,个位是 a[n][1] 输出时注意倒过来。
//*********************************
int a[][];
void ktl()
{
int i,j,yu,len;
a[][]=;
a[][]=;
a[][]=;
a[][]=;
len=;
for(i=;i<;i++)
{
yu=;
for(j=;j<=len;j++)
{
int t=(a[i-][j])*(*i-)+yu;
yu=t/;
a[i][j]=t%;
}
while(yu)
{
a[i][++len]=yu%;
yu/=;
}
for(j=len;j>=;j--)
{
int t=a[i][j]+yu*;
a[i][j]=t/(i+);
yu = t%(i+);
}
while(!a[i][len])
{
len--;
}
a[i][]=len;
} }
int main()
{
ktl();
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=a[n][];i>;i--)
{
printf("%d",a[n][i]);
}
puts("");
}
return ;
}
3.C++代码
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std; int a[][]; //大数卡特兰数
int b[]; //卡特兰数的长度 void catalan() //求卡特兰数
{
int i, j, len, carry, temp;
a[][] = b[] = ;
len = ;
for(i = ; i <= ; i++)
{
for(j = ; j < len; j++) //乘法
a[i][j] = a[i-][j]*(*(i-)+);
carry = ;
for(j = ; j < len; j++) //处理相乘结果
{
temp = a[i][j] + carry;
a[i][j] = temp % ;
carry = temp / ;
}
while(carry) //进位处理
{
a[i][len++] = carry % ;
carry /= ;
}
carry = ;
for(j = len-; j >= ; j--) //除法
{
temp = carry* + a[i][j];
a[i][j] = temp/(i+);
carry = temp%(i+);
}
while(!a[i][len-]) //高位零处理
len --;
b[i] = len;
}
} int main()
{
int i, n;
catalan();
while(scanf("%d", &n) != EOF)
{
for(i = b[n]-; i>=; i--)
{
printf("%d", a[n][i]);
}
printf("\n");
} return ;
}
HDU 1023 Traning Problem (2) 高精度卡特兰数的更多相关文章
- 1023 Train Problem II(卡特兰数)
Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...
- HDU 1023 Train Problem II (大数卡特兰数)
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1023 Train Problem II (卡特兰数,经典)
题意: 给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列. 思路: 卡特兰数的经典例子.n<101,用递推式解决.需要使用到大数.n=100时大概有200位以下. #inclu ...
- HDU 1023 Train Problem II( 大数卡特兰 )
链接:传送门 题意:裸卡特兰数,但是必须用大数做 balabala:上交高精度模板题,增加一下熟悉度 /************************************************ ...
- Train Problem II(卡特兰数 组合数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- 【HDU 5370】 Tree Maker(卡特兰数+dp)
Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting gam ...
- HDU 1134 Game of Connections(卡特兰数+大数模板)
题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 20 ...
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
随机推荐
- shopex商城的部署和安装
1.在网站上下载最新的压缩包: 2.shopex是商业软件,不开源,且源代码是加密的! 3.如果你出现zend的错误,是因为你的php环境没有安装此插件,推荐使用phpstudy最新版本,我使用的ph ...
- How much training data do you need?
How much training data do you need? //@樵夫上校: 0. 经验上,10X规则(训练数据是模型参数量的10倍)适用与大多数模型,包括shallow networ ...
- Batman崛起之地——Gotham
刚看完FOX美剧<Gotham>第一集,很喜欢这种充满黑暗元素的影视剧,虽然是电视剧,却有电影的紧凑感和叙事分格. Gotham虚构了一个日渐堕落的美国城市,它充斥暴力.性等各种犯罪,普通 ...
- XSS Filter Evasion Cheat Sheet 中文版
前言 译者注: 翻译本文的最初原因是当我自己看到这篇文章后,觉得它是非常有价值.但是这么著名的一个备忘录却一直没有人把它翻译成中文版.很多人仅仅是简单的把文中的 各种代码复制下来,然后看起来很刁的发在 ...
- Android文件保存和读取
public class DataActivity extends Activity { private EditText filenameText; private EditText content ...
- IMAP(Internet Mail Access Protocol,Internet邮件访问协议)以前称作交互邮件访问协议(Interactive Mail Access Protocol)。
IMAP(Internet Mail Access Protocol,Internet邮件访问协议)以前称作交互邮件访问协议(Interactive Mail Access Protocol).IMA ...
- 老项目的#iPhone6与iPhone6Plus适配#Icon适配
本文永久地址为http://www.cnblogs.com/ChenYilong/p/4020373.html ,转载请注明出处. 这是Evernote印象笔记的链接:https://www ...
- 批量删除wordpress垃圾评论留言
wordpress博客的存在,垃圾评论注定会找上门来.大家还可以用Akismet.Bad Behavior.Spam Karma等一些其他的插件或者直接用程序写个验证码函数对留言进行验证来过滤 垃圾评 ...
- [Effective JavaScript 笔记]第50条:迭代方法优于循环
"懒"程序员才是好程序员.复制和粘贴样板代码,一但代码有错误,或代码功能修改,那么程序在修改的时候,程序员需要找到所有相同功能的代码一处处进行修改.这会使人重复发明轮子,而且在别人 ...
- 开机提示grub可咋办啊
导读 GRUB是多启动规范的实现,它允许用户可以在计算机内同时拥有多个操作系统,并在计算机启动时选择希望运行的操作系统.GRUB可用于选择操作系统分区上的不同内核,也可用于向这些内核传递启动参数. 1 ...