题目代号: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. 使用itchat获取微信好友的男女比例

    # 使用itchat获取微信好友的男女比例 import itchat itchat.auto_login(hotReload=True) friends = itchat.get_friends(u ...

  2. Linux操作系统优化

    figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...

  3. mybatis中resultMap的使用

    在mybatis中,使用<select>标签,必须要设置resultType属性 或 resultMap属性 否则会报错! resultType一般是返回简单类型的查询结果,涉及一张表 可 ...

  4. JetBrains视图

    三种视图模式:

  5. redis进阶知识

    原文地址:https://my.oschina.net/u/4052893/blog/3001173 一.缓存雪崩 1.1什么是缓存雪崩? 回顾一下我们为什么要用缓存(Redis): 现在有个问题,如 ...

  6. Views的补充

    views的补充 请求头一般与请求内容用/r/n/r/n隔开 请求头包含的内容 request.Meta(...) 一般在下面几种方法里面取不到的东西需要去原生的头里面去取,比如用户的终端类型 req ...

  7. C语言中整形数组、字符数组、字符串的区别

    一. 第一 整型数组的存放,数组最后是不加'\0'的,字符串会自动加上,因此存放字符的时候数组的大小要比实际字符的多一个 第二 整型数组 每一个单元是4个字节的,字符串是一个一个字符存放的,每个字符占 ...

  8. spark 在启动的时候出现JAVA_HOME not set

    解决方法:在sbin目录下的spark-config.sh 中添加对应的jdk 路径,然后使用scp -r 命令复制到各个worker节点

  9. 前端Unicode字符图标

    前端Unicode字符图标 原文链接地址:http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/20141225979.html

  10. [七月挑选]使用idea创建spring boot 项目

    title: 使用idea创建spring boot 项目 参考lindaZ的IntelliJ IDEA 创建spring boot 的Hello World 项目 1.Open IDEA,choos ...