跳到正文

个人记录

高精度模板

Mazen约 19 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>

using namespace std;

class Bigtemp {

public: // 数据
vector<int> arr; int sign;
static const int BASE = 1e8;
static const int WIDTH = 8;
static constexpr double PI = acos(-1);
public: // 初始化 与 基本函数
Bigtemp(long long temp = 0) { *this = temp; } Bigtemp(string s) { *this = s; } Bigtemp(const Bigtemp &B) { *this = B; }
bool is_zero(vector<int> g) { return g.empty() || (g.size() == 1 && !g[0]); }
void clear() { while (arr.size() > 1 && arr.back() == 0) arr.pop_back(); if (is_zero(arr)) sign = 1; }
int size() {
    int t = arr[arr.size() - 1], cnt = 0;
    while (t) cnt ++ , t /= 10;
    return WIDTH * (arr.size() - 1) + cnt;
}
string to_str() {
    string s, p;
    if (is_zero(arr)) { return "0"; }
    for (int i = arr.size() - 1, x; i >= 0; i -- )
    {
        p.clear(); x = arr[i];
        for (int j = 0; x || (i != arr.size() - 1 && j < WIDTH); j ++ )
            p = char('0' + x % 10) + p, x /= 10;
        s += p;
    }
    return s;
}
public: // 比较运算符
bool operator< (Bigtemp B) { if (sign != B.sign) return sign > B.sign; if(arr.size() != B.arr.size()) return (!sign) ^ (arr.size() < B.arr.size()); for (int i = arr.size() - 1; i >= 0; i -- ) if(arr[i] != B.arr[i]) return (!sign) ^ (arr[i] < B.arr[i]); return false; }
bool operator> (Bigtemp B) { return B < *this; }
bool operator<= (Bigtemp B) { return !(*this > B); }
bool operator>= (Bigtemp B) { return !(*this < B); }
bool operator!= (Bigtemp B) { return *this > B || *this < B; }
bool operator== (Bigtemp B) { return !(*this != B); }
public: // 单目运算符
Bigtemp operator- () { Bigtemp C = *this; C.sign = (C == 0 ? 1 : !C.sign); return C; }
int operator[] (int id)
{
    int d = id / WIDTH, res = id % WIDTH, x = arr[d];
    if (!res) return arr[d - 1] / WIDTH;
    while ( -- res) x /= 10;
    return x % 10;
}

bool operator! () { return *this == 0; }
public: // 四则运算 与 赋值

// 赋值
Bigtemp operator= (long long x)
{
    sign = (x >= 0);
    x = (x < 0 ? -x : x);
    arr.clear();
    do {
        arr.push_back(x % BASE);
        x /= BASE;
    } while (x);
    clear();
    return *this;
}

Bigtemp operator= (string str)
{
    if(str.size() == 0) return *this;

    arr.clear(), sign = 1;
    int res = 0, len = (str.size() + WIDTH - 1) / WIDTH, start, end;
    for (int i = 0; i < len; i ++ )
    {
        end = str.size() - i * WIDTH - 1;
        start = max(0, end - WIDTH + 1);
        if(str[start] == '-') start ++ , sign = 0;
        res = 0;
        while (start <= end) res = res * 10 + str[start ++ ] - '0';
        arr.push_back(res);
    }
    clear();
    return *this;
}

// 加法
Bigtemp operator+ (Bigtemp B)
{
    if (sign ^ B.sign)
        return sign == 1 ? *this - (-B) : B - (-*this);

    Bigtemp C;
    C.arr.clear(), C.sign = sign;
    for (int i = 0, res = 0; ; i ++ )
    {
        if(res == 0 && i >= arr.size() && i >= B.arr.size()) break;
        if(i < arr.size()) res += arr[i];
        if(i < B.arr.size()) res += B.arr[i];
        C.arr.push_back(res % BASE);
        res /= BASE;
    }
    C.clear();
    return C;
}

Bigtemp& operator+= (Bigtemp B) { *this = *this + B; return *this; }
Bigtemp& operator+= (string s) { Bigtemp B = s; *this += B; return *this; }
Bigtemp operator++ () { *this += 1; return *this; }
Bigtemp operator++ (int) { *this += 1; return *this; }

// 减法
Bigtemp operator- (Bigtemp B)
{
    if (sign ^ B.sign)
        return sign == 1 ? *this + (-B) : -((-*this) + B);
    if (!sign && !B.sign)
        if (*this > B) return ((-B) - (-*this));
        else return (-((-*this) - (-B)));
    if (B > *this) return (-(B - *this));

    Bigtemp C;
    C.arr.clear();
    for (int i = 0, t = 0; i < arr.size(); i ++ )
    {
        t = arr[i] - t;
        if(i < B.arr.size()) t -= B.arr[i];
        C.arr.push_back((t + BASE) % BASE);
        if(t < 0) t = 1;
        else t = 0;
    }
    C.clear();
    return C;
}

Bigtemp& operator-= (Bigtemp B) { *this = *this - B; return *this; }
Bigtemp& operator-= (string s) { Bigtemp B = s; *this -= B; return *this; }
Bigtemp operator-- () { *this -= 1; return *this; }
Bigtemp operator-- (int) { *this -= 1; return *this; }

// 乘法
Bigtemp operator* (long long x)
{
    Bigtemp C;
    C.arr.clear();
    for (int i = 0; i <= arr.size() + 1; i ++ ) C.arr.push_back(0);
    for (int i = 0; i < arr.size(); i ++ )
    {
        C.arr[i] += x * arr[i] % BASE;
        C.arr[i + 1] += x * arr[i] / BASE;
    }
    for (int i = 0; i <= arr.size() + 1; i ++ )
    {
        C.arr[i + 1] += C.arr[i] / BASE;
        C.arr[i] %= BASE;
    }
    C.clear();
    C.sign = (x >= 0 ? 1 : -1) * (sign ? 1 : -1);
    return C;
}

friend Bigtemp operator* (Bigtemp A, Bigtemp B)
{
    struct Complex {
        double x, y;
        Complex operator+ (const Complex& t) const
        {
            return {x + t.x, y + t.y};
        }
        Complex operator- (const Complex& t) const
        {
            return {x - t.x, y - t.y};
        }
        Complex operator* (const Complex& t) const
        {
            return {x * t.x - y * t.y, x * t.y + y * t.x};
        }
    };
    string s1 = A.to_str(), s2 = B.to_str();
    int n = s1.size() - 1, m = s2.size() - 1, bit = 0, tot;
    while ((1 << bit) < n + m + 1) bit ++ ;
    tot = 1 << bit;
    vector<Complex> Complex_A(tot), Complex_B(tot);
    for (int i = 0; i <= n; i ++ ) Complex_A[i].x = s1[n - i] - '0';
    for (int i = 0; i <= m; i ++ ) Complex_B[i].x = s2[m - i] - '0';
    vector<int> rev(tot);
    for (int i = 0; i < tot; i ++ )
        rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));

    function<void(vector<Complex>&, vector<int>&, int, int, int)> fft = []
            (vector<Complex> &A, vector<int> &rev, int inv, int bit, int tot)
    {
        for (int i = 0; i < tot; i ++ )
            if (i < rev[i]) swap(A[i], A[rev[i]]);
        for (int mid = 1; mid < tot; mid <<= 1)
        {
            Complex w1 = {cos(PI / mid), inv * sin(PI / mid)};
            for (int i = 0; i < tot; i += mid * 2)
            {
                Complex wk = {1, 0};
                for (int j = 0; j < mid; j ++ , wk = wk * w1)
                {
                    auto x = A[i + j], y = wk * A[i + j + mid];
                    A[i + j] = x + y, A[i + j + mid] = x - y;
                }
            }
        }
    };

    fft(Complex_A, rev, 1, bit, tot), fft(Complex_B, rev, 1, bit, tot);
    for (int i = 0; i < tot; i ++ ) Complex_A[i] = Complex_A[i] * Complex_B[i];
    fft(Complex_A, rev, -1, bit, tot);
    vector<int> res(tot + 10);
    int k = 0;
    for (int i = 0, t = 0; i < tot || t; i ++ )
    {
        t += Complex_A[i].x / tot + 0.5;
        res[k ++ ] = t % 10;
        t /= 10;
    }
    while (k > 1 && !res[k - 1]) k -- ;

    Bigtemp C; C.arr.clear();
    for (int i = 0; i < k; i += WIDTH)
    {
        int t = 0;
        for (int j = min(k - 1, i + WIDTH - 1); j >= i; j -- )
            t = t * 10 + res[j];
        C.arr.push_back(t);
    }
    C.clear();
    C.sign = !(A.sign ^ B.sign);
    return C;
}

