HDU 1023 Train Problem II (卡特兰数,经典)
题意:
给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列。
思路:
卡特兰数的经典例子。n<101,用递推式解决。需要使用到大数。n=100时大概有200位以下。
#include <bits/stdc++.h>
using namespace std;
const int N=;
vector<string> vect;
void _mult(string num1, string num2, string &result )
{
reverse(num1.begin(),num1.end()); //反转
reverse(num2.begin(),num2.end());
result="";
int i, j, re_int[]; //********这里的150是位数,根据需要可以增大或减小*********
memset(re_int, , sizeof(re_int));
for(i=; i<num1.length(); i++) //两串作乘法,结果存放于re_int数组中, 最多可达150位!
for(j=; j<num2.length(); j++)
re_int[i+j] += ((num1[i]-) * (num2[j]-));
int jinwei=, zhi;
for(i=; i<num1.length()+num2.length(); i++) //单独处理进位问题,上一步中的数组每个元素都有可能超过10的,所以没处理进位
{
zhi = re_int[i]+jinwei;
re_int[i] = zhi%;
jinwei = zhi/;
}
for(i=num1.length()+num2.length()-; i>=; i--) //将i打个标记,数组re_int的前面部分可能全0,要去掉
if(re_int[i]!=) break;
for(;i>=;i--) //将整型数组转成字符串
result = result+(char)(re_int[i]+);
if(result=="") //若结果还是空,乘法的结果是0?
result="";
}
void div(char * src,int n,char *dest)
{
int len = strlen(src),i,k,t=,s=;
bool flag = true; //商是否有了第一个有效位,防止商首部一直出现0
for(i=,k=; i<len; i++)
{
t = s*+(src[i]-); //新余数
if(t/n> || t==) //余数为0要修改商
{
dest[k++] = t/n+,s = t%n,flag = false;
}
else //不够除,修改余数
{
s = t;
if(!flag) //商已经有有效位了,补零
dest[k++] = '';
}
}
dest[k]='\0';
} void precal()
{
string s="";
vect.push_back(s);
vect.push_back(s);
char c[], dest[];
for(int i=; i<; i++)
{
string q1="",res;
int a=*i-; //第一个括号
while(a)
{
q1+=(a%+'');
a/=;
}
reverse(q1.begin(),q1.end());
_mult(q1,vect[i-],res); //乘法
strcpy(c,res.c_str());
div(c,i+,dest); //除法
s=dest;
vect.push_back(dest);
}
}
int main()
{
//freopen("e://input.txt", "r", stdin);
int n;
precal();
while(~scanf("%d", &n))
cout<<vect[n]<<endl;
return ;
}
AC代码
HDU 1023 Train Problem II (卡特兰数,经典)的更多相关文章
- HDOJ 1023 Train Problem II 卡特兰数
火车进站出站的问题满足卡特兰数...卡特兰数的相关知识如下: 卡特兰数又称卡塔兰数,是组合数学中一个常出现在各种计数问题中出现的数列.由以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)命名. ...
- HDU 1023 Train Problem II (大数卡特兰数)
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- HDU 1023 Train Problem II( 大数卡特兰 )
链接:传送门 题意:裸卡特兰数,但是必须用大数做 balabala:上交高精度模板题,增加一下熟悉度 /************************************************ ...
- C - Train Problem II——卡特兰数
题目链接_HDU-1023 题目 As we all know the Train Problem I, the boss of the Ignatius Train Station want to ...
- HDU-1023 Train Problem II 卡特兰数(结合高精度乘除)
题目链接:https://cn.vjudge.net/problem/HDU-1023 题意 卡特兰数的应用之一 求一个长度为n的序列通过栈后的结果序列有几种 思路 一开始不知道什么是卡特兰数,猜测是 ...
- HDU 1023 Train Problem II 大数打表Catalan数
一个出栈有多少种顺序的问题.一般都知道是Catalan数了. 问题是这个Catalan数非常大,故此须要使用高精度计算. 并且打表会速度快非常多.打表公式要熟记: Catalan数公式 Cn=C(2n ...
- 1023 Train Problem II(卡特兰数)
Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want ...
随机推荐
- POJ 2140
#include<iostream> #include<stdio.h> using namespace std; int main() { int num; int i; i ...
- UVA 10635 Prince and Princess
题意描述:有两个长度分别为p+1和q+1的序列,每个元素中的各个元素互不相同.都是1~n^2之间的整数,求A和B的最长公共子序列.(2<=n<=250,1<=p,q<=n^2) ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 2(Big Number)
这里的高精度都是要去掉前导0的, 第一题:424 - Integer Inquiry UVA:http://uva.onlinejudge.org/index.php?option=com_onlin ...
- hdu 4767 Bell
思路:矩阵快速幂+中国剩余定理!! 查资料得到2个公式: 1) B[n+p] = B[n] + B[n+1] mod p ; 2) B[p^m+n] = ...
- iOS开发--成员变量与属性
属性变量 @interface MyClass:NSObject{ MyObjecct *_object; } @property(nonamtic, retain) MyObjecct *objec ...
- 给View换字体
注意,给View换字体是直接换.在Delegate里换的只是某一列的字体 class delegate : public QStyledItemDelegate { public: ) : QStyl ...
- jquery:cookie
jquery使用cookie需要引入cookie插件: 插件下载地址:http://plugins.jquery.com/cookie/ cookie设置.获取和删除 <body> < ...
- CentOS下支持exFAT与NTFS
exFAT: 1.下载fuse-exfat支持软件: exfat支持是通过fuse模块的方式支持的,其项目地址是: https://code.google.com/p/exfat/ ,当前版本是:1. ...
- Linux系统管道命令符
管道命令符“|”的作用是将前一个命令的标准输出作为后一个命令的标准输入,格式为“命令A | 命令B” 以下实例中,通过grep命令搜索关键字“/sbin/nologin”在/etc/passwd中查找 ...
- spring springmvc mybatis 整合
环境 apache-tomcat-8.0.33.jdk1.8.0_05 maven Dynamic Web Module 2.5 1.各个xml配置文件的配置 (1)pom.xml 配置清单文件 连接 ...