高精模板

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define The Bignum of AK
#define FOR(i,x,y) for(int i=(x);i<=(y);i++)
#define DOR(i,x,y) for(int i=(x);i>=(y);i--)
char str[1005]; 
struct Bignum{
    static const int P=10000,M=1005;
    int num[M],len;
    Bignum(){memset(num,0,sizeof(num));len=1;}//左闭右开,len仅表示长度 
    void Read(){
        scanf("%s",str);
        len=0;
        int sz=strlen(str);
        for(int i=sz-1;i>=0;i-=4){
            num[len]=0;
            for(int j=max(0,i-3);j<=i;j++){
                num[len]=(num[len]<<3)+(num[len]<<1)+(str[j]^48);
            }
            len++;
        }
        while(len>1&&!num[len-1])len--;
    }

    void print(){
        printf("%d",num[len-1]);
        DOR(i,len-2,0)printf("%04d",num[i]);
        puts("");
    }

    bool operator <(const Bignum &p)const{
        if(len!=p.len)return len<p.len;
        for(int i=len-1;i>=0;i--)if(num[i]!=p.num[i])return num[i]<p.num[i];
        return false;
    }
    bool operator >(const Bignum &p)const{return p<*this;}
    bool operator <=(const Bignum &p)const{return !(p<*this);}
    bool operator ==(const Bignum &p)const{return !(p<*this||*this<p);}
    bool operator !=(const Bignum &p)const{return p<*this||*this<p;} 

    Bignum operator +(const int &p)const{
        Bignum B;B=*this;
        B.num[0]+=p;
        int step=0;
        while(B.num[step]>=P){
            B.num[step]-=P;
            B.num[step+1]++;
            step++;
        }
        while(B.num[len])B.len++;
        return B;
    }

    Bignum operator +(const Bignum &A)const{
        Bignum B;
        B.len=max(len,A.len);
        FOR(i,0,B.len-1){
            B.num[i]+=A.num[i]+num[i];
            if(B.num[i]>=P){
                B.num[i]-=P;
                B.num[i+1]++;   
            }
        }
        while(B.num[B.len])B.len++;
        return B;
    }

    Bignum operator -(const int &p)const{//保证非负 
        Bignum B;B=*this;
        B.num[0]-=p;
        int step=0;
        while(B.num[step]<0){
            B.num[step]+=P;
            B.num[step+1]--;
            step++;     
        }
        while(B.len>1&&!B.num[B.len-1])B.len--;
        return B;
    }

    Bignum operator -(const Bignum &A)const{
        Bignum B;
        B.len=max(len,A.len);
        FOR(i,0,B.len-1){
            B.num[i]+=num[i]-A.num[i];
            if(B.num[i]<0){
                B.num[i]+=P;
                B.num[i+1]--;
            }
        }
        while(B.len>1&&!B.num[B.len-1])B.len--;
        return B;
    }

    Bignum operator *(const int &p)const{
        Bignum B;
        B.len=len;
        FOR(i,0,len-1){
            B.num[i]+=num[i]*p;
            if(B.num[i]>=P){
                B.num[i+1]+=B.num[i]/P; 
                B.num[i]%=P;
            }
        }
        while(B.num[B.len])B.len++;
        return B;
    }

    Bignum operator *(const Bignum &A)const{
        Bignum B;
        B.len=len+A.len-1;
        FOR(i,0,len-1){
            FOR(j,0,A.len-1){
                B.num[i+j]+=num[i]*A.num[j];
                if(B.num[i+j]>=P){
                    B.num[i+j+1]+=B.num[i+j]/P;
                    B.num[i+j]%=P;
                }
            }
        }
        while(B.num[B.len])B.len++;
        return B;
    }

    Bignum operator /(const int &p)const{
        Bignum B;B=*this;
        DOR(i,B.len-1,0){
            if(i)B.num[i-1]+=B.num[i]%p*P;
            B.num[i]/=p;
        }
        while(B.len>1&&!B.num[B.len-1])B.len--;
        return B;   
    }
    Bignum operator /(const Bignum &A)const{
        Bignum l,r,res;
        if(*this<A)return res;
        r=*this;
        while(l<=r){
            Bignum mid=(l+r)/2;
            if((mid*A)<=*this){
                res=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        return res;
    } 
};
int main(){
    Bignum A,B;
    A.Read();
    B.Read();
    Bignum C=A/B;
    C.print();
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值