关于高精度模板
  • 板块学术版
  • 楼主吴思诚
  • 当前回复7
  • 已保存回复7
  • 发布时间2025/7/30 12:05
  • 上次更新2025/7/30 16:53:57
查看原帖
关于高精度模板
371848
吴思诚楼主2025/7/30 12:05
#include<cstring>
#include<iostream>
#define mem cerr<<abs(&_v-&_u)/1024.0/1024<<"MB\n"
#define A *this
using ull=long long;
using ll=long long;
using namespace std;
const int N=51;//压位后位数
const int M=100010;//输入大数长度
const int W=10;//压位数
const ull k=1e10;//进制模数
bool _u;
int p;
char output[20];//用于printf输出前导0
struct bigInt{
    ull a[N];
    bigInt(){
        for(int i=0;i<=50;++i)a[i]=0;
    }
    template <typename T>
    bigInt(T x){
        for(int i=0;i<=50;++i)a[i]=0;
        a[1]=x;
    }
    void swap(bigInt &B){
        bigInt tmp=A;
        A=B;
        B=tmp;
    }
    void print(){
        for(int i=50;i;--i)
            printf(output,a[i]);
    }
    void operator=(const bigInt&B){
        for(int i=1;i<=50;++i)
            a[i]=1;//这里为什么会RE,而且只有调用a*b时会这样
    }
    void operator=(int B){
        a[1]=B;
    }
    bigInt operator+(const bigInt&B)const{
        bigInt C;
        int t=0;
        for(int i=1;i<=50;++i){
            C.a[i]=t+a[i]+B.a[i];
            t=C.a[i]/k;
            C.a[i]%=k;
        }
        return C;
    }
    bigInt operator*(const bigInt&B)const{
        bigInt C(1141514);
        for(int i=1;i<=50;++i){
            ll c;
            for(int j=1;j<=50;++j){
                c=(long double)a[i]*B.a[j]/k;
                ll tmp=a[i]*B.a[j]-c*k;
                if(tmp<0)tmp+=k;
                C.a[i+j-1]+=tmp;
                C.a[i+j]+=c;
                C.a[i+j]+=C.a[i+j-1]/k;
                C.a[i+j-1]%=k;
            }}
        for(int i=1;i<50;++i)
            C.a[i+1]+=C.a[i]/k,C.a[i]%=k;
        return C;
    }
    void operator+=(const bigInt&B){
        A=A+B;
    }
    void operator*=(const bigInt&B){//???
        A=A*B;
    }
    bool operator<(const bigInt&B)const{
        for(int i=50;i;--i)
            if(a[i]<B.a[i])return 1;
            else if(a[i]>B.a[i])return 0;
        return 0;
    }
    bool operator>(const bigInt&B)const{
        return !(A<B);
    }
    bool operator==(const bigInt&B)const{
        for(int i=50;i;--i)
            if(a[i]!=B.a[i])return 0;
        return 1;
    }
    bool operator!=(const bigInt&B)const{
        return !(A==B);
    }
    bool operator<=(const bigInt&B)const{
        return A<B||A==B;
    }
    bool operator>=(const bigInt&B)const{
        return A>B||A==B;
    }
    bigInt operator-(bigInt&B){
        if(A<B)swap(B);
        bigInt C=A;
        for(int i=1;i<=50;++i)
            if(C.a[i]<B.a[i])--C.a[i+1],C.a[i]=C.a[i]+k-B.a[i];
            else C.a[i]-=B.a[i];
        if(A>B)swap(B);
        return C;
    }
    void operator-=(bigInt&B){
        A=A-B;
    }
};
bool _v;
int main(){
    #ifdef LOCAL
    freopen("1.txt","r",stdin);
    #endif
    sprintf(output,"%%0%dllu",W);//写入前导0格式
    cin>>p;
    bigInt c;
    bigInt a(2),b(2);
    while(p){
        if(p&1)a*=b;
        b*=b;
        p>>=1;
    }
    a.print();
    return 0;
}

移步第 3636 行,在 121121 行调用 a*=b 后就RE了,返回 32212254773221225477

2025/7/30 12:05
加载中...