nyoj 164&&poj2084 Game of Connections 【卡特兰
来源:互联网 发布:手机号码测吉凶80算法 编辑:程序博客网 时间:2024/06/05 09:58
题意:将1~2n个数按照顺时针排列好,用一条线将两个数字连接起来要求:线之间不能有交点,同一个点只允许被连一次。
最后问给出一个n,有多少种方式满足条件。
分析:
ans[n]表示n的中的种类数。 规定ans[0] = ans[1] = 1;
假设给出的数是n那么从1开始, 与1之间相连的数与1之间间隔的对数分别是0, 1, 。。n-1, 那么我们就可以将他们分割成两部分,对于每一部分我们分别将其的结果求出,之后再相乘就是间隔对数s(s是0, 。。n-1)的总的种类数。
最后我们可以总结出ans[n] = ans[0]*ans[n-1]+ans[1]*ans[n-2]+...ans[n-1]*ans[0];即为卡特兰数。
假设给出的是4,那么一共有8个数,按照顺时针排列,我们假设从1开始,那么1可以与2(之间相差0个数), 4(之间相差2个数), 6(之间相差4个数), 8(之间相差6个数),假如我们知道相差n个数的的种类数,那么我们只需要将他们相加,即为我们所要求的总种类数。下面我就按照上面的例子即n=4分析一下。相差为0的时候,我们只需要考虑剩下的三对即可,则相差为0的种类数就为ans[3]*ans[0],之间相差2的时候我们就吧原有的序列分成了两部分,第一部分只有2个数,第二部分有4个数,那么相差为2的种类数就是ans[2]*ans[1];相差为4的其实就是上面情况的第一部分和第二部分颠倒了,这种情况下的种类数是ans[1]*ans[2],相差为6就是第一种情况的颠倒,所以种类数是ans[3]*ans[0];
代码:
- #include <cstdio>
- #include <cstring>
- int ans[102][100];
- void table(){
- ans[0][0] = ans[1][0] = 1;
- int i, j;
- for(i = 2; i < 102; i ++){
- int c = 0;
- for(j = 0; j < 100; j ++){
- ans[i][j] = ans[i-1][j]*(4*i-2)+c;
- c = ans[i][j]/10;
- ans[i][j] %= 10;
- }
- int z = 0;
- for(j = 99; j >= 0; j --){
- z= z*10+ans[i][j];
- ans[i][j] = z/(i+1);
- z %= (i+1);
- }
- }
- }
- int main(){
- table();
- int temp;
- while(scanf("%d", &temp), temp != -1){
- int i = 99;
- while(ans[temp][i] == 0) i --;
- while(i >= 0) printf("%d", ans[temp][i]), i--;
- printf("\n");
- }
- return 0;
- }
#include <cstdio>#include <cstring>int ans[102][100];void table(){ans[0][0] = ans[1][0] = 1;int i, j;for(i = 2; i < 102; i ++){int c = 0;for(j = 0; j < 100; j ++){ans[i][j] = ans[i-1][j]*(4*i-2)+c;c = ans[i][j]/10;ans[i][j] %= 10;}int z = 0;for(j = 99; j >= 0; j --){z= z*10+ans[i][j];ans[i][j] = z/(i+1);z %= (i+1);}}}int main(){table();int temp;while(scanf("%d", &temp), temp != -1){int i = 99;while(ans[temp][i] == 0) i --;while(i >= 0) printf("%d", ans[temp][i]), i--;printf("\n");}return 0;}题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=164
http://poj.org/problem?id=2084
0 0
- nyoj 164&&poj2084 Game of Connections 【卡特兰】
- nyoj 164&&poj2084 Game of Connections 【卡特兰
- NYOJ---164 &&HDOJ1134&&POJ2084 --> Game of Connections
- poj2084 Game of Connections
- poj2084 Game of Connections
- POJ Game of Connections 卡特兰数
- Game of Connections 卡特兰数
- Game of Connections(卡特兰数)
- hdu 1134 Game of Connections(卡特兰(Catalan)公式)
- hdu 1134 Game of Connections(卡特兰数)
- HDU 1134 Game of Connections 高精度卡特兰数
- hdu1134 Game of Connections 卡特兰数(大数)
- POJ 2084 Game of Connections 卡特兰数
- POJ 2084 Game of Connections(卡特兰数,JAVA)
- 卡特兰数:poj 2084 Game of Connections+hdu 1023
- Poj 2084 Game of Connections(高精度卡特兰数)
- hdu 1134 Game of Connections 【卡特兰数+大数】
- hdu 1134 Game of Connections(卡特兰数模板题)
- POJ2431 Expedition(排序+优先队列)
- MAC地址和IP地址的关系 .
- Ural 1119 Metro(DP) .
- 计算字符个数——来自华为OJ平台测试基础篇
- 1.二分加速法(很容易遇到此类题)
- nyoj 164&&poj2084 Game of Connections 【卡特兰
- windows程序设计篇——插入位图
- BZOJ 2048 2009国家集训队 书堆 数学算法 .
- 使用 IOS 自带 API 扫描二维码
- 李林APUE之进程的封装
- sicily1024
- 《数据挖掘概念与技术》学习笔记 .
- Android中相机和相册使用分析
- jbpm 笔记 流程部署