[模板] 高精

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
struct Big
{
    static const int BASE=100000000;
    static const int WIDTH=8;
    long long s[10000],len;
    Big()
    {
        memset(s,0,sizeof(s));len=0;
    }
    void read()
    {
        char str[5005];
        scanf("%s",str);
        int w=1;
        for(int i=strlen(str)-1;i>=0;i--)
        {
            s[len]+=w*(str[i]-'0');
            w*=10;
            if(w==BASE){w=1;if(i!=0)len++;}
        }
    }
    Big operator +(int b)
    {
        Big x=*this;
        x.s[0]+=b;
        for(int i=0;i<=x.len;i++)
        {
            x.s[i+1]+=x.s[i]/BASE;
            x.s[i]%=BASE;
        }
        if(x.s[x.len+1])x.len++;
        return x;
    }
    Big operator + (const Big &b)
    {
        Big x;
        for(int i=0;i<=len||i<=b.len;i++)
        {
            if(i<=b.len)x.s[i]+=b.s[i];
            if(i<=len)x.s[i]+=s[i];
            x.s[i+1]+=x.s[i]/BASE;
            x.s[i]%=BASE;
        }
        x.len=max(len,b.len);
        if(x.s[x.len+1])x.len++;
        return x;
    }   
    Big operator - (const Big &b)
    {
        Big x;
        for(int i=0;i<=len;i++)
        {
            while(s[i]<b.s[i])
            {
                s[i]+=BASE;s[i+1]--;
            }
            x.s[i]=s[i]-b.s[i];
        }
        x.len=len;
        while(x.s[x.len]==0&&x.len)x.len--;
        return x;
    }
    Big operator *(int b)
    {
        Big x;
        for(int i=0;i<=len;i++)
        {
            x.s[i]+=s[i]*b;
            x.s[i+1]+=x.s[i]/BASE;
            x.s[i]%=BASE;
        }
        x.len=len;
        if(x.s[x.len+1])x.len++;
        return x;
    }
    Big operator *(const Big &b)
    {
        Big x;
        for(int i=0;i<=len;i++)
          for(int j=0;j<=b.len;j++)
          {
            x.s[i+j]+=s[i]*b.s[j];
            x.s[i+j+1]+=x.s[i+j]/BASE;
            x.s[i+j]%=BASE;
          }
        x.len=len+b.len;
        if(x.s[len+b.len+1])x.len++;
        return x;
    }
    Big operator / (int b)
    {
        Big x;
        for(int i=len;i>0;i--)
        {
            x.s[i]=s[i]/b;
            s[i-1]+=(s[i]%b)*BASE;  
        }
        x.s[0]=s[0]/b;
        x.len=len;
        while(x.s[x.len]==0&&x.len)x.len--;
        return x;
    }
    Big operator /(const Big &c)
    {
        Big x,f,b=c;
        for(int i=len;i>=0;i--)
        {
            f=f+s[i];
            if(f>=b) 
            {
                int l=0,r=BASE;
                while(l<r)
                {
                    int mid=(l+r+1)>>1;
                    if(f>=(b*mid))l=mid;
                    else r=mid-1;
                }
                x.s[i]=l;f=f-(b*l);
            }
            f=f*BASE;
        }
        x.len=len-b.len+1;
        while(x.s[x.len]==0&&x.len)x.len--;
        return x;
    }   
    bool operator <(const Big &b)
    {
        if(len<b.len)return true;
        if(len>b.len)return false;
        for(int i=len;i>=0;i--)
        {
            if(s[i]<b.s[i])return true;
            if(s[i]>b.s[i])return false;
        }
        return false;
    }
    bool operator >=(const Big &b){
        if (*this < b) return 0; return 1;
    }
    void print()
    {
        printf("%d",s[len]);
        for(int i=len-1;i>=0;i--)
        printf("%08d",s[i]);
    }
};
int main()
{
    Big a,b;
    a.read();b.read();
    (a+b).print();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值