高精

发布时间 2023-09-24 08:23:23作者: ussumer

Part 1:无除法取模无压位

#include<bits/stdc++.h>
#define F(i,i0,n) for(int (i)=(i0);(i)<=(n);++(i))
#define pii pair<int,int>
#define fr first
#define sc second
using namespace std;
inline int rd() {
    int f = 0, x = 0;char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-')f = 1;ch = getchar();}
    while(isdigit(ch)) {x = (x << 3) + (x << 1) + ch - 48;ch = getchar();}
    return f ? -x : x;
}
const int N = 5e4 + 5;
const int P = 10, MAXP = 5005;
struct big {
    int len, d[MAXP];
    big() {
        memset(d, 0, sizeof(d));
        len = 1;
    }
    big(int num) {*this = num;}
    big(const char *c) {*this = c;}
    big operator =(int num) {
        len = 0;
        while(num) {
            d[len++] = num % 10;
            num /= 10;
        }
        return *this;
    }
    big operator =(const char *num) {
        for(int i = 0; num[i] == '0'; num++);//去前导0 
        len = strlen(num);
        if(!len){
            d[0]=0;len=1;
            return *this;
        }
        for(int i = 0; i < len; ++i)
            d[i] = num[len - i - 1] - '0';
        return *this;
    }
    big operator +(const big &b)const {
        big c;
        c.len = 0;
        for(int i = 0, g = 0; g || i < max(len, b.len); ++i) {
            int x = g;
            if(i < len)x += d[i];
            if(i < b.len)x += b.d[i];
            c.d[c.len++] = x % 10;
            g = x / 10;
        }
        return c;
    }
    void clean() {
        while(len > 1 && !d[len - 1])len--;
    }
    big operator *(const big &b) const {
        big c;
        c.len = len + b.len;
        for(int i = 0; i < len; ++i)
            for(int j = 0; j < b.len; ++j)
                c.d[i + j] += d[i] * b.d[j];

        for(int i = 0; i < c.len; ++i) {
            c.d[i + 1] += c.d[i] / 10;
            c.d[i] %= 10;
        }
        c.clean();
        return c;
    }
    big operator -(const big &b)const {
        big c;
        c.len = 0;
        for(int i = 0, g = 0; i < len; ++i) {
            int x = d[i] - g;
            if(i < b.len)x -= b.d[i];
            if(x >= 0)g = 0;
            else {
                g = 1;
                x += 10;
            }
            c.d[c.len++] = x;
        }
        c.clean();
        return c;
    }
    bool operator <(const big &b)const {
        if(len != b.len)return len < b.len;
        for(int i = len - 1; ~i; --i)
            if(d[i] != b.d[i])return d[i] < b.d[i];
        return 0;
    }
    bool operator >(const big &b)const {
        if(len != b.len)return len > b.len;
        for(int i = len - 1; ~i; --i)
            if(d[i] != b.d[i])return d[i] > b.d[i];
        return 0;
    }
    big operator +=(const big &b) {*this = *this + b;return *this;}
    big operator *=(const big &b) {*this = *this * b;return *this;}
    big operator -=(const big &b) {*this = *this - b;return *this;}
    bool operator ==(const big &b) {return !(*this > b) && !(*this < b);}
    bool operator !=(const big &b) {return !(*this == b);}
    bool operator <=(const big &b) {return *this < b || *this == b;}
    bool operator >=(const big &b) {return *this > b || *this == b;}
    void out()const {
        for(int i = len - 1; i >= 0; --i)cout << d[i];
        cout << '\n';
    }
    void in() {
        char t[MAXP];
        scanf("%s", t);
        *this = t;
    }
};
signed main() {
    return 0;
}

Part 2:完全体(别问为什么还是没有压位,因为懒)

#include<bits/stdc++.h>
#define F(i,i0,n) for(int (i)=(i0);(i)<=(n);++(i))
#define pii pair<int,int>
#define fr first
#define sc second
using namespace std;
inline int rd() {
    int f = 0, x = 0;char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-')f = 1;ch = getchar();}
    while(isdigit(ch)) {x = (x << 3) + (x << 1) + ch - 48;ch = getchar();}
    return f ? -x : x;
}
const int N = 5e4 + 5;
const int P = 10, MAXP = 5005;
struct big {
    int len, d[MAXP];
    big() {
        memset(d, 0, sizeof(d));
        len = 1;
    }
    big(int num) {*this = num;}
    big(const char *c) {*this = c;}
    big operator =(int num) {
        len = 0;
        while(num) {
            d[len++] = num % 10;
            num /= 10;
        }
        return *this;
    }
    big operator =(const char *num) {
        for(int i = 0; num[i] == '0'; num++);//去前导0 
        len = strlen(num);
        if(!len){
            d[0]=0;len=1;
            return *this;
        }
        for(int i = 0; i < len; ++i)
            d[i] = num[len - i - 1] - '0';
        return *this;
    }
    big operator +(const big &b)const {
        big c;
        c.len = 0;
        for(int i = 0, g = 0; g || i < max(len, b.len); ++i) {
            int x = g;
            if(i < len)x += d[i];
            if(i < b.len)x += b.d[i];
            c.d[c.len++] = x % 10;
            g = x / 10;
        }
        return c;
    }
    void clean() {
        while(len > 1 && !d[len - 1])len--;
    }
    big operator *(const big &b) const {
        big c;
        c.len = len + b.len;
        for(int i = 0; i < len; ++i)
            for(int j = 0; j < b.len; ++j)
                c.d[i + j] += d[i] * b.d[j];

        for(int i = 0; i < c.len; ++i) {
            c.d[i + 1] += c.d[i] / 10;
            c.d[i] %= 10;
        }
        c.clean();
        return c;
    }
    big operator -(const big &b)const {
        big c;
        c.len = 0;
        for(int i = 0, g = 0; i < len; ++i) {
            int x = d[i] - g;
            if(i < b.len)x -= b.d[i];
            if(x >= 0)g = 0;
            else {
                g = 1;
                x += 10;
            }
            c.d[c.len++] = x;
        }
        c.clean();
        return c;
    }
    big operator /(const big &b)const {
        big c, f = 0;
        for(int i = len - 1; i >= 0; --i) {
            f = f * 10;
            f.d[0] = d[i];
            while(f >= b) {
                f -= b;
                c.d[i]++;
            }
        }
        c.len = len;
        c.clean();
        return c;
    }
    
    big operator%(const big &b) {
        big r = *this / b;
        r = *this - r * b;
        r.clean();
        return r;
    }
    
    bool operator <(const big &b)const {
        if(len != b.len)return len < b.len;
        for(int i = len - 1; ~i; --i)
            if(d[i] != b.d[i])return d[i] < b.d[i];
        return 0;
    }
    bool operator >(const big &b)const {
        if(len != b.len)return len > b.len;
        for(int i = len - 1; ~i; --i)
            if(d[i] != b.d[i])return d[i] > b.d[i];
        return 0;
    }
    big operator +=(const big &b) {*this = *this + b;return *this;}
    big operator *=(const big &b) {*this = *this * b;return *this;}
    big operator -=(const big &b) {*this = *this - b;return *this;}
    big operator /=(const big &b) {*this = *this / b;return *this;}
    big operator %=(const big &b) {*this = *this % b;return *this;}
    bool operator ==(const big &b) {return !(*this > b) && !(*this < b);}
    bool operator !=(const big &b) {return !(*this == b);}
    bool operator <=(const big &b) {return *this < b || *this == b;}
    bool operator >=(const big &b) {return *this > b || *this == b;}
    void out()const {
        for(int i = len - 1; i >= 0; --i)cout << d[i];
        cout << '\n';
    }
    void in() {
        char t[MAXP];
        scanf("%s", t);
        *this = t;
    }
};
signed main() {
    return 0;
}