跳到正文

剪贴板

剪贴板 qek38tk4

原文
剪贴板信息

剪贴板信息

收录
更新

高精度模板

C++
const int mx=2e4+10;
struct bign{
    int l,s[mx];
    bign(){l=1, memset(s,0,sizeof(s));}
    bign(const int x){*this=x;}
    bign operator =(const int x){
        char str[25];
        sprintf(str,"%d",x);
        return *this=str;
    }
    bign operator =(const char *num){
        l=strlen(num);
        memset(s,0,sizeof(s));
        for(int i=1;i<=l;i++) s[i]=num[l-i]-'0';
        return *this;
    }
    bign operator +(const bign b){
        int len=l>b.l?l:b.l;
        bign c;
        for(int i=1;i<=len;i++) c.s[i]+=s[i]+b.s[i], c.s[i+1]+=c.s[i]/10, c.s[i]%=10;
        c.l=len+1;
        c.clean();
        return c;
    }
    bign operator -(const bign b){
        bign c;
        for(int i=1;i<=l;i++){
            if(s[i]<b.s[i]) s[i]+=10, s[i+1]--;
            c.s[i]=s[i]-b.s[i];
        }
        c.l=l;
        c.clean();
        return c;
    }
    bign operator *(const bign b){
        bign c;
        for(int i=1;i<=l;i++) for(int j=1;j<=b.l;j++) c.s[i+j-1]+=s[i]*b.s[j], c.s[i+j]+=c.s[i+j-1]/10, c.s[i+j-1]%=10;
        c.l=l+b.l+1;
        c.clean();
        return c;
    }
    bign operator /(const bign b){
        bign c,f=0;
        for(int i=l;i>=1;i--){
            f=f*10, f.s[1]=s[i];
            while(f>=b) f=f-b,c.s[i]++;
        }
        c.l=l;
        c.clean();
        return c;
    }
    bign operator %(const bign b){
        bign c,f=0;
        for(int i=l;i>=1;i--){
            f=f*10, f.s[1]=s[i];
            while(f>=b) f=f-b,c.s[i]++;
        }
        f.l=l;
        f.clean();
        return f;
    }
    void clean(){while(l>1&&!s[l]) l--;}
    bool operator >(const bign b){
        if(l!=b.l) return l>b.l;
        for(int i=l;i>=1;i--) if(s[i]!=b.s[i]) return s[i]>b.s[i];
        return false;
    }
    bool operator <(const bign b){
        if(l!=b.l) return l<b.l;
        for(int i=l;i>=1;i--) if(s[i]!=b.s[i]) return s[i]<b.s[i];
        return false;
    }
    bool operator ==(const bign b){
        return !(*this>b)&&!(*this<b);
    }
    bool operator >=(const bign b){
        return !(*this<b);
    }
    bool operator <=(const bign b){
        return !(*this>b);
    }
};
istream& operator >>(istream &in, bign &b){
    string str;
    in>>str;
    b=str.c_str();
    return in;
}
ostream& operator <<(ostream &out, const bign &b){
    for(int i=b.l;i>=1;i--) out<<b.s[i];
    return out;
}
bign fun(int x){
    bign s=1;
    for(int i=1;i<=x;i++) s=s*i;
    return s;
}