跳到正文

算法·理论

背诵1

AFOed_on_20260726约 6 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

高精度加减乘除取余

C++
#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e6+5;
struct Bint{
    int n;
    char s[maxn];
};

Bint Input(){
    Bint A;
    cin>>A.s;
    A.n=strlen(A.s);
    for(int i=0,j=A.n-1;i<j;i++,j--) swap(A.s[i],A.s[j]);
    return A;
}

void Output(Bint A) {
    for(int i=A.n-1;i>=0;i--) cout<<A.s[i];
}

int Compare(Bint A, Bint B){
    if(A.n != B.n) return A.n > B.n ? 1 : -1;
    for(int i=A.n-1;i>=0;i--){
        if(A.s[i] != B.s[i]) return A.s[i] > B.s[i] ? 1 : -1;
    }
    return 0;
}

Bint operator+(Bint A, Bint B){
    Bint C;
    C.n = max(A.n, B.n);
    int c = 0;
    for(int i=0;i<C.n;i++){
        int x = c;
        if(i < A.n) x += A.s[i]-'0';
        if(i < B.n) x += B.s[i]-'0';
        C.s[i] = x % 10 + '0';
        c = x / 10;
    }
    if(c) C.s[C.n++] = '1';
    return C;
}

Bint operator-(Bint A, Bint B){
    Bint C;
    if(Compare(A, B) < 0){
        C = B - A;
        C.s[C.n] = '-';
        C.n++;
        reverse(C.s, C.s + C.n);
        return C;
    }
    C.n = A.n;
    int b = 0;
    for(int i=0;i<A.n;i++){
        int x = A.s[i]-'0' - b;
        int y = i < B.n ? B.s[i]-'0' : 0;
        if(x < y){
            x += 10;
            b = 1;
        } else {
            b = 0;
        }
        C.s[i] = (x - y) + '0';
    }
    while(C.n>1 && C.s[C.n-1]=='0') C.n--;
    return C;
}

Bint operator*(Bint A, Bint B){
    Bint C;
    C.n = A.n + B.n;
    int X[maxn] = {};
    for(int i=0;i<A.n;i++)
        for(int j=0;j<B.n;j++)
            X[i+j] += (A.s[i]-'0') * (B.s[j]-'0');
    for(int i=0;i<C.n;i++){
        C.s[i] = X[i] % 10 + '0';
        X[i+1] += X[i] / 10;
    }
    while(C.n>1 && C.s[C.n-1]=='0') C.n--;
    return C;
}

Bint DivMod(Bint A, Bint B, Bint &R){
    Bint Q;
    Q.n = 1; Q.s[0] = '0';
    R = A;

    if(Compare(A, B) < 0){
        return Q;
    }

    Bint base;
    base.n = 1; base.s[0] = '1';

    while(Compare(R, B) >= 0){
        Bint tB = B;
        Bint tBase = base;

        while(Compare(R, tB) >= 0){
            tB.n++;
            for(int i=tB.n-1;i>0;i--) tB.s[i] = tB.s[i-1];
            tB.s[0] = '0';

            tBase.n++;
            for(int i=tBase.n-1;i>0;i--) tBase.s[i] = tBase.s[i-1];
            tBase.s[0] = '0';
        }

        tB.n--;
        for(int i=0;i<tB.n;i++) tB.s[i] = tB.s[i+1];

        tBase.n--;
        for(int i=0;i<tBase.n;i++) tBase.s[i] = tBase.s[i+1];

        while(Compare(R, tB) >= 0){
            R = R - tB;
            Q = Q + tBase;
        }
    }

    return Q;
}

Bint operator/(Bint A, Bint B){
    Bint R;
    return DivMod(A, B, R);
}

Bint operator%(Bint A, Bint B){
    Bint R;
    DivMod(A, B, R);
    return R;
}

int main(){
    Bint A = Input(), B = Input();

    Output(A + B); cout<<endl;
    Output(A - B); cout<<endl;
    Output(A * B); cout<<endl;
    Output(A / B); cout<<endl;
    Output(A % B); cout<<endl;

    return 0;
}
原文