跳到正文

剪贴板

剪贴板 de2kwzra

xuziheng0905
原文
剪贴板信息

剪贴板信息

发布
收录
更新

大整型(bigint)

版本:1.0

先看前情提要,再看这个

C++
#ifndef BIGINT_H  // bigint.h 1.0
#define BIGINT_H

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>

// 最大位数限制,可根据需要调整
#define MAX_SIZE 1000

struct bigint {
    int f;              // 符号位: 1 为正, -1 为负
    int l;              // 当前有效数字长度
    int s[MAX_SIZE + 5]; // 数位数组,s[1]为个位

    // ==================== 基础工具函数 ====================

    inline void to_empty() {
        memset(s, 0, sizeof(s));
        f = l = 1;
    }

    inline void push(long long x = 0) {
        to_empty();
        while (x) {
            s[l++] = x % 10;
            x /= 10;
        }
    }

    inline void push(std::string st = "0") {
        to_empty();
        l = st.size();
        for (int i = 1; i <= l; i++) {
            s[i] = st[l - i] - '0';
        }
    }

    bigint(long long x = 0) { push(x); }

    inline int top() { return s[l]; }
    inline int size() { return l; }

    inline void resize() {
        l = MAX_SIZE;
        while (!s[l] && l > 1) l--;
    }

    inline bool empty() { return l < 2 && !s[1]; }

    inline void get() {
        std::string st;
        std::cin >> st;
        to_empty();
        if (st[0] == '-') {
            f = -1;
            st.erase(0, 1);
        }
        push(st);
    }

    inline void put() {
        if (f == -1) putchar('-');
        for (int i = l; i; i--) printf("%d", s[i]);
        putchar('\n');
    }
};

// ==================== 比较运算符 (bigint vs bigint) ====================

inline bool operator==(bigint a, bigint b) {
    if (a.f != b.f || a.l != b.l) return false;
    for (int i = a.l; i; i--)
        if (a.s[i] != b.s[i]) return false;
    return true;
}

inline bool operator<(bigint a, bigint b) {
    if (a.f != b.f) return a.f < b.f;
    if (a.l != b.l) return a.f > 0 ? a.l < b.l : a.l > b.l;
    for (int i = a.l; i; i--)
        if (a.s[i] != b.s[i]) return a.s[i] < b.s[i];
    return false;
}

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

// ==================== 比较运算符 (bigint vs long long) ====================

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

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

// ==================== 四则运算 ====================

inline bigint operator+(bigint a, bigint b) {
    bigint c, f;
    // 正 + 正
    if (a.f == 1 && b.f == 1) {
        for (int i = 1; i <= MAX_SIZE; i++) {
            c.s[i] += a.s[i] + b.s[i];
            c.s[i + 1] += c.s[i] / 10;
            c.s[i] %= 10;
        }
        c.resize();
        return c;
    }
    // 正 + 负
    if (a.f == 1 && b.f == -1) {
        b.f = 1;
        if (a < b) { std::swap(a, b); a.f = -1; }
        for (int i = 1; i <= MAX_SIZE; i++) {
            if (a.s[i] < b.s[i]) { a.s[i + 1]--; a.s[i] += 10; }
            a.s[i] -= b.s[i];
        }
        a.resize();
        return a;
    }
    // 负 + 正
    if (a.f == -1 && b.f == 1) {
        std::swap(a, b);
        b.f = 1;
        if (a < b) { std::swap(a, b); a.f = -1; }
        for (int i = 1; i <= MAX_SIZE; i++) {
            if (a.s[i] < b.s[i]) { a.s[i + 1]--; a.s[i] += 10; }
            a.s[i] -= b.s[i];
        }
        a.resize();
        return a;
    }
    // 负 + 负
    a.f = b.f = 1;
    f = a + b;
    f.f = -1;
    return f;
}

