POJ 2084
第一题组合数学题。可以使用递推,设1与其他各数分别连边,假设N=3;若1-4,则圆分成两部分计数,此时可以利用乘法原理。(高精度)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string> using namespace std; const int maxn = 200;
struct bign {
int len, s[maxn]; bign() {
memset(s, 0, sizeof(s));
len = 1;
} bign(int num) {
*this = num;
} bign(const char* num) {
*this = num;
} bign operator =(int num) { //直接以整数赋值
char s[maxn];
sprintf(s, "%d", num);
*this = s;
return *this;
} bign operator =(const char* num) { //以字符串赋值
len = strlen(num);
for(int i = 0; i < len; i++)
s[i] = num[len - i - 1] - '0';
return *this;
} string str() const { //将bign转化成字符串
string res = "";
for(int i = 0; i < len; i++)
res = (char) (s[i] + '0') + res;
if(res == "")
res = "0";
return res;
} bign operator +(const bign& b) const { //重载+号运算
bign c;
c.len = 0;
for(int i = 0, g = 0; g || i < max(len, b.len); i++) {
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % 10;
g = x / 10;
}
return c;
} void clean() { //去掉前到0
while(len > 1 && !s[len - 1])
len--;
} bign operator *(const bign& b) { //重载*号运算
bign c;
c.len = len + b.len;
for(int i = 0; i < len; i++)
for(int j = 0; j < b.len; j++)
c.s[i + j] += s[i] * b.s[j];
for(int i = 0; i < c.len - 1; i++) {
c.s[i + 1] += c.s[i] / 10;
c.s[i] %= 10;
}
c.clean();
return c;
} bign operator -(const bign& b) { //重载-号运算
bign c;
c.len = 0;
for(int i = 0, g = 0; i < len; i++) {
int x = s[i] - g;
if(i < b.len)
x -= b.s[i];
if(x >= 0)
g = 0;
else {
g = 1;
x += 10;
}
c.s[c.len++] = x;
}
c.clean();
return c;
} bool operator <(const bign& b) const { //重载<号运算
if(len != b.len)
return len < b.len;
for(int i = len - 1; i >= 0; i--)
if(s[i] != b.s[i])
return s[i] < b.s[i];
return false;
} bool operator >(const bign& b) const { //重载>号运算
return b < *this;
} bool operator <=(const bign& b) { //重载<=号运算
return !(b > *this);
} bool operator ==(const bign& b) { //重载>=号运算
return !(b < *this) && !(*this < b);
} bign operator +=(const bign& b) { //重载+=号运算
*this = *this + b;
return *this;
}
}; istream& operator >>(istream &in, bign& x) { //重载输入运算符
string s;
in >> s;
x = s.c_str();
return in;
} ostream& operator <<(ostream &out, const bign& x) { //重载输出运算符
out << x.str();
return out;
} bign ans[210]; void initial(){
ans[2]=1;
ans[4]=2;
for(int i=5;i<=200;i++){
ans[i]=ans[i-2]+ans[i-2];
for(int j=3;j<i;j++){
ans[i]=ans[i]+ans[j-2]*ans[i-j];
}
}
} int main(){
initial();
int n;
while(scanf("%d",&n),n!=-1){
cout<<ans[2*n]<<endl;
}
return 0;
}
POJ 2084的更多相关文章
- POJ 2084 Catalan数+高精度
POJ 2084 /**************************************** * author : Grant Yuan * time : 2014/10/19 15:42 * ...
- POJ 2084 Game of Connections
卡特兰数. #include<stdio.h> #include<string.h> ; ; void mul(__int64 a[],int len,int b) { int ...
- (组合数学3.1.2.2)POJ 2084 Game of Connections(卡特兰数公示的实现)
package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class POJ_2084 ...
- POJ 2084 Game of Connections(卡特兰数)
卡特兰数源于组合数学,ACM中比较具体的使用例子有,1括号匹配的种数.2在栈中的自然数出栈的种数.3求多边形内三角形的个数.4,n个数围城圆圈,找不相交线段的个数.5给定n个数,求组成二叉树的种数…… ...
- POJ 2084 Catalan
Game of Connections Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8772 Accepted: 43 ...
- poj——2084 Game of Connections
Game of Connections Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8664 Accepted: 42 ...
- POJ 2084 Game of Connections 卡特兰数
看了下大牛们的,原来这题是卡特兰数,顺便练练java.递归式子:h(0)=1,h(1)=1 h(n)= h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) ( ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
随机推荐
- HDU 4228
很明显可以转化为反素数的题目.由于有n种不同的方式,所以,数的约数可以为2*n或者2*n-1 #include <iostream> #include <cstdio> #in ...
- hashmap的put方法源码分析
put主源码如下: public V put(K key, V value) { if (key == null) return putForNullKey(value); int hash = ha ...
- win7-32虚拟机安装
前置条件:安装好VMware-workstation 一.本人本机win7—32位 准备win7_32位镜像文件GSP1RMCULFRER_CN_DVD.iso 新建一个文件夹,将它保存在我们的新建文 ...
- [Swift]二进制、八进制、十进制、十六进制之间的转换
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- ubuntu下无法将iNode绑定到侧边栏的解决办法
title: ubuntu下无法将iNode绑定到侧边栏的解决办法 toc: false date: 2018-09-01 17:43:52 categories: methods tags: ubu ...
- ROS-SLAM仿真-hector
前言:hector_slam可以很好的在空中机器人,手持构图设备及特种机器人中运行. hector_slam不需要订阅里程计信息/odmo消息,而是直接使用激光估算里程计信息,因此,当机器人速度较快时 ...
- 新手配置vux
1.首先跟平常一样创建一个vue的项目 2.开始配置vux 第一步 安装vux npm install vux --save 第二步 安装vux-loader npm install vux-loa ...
- UWP tips (与wp8.1的不同)
一.异步调用之后,要更新UI时,代码如下 await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&g ...
- javascript动画函数封装(升级版)
//把 任意对象 的 任意数值属性 改变为 任意的目标值 function animate(obj, json, fn) { clearInterval(obj.timer); obj.timer = ...
- Maintain Aspect Ratio Mixin
Maintain Aspect Ratio Mixin Maintain the aspect ratio of a div with CSS RESPONSIVE ASPECT RATIOS WIT ...