高精度模板

来源:互联网 发布:c语言教材 谭浩强 编辑:程序博客网 时间:2024/06/12 01:30

纯手打。。。

struct Big {    int num[M],l;    Big() {        memset(num,0,sizeof(num));        l=1;    }    void Rd() {        scanf("%s",str);        l=0;        int sl=strlen(str);        for(int i=sl-1; i>=0; i-=4) {            int sum=0;            for(int j=max(0,i-3); j<=i; j++)sum=sum*10+str[j]-'0';            num[l++]=sum;        }    }    void Pt() {        printf("%d",num[l-1]);        for(int i=l-2; i>=0; i--)printf("%04d",num[i]);    }    void operator =(const int &a) {        l=0;        int sum=a;        while(sum) {            num[l++]=sum%10000;            sum/=10000;        }    }    bool operator <(const Big &s)const {        if(l!=s.l)return l<s.l;        for(int i=l-1; i>=0; i--)            if(num[i]!=s.num[i]) return num[i]<s.num[i];        return false;    }    bool operator >(const Big &s)const {        if(l!=s.l)return l<s.l;        for(int i=l-1; i>=0; i--)            if(num[i]!=s.num[i]) return num[i]>s.num[i];        return false;    }    bool operator ==(const Big &s)const {        if(l!=s.l)return l<s.l;        for(int i=l-1; i>=0; i--)            if(num[i]!=s.num[i]) return false;        return false;    }    Big operator +(const int &a)const {        Big tmp=*this;        tmp.num[0]+=a;        int cnt=0;        while(tmp.num[cnt]>=10000) {            tmp.num[cnt+1]++;            tmp.num[cnt]-=10000;            ++cnt;        }        while(tmp.num[tmp.l])++tmp.l;        return tmp;    }    Big operator -(const int &a)const {        Big tmp=*this;        tmp.num[0]-=a;        int cnt=0;        while(tmp.num[cnt]<0) {            tmp.num[cnt]+=10000;            --tmp.num[cnt];            ++cnt;        }        while(tmp.num[tmp.l-1]==0&&tmp.l>1)--tmp.l;        return tmp;    }    Big operator *(const int &a)const {        Big tmp;        tmp.l=l;        for(int i=0; i<l; i++) {            tmp.num[i]+=num[i]*a;            if(tmp.num[i]>=10000) {                tmp.num[i+1]+=tmp.num[i]/10000;                tmp.num[i]%=10000;            }        }        if(tmp.num[tmp.l]!=0)tmp.l++;        return tmp;    }    Big operator /(const int &a)const {        Big tmp;        for(int i=0; i<l; i++)tmp.num[i]=num[i];        for(int i=l-1; i>0; i--) {            tmp.num[i-1]+=tmp.num[i]%a*10000;            tmp.num[i]/=a;        }        tmp.num[0]/=a;        tmp.l=l;        while(tmp.l>1&&tmp.num[tmp.l-1]==0)--tmp.l;        return tmp;    }    Big operator +(const Big &s)const {        Big tmp;        tmp.l=max(l,s.l);        for(int i=0; i<tmp.l; i++) {            tmp.num[i]+=num[i]+s.num[i];            if(tmp.num[i]>=10000) {                tmp.num[i]-=10000;                tmp.num[i+1]++;            }        }        if(tmp.num[tmp.l]!=0)tmp.l++;        return tmp;    }    Big operator -(const Big &s)const {        Big tmp;        tmp.l=l;        for(int i=l-1; i>=0; i--) {            tmp.num[i]=num[i]-s.num[i];            if(tmp.num[i]<0) {                tmp.num[i+1]--;                tmp.num[i]+=10000;            }        }        while(tmp.num[tmp.l-1]==0) tmp.l--;        return tmp;    }    Big operator *(const Big &s)const {        Big tmp;        for(int i=0; i<l; i++)            for(int j=0; j<s.l; j++) {                tmp.num[i+j]+=num[i]*s.num[j];                if(tmp.num[i+j]>=10000) {                    tmp.num[i+j+1]+=tmp.num[i+j]/10000;                    tmp.num[i+j]%=10000;                }            }        tmp.l=l+s.l-1;        if(tmp.num[tmp.l])tmp.l++;        return tmp;    }    Big operator /(const Big &s)const {        Big L,R,res;        if(*this<s)return res;        R=*this;        while(!(L>R)) {            Big mid=(L+R)/2;            if(!(mid*s>*this)) {                res=mid;                L=mid+1;            } else R=mid-1;        }        return res;    }};
原创粉丝点击