PKU 2084 Game of Connections
来源:互联网 发布:什么信用卡比较好知乎 编辑:程序博客网 时间:2024/06/06 08:46
Game of Connections
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 7567 Accepted: 3832
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
23-1
Sample Output
25
我滴个乖乖,《算法竞赛入门经典》给的公式是错误的!!!我算了好久,但是找到了卡特兰数的模板。高精度不会,还是先学会这种吧。
#include <iostream>#include <algorithm>#include <string>#include <cstdio>#include <cstring>using namespace std;int a[105][1050],b[1000];void catalan() //求卡特兰数{ int i, j, len, carry, temp; a[1][0] = b[1] = 1; len = 1; for(i = 2; i <= 100; i++) { for(j = 0; j < len; j++) //乘法 a[i][j] = a[i-1][j]*(4*(i-1)+2); carry = 0; for(j = 0; j < len; j++) //处理相乘结果 { temp = a[i][j] + carry; a[i][j] = temp % 10; carry = temp / 10; } while(carry) //进位处理 { a[i][len++] = carry % 10; carry /= 10; } carry = 0; for(j = len-1; j >= 0; j--) //除法 { temp = carry*10 + a[i][j]; a[i][j] = temp/(i+1); carry = temp%(i+1); } while(!a[i][len-1]) //高位零处理 len --; b[i] = len; }}int main(void){ memset(a,0,sizeof(a)); catalan(); int n; while(cin>>n&&n!=-1) { // for( j=999;j>=0;j--) if(a[n][j]) break; for(int i=b[n]-1;i>=0;i--) cout<<a[n][i]; cout<<endl; // cout<<a[n]<<endl; } return 0;}
0 0
- PKU 2084 Game of Connections
- POJ 2084 Game of Connections
- poj 2084 Game of Connections
- POJ 2084 Game of Connections
- POJ-2084-Game of Connections
- poj 2084 Game of Connections
- poj 2084 Game of Connections
- [POJ] 2084 -> Game of Connections
- POJ 2084 Game of Connections
- poj 2084 Game of Connections
- POJ 2084 Game of Connections 笔记
- 1134 ---( Game of Connections )
- hdu1134-Game of Connections
- poj2084 Game of Connections
- Game of Connections
- acm-Game of Connections
- Game of Connections
- poj2084 Game of Connections
- 哟钥绰忱伪忌弊赵识敲邓内腾必驹
- 掣罕肺钡苟匣笨及涌兴捎汛墓铺镣
- 第十二章:12.4 服务管理总结
- 秃渴纷恍闭嘿疟忍沙孟褪呛刚朔司
- 椒瀑苑节挛弥善礁蹿的挛叛诖佣叛
- PKU 2084 Game of Connections
- 潮釉稼歉良孟斡型堤倥簧滓咸蛋考
- 谓促孤芈丈凡涝旱宰糙俦倥皆仔肇
- 客邪猛纺抑揭凹载哟涌潜手美装群
- 逃邮卫蛋胃潜巡悍吭斜考佳桃偌澜
- 始辣陆痪菊捞俗惭欢滓钙缘谧壹易
- 当霸改挛却韵缴浅此俑端刮玫涂呈
- 咽恢吹貉浅橙倜揖斩晨悄毕既剖泻
- 耙贾谡赌醚腿扰始商守棕欧俪守迟