inline bigint operator-(bigint a, bigint b) {
    bigint c;
    // 异号: 转为加法
    if (a.f != b.f) {
        int bo = (a.f < b.f ? -1 : 1);
        a.f = b.f = 1;
        c = a + b;
        c.f = bo;
        return c;
    }
    // 正 - 正
    if (a.f == 1 && b.f == 1) {
        if (a < b) { std::swap(a, b); a.f = -1; }
        for (int i = 1; i <= MAX_SIZE; i++) {
            if (a.s[i] < b.s[i]) { a.s[i + 1]--; a.s[i] += 10; }
            a.s[i] -= b.s[i];
        }
        a.resize();
        return a;
    }
    // 负 - 负
    a.f = b.f = 1;
    std::swap(a, b);
    if (a < b) { std::swap(a, b); a.f = -1; }
    for (int i = 1; i <= MAX_SIZE; i++) {
        if (a.s[i] < b.s[i]) { a.s[i + 1]--; a.s[i] += 10; }
        a.s[i] -= b.s[i];
    }
    a.resize();
    return a;
}

inline bigint operator*(bigint a, bigint b) {
    bigint c;
    c.f = a.f * b.f;
    for (int i = 1; i <= a.l; i++)
        for (int j = 1; j <= b.l; j++)
            c.s[i + j - 1] += a.s[i] * b.s[j];
    for (int i = 1; i <= MAX_SIZE; i++) {
        c.s[i + 1] += c.s[i] / 10;
        c.s[i] %= 10;
    }
    c.resize();
    return c;
}

inline bigint operator/(bigint a, bigint b) {
    bigint c = b, f;
    f.f = a.f * b.f;
    a.f = b.f = 1;
    int cnt = a.l - b.l;
    bool bo = true;
    while (cnt > -1) {
        for (int i = 1; i <= b.l; i++) {
            if (a.s[i + cnt] != b.s[i]) {
                bo = (a.s[i + cnt] > b.s[i]);
                break;
            }
        }
        if (!bo) {
            a.s[a.l - 1] += a.s[a.l] * 10;
            cnt--;
            a.l--;
            bo = true;
            continue;
        }
        for (int i = 1; i <= b.l; i++) {
            if (a.s[i + cnt] < b.s[i]) {
                a.s[i + cnt] += 10;
                a.s[i + cnt - 1]--;
            }
            a.s[i + cnt] -= b.s[i];
        }
        f.s[cnt + 1]++;
    }
    f.resize();
    return f;
}

inline bigint operator%(bigint a, bigint b) { return a - a / b * b; }

// ==================== 自增 / 自减 ====================

inline bigint operator++(bigint &a) {
    if (a == -1) { a.to_empty(); return a; }
    if (a.f == 1) {
        a.s[1]++;
        int i = 1;
        while (a.s[i] > 9) { a.s[i + 1]++; a.s[i++] -= 10; }
        if (a.s[a.l + 1]) a.l++;
        return a;
    }
    a.s[1]--;
    int i = 1;
    while (a.s[i] < 0) { a.s[i + 1]--; a.s[i++] += 10; }
    a.resize();
    return a;
}

inline bigint operator--(bigint &a) {
    if (a.empty()) { a.push(-1); return a; }
    if (a.f == 1) {
        a.s[1]--;
        int i = 1;
        while (a.s[i] < 0) { a.s[i + 1]--; a.s[i++] += 10; }
        a.resize();
        return a;
    }
    a.s[1]++;
    int i = 1;
    while (a.s[i] > 9) { a.s[i + 1]++; a.s[i++] -= 10; }
    if (a.s[a.l + 1]) a.l++;
    return a;
}

// ==================== 位移运算 ====================

inline bigint operator<<(bigint a, bigint b) {
    while (!b.empty()) {
        --b;
        for (int i = 1; i <= MAX_SIZE; i++) a.s[i] <<= 1;
        for (int i = 1; i <= MAX_SIZE; i++) {
            a.s[i + 1] += a.s[i] / 10;
            a.s[i] %= 10;
        }
    }
    a.resize();
    return a;
}

inline bigint operator>>(bigint a, bigint b) {
    while (!b.empty()) {
        --b;
        for (int i = MAX_SIZE; i; i--) {
            a.s[i - 1] += ((a.s[i] & 1) << 3) + ((a.s[i] & 1) << 1);
            a.s[i] >>= 1;
        }
    }
    a.resize();
    return a;
}

// ==================== 一元运算符 ====================

inline bigint operator+(bigint x) { return x; }
inline bigint operator-(bigint x) { x.f = -x.f; return x; }
inline bigint abs(bigint x) { return x > 0 ? x : -x; }

// ==================== 混合类型算术运算 (bigint & long long) ====================

