POJ2084—Game of Connections(c++高精度)

POJ 2084 连接游戏 (卡特兰数) POJ 2084 连接游戏 (卡特兰数) 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 circl... 阅读详情

Game of Connections

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,然后从1到2n这么2n个数围成一圈,每两个数连成一条线即可,但是要求线不能相交。


思路:

  • 一开始没看出来是卡特兰数。是这样想的,我从1开始连,那么1有多少种连法呢。1能连2,1能连4,1能连6…..1不能连3因为连3后2就是一个单个数字那么肯定有线要穿过1与3的连线,以此类推。

  • 那么比如我们有6个数,1连到2以后,剩下数全部在连线的一侧,可以看做不管1和2了,是新的4个数来连线,再比如1连4,那么就分成两部分,一侧是2和3两个数,另一侧是5和6两个数,那么很容易就能想到递推式。f(n) = f(j) * f(i-j-1) ; 1<=n<=100,0<=j<=i-1;

  • 同时还有一个问题就是高精度,这个题到后面的时候,数字很大,用大数模板。


AC代码



    #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 a[103];

    int main()
    {
        a[0] = 1;
        a[1] = 1;
        a[2] = 2;
        a[3] = 5;

        for(int i = 4;i<=100;i++)
        {   
            for(int j = 0;j < i; j++)
            {
                a[i] += a[j] * a[i-1-j];
            }
        }

        int n;
        while(scanf("%d",&n)!=EOF && n != -1)
        {
            cout<<a[n]<<endl;
        } 
        return 0;
    }
基于图神经网络的社交网络虚假账号检测系统 (Graph-based Fake Account Detection System, GFADS) FOLLOWSRETWEETSSHAREDCOMMENTSLIKESMENTIONSUseruser_idusernamejoin_datebioavatar_urllocationverifiedis_fakeLOAD CSV这是决定模型性能的关键环节,需结合和。XXedge_indexy新账号缺乏历史数据和丰富关系。全网图可能包含数亿节点和数十亿边,难以直接训练。社交网络是动态变化的,虚假账号策略也在不断进化。获取大规模、高质量的真实/虚假账号标注数据非常困难。 阅读详情

相关推荐

基于LightGBM和多模型融合的量化交易研究:沪深300为例【附代码】

📊 数据科学与大数据专业 | 数据分析与模型构建 | 数据驱动决策✨ 专业领域:数据挖掘与清洗大数据处理与存储技术机器学习与深度学习模型数据可视化与报告生成分布式计算与云计算数据安全与隐私保护💡 擅长工具:Python/R/Matlab 数据分析与建模Hadoop/Spark 大数据处理平台SQL数据库管理与优化Tableau/Power BI 数据可视化工具TensorFlow/PyTorch 深度学习框架✅数据科学与大数据技术毕业论文【数据】

lagougongzuoshi的博客 1669

渣渣补题——学习卡特兰数(别看了,没代码)

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 li...

weixin_44536116的博客 481

Ubuntu下安装Hive3.1.2教程(附MySQL安装方法及安装包)

