TOJ 1179.Game of Connections(大数模板)

题目链接:http://acm.tju.edu.cn/toj/showp1179.html


1179.    Game of Connections
Time Limit: 2.0 Seconds    Memory Limit: 65536K
Total Runs: 467    Accepted Runs: 205



This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.

It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?


Input

Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 ≤ n ≤ 100.


Output

For each n, print in a single line the number of ways to connect the 2n numbers into pairs.


Sample Input

2
3
-1


Sample Output

2
5



Source: China 2004 - Shanghai Preliminary
Submit   List    Runs   Forum   Statistics

以下就是转载的大数模板:


Catalan数

  中文:卡特兰数

  原理:

  令h(1)=1,h(0)=1,catalan数满足递归式:

  h(n)= h(1)*h(n-1) + h(2)*h(n-2) + ... + h(n-1)h(1) (其中n>=2)

  另类递归式:

  h(n)=((4*n-2)/(n+1))*h(n-1);

  该递推关系的解为:

  h(n+1)=C(2n,n)/(n+1) (n=1,2,3,...)

  我并不关心其解是怎么求出来的,我只想知道怎么用catalan数分析问题。

  我总结了一下,最典型的四类应用:(实质上却都一样,无非是递归等式的应用,就看你能不能分解问题写出递归式了)

  1.括号化问题。

  矩阵链乘: P=a1×a2×a3×……×an,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,试问有几种括号化的方案?(h(n)种)

  2.出栈次序问题。

  一个栈(无穷大)的进栈序列为1,2,3,..n,有多少个不同的出栈序列?

  类似:有2n个人排成一行进入剧场。入场费5元。其中只有n个人有一张5元钞票,另外n人只有10元钞票,剧院无其它钞票,问有多少中方法使得只要有10元的人买票,售票处就有5元的钞票找零?(将持5元者到达视作将5元入栈,持10元者到达视作使栈中某5元出栈)

  3.将多边行划分为三角形问题。

  将一个凸多边形区域分成三角形区域的方法数?

  类似:一位大城市的律师在她住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她

  从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路?

  类似:在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数?

  4.给顶节点组成二叉树的问题。

  给定N个节点,能构成多少种不同的二叉树?

  (能构成h(N)个)

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<cstring>  
  3. #include<cstdio>  
  4. #include<iomanip>  
  5. #include<algorithm>  
  6. using namespace std;  
  7.   
  8. #define MAXN 9999  
  9. #define MAXSIZE 10  
  10. #define DLEN 4  
  11.   
  12. class BigNum  
  13. {  
  14. private:  
  15.     int a[500];    //可以控制大数的位数  
  16.     int len;       //大数长度  
  17. public:  
  18.     BigNum(){ len = 1;memset(a,0,sizeof(a)); }   //构造函数  
  19.     BigNum(const int);       //将一个int类型的变量转化为大数  
  20.     BigNum(const char*);     //将一个字符串类型的变量转化为大数  
  21.     BigNum(const BigNum &);  //拷贝构造函数  
  22.     BigNum &operator=(const BigNum &);   //重载赋值运算符,大数之间进行赋值运算  
  23.   
  24.     friend istream& operator>>(istream&,  BigNum&);   //重载输入运算符  
  25.     friend ostream& operator<<(ostream&,  BigNum&);   //重载输出运算符  
  26.   
  27.     BigNum operator+(const BigNum &) const;   //重载加法运算符,两个大数之间的相加运算  
  28.     BigNum operator-(const BigNum &) const;   //重载减法运算符,两个大数之间的相减运算  
  29.     BigNum operator*(const BigNum &) const;   //重载乘法运算符,两个大数之间的相乘运算  
  30.     BigNum operator/(const int   &) const;    //重载除法运算符,大数对一个整数进行相除运算  
  31.   
  32.     BigNum operator^(const int  &) const;    //大数的n次方运算  
  33.     int    operator%(const int  &) const;    //大数对一个int类型的变量进行取模运算  
  34.     bool   operator>(const BigNum & T)const;   //大数和另一个大数的大小比较  
  35.     bool   operator>(const int & t)const;      //大数和一个int类型的变量的大小比较  
  36.   
  37.     void print();       //输出大数  
  38. };  
  39. BigNum::BigNum(const int b)     //将一个int类型的变量转化为大数  
  40. {  
  41.     int c,d = b;  
  42.     len = 0;  
  43.     memset(a,0,sizeof(a));  
  44.     while(d > MAXN)  
  45.     {  
  46.         c = d - (d / (MAXN + 1)) * (MAXN + 1);  
  47.         d = d / (MAXN + 1);  
  48.         a[len++] = c;  
  49.     }  
  50.     a[len++] = d;  
  51. }  
  52. BigNum::BigNum(const char*s)     //将一个字符串类型的变量转化为大数  
  53. {  
  54.     int t,k,index,l,i;  
  55.     memset(a,0,sizeof(a));  
  56.     l=strlen(s);  
  57.     len=l/DLEN;  
  58.     if(l%DLEN)  
  59.         len++;  
  60.     index=0;  
  61.     for(i=l-1;i>=0;i-=DLEN)  
  62.     {  
  63.         t=0;  
  64.         k=i-DLEN+1;  
  65.         if(k<0)  
  66.             k=0;  
  67.         for(int j=k;j<=i;j++)  
  68.             t=t*10+s[j]-'0';  
  69.         a[index++]=t;  
  70.     }  
  71. }  
  72. BigNum::BigNum(const BigNum & T) : len(T.len)  //拷贝构造函数  
  73. {  
  74.     int i;  
  75.     memset(a,0,sizeof(a));  
  76.     for(i = 0 ; i < len ; i++)  
  77.         a[i] = T.a[i];  
  78. }  
  79. BigNum & BigNum::operator=(const BigNum & n)   //重载赋值运算符,大数之间进行赋值运算  
  80. {  
  81.     int i;  
  82.     len = n.len;  
  83.     memset(a,0,sizeof(a));  
  84.     for(i = 0 ; i < len ; i++)  
  85.         a[i] = n.a[i];  
  86.     return *this;  
  87. }  
  88. istream& operator>>(istream & in,  BigNum & b)   //重载输入运算符  
  89. {  
  90.     char ch[MAXSIZE*4];  
  91.     int i = -1;  
  92.     in>>ch;  
  93.     int l=strlen(ch);  
  94.     int count=0,sum=0;  
  95.     for(i=l-1;i>=0;)  
  96.     {  
  97.         sum = 0;  
  98.         int t=1;  
  99.         for(int j=0;j<4&&i>=0;j++,i--,t*=10)  
  100.         {  
  101.             sum+=(ch[i]-'0')*t;  
  102.         }  
  103.         b.a[count]=sum;  
  104.         count++;  
  105.     }  
  106.     b.len =count++;  
  107.     return in;  
  108.   
  109. }  
  110. ostream& operator<<(ostream& out,  BigNum& b)   //重载输出运算符  
  111. {  
  112.     int i;  
  113.     cout << b.a[b.len - 1];  
  114.     for(i = b.len - 2 ; i >= 0 ; i--)  
  115.     {  
  116.         cout.width(DLEN);  
  117.         cout.fill('0');  
  118.         cout << b.a[i];  
  119.     }  
  120.     return out;  
  121. }  
  122.   
  123. BigNum BigNum::operator+(const BigNum & T) const   //两个大数之间的相加运算  
  124. {  
  125.     BigNum t(*this);  
  126.     int i,big;      //位数  
  127.     big = T.len > len ? T.len : len;  
  128.     for(i = 0 ; i < big ; i++)  
  129.     {  
  130.         t.a[i] +=T.a[i];  
  131.         if(t.a[i] > MAXN)  
  132.         {  
  133.             t.a[i + 1]++;  
  134.             t.a[i] -=MAXN+1;  
  135.         }  
  136.     }  
  137.     if(t.a[big] != 0)  
  138.         t.len = big + 1;  
  139.     else  
  140.         t.len = big;  
  141.     return t;  
  142. }  
  143. BigNum BigNum::operator-(const BigNum & T) const   //两个大数之间的相减运算  
  144. {  
  145.     int i,j,big;  
  146.     bool flag;  
  147.     BigNum t1,t2;  
  148.     if(*this>T)  
  149.     {  
  150.         t1=*this;  
  151.         t2=T;  
  152.         flag=0;  
  153.     }  
  154.     else  
  155.     {  
  156.         t1=T;  
  157.         t2=*this;  
  158.         flag=1;  
  159.     }  
  160.     big=t1.len;  
  161.     for(i = 0 ; i < big ; i++)  
  162.     {  
  163.         if(t1.a[i] < t2.a[i])  
  164.         {  
  165.             j = i + 1;  
  166.             while(t1.a[j] == 0)  
  167.                 j++;  
  168.             t1.a[j--]--;  
  169.             while(j > i)  
  170.                 t1.a[j--] += MAXN;  
  171.             t1.a[i] += MAXN + 1 - t2.a[i];  
  172.         }  
  173.         else  
  174.             t1.a[i] -= t2.a[i];  
  175.     }  
  176.     t1.len = big;  
  177.     while(t1.a[len - 1] == 0 && t1.len > 1)  
  178.     {  
  179.         t1.len--;  
  180.         big--;  
  181.     }  
  182.     if(flag)  
  183.         t1.a[big-1]=0-t1.a[big-1];  
  184.     return t1;  
  185. }  
  186.   
  187. BigNum BigNum::operator*(const BigNum & T) const   //两个大数之间的相乘运算  
  188. {  
  189.     BigNum ret;  
  190.     int i,j,up;  
  191.     int temp,temp1;  
  192.     for(i = 0 ; i < len ; i++)  
  193.     {  
  194.         up = 0;  
  195.         for(j = 0 ; j < T.len ; j++)  
  196.         {  
  197.             temp = a[i] * T.a[j] + ret.a[i + j] + up;  
  198.             if(temp > MAXN)  
  199.             {  
  200.                 temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);  
  201.                 up = temp / (MAXN + 1);  
  202.                 ret.a[i + j] = temp1;  
  203.             }  
  204.             else  
  205.             {  
  206.                 up = 0;  
  207.                 ret.a[i + j] = temp;  
  208.             }  
  209.         }  
  210.         if(up != 0)  
  211.             ret.a[i + j] = up;  
  212.     }  
  213.     ret.len = i + j;  
  214.     while(ret.a[ret.len - 1] == 0 && ret.len > 1)  
  215.         ret.len--;  
  216.     return ret;  
  217. }  
  218. BigNum BigNum::operator/(const int & b) const   //大数对一个整数进行相除运算  
  219. {  
  220.     BigNum ret;  
  221.     int i,down = 0;  
  222.     for(i = len - 1 ; i >= 0 ; i--)  
  223.     {  
  224.         ret.a[i] = (a[i] + down * (MAXN + 1)) / b;  
  225.         down = a[i] + down * (MAXN + 1) - ret.a[i] * b;  
  226.     }  
  227.     ret.len = len;  
  228.     while(ret.a[ret.len - 1] == 0 && ret.len > 1)  
  229.         ret.len--;  
  230.     return ret;  
  231. }  
  232. int BigNum::operator %(const int & b) const    //大数对一个int类型的变量进行取模运算  
  233. {  
  234.     int i,d=0;  
  235.     for (i = len-1; i>=0; i--)  
  236.     {  
  237.         d = ((d * (MAXN+1))% b + a[i])% b;  
  238.     }  
  239.     return d;  
  240. }  
  241. BigNum BigNum::operator^(const int & n) const    //大数的n次方运算  
  242. {  
  243.     BigNum t,ret(1);  
  244.     int i;  
  245.     if(n<0)  
  246.         exit(-1);  
  247.     if(n==0)  
  248.         return 1;  
  249.     if(n==1)  
  250.         return *this;  
  251.     int m=n;  
  252.     while(m>1)  
  253.     {  
  254.         t=*this;  
  255.         for( i=1;i<<1<=m;i<<=1)  
  256.         {  
  257.             t=t*t;  
  258.         }  
  259.         m-=i;  
  260.         ret=ret*t;  
  261.         if(m==1)  
  262.             ret=ret*(*this);  
  263.     }  
  264.     return ret;  
  265. }  
  266. bool BigNum::operator>(const BigNum & T) const   //大数和另一个大数的大小比较  
  267. {  
  268.     int ln;  
  269.     if(len > T.len)  
  270.         return true;  
  271.     else if(len == T.len)  
  272.     {  
  273.         ln = len - 1;  
  274.         while(a[ln] == T.a[ln] && ln >= 0)  
  275.             ln--;  
  276.         if(ln >= 0 && a[ln] > T.a[ln])  
  277.             return true;  
  278.         else  
  279.             return false;  
  280.     }  
  281.     else  
  282.         return false;  
  283. }  
  284. bool BigNum::operator >(const int & t) const    //大数和一个int类型的变量的大小比较  
  285. {  
  286.     BigNum b(t);  
  287.     return *this>b;  
  288. }  
  289.   
  290. void BigNum::print()    //输出大数  
  291. {  
  292.     int i;  
  293.     cout << a[len - 1];  
  294.     for(i = len - 2 ; i >= 0 ; i--)  
  295.     {  
  296.         cout.width(DLEN);  
  297.         cout.fill('0');  
  298.         cout << a[i];  
  299.     }  
  300.     cout << endl;  
  301. }  
  302. int main(void)  
  303. {  
  304.     int i,n;  
  305.     BigNum x[101];      //定义大数的对象数组  
  306.     x[0]=1;  
  307.     for(i=1;i<101;i++)  
  308.         x[i]=x[i-1]*(4*i-2)/(i+1);  
  309.     while(scanf("%d",&n)==1 && n!=-1)  
  310.     {  
  311.         x[n].print();  
  312.     }  
  313. }  

