卡特兰数  但是个高精度 一开始用最普通的递推式 超时了 百度百科了一下 用另类递推式过了 ~~

这个大数类是做数据结构课程设计的时候写的...

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 1010
#define INF 0x7fffffff
#define inf 10000000
#define MOD 34943
#define ull unsigned long long
#define ll long long
using namespace std; #define MAXN 9999
#define MAXSIZE 10
#define DLEN 4 class BigNum
{
private:
int a[500]; //可以控制大数的位数
int len; //大数长度
public:
BigNum()
{
len = 1; //构造函数
memset(a,0,sizeof(a));
}
BigNum(const int); //将一个int类型的变量转化为大数
BigNum(const BigNum &); //拷贝构造函数
BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算 friend ostream& operator<<(ostream&, BigNum&); //重载输出运算符
BigNum operator/(const int &) const;
BigNum operator+(const BigNum &) const; //重载加法运算符,两个大数之间的相加运算
BigNum operator*(const BigNum &) const; //重载乘法运算符,两个大数之间的相乘运算 };
BigNum::BigNum(const int b) //将一个int类型的变量转化为大数
{
int c,d = b;
len = 0;
memset(a, 0, sizeof(a));
while(d > MAXN)
{
c = d - (d / (MAXN + 1)) * (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
} BigNum::BigNum(const BigNum & T) : len(T.len) //拷贝构造函数
{
memset(a, 0, sizeof(a));
for(int i = 0 ; i < len ; i++)
a[i] = T.a[i];
}
BigNum & BigNum::operator=(const BigNum & n) //重载赋值运算符,大数之间进行赋值运算
{
len = n.len;
memset(a,0,sizeof(a));
for(int i = 0 ; i < len ; i++)
a[i] = n.a[i];
return *this;
} ostream& operator<<(ostream& out, BigNum& b) //重载输出运算符
{
int i;
cout << b.a[b.len - 1];
for(i = b.len - 2 ; i >= 0 ; i--)
{
cout.width(DLEN);
cout.fill('0');
cout << b.a[i];
}
return out;
} BigNum BigNum::operator+(const BigNum & T) const //两个大数之间的相加运算
{
BigNum t(*this);
int i,big; //位数
big = T.len > len ? T.len : len;
for(i = 0 ; i < big ; i++)
{
t.a[i] +=T.a[i];
if(t.a[i] > MAXN)
{
t.a[i + 1]++;
t.a[i] -=MAXN+1;
}
}
if(t.a[big] != 0)
t.len = big + 1;
else
t.len = big;
return t;
} BigNum BigNum::operator*(const BigNum & T) const //两个大数之间的相乘运算
{
BigNum ret;
int i,j,up;
int temp,temp1;
for(i = 0 ; i < len ; i++)
{
up = 0;
for(j = 0 ; j < T.len ; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN)
{
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
}
else
{
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
} BigNum BigNum::operator/(const int & b) const //大数对一个整数进行相除运算
{
BigNum ret;
int i,down = 0;
for(i = len - 1 ; i >= 0 ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
BigNum f[maxn]; void init()
{
f[0] = f[1] = 1;
for(int i = 2; i <= 1000; ++ i)
f[i] = f[i-1] * (4*i-2) / (i+1);
}
int main()
{
init();
int n;
while(scanf("%d", &n) == 1)
cout << f[n] << endl;
return 0;
}

uva 10303的更多相关文章

  1. UVA 10303 - How Many Trees?(数论 卡特兰数 高精度)

    Problem D How Many Trees? Input: standard input Output: standard output Memory Limit: 32 MB A binary ...

  2. UVA 10303 How Many Trees? (catlan)

    刚开始没看出时卡特兰数列.直接套高精度版 #include <map> #include <set> #include <list> #include <cm ...

  3. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  4. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  5. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  6. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  7. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  8. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

  9. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

随机推荐

  1. VxWorks 6.9 内核编程指导之读书笔记 -- VxWorks Kernel application (二)

    #1 内核对象的静态实例化 内核对象的静态实例化 任务的静态实例化 VX_TASK宏用来在编译时声明一个任务对象.该宏带有2个参数:任务名和栈大小.不像taskSpawn函数,任务名称可以是NULL. ...

  2. ./configure:command not found 解决方法

    有些下载下来的源码没有MAKEFILE文件,但是会有MAKEFILE.IN 和 configure, MAKEFILE文件则由后两个文件生成. 如果执行: $./configure   提示错误:./ ...

  3. linux 环境变量的设置【转】

    现在使用linux的朋友越来越多了,在linux下做开发首先就是需要配置环境变量,下面以java环境变量为例介绍三种配置环境变量的方法. 1.修改/etc/profile文件 如果你的计算机仅仅作为开 ...

  4. JAVA:三种集合LIST、SET、MAP

    1. 集合框架介绍 我 们知道,计算机的优势在于处理大量的数据,在编程开发中,为处理大量的数据,必须具备相应的存储结构,数组可以用来存储并处理大量类型相同的数 据,但是会发现数组在应用中的限制:数组长 ...

  5. opencv 手写选择题阅卷 (二)字符识别

    opencv 手写选择题阅卷 (二)字符识别 选择题基本上只需要识别ABCD和空五个内容,理论上应该识别率比较高的,识别代码参考了网上搜索的代码,因为参考的网址比较多,现在也弄不清是参考何处的代码了, ...

  6. ubuntu OPENCV移植

    Installing OpenCV 2.4.1 in Ubuntu 12.04 LTS 这是转载国外一篇文章  移植PC上的OPENCV  http://www.samontab.com/web/20 ...

  7. MotionEvent中getX()和getRawX()的区别

    http://blog.csdn.net/ztp800201/article/details/17218067 public class Res extends Activity implements ...

  8. WinFrom下连接字符串的数据库文件路径问题

    一直以为连接字符串中的系统变量|DataDirectory|就是在ASP.NET中代替App_Data的绝对路径.原来在WinForm程序中也能用|DataDirectory|,不过指代的是exe文件 ...

  9. 通过 Javacore 了解线程运行状况

    Javacore 是一个当前 JVM 运行状态的快照.通过对Javacore 的分析,可以了解在 JVM 中运行的应用程序的当前状态,比如是否“卡”在某一点上,或在某些代码上运行时间太长. Javac ...

  10. SQLserver关于教学楼中教室在某个时间段是否被占用的数据库设计

    需求:关于教学楼中教室在周几某个时间段是否被占用的数据库设计 具体代码如下: -------------------教学楼维度 CREATE TABLE TeacBuild ( tbID int no ...