文章目录前言一、安装Hive1. 下载压缩包并解压2. 安装Hive3. 配置环境变量4. 修改配置文件二、MySQL安装与配置1. 安装MySQL2. 安装MySQL jdbc包3. 为Hive创建MySQL账号三、验证Hive安装及错误处理1. 启动Hadoop2. 启动hive3. 运行Hive实例总结 前言 Hive 的安装需要建立在 Hadoop 的之上(类似于Hbase),关于 Hadoop 的单机安装以及伪分布式安装可以参考作者的另两篇博客: Ubuntu下单机安装Hadoop详细教程(附

程序必须为人类阅读而编写,并且仅仅碰巧可以让机器执行 1万+

poj2084卡特兰数(大数,java)

题目链接:http://poj.org/problem?id=2084 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 c...

mumei的博客 473

汽车LED驱动芯片TPS92682-Q1 SPI接口配置与调试实战指南

SPI(串行外设接口)作为一种高速、全双工的同步串行通信总线,广泛应用于微控制器与外围设备之间的数据交换。其工作原理基于主从架构,通过时钟、数据线和片选信号实现设备间的精准时序控制。在汽车电子和工业控制领域,SPI因其简单高效、易于扩展的特性,成为配置复杂电源管理芯片、传感器和存储器的关键技术。通过SPI接口,工程师能够动态调整设备参数、实时读取状态信息,并实现复杂的故障诊断与保护策略,极大地提升了系统的灵活性与可靠性。本文以汽车级双通道LED恒流驱动器TPS92682-Q1为例,深入解析如何通过SPI接口

weixin_29185493的博客 288

Jetson AGX Orin 新手避坑:解决‘找不到nvidia-jetpack包’的完整修复指南

本文详细介绍了Jetson AGX Orin设备在安装JetPack开发套件时遇到的'nvidia-jetpack包缺失'问题的完整解决方案。通过分析问题根源、提供分步修复指南以及系统优化建议,帮助开发者快速解决这一常见问题,充分发挥Jetson AGX Orin的边缘计算性能。

weixin_42520909的博客 460

HDU1134-Game of Connections

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 li...

weixin_43357583的博客 298

TIM 2024 | AD-YOLO:一种结合 Swin Transformer 和注意力机制的实时 YOLO 网络用于机场场景检测

实时获取机场场景信息对于保障机场安全和提升机场运营效率至关重要。然而,机场场景图像中的人员和车辆目标尺寸较小,而且公开的机场数据不足,这使得机场物体检测成为一项颇具挑战的任务,难以同时实现高精度和实时检测。本文提出了一种全新的机场物体检测方法——机场检测器YOLO(AD - YOLO),它融合了改进的你只看一次(YOLO)算法、Swin Transformer和注意力机制的优势,以应对这一挑战。

小白学视觉 315

用JAVA 实现卡特兰数

卡特兰数详解 JAVA 实现大数 详解   例1: B - Game of Connections 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 gr...

JKdd123456的博客 1742

RHEL 8 与 RHEL 7 与 RHEL 6 的比较,RHEL 8强到离谱!

Red Hat Enterprise Linux(RHEL)是企业级 Linux 发行版中最受欢迎的操作系统之一。RHEL 为服务器和企业环境提供了高性能、稳定性和安全性,适用于各种数据中心和云计算平台。自 RHEL 6 以来,Red Hat 对其操作系统进行了多次重大更新,RHEL 7 和 RHEL 8 带来了许多新的特性、工具和架构变化。

网络技术联盟站 707

133.在 Vue3 中使用 OpenLayers 实现画多边形、任意编辑、遮罩与剪切处理功能

《Vue3+OpenLayers实现多边形绘制与区域处理》摘要:本文介绍如何基于Vue3+OpenLayers+ol-ext实现地图多边形交互功能。核心功能包括:鼠标绘制任意多边形、拖动顶点编辑边界、遮罩显示(非选区变红)、选区剪切显示及功能重置。技术方案采用OpenLayers处理地图渲染,ol-ext实现高级遮罩/剪切效果,ElementPlus提供UI控件。该方案适用于城市规划、园区展示等需要区域标记的场景,完整代码展示了从初始化、绘制、编辑到特效处理的全流程实现。

小彭的博客 958

poj 2084 Game of Connections

题意:2*n个点(一个圆周上的)连线,要求每个点必须与且仅与一个点相连,问有多少种方法。 思路: 对于2*n个点,任意选择其中一个点,这个点能和与它相距偶数个点的点相连,而相连的这条线会把整个图形分成两个子问题,即得到递推公式: f[n] = f[0]*f[n-1] + f[1]*f[n-2] + f[2]*f[n-3] + ... + f[n-1]*f[0]。 注意高精度。 据说这是卡

jasison 1404

FAA和EASA在机载电子硬件适航中的发展历史与差异

FAA和EASA在机载电子硬件的适航审查中虽然存在一些差异,但通过近年来的合作,这些差异正在逐步缩小。两者通过。

qq_41854911的博客 745

Poj 2084 Game of Connections 卡特兰数高精度

Poj 2084 Game of Connections 卡特兰数高精度 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 c...

weixin_30834783的博客 164

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Poj 2084 Game of Connections(高精度卡特兰数)

Game of Connections Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7610   Accepted: 3847 Description This is a small but ancient game. You are supposed to

u013082590的专栏 716

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Game of Connections(POJ-2084)

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...

Alex_McAvoy的博客 1119

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

POJ 2084 Game of Connections

/*高精度的Catalan数题目 公式: h ( n ) = h ( n - 1 ) * ( 4 * n - 2 ) / n + 1 */ Source Code Problem: 2084   User: imutzcy Memory: 296K   Time: 0MS Language: C++   Result: Accepted

imut zcy,I promise You. 394

POJ 2084 Game of Connections(卡特兰数)

  卡特兰数源于组合数学,ACM中比较具体的使用例子有,1括号匹配的种数。2在栈中的自然数出栈的种数。3求多边形内三角形的个数。4,n个数围城圆圈,找不相交线段的个数。5给定n个数,求组成二叉树的种数……   此题就是第4个样例,是裸卡特兰数,但是这里牵扯的大数,可以使用java的大数类解决,但是我这里使用高精度乘法和除法模拟的(主要是java不会)。   此处的递推式为H【1】 = 1;H【...

weixin_30426065的博客 145

Game of Connections(大数java+Catalan数)

Link:http://poj.org/problem?id=2084 Problem:

林下的码路 951

【POJ】2084Game of Connections(大数的卡特兰数处理)

Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9193   Accepted: 4506 Description This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, . ...

红点雷龙XL 381

hdu1134 Game of Connections 卡特兰数(大数)

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

ccsu_cherish 805

求连线数问题 Game of connections

<br />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.

maybeevil的专栏 521

poj 2084 卡特兰数

poj 2084卡特兰数,比较裸,但是数字比较大需要用到大数import java.math.BigInteger; import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner cin=new Scanner(System.in);

qq_33688997的博客 441

英语题目作业(24)

原题: Game of Connections 1000ms32768K 描述: 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 seg...

m0_65788482的博客 1901

POJ 2084 (Catalan数)

卡特兰数三大公式 C(n)=C(n-1)*(4*n-2)/(n+1) C(n)=C(2n-1,n+1)-C(2n-1,n-1) C(n)=C(2n,n)/(n+1) Program p2084; Const F=10000; Type arr=record a:array[1..10000] of longint; len:longint; ...

爱.NET 198

POJ 2084 大数 + DP

题意 传送门 POJ 2084 题解 若满足各点对连线且不交叉,那么对于某条连线,其上方与下方的点数为偶数,那么枚举某点的连线即得到递推公式。设 dp[i]dp[i]dp[i] 代表点对数为 iii 的连线总数,则有 dp[i]=∑j=0i−1dp[j]×dp[i−1−j]dp[i]=\sum\limits_{j=0}^{i-1}dp[j]\times dp[i-1-j]dp[i]=j=0∑i−1​dp[j]×dp[i−1−j] #include <algorithm> #include &l

neweryyy的博客 229
上一篇: 对抗女巫的魔法碎片(贪心,排序)
下一篇: struts2的值栈的初步认识
WaveBridge
博客等级 码龄10年 8粉丝 26原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值