高精度运算

  1. 数据读入,加减乘除,阶乘,取余。
  2. 不支持负数。
  3. 乘除只支持大整数和小整数的运算。
  4. 包括了所有已知的算法竞赛当中的大数运算。
  5. 这应该是迭代比较完美的一代了,终于完成了高精度模板,继续加油!
const int maxLen = 1005;
struct BigInt{//not support negative
    long long num[maxLen];
    int digit = 0;

    BigInt() = default;
    BigInt(char a[]){
        int la = strlen(a);
        int i = la-8, j = 0;

        for(; i >= 0; i -= 8)
            sscanf(a+i,"%8d",&num[j++]);
        int t;  num[j] = 0;
        if(i+8 > 0){
            for(int k = 0; i+8 > 0; ++k){
                sscanf(a+k,"%1d",&t);
                num[j] = num[j]*10 + t;
                --i;
            }
            ++j;
        }
        num[j] = -1;  digit = j;
    }
    BigInt(int a){
        int j = 0;
        num[j++] = a%100000000;
        if(a >= 100000000)
            num[j++] = a/100000000;
        num[j] = -1;  digit = j;
    }
    BigInt(long long a){
        int j = 0;
        num[j++] = a%100000000;
        a /= 100000000;
        if(a > 0){
            num[j++] = a%100000000;
            a /= 100000000;
            if(a > 0){
                num[j++] = a%100000000;
            }
        }
        num[j] = -1;  digit = j;
    }

    int bigCmp(const BigInt rhs){
        if(digit > rhs.digit)
            return 1;
        else if(digit < rhs.digit)
            return -1;
        else{
            for(int i = digit-1; i >= 0; --i){
                if(num[i] > rhs.num[i])
                    return 1;
                else if(num [i] < rhs.num[i])
                    return -1;
            }
            return 0;
        }
    }

    BigInt operator + (const BigInt rhs){
        BigInt ans;  int idx = 0, carry = 0;

        while(num[idx]!=-1 && rhs.num[idx]!=-1){
            ans.num[idx] = num[idx]+rhs.num[idx]+carry;
            carry = ans.num[idx] / 100000000;
            ans.num[idx++] %= 100000000;
        }
        if(num[idx] != -1){
            while(num[idx] != -1){
                ans.num[idx] = num[idx] + carry;
                carry = ans.num[idx] / 100000000;
                ans.num[idx++] %= 100000000;
            }
        }
        else{
            while(rhs.num[idx] != -1){
                ans.num[idx] = rhs.num[idx] + carry;
                carry = ans.num[idx] / 100000000;
                ans.num[idx++] %= 100000000;
            }
        }
        if(carry > 0)
            ans.num[idx++] = carry;
        ans.num[idx] = -1;
        ans.digit = idx;

        return ans;
    }

    BigInt operator * (const int rhs){
        BigInt ans;  int carry = 0,idx = 0;

        if(rhs != 0){
            for(idx = 0; num[idx] != -1; ++idx){
                ans.num[idx] = num[idx]*rhs + carry;
                carry = ans.num[idx] / 100000000;
                ans.num[idx] %= 100000000;
            }
            if(carry > 0)
                ans.num[idx++] = carry;
        }
        else{
            ans.num[idx++] = 0;
        }
        ans.num[idx] = -1;
        ans.digit = idx;
        return ans;
    }

    BigInt operator - (const BigInt rhs){//l >= r
        BigInt ans;
        int idx = 0, carry = 0;
        while(rhs.num[idx] != -1){
            ans.num[idx] = num[idx] - rhs.num[idx] - carry;
            carry = 0;
            if(ans.num[idx] < 0){
                ans.num[idx] += 100000000;
                carry = 1;
            }
            ++idx;
        }
        while(num[idx] != -1){
            ans.num[idx] = num[idx] - carry;
            carry = 0;
            if(ans.num[idx] < 0){
                ans.num[idx] += 100000000;
                carry = 1;
            }
            ++idx;
        }
        ans.num[idx] = -1;
        ans.digit = idx;
        return ans;
    }

    void frac(int n){
        num[0] = 1;  num[1] = -1;  digit = 1;
        int carry, idx;
        for(int i = 2; i <= n; ++i){
            carry = 0;
            for(idx = 0; num[idx] != -1; ++idx){
                num[idx] = num[idx]*i + carry;
                carry = num[idx] / 100000000;
                num[idx] %= 100000000;
            }
            if(carry > 0)
                num[idx++] = carry;
            num[idx] = -1;
            digit = idx;
        }
    }

    long long operator % (const int rhs){
        int idx = digit-1;
        long long r = num[idx--]%rhs;
        while(idx >= 0){
            r = (r*100000000LL+num[idx])%rhs;
            --idx;
        }
        return r;
    }

    BigInt operator / (const int rhs){
        BigInt ans;
        int i = digit-1, r = 0;
        while(i >= 0){
            ans.num[i] = (num[i]+r*100000000LL)/(long long)rhs;
            r = (num[i]+r*100000000LL)%(long long)rhs;
            --i;
        }
        ans.digit = digit;
        while(ans.digit > 1 && ans.num[ans.digit-1] == 0)
            ans.digit--;
        ans.num[ans.digit] = -1;
        return ans;
    }

    void Print(){
        if(digit > 0){
            int i = digit-1;
            printf("%lld",num[i]);
            while(i--)
                printf("%08lld",num[i]);
            printf("\n");
        }
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值