HDU 1134 卡特蘭數 大數乘法除法

來源:互聯網
上載者:User
Problem DescriptionThis 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? 


InputEach 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. 


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


Sample Input

23-1
 


Sample Output

25
 

#include <stdio.h>#include <string.h>#include <iostream>using namespace std;int n;#define BASE 10000#define UNIT 4#define FORMAT "%04d"class BigNum{public:int a[20];int length;BigNum(const int k){ //用小於BASE的k初始化大數memset(a, 0, sizeof(a));a[0] = k;length = 1;}BigNum(){memset(a, 0, sizeof(a));length = 0;}BigNum operator * (const BigNum & B){BigNum ans;int i,j,up=0,num;for(i=0; i<length; i++){up = 0; //每次迴圈都要初始化為0for(j=0; j<B.length; j++){num = up + a[i] * B.a[j] + ans.a[i+j];up = num / BASE;num = num % BASE;//cout << num << endl;ans.a[i+j] = num;}//cout << up << endl;if(up > 0)ans.a[i+j] = up;}ans.length = i+j;while(ans.a[ans.length -1] == 0 && ans.length > 1)ans.length--;return ans;}BigNum operator /(const int & k) const{  // k < BASE, 對此題適用 BigNum ans;int down=0,i,num;for(i=length-1; i>=0; i--){num = ( (down * BASE) + a[i] ) / k;down =  ( (down * BASE) + a[i] ) % k;ans.a[i] = num;}ans.length = length;while(ans.a[ans.length-1] == 0 && ans.length > 1)ans.length -- ;return ans;}void print(){printf("%d", a[length-1]);for(int i=length-2; i>=0; i--)printf(FORMAT,a[i]);}};//f(n) = C(2n,n)/(n+1)int main(){BigNum nums[101];nums[1] = BigNum(1);nums[2] = BigNum(2);nums[3] = BigNum(5);for(int i=4; i<=100; i++){nums[i] = nums[i-1] * (4*i-2)/(i+1);}int n;while(scanf("%d", &n), n>0){nums[n].print();printf("\n");}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.