题目代号: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. 索尼A6300

    1. 开机提示的NTSC NTSC和PAL,是两种不同视频录制标准,也就是说拍摄视频的时候才有用. a6300 1080P下,最大可以以 每秒 120p(ntsc下)或者 100p(pal下)录制.播 ...

  2. Go语言入门篇-环境准备

    一.GO语言特点 静态类型:首先要明确变量类型,如上所示. 编译型:指GO语言要被编译成机器能识别机器代码. GO语言开源. 编程范式:支持“函数式”和“面向对象” GO语言原生的支持并发编程:即GO ...

  3. TestNG+extentReports+log4j2 完善自动化测试框架——美观的报告和保留日志文件

    1:导入Maven依赖<dependency> <groupId>com.aventstack</groupId> <artifactId>extent ...

  4. Express中间件body-parser

    在http请求种,POST.PUT.PATCH三种请求方法中包含着请求体,也就是所谓的request,在Nodejs原生的http模块中,请求体是要基于流的方式来接受和解析. body-parser是 ...

  5. UUID工具类及使用

    1.工具类: package UUIdtest; import java.util.UUID; public class UUIDUtil { public static String getUUID ...

  6. 你知道 Java 类是如何被加载的吗?

    前言 最近给一个非 Java 方向的朋友讲了下双亲委派模型,朋友让我写篇文章深度研究下JVM 的 ClassLoader,我确实也好久没写 JVM 相关的文章了,有点手痒痒,涂了皮炎平也抑制不住的那种 ...

  7. C语言数组名取地址。。。

    int main(){ int a[5] = { 1, 2, 3, 4, 5 }; printf("%08X ,%08X ,%08X ,%08X", a, &a, a + ...

  8. django2.0变动数据库设置外键报错

    1.报错TypeError: __init__() missing 1 required positional argument: 'on_delete' django2.0以后创建数据库外键的时候必 ...

  9. 如何将DataTable转换成List<T>

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  10. vue filters过滤

    <template> <div class="filters"> <h1 v-text="filtersTitle">< ...