Written by razrlele
12:04 October 10, 2014
Game of Connections
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7901 | Accepted: 3973 |
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?
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.
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
1 2 3 |
2 3 -1 |
Sample Output
1 2 |
2 5 |
Source
这道题就是要在有2*n个点的圆上连线,每个点必须且仅与一个点连线。这样的话每个点只能跟自己相隔偶数个的点相连,然后就可以得出一个递推公式:
f(n) = f(n-1) * f(0)+f(n-2) * f(1)+f(n-3) * f(2)…f(0) * f(n-1)
这就是传说中的卡塔兰数。
最后还是照着网上的大数模板敲了一遍,之前一直还没研究过模板的说。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
/************************************************************************* > File Name: poj2084.cpp > Author: razrLeLe > Mail: razrlele@outlook.com > Homepage: https://yueyu.io > Created Time: 2014年10月09日 星期四 20时52分57秒 ************************************************************************/ #define LOCAL #include <vector> #include <list> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <string.h> #define INF 0x3f3f3f3f int n; struct BigInt { int data[60]; BigInt& operator = (const BigInt &ob) { memcpy(data, ob.data, sizeof(data)); return *this; } BigInt& operator += (const BigInt &ob) { if(data[0] < ob.data[0]) data[0] = ob.data[0]; for(int i = 1; i <= data[0]; ++i) { data[i] += ob.data[i]; if(data[i] >= 10) { data[i] -= 10; data[i+1]++; } } if(data[data[0]+1] != 0) data[0]++; return *this; } void setDigit(int n) { memset(data,0,sizeof(data)); data[0] = 1; data[1] = n; } BigInt operator * (const BigInt &ob) { BigInt pro; pro.setDigit(0); pro.data[0] = data[0] + ob.data[0]-1; for(int i = 1; i <= data[0]; i++) for(int j = 1; j <= ob.data[0]; j++) pro.data[i+j-1] += data[i] * ob.data[j]; for(int i = 1; i <= pro.data[0]; i++) { pro.data[i+1] += pro.data[i]/10; pro.data[i] %= 10; } while(pro.data[pro.data[0]] >= 10) { pro.data[pro.data[0] +1] += pro.data[pro.data[0]] /10; pro.data[pro.data[0]] %= 10; pro.data[0]++; } return pro; } void print() { for(int i = data[0]; i > 0; --i) printf("%d", data[i]); } }f[105]; void calc() { f[0].setDigit(1); f[1].setDigit(1); for(int i = 2; i <= 100; ++i) { f[i].setDigit(0); for(int j = 0; j < i; j++) f[i] += f[j] * f[i-j-1]; } return ; } int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif calc(); while(scanf("%d", &n) && n != -1) { f[n].print(); printf("\n"); } return 0; } |