题目代号:HDU 1134

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1134

Game of Connections

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

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
2
3
-1
 
Sample Output
2
5
题目大意:给出一个值n(n<=100),1,2,3,···2n围成一个圈,问有多少种方式让每个数字成对连接的同时不相交。因为数据非常大,所以我选择先用大数模板算出卡特兰数的前一百个数据之后打表。
打表代码:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; string num[]={"",""}; //string比较函数:相等返回0,str1>str2返回1,str1<str2返回-1.
int Compare(string str1,string str2)
{
if(str1.length() > str2.length()) return ;
else if(str1.length() < str2.length()) return -;
else return str1.compare(str2);
} string Big_Plus(string str1,string str2)
{
string ans;
int len1=str1.length();
int len2=str2.length();
//将长度较小的前面补0,使两个string长度相同
if(len1<len2){
for(int i=;i<=len2-len1;i++){
str1=""+str1;
}
}else {
for(int i=;i<=len1-len2;i++){
str2=""+str2;
}
}
int len=max(len1,len2);
int carry=;
for(int i=len-;i>=;i--){
int tmp=str1[i]-''+str2[i]-''+carry;
carry=tmp/;
tmp%=;
ans=char(tmp+'')+ans;
}
if(carry) ans=char(carry+'')+ans;
return ans;
} //支持大数减小数
string Big_Sub(string str1,string str2)
{
string ans;
int carry=;
int difference=str1.length()-str2.length();//长度差
for(int i=str2.length()-;i>=;i--){
if(str1[difference+i]<str2[i]+carry){
ans=char(str1[difference+i]+-str2[i]-carry+'')+ans;
carry=;
}else {
ans=char(str1[difference+i]-str2[i]-carry+'')+ans;
carry=;
}
}
for(int i=difference-;i>=;i--){
if(str1[i]-carry>=''){
ans=char(str1[i]-carry)+ans;
carry=;
}else {
ans=char(str1[i]-carry+)+ans;
carry=;
}
}
//去除前导0
ans.erase(,ans.find_first_not_of(''));
if(ans.empty()) ans="";
return ans;
} string Big_Mul(string str1,string str2)
{
string ans;
int len1=str1.length();
int len2=str2.length();
for(int i=len2-;i>=;i--){
string tmpstr="";
int data=str2[i]-'';
int carry=;
if(data!=){
for(int j=;j<=len2--i;j++){
tmpstr+="";
}
for(int j=len1-;j>=;j--){
int t=data*(str1[j]-'')+carry;
carry=t/;
t%=;
tmpstr=char(t+'')+tmpstr;
}
if(carry!=) tmpstr=char(carry+'')+tmpstr;
}
ans=Big_Plus(ans,tmpstr);
}
ans.erase(,ans.find_first_not_of(''));
if(ans.empty()) ans="";
return ans;
} //正数相除,商为quotient,余数为residue void Big_Div(string str1,string str2,string& quotient,string& residue)
{
quotient=residue="";//商和余数清空
if(str2==""){//;判断除数是否为0
quotient=residue="ERROR";
return;
}
if(str1==""){//判断被除数是否为0
quotient=residue="";
return;
}
int res=Compare(str1,str2);
if(res<){//被除数小于除数
quotient="";
residue=str1;
return;
}else if(res==){
quotient="";
residue="";
return ;
}else {
int len1=str1.length();
int len2=str2.length();
string tmpstr;
tmpstr.append(str1,,len2-);//将str1的前len2位赋给tmpstr
for(int i=len2-;i<len1;i++){
tmpstr=tmpstr+str1[i];//被除数新补充一位
tmpstr.erase(,tmpstr.find_first_not_of(''));//去除前导0
if(tmpstr.empty()) tmpstr="";
for(char ch='';ch>='';ch--){//试商
string tmp,ans;
tmp=tmp+ch;
ans=Big_Mul(str2,tmp);//计算乘积
if(Compare(ans,tmpstr)<=){//试商成功
quotient=quotient+ch;
tmpstr=Big_Sub(tmpstr,ans);//减掉乘积
break;
}
}
}
residue=tmpstr;
}
quotient.erase(,quotient.find_first_not_of(''));
if(quotient.empty()) quotient="";
} string change(int num)
{
string n="";
stack<char>M;
while(num>)
{
M.push(num%+'');
num/=;
}
while(!M.empty())
{
n+=M.top();
M.pop();
}
return n;
} int change(string num)
{
int n=num[]-'';
for(int i=;i<num.size();i++)
n=n*+num[i]-'';
return n;
} int main()
{
int n;
for(int i=;i<=;i++)
{
if(i>)for(int j=;j<i;j++)
{
num[i]=Big_Plus(Big_Mul(num[j],num[i-j-]),num[i]);
}
cout<<"\""<<num[i]<<"\","<<endl;
}
return ;
}

  

