zoukankan      html  css  js  c++  java
  • HDU 1134 卡特兰数 大数乘法除法

    Problem Description
    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
     


    #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; //每次循环都要初始化为0
    			for(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("
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    PHP浮点数引起的四舍五入问题
    几项有用的JQUERY代码
    php5.5新特性之yield理解
    【转】循环、迭代、遍历和递归
    phpexcel导入excel文件报the filename xxx is not recognised as an OLE file错误。
    php缩放gif和png格式透明背景变成黑色的解决方法
    javascript统计输入文本的简易方法
    分享几个实用的jquery工具函数
    thinkphp中ajax技术
    正斜杠与反斜杠
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3163000.html
Copyright © 2011-2022 走看看