inline bigint operator+(bigint a, long long b) { bigint c(b); return a + c; }
inline bigint operator+(long long a, bigint b) { bigint c(a); return c + b; }
inline bigint operator-(bigint a, long long b) { bigint c(b); return a - c; }
inline bigint operator-(long long a, bigint b) { bigint c(a); return c - b; }
inline bigint operator*(bigint a, long long b) { bigint c(b); return a * c; }
inline bigint operator*(long long a, bigint b) { bigint c(a); return c * b; }
inline bigint operator/(bigint a, long long b) { bigint c(b); return a / c; }
inline bigint operator/(long long a, bigint b) { bigint c(a); return c / b; }
inline bigint operator%(bigint a, long long b) { bigint c(b); return a % c; }
inline bigint operator%(long long a, bigint b) { bigint c(a); return c % b; }
inline bigint operator<<(bigint a, long long b) { bigint c(b); return a << c; }
inline bigint operator<<(long long a, bigint b) { bigint c(a); return c << b; }
inline bigint operator>>(bigint a, long long b) { bigint c(b); return a >> c; }
inline bigint operator>>(long long a, bigint b) { bigint c(a); return c >> b; }

// ==================== 复合赋值运算符 ====================

inline bigint operator+=(bigint &a, bigint b)     { a = a + b; return a; }
inline bigint operator+=(bigint &a, long long b)  { a = a + b; return a; }
inline bigint operator-=(bigint &a, bigint b)     { a = a - b; return a; }
inline bigint operator-=(bigint &a, long long b)  { a = a - b; return a; }
inline bigint operator*=(bigint &a, bigint b)     { a = a * b; return a; }
inline bigint operator*=(bigint &a, long long b)  { a = a * b; return a; }
inline bigint operator/=(bigint &a, bigint b)     { a = a / b; return a; }
inline bigint operator/=(bigint &a, long long b)  { a = a / b; return a; }
inline bigint operator%=(bigint &a, bigint b)     { a = a % b; return a; }
inline bigint operator%=(bigint &a, long long b)  { a = a % b; return a; }
inline bigint operator<<=(bigint &a, bigint b)    { a = a << b; return a; }
inline bigint operator<<=(bigint &a, long long b) { a = a << b; return a; }
inline bigint operator>>=(bigint &a, bigint b)    { a = a >> b; return a; }
inline bigint operator>>=(bigint &a, long long b) { a = a >> b; return a; }

/*
 * 可直接配合 STL 算法使用:
 *   abs, max, min, __gcd,
 *   lower_bound, upper_bound,
 *   sort, stable_sort
 * 以及位移运算和除位运算外的各类运算
 */

#endif  // BIGINT_H

更新日志:

  • 24.07.10-12:添加 get,put,top,size,empty 函数
  • 24.07.13-15:添加正整数 加 / 减 / 乘法
  • 24.07.16:添加负数
  • 24.07.18:添加比较运算
  • 24.07.20:添加负数乘法
  • 24.07.22:添加前导运算符 ++,--
  • 24.07.23:修复 ++(-1) 正负异常,--(0) 数值异常Bug
  • 24.07.28-31: bigint 加/减/乘法更新完毕
  • 24.08.01-07:休息了一周,断更了…
  • 24.08.08-09:添加位移运算 <<,>>
  • 24.08.07-16:添加 bigint,long long 之间的 + / - / * / << / >> 及比较运算
  • 24.08.18:添加 resize,to_empty 函数并代入
  • 24.08.19:修复减法数值异常Bug
  • 24.08.20:添加 +=,-=,*=,<<=,>>=
  • 24.08.22:为to_empty 函数添加了将 bigint 长度设为1,正负设为正的功能(即变为0)
  • 24.08.26:用 << 和 >> 代替 *2 和 /2 ,提升速度
  • 24.08.28:利用位压缩技术,开始写 bigint 1.1
  • 24.09.06:添加 string 转 bigint 功能
  • 24.09.17:添加 +x 和 -x 表示
  • 24.09.24:用 &1 代替 %2,提升速度
  • 24.10.02:添加 MAX_SIZE , 格式化代码
  • 24.10.16:添加 abs(bigint x)
  • 26.05.23:经某用户反馈,修复了除法的部分问题。
  • 26.08.14:借助生成式大模型美化了码风。