打表完成后的代码很容易:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; string num[]= {"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
}; int main()
{
int n;
while(cin>>n,n!=-)
{
cout<<num[n]<<endl;
}
return ;
}

  

HDU 1134 Game of Connections(卡特兰数+大数模板)的更多相关文章

  1. hdu 1130,hdu 1131(卡特兰数,大数)

    How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

    题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...

  3. HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  4. hdu 1134 Game of Connections

    主要考察卡特兰数,大数乘法,除法…… 链接http://acm.hdu.edu.cn/showproblem.php?pid=1134 #include<iostream>#include ...

  5. 【hdoj_1133】Buy the Ticket(卡特兰数+大数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目的意思是,m个人只有50元钱,n个人只有100元整钱,票价50元/人.现在售票厅没钱,只有50元 ...

  6. POJ2084 Game of Connections 卡特兰数 关于卡特兰数经典的几个问题

    Game of Connections Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9128   Accepted: 44 ...

  7. HDU 1023 Train Problem II (卡特兰数,经典)

    题意: 给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列. 思路: 卡特兰数的经典例子.n<101,用递推式解决.需要使用到大数.n=100时大概有200位以下. #inclu ...

  8. Train Problem II(卡特兰数+大数乘除)

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu-1130(卡特兰数+大数乘法,除法模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1130 卡特兰数:https://blog.csdn.net/qq_33266889/article/d ...

随机推荐

  1. 深度学习之美(张玉宏)——第四章 人生苦短我用python

    1 函数参数 (1)收集参数:以一个星号*加上形参名的方式,表示这个函数的实参个数不定,可能0个可能n个. def varParaFun(name,*param): print('位置参数是:',na ...

  2. 【转】MySQL查询缓存详解

    [转]MySQL查询缓存详解 转自:https://www.cnblogs.com/Alight/p/3981999.html 相关文章:http://www.zsythink.net/archive ...

  3. Spring Task 任务调度(定时器)

    1.任务调度SpringTask 1.1什么是任务调度 在企业级应用中,经常会制定一些“计划任务”,即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作.常见的 ...

  4. Thinkphp设置PC和手机端模板

    <?php // 判断手机端 function ismobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X ...

  5. while与格式化的练习

    练习 判断下列逻辑语句的结果,一定要自己先分析 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 2)n ...

  6. linux查看端口是否被占用

    1.使用lsof lsof -i:端口号查看某个端口是否被占用 2.使用netstat 使用netstat -anp|grep 80

  7. Python爬虫之selenium高级功能

    Python爬虫之selenium高级功能 原文地址 表单操作 元素拖拽 页面切换 弹窗处理 表单操作 表单里面会有文本框.密码框.下拉框.登陆框等. 这些涉及与页面的交互,比如输入.删除.点击等. ...

  8. java语法糖:(1)可变长度参数以及foreach循环原理

    语法糖 语法糖:是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的字节码或者特定的方式对这些语法做一些处理,开发者就可以直接方便地使用 ...

  9. TCP软件环境测试

    利用合宙官网上的云平台->TCP透传云,建立一个TCP服务. http://tcplab.openluat.com/ [注意事项] 如3分钟内没有客户端接入则会自动关闭. 每个服务器最大客户端连 ...

  10. 05java基础

    1.BigInteger和BigDecimal类 package cn.jxufe.java.chapter5.demo01; import java.math.BigInteger; public ...