HDU1134 Game of Connections

来源:互联网 发布:手机淘宝所在地怎么改 编辑:程序博客网 时间:2024/05/18 02:41

Game of Connections

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4430    Accepted Submission(s): 2574


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
23-1
 

Sample Output
25
      这个题一开始推着做,得到递推式h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)*h(0) (n>=2),是卡特兰数。这里用到大数运算。
      另附上卡特兰数的公式:
                h(n)=h(n-1)*(4*n-2)/(n+1)(n>=1)

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int a[102][102];void multiply(int d,int k);void divide(int d,int k);int main(){    int n;    memset(a[1],0,sizeof(a[1]));    a[1][99] = 1;    for(int i = 2;i <= 100;i++){        multiply(i-1,4*i-2);        divide(i,i+1);    }    while(~scanf("%d",&n)&&n!=-1){            int i;            for(i = 0;i < 100&&!a[n][i];i++);            for(int j = i;j < 100;j++)                printf("%d",a[n][j]);            printf("\n");    }    return 0;}void multiply(int d,int k){  int temp,add = 0;  for(int i = 99;i >= 0;i--){    temp = a[d][i]*k+add;    a[d+1][i] = temp%10;    add = temp/10;  }  return;}void divide(int d,int k){  int temp=0;  for(int i = 0;i < 100;i++){    temp = temp*10+a[d][i];    a[d][i] = temp/k;    temp = temp%k;  }  return;}



0 0
原创粉丝点击