高精度算法模板

· · 个人记录

搞了一下午

#include<bits/stdc++.h>
using namespace std;
struct hugeint;
struct hugeint {
    int a[1000];
    int len;
    friend bool operator < (const hugeint a,const int b) {
        int _b=b,l=0,k[1000];
        while(_b)l++,k[l]=b%10,_b/=10;
        if(a.len>l)return false;
        if(a.len<l)return true;
        for(int i=a.len; i>=1; i--)if(a.a[i]!=k[i])return a.a[i]<k[i];
        return false;
    }
    friend bool operator < (hugeint a,hugeint b) {
        if(a.len!=b.len)return a.len<b.len;
        for(int i=a.len; i>=1; i--)if(a.a[i]!=b.a[i])return a.a[i]<b.a[i];
        return false;
    }
    friend bool operator == (hugeint a,hugeint b) {
        if(a.len!=b.len)return false;
        for(int i=a.len; i>=1; i--)if(a.a[i]!=b.a[i])return false;
        return true;
    }
    friend hugeint operator + (const hugeint a,const hugeint b) {
        hugeint c;
        c.len=max(a.len,b.len);
        memset(c.a,0,sizeof c.a);
        for(int i=1; i<=c.len; i++) {
            if(i>a.len)c.a[i]+=b.a[i];
            else if(i>b.len)c.a[i]+=a.a[i];
            else c.a[i]+=a.a[i]+b.a[i];
            c.a[i+1]+=c.a[i]/10,c.a[i]%=10;
        }
        if(c.a[c.len+1]>0)c.len++;
        return c;
    }
    friend hugeint operator - (const hugeint _a,const hugeint _b) {
        if(_a==_b)return (hugeint) {
            {0,0},1
        };
        hugeint a=_a,b=_b;
        if(a<b) {
            //putchar('-');
            return b-a;
        }
        a.len=a.len;
        for(int i=1; i<=a.len; i++) {
            if(i<=b.len)a.a[i]-=b.a[i];
            if(a.a[i]<0)a.a[i+1]--,a.a[i]+=10;
        }
        while(a.a[a.len]==0&&a.len>1)a.len--;
        return a;
    }
    friend hugeint operator * (const hugeint a,const hugeint b) {
        if(a==(hugeint) {
        {0,0},1
    }||b==(hugeint) {
        {0,0},1
    })return (hugeint) {
            {0,0},1
        };
        hugeint c;
        c.len=a.len+b.len;
        memset(c.a,0,sizeof c.a);
        for(int i=1; i<=a.len; i++)for(int j=1; j<=b.len; j++) {
                c.a[i+j-1]+=a.a[i]*b.a[j];
            }
        for(int i=1; i<=c.len; i++) {
            c.a[i+1]+=c.a[i]/10,c.a[i]%=10;
        }
        while(c.a[c.len]==0)c.len--;
        return c;
    }
    friend  void numcpy(hugeint p, hugeint &q, int det) {
        for (int i=1; i<=p.len; i++) {
            q.a[i+det-1]=p.a[i];
        }
        q.len=p.len+det-1;
    }
    friend hugeint operator / (const hugeint _a,const hugeint _b) {
        hugeint tmp,c,a=_a,b=_b;
        c.len=a.len-b.len+1;
        memset(c.a,0,sizeof c.a);
        for (int i=c.len; i>0; i--) {
            memset(tmp.a,0,sizeof(tmp.a));
            numcpy(b,tmp,i);
            while(tmp<a||tmp==a) {
                c.a[i]++;
                a=a-tmp;
                /*cout<<a.len<<"#:"<<endl;
                for(int j=a.len; j>=1; j--)cout<<a.a[j];
                cout<<endl;*/
            }

        }
        while (c.len>1&&c.a[c.len]==0)c.len--;
        return c;
    }
};
hugeint change(string a) {
    hugeint b;
    b.len=a.size();
    for(int i=0; i<b.len; i++)b.a[b.len-i]=a[i]-'0';
    return b;
}
hugeint ch(int a) {
    hugeint k;
    k.len=0;
    while(a)k.len++,k.a[k.len]=a%10,a/=10;
    return k;
}
hugeint get() {
    string a;
    cin>>a;
    return change(a);
}
void show(hugeint a) {
    //printf("%d#\n",a.len);
    for(int i=a.len; i>=1; i--)
        if(a.a[i]>=10||a.a[i]<0) {
            printf("\n\n\n\error!\n\n\n\n");
            return;
        } else printf("%d",a.a[i]);
    printf("\n");
}
string a,b;
int main() {

}