高精度运算合集

· · 个人记录

花了一个下午打的,我太难了QwQ

使用的是重载运算符,代码不长,比某些人只打四个运算符就能打三百行好太多了。

目前只支持正整数运算,可以算出负数(如 a-b )。

每个运算符都写了高精和低精

太高级的运算符写不来,但日常做题绝对够用

主要面向对象:刚学高精的新手们

复杂度挺高的,没压位(FFT,NTT什么的不会),大佬们勿喷

注意:‘ \ll ’指十进制下左移,‘ \gg ’同理

好了上代码吧,请尽情欣赏这个艺术品吧。

2021.9.4 更新string转bigint

#include <bits/stdc++.h>
using namespace std;
struct bigint
{
    int n, a[10010];
    bool f;
    bigint()
    {
        clear();
    }
    void clear()
    {
        n = 1, f = 1;
        memset(a, 0, sizeof(a));
    }
    void read()
    {
        cin >> n;
        for (int i = n; i >= 1; i--)
            cin >> a[i];
    }
    void write()
    {
        if (!f)
            cout << '-';
        for (int i = n; i >= 1; i--)
            cout << a[i];
        cout << endl;
    }
    void operator=(int x)
    {
        clear();
        if (x == 0)
            return;
        if (x < 0)
            x = -x, f = 0;
        n = 0;
        while (x)
        {
            a[++n] = x % 10;
            x /= 10;
        }
    }
    void operator=(string x)
    {
        clear();
        int st = 0;
        if (x[0] == '-')
            f = 0, st++;
        n = 0;
        int len = x.size();
        for (int i = st; i < len; i++)
            a[++n] = x[len - i - 1] - '0';
    }
    void operator=(bigint x)
    {
        clear();
        n = x.n;
        f = x.f;
        for (int i = 1; i <= n; i++)
            a[i] = x.a[i];
    }
    bool operator==(bigint x)
    {
        if (n != x.n)
            return 0;
        for (int i = n; i >= 1; i++)
            if (a[i] != x.a[i])
                return 0;
        return 1;
    }
    bool operator!=(bigint x)
    {
        return !operator==(x);
    }
    bool operator<(bigint x)
    {
        if (n == x.n)
            for (int i = n; i >= 1; i--)
                if (a[i] != x.a[i])
                    return a[i] < x.a[i];
        return n < x.n;
    }
    bool operator>(bigint x)
    {
        if (n == x.n)
            for (int i = n; i >= 1; i--)
                if (a[i] != x.a[i])
                    return a[i] > x.a[i];
        return n > x.n;
    }
    bool operator<=(bigint x)
    {
        return !operator>(x);
    }
    bool operator>=(bigint x)
    {
        return !operator<(x);
    }
    bool operator!()
    {
        if (n != 1)
            return 0;
        return a[1] == 0;
    }
    bool operator==(int x)
    {
        bigint y;
        y = x;
        return operator==(y);
    }
    bool operator!=(int x)
    {
        bigint y;
        y = x;
        return operator!=(y);
    }
    bool operator<(int x)
    {
        bigint y;
        y = x;
        return operator<(y);
    }
    bool operator>(int x)
    {
        bigint y;
        y = x;
        return operator>(y);
    }
    bool operator<=(int x)
    {
        bigint y;
        y = x;
        return operator<=(y);
    }
    bool operator>=(int x)
    {
        bigint y;
        y = x;
        return operator>=(y);
    }
    bigint operator+(bigint x)
    {
        bigint y;
        y.n = max(n, x.n);
        int s = 0;
        for (int i = 1; i <= y.n; i++)
        {
            s += a[i] + x.a[i];
            y.a[i] = s % 10;
            s /= 10;
        }
        while (s)
        {
            y.a[++y.n] += s % 10;
            s /= 10;
        }
        return y;
    }
    bigint operator+(int x)
    {
        bigint y;
        y = x;
        return operator+(y);
    }
    bigint operator-(bigint x)
    {
        bigint y, z = *this;
        if (z < x)
            y.f = 0, swap(z, x);
        y.n = z.n;
        for (int i = 1; i <= y.n; i++)
        {
            if (z.a[i] < x.a[i])
            {
                z.a[i + 1]--;
                z.a[i] += 10;
            }
            y.a[i] = z.a[i] - x.a[i];
        }
        while (y.a[y.n] == 0 && y.n > 1)
            y.n--;
        return y;
    }
    bigint operator-(int x)
    {
        bigint y;
        y = x;
        return operator-(y);
    }
    bigint operator*(bigint x)
    {
        bigint y;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= x.n; j++)
                y.a[i + j - 1] += a[i] * x.a[j];
        y.n = n + x.n;
        for (int i = 1; i < y.n; i++)
            if (y.a[i] >= 10)
            {
                y.a[i + 1] += y.a[i] / 10;
                y.a[i] %= 10;
            }
        while (y.a[y.n] == 0 && y.n > 1)
            y.n--;
        return y;
    }
    bigint operator*(int x)
    {
        bigint y;
        y = x;
        return operator*(y);
    }
    bigint operator/(bigint x)
    {
        bigint y, z = *this;
        if (x == y)
            return x;
        y.n = z.n - x.n + 1;
        for (int i = y.n; i >= 1; i--)
        {
            bigint t = x << (i - 1);
            while (z >= t)
            {
                y.a[i]++;
                z -= t;
            }
        }
        while (y.a[y.n] == 0 && y.n > 1)
            y.n--;
        return y;
    }
    bigint operator/(int x)
    {
        bigint y;
        y = x;
        return operator/(y);
    }
    bigint operator%(bigint x)
    {
        bigint z = *this;
        return z - z / x * x;
    }
    bigint operator%(int x)
    {
        bigint y;
        y = x;
        return operator%(y);
    }
    bigint operator<<(int l)
    {
        bigint x;
        for (int i = 1; i <= n; i++)
            x.a[i + l] = a[i];
        x.n = n + l;
        return x;
    }
    bigint operator>>(int l)
    {
        bigint x;
        x.n = n - l;
        for (int i = 1; i <= x.n; i++)
            x.a[i] = a[i + l];
        return x;
    }
    void operator+=(bigint x)
    {
        bigint z = *this;
        *this = z + x;
    }
    void operator-=(bigint x)
    {
        bigint z = *this;
        *this = z - x;
    }
    void operator*=(bigint x)
    {
        bigint z = *this;
        *this = z * x;
    }
    void operator/=(bigint x)
    {
        bigint z = *this;
        *this = z / x;
    }
    void operator%=(bigint x)
    {
        bigint z = *this;
        *this = z % x;
    }
    void operator+=(int x)
    {
        bigint z = *this;
        *this = z + x;
    }
    void operator-=(int x)
    {
        bigint z = *this;
        *this = z - x;
    }
    void operator*=(int x)
    {
        bigint z = *this;
        *this = z * x;
    }
    void operator/=(int x)
    {
        bigint z = *this;
        *this = z / x;
    }
    void operator%=(int x)
    {
        bigint z = *this;
        *this = z % x;
    }
};

喜欢的话,点个赞吧OwO