高精度模板

简介

本模板实现了高精度加/减/乘/除(单精度)/小于/输入输出,可满足大部分使用需求,并具有较好的拓展性实现取模/双精度除法等

输入/输出

bint a, b;
cin >> a >> b;
cout << a << b << endl;

加法

A+B Problem

bint a, b;
cin >> a >> b;
cout << a + b << endl;

小于

bint a, b;
cin >> a >> b;
if(a < b) {
    ...
}

减法

注意:当模板中的减法默认不出现负数,即被减数大于减数,当被减数小于减数时可以交换两数再相减,并在输出答案时加上负号

A-B Problem

bint a, b;
cin >> a >> b;
if(a < b) {
    cout << '-' << b - a << endl;
} else {
    cout << a - b << endl;
}

乘法

A*B Problem

bint a, b;
cin >> a >> b;
cout << a * b << endl;

除法(单精度)

注意:模板中的除法为单精度除法,当除数达到 \(10^9\) 数量级时需要把 vector<> v 类型改为 long long 才能保证结果正确

A/B Problem

bint a;
int b;
cin >> a >> b;
cout << a / b << endl;

代码

struct bint {
    vector<int> v;

    bint() {
        *this = 0;
    }
    
    bint(int x) {
        *this = x;
    }

    bint& operator=(int x) {
        v.clear();
        do {
            v.push_back(x % 10);
        } while (x /= 10);
        return *this;
    }

    bint& operator=(const bint& x) {
        v.resize(x.v.size());
        for(int i = v.size() - 1; i >= 0; i--)
            v[i] = x.v[i];
        return *this;
    }
};

ostream& operator<<(ostream& out, const bint& x)
{
    for(int i = x.v.size() - 1; i >= 0; i--)
        out << (char)(x.v[i] + '0');
    return out;
}

istream& operator>>(istream& in, bint& x)
{
    string str;
    in >> str;
    x.v.clear();
    for(int i = str.size() - 1; i >= 0; i--)
        x.v.push_back(str[i] - '0');
    return in;
}

bint operator+(const bint& a, const bint& b)
{
    bint res;
    res.v.clear();
    bool carry = false;
    int len = max(a.v.size(), b.v.size());
    for(int i = 0; i < len; i++) {
        int add = 0;
        if(i < (int)a.v.size()) add += a.v[i];
        if(i < (int)b.v.size()) add += b.v[i];
        if(carry) {
            add++;
            carry = false;
        }
        if(add >= 10) {
            add -= 10;
            carry = true;
        }
        res.v.push_back(add);
    }
    if(carry)
        res.v.push_back(1);
    return res;
}

bint operator-(const bint& a, const bint& b)
{
    bint res;
    res.v.clear();
    bool carry = false;
    for(int i = 0; i < (int)a.v.size(); i++) {
        int sub = a.v[i];
        if(i < (int)b.v.size()) sub -= b.v[i];
        if(carry) {
            sub--;
            carry = false;
        }
        if(sub < 0) {
            sub += 10;
            carry = true;
        }
        res.v.push_back(sub);
    }
    int siz = res.v.size();
    while(siz > 1 && res.v[siz - 1] == 0)
        siz--;
    res.v.resize(siz);
    return res;
}

bint operator*(const bint& a, const bint& b)
{
    bint res;
    res.v.resize(a.v.size() + b.v.size());
    for(int i = 0; i < (int)a.v.size(); i++) {
        for(int j = 0; j < (int)b.v.size(); j++) {
            res.v[i + j] += a.v[i] * b.v[j];
            res.v[i + j + 1] += res.v[i + j] / 10;
            res.v[i + j] %= 10;
        }
    }
    int siz = res.v.size();
    while(siz > 1 && res.v[siz - 1] == 0)
        siz--;
    res.v.resize(siz);
    return res;
}

bint operator/(const bint& a, int b)
{
    bint res(a);
    int num, rm;
    for(int i = res.v.size() - 1; i >= 1; i--) {
        rm = res.v[i] % b;
        num = res.v[i] / b;
        res.v[i] = num;
        res.v[i - 1] += rm * 10ll;
    }
    res.v[0] /= b;
    int siz = res.v.size();
    while(siz > 1 && res.v[siz - 1] == 0)
        siz--;
    res.v.resize(siz);
    return res;
}

bool operator<(const bint& a, const bint& b)
{
    if(a.v.size() != b.v.size())
        return a.v.size() < b.v.size();
    for(int i = a.v.size() - 1; i >= 0; i--)
        if(a.v[i] != b.v[i])
            return a.v[i] < b.v[i];
    return false;
}

参考

本模板参考了 Menci's Blog

posted @ 2022-02-07 22:13  f(k(t))  阅读(38)  评论(0编辑  收藏  举报