高精度板子

· · 个人记录

讲真,不会高精度还是很虚的

为了寻求心理安慰,我结合网上的博客搞了一个阉割版的高精度板子出来

有以下问题

然而还是有优点,好记,真的好记,而且好打,不遇到毒瘤应该也够用

板子如下

const int base=4;
const int power=1e4;
struct big {
    int A[maxn];
    int & operator [](int pos) {
        return A[pos];
    }
    big() {
        memset(A,0,sizeof(A));
        A[0]=1;
    }
    big(char *S) {
        memset(A,0,sizeof(A));
        int len=strlen(S);
        A[0]=(len+base-1)/base;
        reverse(S,S+len);
        for(int i=0,w,t=0; i<len; i++,w*=10) {
            if(i%base==0) {
                ++t;
                w=1;
            }
            A[t]+=w*(S[i]-'0');
        }
    }
    big(int a) {
        memset(A,0,sizeof(A));
        while(a) {
            A[++A[0]]=a%power;
            a/=power;
        }
        A[0]=max(A[0],1);
    }
    friend bool operator <(big a,big b) {
        if(a[0]<b[0])
            return true;
        else if(a[0]>b[0])
            return false;
        else {
            for(int i=a[0]; i>=1; i--)
                if(a[i]!=b[i])
                    return a[i]<b[i];
            return false;
        }
    }
    friend big operator +(big a,big b) {
        big c;
        c[0]=max(a[0],b[0]);
        for(int i=1; i<=c[0]; i++) {
            c[i]+=a[i]+b[i];
            c[i+1]+=c[i]/power;
            c[i]%=power;
        }
        if(c[c[0]+1])
            ++c[0];
        return c;
    }
    friend big operator -(big a,big b) {
        big c=a;
        for(int i=1; i<=c[0]; i++) {
            c[i]-=b[i];
            if(c[i]<0) {
                c[i]+=power;
                --c[i+1];
            }
        }
        while(c[0]>1&&!c[c[0]])
            --c[0];
        return c;
    }
    friend big operator *(big a,big b) {
        big c;
        c[0]=a[0]+b[0]-1;
        for(int i=1; i<=a[0]; i++)
            for(int j=1; j<=b[0]; j++) {
                c[i+j-1]+=a[i]*b[j];
                c[i+j]+=c[i+j-1]/power;
                c[i+j-1]%=power;
            }
        if(c[c[0]+1])
            ++c[0];
        return c;
    }
    friend big operator /(big a,int b) {
        big c;
        long long sum=0;
        for(int i=a[0]; i>=1; i--) {
            sum=sum*power+a[i];
            c[i]+=sum/b;
            sum%=b;
            if(c[i])
                c[0]=max(c[0],i);
        }
        return c;
    }
    il void Print(){
        print("%d",A[A[0]]);
        for(int i=A[0]-1;i>=1;i--)
            printf("%04d",A[i]);
    }
};