HDU 1134 Game of Connections(卡特兰数+大数模板)
题目代号: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
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?
number n, except the last line, which is a number -1. You may assume that 1
<= n <= 100.
to connect the 2n numbers into pairs.
# 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(卡特兰数+大数模板)的更多相关文章
- hdu 1130,hdu 1131(卡特兰数,大数)
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)
题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- hdu 1134 Game of Connections
主要考察卡特兰数,大数乘法,除法…… 链接http://acm.hdu.edu.cn/showproblem.php?pid=1134 #include<iostream>#include ...
- 【hdoj_1133】Buy the Ticket(卡特兰数+大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目的意思是,m个人只有50元钱,n个人只有100元整钱,票价50元/人.现在售票厅没钱,只有50元 ...
- POJ2084 Game of Connections 卡特兰数 关于卡特兰数经典的几个问题
Game of Connections Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9128 Accepted: 44 ...
- HDU 1023 Train Problem II (卡特兰数,经典)
题意: 给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列. 思路: 卡特兰数的经典例子.n<101,用递推式解决.需要使用到大数.n=100时大概有200位以下. #inclu ...
- Train Problem II(卡特兰数+大数乘除)
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu-1130(卡特兰数+大数乘法,除法模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1130 卡特兰数:https://blog.csdn.net/qq_33266889/article/d ...
随机推荐
- 精读《What's new in javascript》
1. 引言 本周精读的内容是:Google I/O 19. 2019 年 Google I/O 介绍了一些激动人心的 JS 新特性,这些特性有些已经被主流浏览器实现,并支持 polyfill,有些还在 ...
- realloc ------ 扩大malloc得到的内存空间
char* p = malloc(1024);char* q = realloc(p,2048); 现在的问题是我们应该如何处理指针 p. 刚开始按照我最直观的理解,如果就是直接将 p = NULL; ...
- 通过proxychains实现Ubuntu终端代理
1.在终端内使用代理,需要使用proxychains: sudo apt-get install proxychains 2.编辑 /etc/proxychains.conf sudo gedit / ...
- [DS+Algo] 009 树的介绍
目录 1. 树的概念 2. 树的术语 3. 树的种类 4. 常见应用场景 5. 二叉树 1. 树的概念 每个结点(节点)有 0 个或多个子结点 没有父结点的结点称为根结点 每一个非根结点有且只有一个父 ...
- [Git] 025 标签命令
0. 前言 小时候看<剑花-烟雨-江南>,惊讶于那个多重身份的"小侯爷" 后来发现,现实中拥有比小侯爷更多身份的人多如牛毛 其实,在 "Git" 中 ...
- CentOS下搭建docker+.net core
运行环境: CentOS 7.0 容器:Docker 1.13.1 .Net Core版本: .NET Core 2.1,安装详见 CentOS 7 下安装.NET Core SDK 2.1 1.安装 ...
- 洛谷 P5663 加工零件 & [NOIP2019普及组] (奇偶最短路)
传送门 解题思路 很容易想到用最短路来解决这一道问题(题解法),因为两个点之间可以互相无限走,所以如果到某个点的最短路是x,那么x+2,x+4也一定能够达到. 但是如何保证这是正确的呢?比如说到某个点 ...
- [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)
[多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...
- hive中的索引创建
1.在hive中创建索引所在表 create table if not exists h_odse.hxy(id int,name string,hobby array<string>,a ...
- 搜索专题: HDU1026Ignatius and the Princess I
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...