Bigtemp& operator*= (long long x) { *this = *this * x; return *this; }
Bigtemp& operator*= (Bigtemp B) { *this = *this * B; return *this; }
Bigtemp& operator*= (string s) { Bigtemp B = s; *this *= B; return *this; }

// 除法
Bigtemp operator/ (int x)
{
    Bigtemp C;
    long long res = 0;
    for (int i = arr.size() - 1; i >= 0; i -- )
    {
        res = res * BASE + arr[i];
        C = C * BASE + res / x;
        res %= x;
    }
    return C;
}

Bigtemp operator/ (Bigtemp B)
{
    if (sign ^ B.sign)
        return sign ? -(*this / (-B)) : -((-*this) / B);
    Bigtemp l = 0, r = *this, mid;

    while (l < r)
    {
        mid = (l + r + 1) / 2;
        if (mid * B > *this) r = mid - 1;
        else l = mid;
    }
    return l;
}

Bigtemp& operator/= (int x) { *this = *this / x; return *this; }
Bigtemp& operator/= (Bigtemp B) { *this = *this / B; return *this; }
Bigtemp& operator/= (string s) { Bigtemp B = s; *this /= B; return *this; }

// 取模
Bigtemp operator% (int x) { return *this - *this / x * x; }
Bigtemp& operator%= (int x) { *this = *this / x; return *this; }
Bigtemp operator% (Bigtemp B) { return *this - *this / B * B; }
Bigtemp& operator%= (Bigtemp B) { *this = *this % B; return *this; }
Bigtemp& operator%= (string s) { Bigtemp B = s; *this %= B; return *this; }