Java

[java]  view plain  copy
  1. /* 
  2.  * 题意 给定2*N各点 线段连接成点对 线段不想交 问有多少种方案 
  3.  * 解题 利用卡特兰数 
  4.  * 前几项为 (OEIS中的数列A000108): 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 
  5.  *  2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 
  6.  *   343059613650, 1289904147324, 
  7.  *  公式 h(n)=((4*n-2)/(n+1))*h(n-1); h(1)=1,h(0)=1&&n>=2 
  8.  */  
  9. import java.util.*;  
  10. import java.math.*;  
  11. public class Main{  
  12.     public static void main(String[] args)   
  13.     {  
  14.         int n;  
  15.         Scanner in=new Scanner(System.in);  
  16.         BigInteger one=BigInteger.ONE;  
  17.         BigInteger four=new BigInteger("4");  
  18.         BigInteger two=new BigInteger("2");  
  19.         BigInteger st=null;  
  20.         BigInteger t=null;  
  21.         while(in.hasNextInt())  
  22.         {  
  23.             n=in.nextInt();  
  24.             if(n==-1)break;  
  25.             t=BigInteger.ONE;  
  26.             for(int i=2;i<=n;i++)  
  27.             {  
  28.                 st=new BigInteger(""+i);  
  29.                 /*h(n)=((4*n-2)/(n+1))*h(n-1); n>=2&&h(1)=1,h(0)=1*/  
  30.                 t=t.multiply(st.multiply(four).subtract(two)).divide(st.add(one));  
  31.             }  
  32.             System.out.println(t);  
  33.         }  
  34.     }  
  35. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值