Game of Connections

Game of Connections

这个题也是直接求卡特兰数,不过数据有一点大,不能够直接求,因为杭电不能交 python 不然我就用 python 写了....
对于这么大的数据,还不能写 python 就只能用高精度

代码:

// Created by CAD on 2019/8/15.
#include <bits/stdc++.h>
#define mst(name, value) memset(name,value,sizeof(name))
using namespace std;
using ll=long long;
struct BigInt {
    const static int mod=1e4;
    const static int blo=4;
    int a[600], len;
    BigInt() { mst(a, 0), len=1; }
    BigInt(int v)                              //用int初始化
    {
        mst(a, 0);
        len=0;
        do
        {
            a[len++]=v%mod;
            v/=mod;
        } while (v);
    }
    BigInt operator+(const BigInt &b) const     //重载加法运算符
    {
        BigInt res;
        res.len=max(len, b.len);
        for (int i=0; i<=res.len; ++i)
            res.a[i]=0;
        for (int i=0; i<res.len; ++i)
            res.a[i]+=((i<len) ? a[i] : 0)+((i<b.len) ? b.a[i] : 0),
            res.a[i+1]+=res.a[i]/mod, res.a[i]%=mod;
        if (res.a[res.len]>0) res.len++;
        return res;
    }
    BigInt operator*(const BigInt &b) const      //重载乘法运算符
    {
        BigInt res;
        for (int i=0; i<len; ++i)
        {
            int up=0;
            for (int j=0; j<b.len; ++j)
            {
                int temp=a[i]*b.a[j]+res.a[i+j]+up;
                res.a[i+j]=temp%mod, up=temp/mod;
            }
            if (up!=0) res.a[i+b.len]=up;
        }
        res.len=len+b.len;
        while (res.a[res.len-1]==0 && res.len>1) res.len--;
        return res;
    }
    void output()                                  //输出
    {
        printf("%d", a[len-1]);
        for (int i=len-2; i>=0; --i)
            printf("%04d", a[i]);
        puts("");
    }
};
BigInt f[105];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    f[0]=f[1]=BigInt(1);
    for (int i=2; i<=100; ++i)
    {
        for (int j=0; j<=i-1; ++j)
            f[i]=f[i]+f[j]*f[i-j-1];
    }
    int n;
    while (cin >> n && n!=-1)
        f[n].output();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/CADCADCAD/p/11361180.html