跳到正文

科技·工程

高精度结构体

qcf2010约 10 分钟
原文

这是 2025年9月24日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月11日
篇幅
约 10 分钟

版本

  1. v3当前
  2. v2
  3. v1
C++
#define ll long long
struct BigInt{
    
    ll value[12501];
    int p;
    
    BigInt(){
        p=1;
        for(int i=1;i<12501;++i) value[i]=0;
    }
    
    void GiveValue(string s){
        reverse(s.begin(),s.end());
        int len=s.size();
        p=0;
        for(int i=1;i<12501;++i) value[i]=0;
        if(s[len-1]=='-'){
            value[0]=1;
            s=s.substr(0,len-1);
        }else value[0]=0;
        while(s.size()%8) s+="0";
        for(int i=0;i+7<=s.size();i+=8){
            string sub=s.substr(i,8);
            ++p;
            for(int j=0;j<8;++j) value[p]+=pow(10,j)*(sub[j]-'0');
        }
    }

    void GiveValue(ll v){
        string s=to_string(v);
        GiveValue(s);
    }

    void Read(){
        string s;
        cin>>s;
        GiveValue(s);
    }

    void Print(string s){
        if(value[1]==0&&p==1){
			cout<<"0";
            cout<<s;
			return;
		}
        if(value[0]==1) cout<<'-';
        for(int i=p;i>=1;--i){
            if(i!=p&&value[i]<10000000) cout<<setfill('0')<<setw(8);
            cout<<value[i];
        }
        cout<<s;
    }
    
    int CompareWith(BigInt b){
        if(value[0]!=b.value[0]) return value[0]?0:2;
        if(p!=b.p) return p>b.p?2:0;
        for(int i=p;i>=1;--i) {
            if(value[i]>b.value[i]) return 2;
            if(value[i]<b.value[i]) return 0;
        }
        return 1;
    }
    
    int CompareWith(ll b){
        BigInt tmp;
        tmp.GiveValue(b);
        return CompareWith(tmp);
    }

    void Add(BigInt b){
        if(value[0]!=b.value[0]){
            b.value[0]=value[0];
            Subtract(b);
            return;
        }
        int carry=0;
        int max_p=max(p,b.p);
        for(int i=1;i<=max_p;++i){
            value[i]+=b.value[i]+carry;
            carry=value[i]/100000000;
            value[i]%=100000000;
        }
        if(carry) value[++max_p]=carry;
        p=max_p;
    }
    
    void Add(ll b){
        BigInt tmp;
        tmp.GiveValue(b);
        Add(tmp);
    }

    void Subtract(BigInt b){
        if(value[0]!=b.value[0]){
            b.value[0]=value[0];
            Add(b);
            return;
        }
        if(CompareWith(b)==0){
            BigInt tmp=b;
            b=*this;
            *this=tmp;
            value[0]^=1;
        }
        int carry=0;
        for(int i=1;i<=p;++i){
            value[i]-=b.value[i]+carry;
            if(value[i]<0){
                value[i]+=100000000;
                carry=1;
            }else carry=0;
        }
        while(p>1&&value[p]==0) --p;
    }

    void Subtract(ll b){
        BigInt tmp;
        tmp.GiveValue(b);
        Subtract(tmp);
    }
 
    void Multiply(BigInt b){
        BigInt res;
        res.p=p+b.p;
        for(int i=1;i<=p;++i){
            for(int j=1;j<=b.p;++j){
                res.value[i+j-1]+=value[i]*b.value[j];
                res.value[i+j]+=res.value[i+j-1]/100000000;
                res.value[i+j-1]%=100000000;
            }
        }
        while(res.p>1&&res.value[res.p]==0) --res.p;
        res.value[0]=value[0]^b.value[0];
        *this=res;
    }

    void Multiply(ll b){
        BigInt tmp;
        tmp.GiveValue(b);
        Multiply(tmp);
    }

    void Divide(BigInt b){
        if(b.p==1&&b.value[1]==0) throw runtime_error("Division by zero!");
        BigInt res,current;
        res.p=p;
        for(int i=p;i>=1;--i){
            current.Multiply(100000000);
            current.Add(value[i]);
            ll l=0,r=99999999,ans=0;
            while(l<=r) {
                ll mid=(l+r)/2;
                BigInt tmp=b;
                tmp.Multiply(mid);
                if(tmp.CompareWith(current)<=1){
                    ans=mid;
                    l=mid+1;
                }else r=mid-1;
            }
            res.value[i]=ans;
            BigInt tmp=b;
            tmp.Multiply(ans);
            current.Subtract(tmp);
        }
        while(res.p>1&&res.value[res.p]==0) --res.p;
        res.value[0]=value[0]^b.value[0];
        *this=res;
    }

    void Divide(ll b){
        BigInt tmp;
        tmp.GiveValue(b);
        Divide(tmp);
    }
};

BigInt operator +(BigInt a,BigInt b){
    a.Add(b);
    return a;
}

BigInt operator +(BigInt a,ll b){
    a.Add(b);
    return a;
}

BigInt operator -(BigInt a,BigInt b){
    a.Subtract(b);
    return a;
}

BigInt operator -(BigInt a,ll b){
    a.Subtract(b);
    return a;
}
 
BigInt operator *(BigInt a,BigInt b){
    a.Multiply(b);
    return a;
}

BigInt operator *(BigInt a,ll b){
    a.Multiply(b);
    return a;
}

BigInt operator /(BigInt a,BigInt b){
    a.Divide(b);
    return a;
}

BigInt operator /(BigInt a,ll b){
    a.Divide(b);
    return a;
}

BigInt operator %(BigInt a,BigInt b){
    BigInt c=a/b;
    return a-b*c;
}

BigInt operator %(BigInt a,ll b){
    BigInt c=a/b;
    return a-c*b;
}