POJ 2084 Game of Connections(卡特兰数)
卡特兰数源于组合数学,ACM中比较具体的使用例子有,1括号匹配的种数。2在栈中的自然数出栈的种数。3求多边形内三角形的个数。4,n个数围城圆圈,找不相交线段的个数。5给定n个数,求组成二叉树的种数……
此题就是第4个样例,是裸卡特兰数,但是这里牵扯的大数,可以使用java的大数类解决,但是我这里使用高精度乘法和除法模拟的(主要是java不会)。
此处的递推式为H【1】 = 1;H【n】 = H【n-1】*(4*n-2)/(n+1){n>=2};代码如下:
需要注意输出的形式,我这里的进制是1000,还可以更大,根据需求吧。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define jz 10000
int a[][];
void chengfa(int k,int num){
int jw = ;
for(int i = ;i >= ;i--){
jw += a[k-][i] * num;
a[k][i] = jw % jz;
jw /= jz;
}
}
void chufa(int k,int num){
int div = ;
for(int i = ;i <= ;i++){
div = div*jz + a[k][i];
a[k][i] = div / num;
div %= num;
}
}
void init(){
memset(a,,sizeof(a));
a[][] = ;
int num;
for(int i = ;i <= ;i++){
num = (*i-);
chengfa(i,num);
num = i+;
chufa(i,num);
}
}
int main()
{
init();
int n;
while(~scanf("%d",&n)){
if(n==-) break;
int pos = -;
for(int i = ;i <= ;i++){
if(a[n][i]){
pos = i;
break;
}
}
printf("%d",a[n][pos]);
for(int i = pos+;i <= ;i++){
printf("%04d",a[n][i]);
}
puts("");
}
return ;
}
POJ 2084 Game of Connections(卡特兰数)的更多相关文章
- POJ 2084 Game of Connections 卡特兰数
看了下大牛们的,原来这题是卡特兰数,顺便练练java.递归式子:h(0)=1,h(1)=1 h(n)= h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) ( ...
- (组合数学3.1.2.2)POJ 2084 Game of Connections(卡特兰数公示的实现)
package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class POJ_2084 ...
- POJ2084 Game of Connections 卡特兰数 关于卡特兰数经典的几个问题
Game of Connections Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9128 Accepted: 44 ...
- POJ 2084 Game of Connections
卡特兰数. #include<stdio.h> #include<string.h> ; ; void mul(__int64 a[],int len,int b) { int ...
- POJ 2084 Catalan数+高精度
POJ 2084 /**************************************** * author : Grant Yuan * time : 2014/10/19 15:42 * ...
- 组合数学的卡特兰数 TOJ 3551: Game of Connections
这个就是卡特兰数的经典问题 直接用这个公式就好了,但是这个题涉及大数的处理h(n)=h(n-1)*(4*n-2)/(n+1) 其实见过好几次大数的处理了,有一次他存的恰好不多于30位,直接分成两部分l ...
- poj——2084 Game of Connections
Game of Connections Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8664 Accepted: 42 ...
- HDU 1134 Game of Connections(卡特兰数+大数模板)
题目代号:HDU 1134 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134 Game of Connections Time Limit: 20 ...
- poj 1095 Trees Made to Order 卡特兰数
这题用到了卡特兰数,详情见:http://www.cnblogs.com/jackge/archive/2013/05/19/3086519.html 解体思路详见:http://blog.csdn. ...
随机推荐
- C库 - 常用文件IO函数
#include<stdio.h> 0. 文件打开关闭FILE *fp = fopen("C:\\a.dat","wb+");fclose(fp); ...
- sqlite 学习
到谷歌上搜sqlite,第一项便是官方网站:www.sqlite.org.进去后,先了解一下大体,感觉还不错. 进入Document页面,大标题SQLite Programming Interface ...
- for、while循环的洪荒之力
在python里,如果说print语句是用得最多的话,那么,要我说,除了for语句,谁都不敢认老二. 下面,让我们来看看for语句能搞出什么花样 1. 计时(无限次数) 说到计时,我们先来试下显示当前 ...
- 2015 Multi-University Training Contest 7
1001 Game On the Tree 1002 Tree Maker 1003 Hotaru's problem Manacher处理好p数组. 暴力举一下公共串即可. # include &l ...
- “psql: could not connect to server: Connection refused” Error when connecting to remote database
问题: I am trying to connect to a postgres database installed in a remote server using the following c ...
- css sprite的使用心得
提高网页显示速度最有效的一个方法是减少页面的HTTP请求次数,为了减少HTTP请求次数,最直接有效的方法是使用精灵图片(CSS sprites),精灵图片是把许多图片放到一张大图片里面,通过CSS来显 ...
- 如何使用kaptcha验证码组件
kaptcha是基于SimpleCaptcha的验证码开源项目. kaptcha是纯配置的,使用起来比较友好.如使用了Servlet,所有配置都在web.xml中.如果你在项目中使用了开源框架(比如S ...
- ios UIApplocation 中APP启动方式
iOS app启动的方式有哪些: 自己启动(用户手动点击启动) urlscheme启动(关于urlScheme的详解)http://www.cnblogs.com/sunfuyou/p/6183064 ...
- python2.7学习记录之三
1.连接数据库 MySQLdb的是一个接口连接到MySQL数据库服务器从Python.它实现了Python数据库API V2.0,并建上的MySQL C API的顶端. 下载地址:https://so ...
- spring管理事务需要注意的
org.springframework.transaction.NoTransactionException: No transaction aspect-managed TransactionSta ...