HDU 1134 Game of Connections
来源:互联网 发布:学霸专用解锁软件 编辑:程序博客网 时间:2024/05/18 21:39
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?
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(0)=1;h(1)=1;h(2)=2;h(3)=5……
卡特兰数的递推公式为:h(n)=h(n-1)*(4*n-2)/(n+1);
非递推公式为C(2n,n)/(n+1);
此题用递推公式求解,并用到大数的乘法和大数的乘法处理,本题对卡特兰数的前100项做了预处理:
#include <iostream>using namespace std;int A[105][105],b[105];//A[i][j]存的是数i的卡特兰,b[i]存的是卡特兰数i的长度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(){ int N; catalan(); //求卡特兰数 while(cin>>N&&N!=-1) { for(int i=b[N]-1;i>=0;i--) cout<<A[N][i]; cout<<endl; } return 0;}
0 0
- HDU 1134 Game of Connections
- HDU 1134 Game of Connections
- HDU 1134 Game of Connections
- HDU 1134 Game of Connections
- Game of Connections HDU
- Game of Connections HDU
- hdu/hdoj 1134 Game of Connections
- hdu 1134 game of connections(Catalan Number)
- [Catalan] HDU 1134 Game of Connections
- 高精度-HDU-1134-Game of Connections
- hdu 1134 Game of Connections (Catalan数)
- HDOJ HDU 1134 Game of Connections
- 1134 ---( Game of Connections )
- hdu 1134 Game of Connections(卡特兰(Catalan)公式)
- hdu 1134 Game of Connections(卡特兰数)
- hdu 题目1134 Game of Connections (卡塔兰数 catalan number)
- HDU 1134 Game of Connections 高精度卡特兰数
- [ACM] hdu 1134 Game of Connections(大数+Catalan数)
- C3-循环
- Redis学习
- java File类
- MarkDown编辑器
- 关于struts.xml
- HDU 1134 Game of Connections
- 批处理中的符号用途说明
- 水煮互联网创业,互联网创业的盲区
- __attribute__((weak))
- 表达式求值
- mysql 常用 sql 语句(一)
- github代码搜索技巧
- C4-一维数组、字符数组
- hdu 2275 Kiki & Little Kiki 1