高精度

来源:互联网 发布:mac安装win10单系统 编辑:程序博客网 时间:2024/04/29 16:33
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");        }    }};

0 0
原创粉丝点击