public: // 类型转化
explicit operator long long() { long long res = 0; for (int i = arr.size() - 1; i >= 0; i -- ) res = res * BASE + arr[i]; return res * (sign ? 1 : -1); }
explicit operator int()
{
    int res = 0;
    for (int i = arr.size() - 1; i >= 0; i -- )
        res = res * BASE + arr[i];
    return res * (sign ? 1 : -1);
}

explicit operator string()
{
    string s, p;
    if (is_zero(arr)) { return "0"; }
    if (!sign) s = '-';
    for (int i = arr.size() - 1, x; i >= 0; i -- )
    {
        p.clear(); x = arr[i];
        while (x) p = char('0' + x % 10) + p, x /= 10;
        s += p;
    }
    return s;
}
};

istream& operator >> (istream& in, Bigtemp& x)
{ string s; if(!(in >> s)) return in; x = s; return in; }

ostream& operator << (ostream& out, Bigtemp x)
{ if (!x.sign) out << '-'; out << x.arr.back(); for (int i = x.arr.size() - 2; i >= 0; i -- ) { char buf[20]; sprintf(buf, "%08lld", x.arr[i]); for (int j = 0; j < Bigtemp::WIDTH; j ++ ) out << buf[j]; } return out; }

int main()
{
	return 0;
}
原文