跳到正文

个人记录

蒟蒻的高精模板(一坨屎山)

用户 815613约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟

版本

  1. v1当前
C++
/*------------------------------------------------------------------------------------------
    高精模板,仅支持正整数运算
                                                    --by Yoru ya seikaii kawaii
------------------------------------------------------------------------------------------*/
#pragma once
#include <bits/stdc++.h>
#define MAXN 10086

struct BigInt
{
    int a[MAXN] = { 0 };
    BigInt(int x = 0)
    {
        for(int i = 0; x; i++)
        {
            a[i] = x % 10;
            x /= 10;
        }
    }

    int &operator[] (int i)
    {
        return a[i];
    }

    void flatten(int L)
    {
        for(int i = 0; i < L; i++)
        {
            a[i + 1] += a[i] / 10;
            a[i] %= 10;
        }
    }

    void print()
    {
        int i;
        for(i = MAXN - 1; i >= 1; i--)
            if(a[i] != 0 && a[i] != -1)
                break;
        if(a[i + 1] == -1)
            putchar('-');
        for(; i >= 0; i--)
            putchar(a[i] + '0');
        putchar('\n');
    }

    void read()
    {
        int x;
        std::cin >> x;
        for(int i = 0; x; i++)
        {
            a[i] = x % 10;
            x /= 10;
        }
    }

    void clear()
    {
        for(int i = 0; i < MAXN; i++)
            a[i] = 0;
    }

    int lenth()
    {
        int len;
        for(len = MAXN - 1; len >= 1; len--)
            if(a[len] != 0 && a[len] != -1)
                break;
        return len + 1;
    }
};

inline BigInt sub(BigInt a, BigInt b)
{
    BigInt c;
    int l = std::max(a.lenth(), b.lenth());
    for(int i = 0; i < l; i++)
    {
        c[i] += a[i] - b[i];
        if(c[i] < 0)
        {
            c[i] += 10;
            a[i + 1] -=1;
        }
    }
    return c;
}

inline bool operator<(BigInt a, BigInt b)
{
    if(a.lenth() < b.lenth())
        return true;
    else if(a.lenth() == b.lenth())
    {
        for(int i = a.lenth() - 1; i >= 0; i--)
            if(a[i] > b[i])
                return false;
        return true;
    }
    return false;
}

inline bool operator>(BigInt a, BigInt b)
{
    if(a.lenth() > b.lenth())
        return true;
    else if(a.lenth() == b.lenth())
    {
        for(int i = a.lenth() - 1; i >= 0; i--)
            if(a[i] < b[i])
                return false;
        return true;
    }
    return false;
}

inline bool operator==(BigInt a, BigInt b)
{
    if(a.lenth() == b.lenth())
    {
        for(int i = a.lenth() - 1; i >= 0; i--)
            if(a[i] != b[i])
                return false;
        return true;
    }
    return false;
}

inline bool operator!=(BigInt a, BigInt b)
{
    return !(a == b);
}

inline bool operator>=(BigInt a, BigInt b)
{
    return !(a < b);
}

inline bool operator<=(BigInt a, BigInt b)
{
    return !(a > b);
}

inline BigInt max(BigInt a, BigInt b)
{
    return (a < b) ? b : a;
}

inline BigInt min(BigInt a, BigInt b)
{
    return (a > b) ? b : a;
}

//高精加高精
inline BigInt operator+(BigInt a, BigInt b)
{
    BigInt c;
    int l = std::max(a.lenth(), b.lenth());
    for(int i = 0; i <= MAXN - l; i++)
    {
        c[i] += a[i] + b[i];
        if(c[i] >= 10)
        {
            c[i + 1] += 1;
            c[i] = c[i] - 10;
        }
    }
    return c;
}

//高精乘高精
inline BigInt operator*(BigInt a, BigInt b)
{
    BigInt c;
    int l = a.lenth() + b.lenth() + 1;
    for(int i  = 0; i <= l; i++)
    {
        for(int j = 0; j <= i; j++)
            c[i] += a[j] * b[i - j];
        if(c[i] >= 10)
        {
            c[i + 1] += c[i] / 10;
            c[i] %= 10;
        }
    }
    return c;
}

//高精除低精(下取整)
inline BigInt operator/(BigInt a, int b)
{
    BigInt c;
    int x = 0;
    for(int i = a.lenth(); i >= 0; i--)
    {
        x = 10 * x + a[i];
        c[i] = x / b;
        x %= b;
    }
    return c;
}

//高精减高精(仅实现正整数相减)
inline BigInt operator-(BigInt a, BigInt b)
{
    BigInt c;
    if(a >= b)
        c = sub(a, b);
    else
    {
        c = sub(b, a);
        c[c.lenth()] = -1;
    }
    return c;
}
原文