POJ 2084 Game of Connections

卡特兰数。

#include<stdio.h>
#include<string.h>
const __int64 base=1000000000;
const int lenth=100;
void mul(__int64 a[],int len,int b)
{
    int i;
    __int64 jw=0;
    for(i=len-1;i>=0;i--)
    {
        jw=jw+a[i]*b;
        a[i]=jw%base;
        jw=jw/base;
    }
}
void div(__int64 a[],int len,int b)
{
    int i;
    __int64 jw=0;
    for(i=0;i<lenth;i++)
    {
        jw=jw*base+a[i];
        a[i]=jw/b;
        jw=jw%b;
    }
}
int main()
{
      int i,j,n;
      __int64 a[101][100];
      memset(a[1],0,sizeof(a[1]));
      a[1][lenth-1]=1;
      for(i=2;i<=100;i++)
      {
          memcpy(a[i],a[i-1],sizeof(a[1]));
          mul(a[i],lenth,4*i-2);
          div(a[i],lenth,i+1);
      }
      while(scanf("%d",&n)!=EOF)
      {
          if(n==-1)  break;
          for(i=0;i<lenth&&a[n][i]==0;i++);
              printf("%I64d",a[n][i++]);
              for(;i<lenth;i++)
                 printf("%0*I64d",9,a[n][i]);
              printf("\n");
      }  

      return 0;
}  
时间: 2024-08-11 01:13:54

POJ 2084 Game of Connections的相关文章

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}:代码如下: 需要注意输出的形式,我这里的进制是100

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) (其中n>=2)   打表172MS import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new S

POJ 2084 Catalan数

[题意]: 一个环上有2*N个连续的数,求将这些数两两连接且连接的边不相交的方法数. [知识点]: 数学+Catalan数 令h(1)=1,h(0)=1 递归式1:h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2) 递归式2:h(n)=((4*n-2)/(n+1))*h(n-1); 该递推关系的解为:h(n)=C(2n,n)/(n+1) (n=1,2,3,...) [题解]: Catalan数典型的应用,2*N的答案为h(N). [

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

POJ 2084

第一题组合数学题.可以使用递推,设1与其他各数分别连边,假设N=3;若1-4,则圆分成两部分计数,此时可以利用乘法原理.(高精度) #include <cstdio> #include <cstring> #include <iostream> #include <string> using namespace std; const int maxn = 200; struct bign { int len, s[maxn]; bign() { memset

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

poj题库分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (