高精度

const int base = 10000;// 压4位
struct BigInt{
    int c[1000], len, sign;
    BigInt(){
    memset(c, 0, sizeof(c));
    len = 1; sign = 0;
    }
    void Zero(){
        while(len > 1 && c[len] == 0) len--; 
        if(len == 1 && c[len] == 0) sign = 0;
    }
    void writein(char *s){
        int k = 1, L = strlen(s);
        for(int i = L-1; i >= 0; i--){
            c[len] += (s[i]-'0') * k;
            k *= 10;
            if(k == base){
                k = 1;
                len++;
            }
        }
    }
    void Read(){
        char s[5000] = {0};
        scanf("%s", s);
        writein(s);
    }
    void Print(){
        if(sign) printf("-");
        printf("%d", c[len]);
        for(int i = len-1; i >= 1; i--) printf("%04d", c[i]);
        printf("\n");
    }
    BigInt operator = (int a){
        char s[100] = {0};
        sprintf(s, "%d", a);
        writein(s);
        return *this;
    }
    bool operator > (const BigInt &b){
        if(len != b.len) return len > b.len;
        for(int i = len; i >= 1; i--){
            if(c[i] != b.c[i]) return c[i] > b.c[i];
        }
        return 0;
    }
    bool operator < (const BigInt &b){
        if(len != b.len) return len < b.len;
        for(int i = len; i >= 1; i--){
            if(c[i] != b.c[i]) return c[i] < b.c[i];
        }
        return 0;
    }
    bool operator == (const BigInt &b){
        if(len != b.len) return 0;
        for(int i = 1; i <= len; i++)
            if(c[i] != b.c[i]) return 0;
        return 1;
    }
    bool operator == (const int &a){
        BigInt b; b = a;
        return *this == b;
    }
    BigInt operator + (const BigInt &b){
        BigInt r; r.len = max(len, b.len) + 1;
        for(int i = 1; i <= r.len; i++){
            r.c[i] += c[i] + b.c[i];
            r.c[i+1] += r.c[i] / base;
            r.c[i] %= base;
        }
        r.Zero();
        return r;
    }
    BigInt operator + (const int &a){
        BigInt b; b = a;
        return *this + b;
    }
    BigInt operator - (const BigInt &b){
        BigInt a, c;// a - c
        a = *this; c = b;
        if(a < c){
            std::swap(a, c);
            a.sign = 1;
        }
        for(int i = 1; i <= len; i++){
            a.c[i] -= c.c[i];
            if(a.c[i] < 0){
                a.c[i] += base;
                a.c[i+1]--;
            }
        }
        a.Zero();
        return a;
    }
    BigInt operator - (const int &a){
        BigInt b; b = a;
        return *this - b;
    }
    BigInt operator * (const BigInt &b){
        BigInt r; r.len = len + b.len + 2;
        for(int i = 1; i <= len; i++){
            for(int j = 1; j <= b.len; j++){
                r.c[j+i-1] += c[i] * b.c[j];
            }
        }
        for(int i = 1; i <= r.len; i++){
            r.c[i+1] += r.c[i] / base;
            r.c[i] %= base;
        }
        r.Zero();
        return r;
    }
    BigInt operator * (const int &a){
        BigInt b; b = a;
        return *this * b;
    }
    BigInt operator / (BigInt b)//整除{
        BigInt t, r;
        if(b == 0) return r;
        r.len = len;
        for(int i = len; i >= 1; i--){
            t = t * base + c[i];
            int div;
            //------try------
                int up = 10000, down = 0;
                while(up >= down){
                    int mid = (up + down) / 2;
                    BigInt ccc ; ccc = b * mid;
                    if(ccc > t) up = mid - 1;
                    else {
                        down = mid + 1;
                        div = mid;
                    }
                }
            //------end------
            r.c[i] = div;
            t = t - b * div;
        }
        //最后的t为余数,要用的自己想办法传出去
        r.Zero();
        return r;
    }
    BigInt operator / (const int &a){
        BigInt b; b = a;
        return *this / b;
    }
    BigInt operator % (const BigInt &b){
    //其实可以复制上面除法的,这里换一种写法
        return *this - *this / b * b;
    }
    BigInt operator % (const int &a){
        BigInt b; b = a;
        return *this % b;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值