【高精度】模板

 自己上学期花了三天打的,long long压9位,支持进制转换,加减乘除,字符串初始化,运算等等,应该无敌了
 2019.5.12 修复了除法的一个微小的溢出bug。另外,注意此处除法和取模尚不支持涉及负数的计算,如有需要请根据绝对值的除法和模进行分类讨论。
 这个模板是一个中等模板,长于ACM竞赛使用的模板,短于需要进一步进行优化的通用模板。四则运算都遵守的是竖式法,没有任何算法的优化。

/*
 * Last updated on 2018-12-11
 * Author:Max.D.
 * Lang:C++98
 * Description:Large integer number arithmetic->hugeint struct
 *     overriding method: + - * / % = == < > <= >= <<(*base) >>(/base) abs print equals_zero.
 *     Initialization: by C's char array or const char array, or by numbers.
 *     max_size shows the length of the data array.
 *     LL means the basic data type.
 *     L means Compression bits in based.
 *     inc means the IO-base for input and output(2-16)
 *     base equals inc^L which is the real base used in operations.
 *
 * */
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

typedef long long LL; //for 64-bit computer long long is the best choice. 
const int max_size=240;
const int L=9; 
const int inc=10;
const LL base=1000000000; //base shouldn't be larger than 2^31 

char buf[20];
int len;

void write(LL x,bool fill)
{
    while(x>0)
        buf[len++]=x%inc+'0',x/=inc;
    if(fill)
        while(len<L)
            buf[len++]='0';
    while(len)
        putchar(buf[--len]);
}

struct hugeint
{
    LL d[max_size],l;
    bool sig;

    inline bool equals_zero() const
    {
        return l==0;
    }

    void operator=(const char *p)
    {
        int f=strlen(p);
        const char *q=p;
        sig=false;
        if(p[0]=='-')
        {
            --f;
            ++q;
            sig=true;
        }
        l=f/L;
        for(int i=0;i<l;i++)
        {
            d[i]=0;
            for(int j=L;j>=1;j--)
                d[i]=d[i]*inc+q[f-i*L-j]-'0';
        }
        if(f%L)
        {
            d[l]=0;
            for(int i=L*l;i<f;i++)
                d[l]=d[l]*inc+q[i-L*l]-'0';
            l++;
        }
        while(l>0&&d[l-1]==0)
            --l;
    }

    void operator=(const hugeint &t)
    {
        l=t.l,sig=t.sig;
        for(int i=0;i<l;i++)
            d[i]=t.d[i];
    }

    void operator=(char *p)
    {
        const char *x=p;
        *this=x;
    }

    template<class T1>
    void operator=(T1 x)
    {
        char str[max_size*L];
        int t=0;
        T1 y=x;
        while(y!=0)
            y/=inc,++t;
        str[t]='\0';
        if(x<0)
            str[0]='-',t++,x=-x;
        while(x!=0)
        {
            str[--t]=x%inc+'0';
            x/=inc;
        }
        *this=str;
    }

    template<class T1>
    hugeint(const T1 &x)
    {
        *this=x;
    }

    hugeint()
    {
    }

    bool operator==(const hugeint &t) const
    {
        if((sig^t.sig)&&(l>0||t.l>0)||l!=t.l)
            return false;
        for(int i=0;i<l;i++)
            if(d[i]!=t.d[i])
                return false;
        return true;
    }

    bool operator<(const hugeint &t) const
    {
        if(sig&&!t.sig)
            return true;
        if(l<t.l)
            return sig^true;
        if(l>t.l)
            return sig^false;
        int i;
        for(i=l-1;i>=0&&t.d[i]==d[i];i--);
        if(i<0)
            return false;
        if(d[i]<t.d[i])
            return sig^true;
        if(d[i]>t.d[i])
            return sig^false;
    }

    bool operator>(const hugeint &t) const
    {
        return t<*this;
    }

    bool operator<=(const hugeint &t) const
    {
        return *this<t||*this==t;
    }

    bool operator>=(const hugeint &t) const
    {
        return t<=*this;
    }

    hugeint operator-() const
    {
        hugeint res=*this;
        res.sig^=true;
        return res;
    }

    hugeint abs() const
    {
        hugeint res=*this;
        res.sig=false;
        return res;
    }

    template<class T1>
    hugeint operator<<(T1 k) const
    {
        hugeint res=*this;
        res.l+=k;
        for(int i=res.l-1;i>=k;i--)
            res.d[i]=res.d[i-k];
        for(int i=k-1;i>=0;i--)
            res.d[i]=0;
        return res;
    }

    template<class T1>
    hugeint operator>>(T1 k) const
    {
        hugeint res=*this;
        res.l=max(res.l-k,(LL) 0);
        for(int i=0;i<res.l;i++)
            res.d[i]=res.d[i+k];
        return res;
    }

    hugeint operator+(const hugeint &t) const
    {
        hugeint res;
        res.sig=sig;
        res.l=max(l,t.l);
        LL c=0,k;
        if(t.sig^sig)
        {
            k=this->abs()<t.abs()?1:-1;
            res.sig^=k==1;
            for(int i=0;i<res.l;i++)
            {
                res.d[i]=((i<t.l?t.d[i]:0)-(i<l?d[i]:0))*k-c;
                c=res.d[i]<0;
                res.d[i]+=c*base;
            }
            while(res.l>0&&res.d[res.l-1]==0)
                --res.l;
            return res;
        }
        for(int i=0;i<res.l;i++)
        {
            res.d[i]=(i<l?d[i]:0)+(i<t.l?t.d[i]:0)+c;
            c=res.d[i]>=base;
            res.d[i]-=c*base;
        }
        if(c)
            res.d[res.l++]=c;
        return res;
    }

    hugeint operator-(const hugeint &t) const
    {
        return *this+(-t);
    }

    hugeint operator*(const hugeint &t) const
    {
        hugeint res;
        if(t.equals_zero()||this->equals_zero())
        {
            res.l=0;
            return res;
        }
        res.sig=sig^t.sig;
        res.l=l+t.l-1;
        LL c;
        for(int i=0;i<=res.l;i++)
            res.d[i]=0;
        for(int i=0;i<l;i++)
        {
            c=0;
            for(int j=0;j<t.l;j++)
            {
                res.d[i+j]=res.d[i+j]+d[i]*t.d[j]+c;
                c=res.d[i+j]/base;
                res.d[i+j]%=base;
            }
            if(c)
                res.d[i+t.l]=c;
        }
        if(res.d[res.l])
            ++res.l;
        while(res.l>0&&res.d[res.l-1]==0)
            --res.l;
        return res;
    }

    hugeint operator/(const hugeint &t) const
    {
        hugeint res,ss=t.abs();
        res.sig=sig^t.sig;
        if(t.equals_zero()||this->abs()<ss)
            res.l=0;
        else
        {
            res.l=l-t.l+1;
            LL le,re,me;
            hugeint r=*this>>(res.l-1);
            for(int i=res.l-1;i>=0;i--)
            {
                le=((r.l>=t.l?r.d[t.l-1]:0)+(r.l>t.l?r.d[t.l]*base:0))/(t.d[t.l-1]+1);
                re=((r.l>=t.l?r.d[t.l-1]:0)+(r.l>t.l?r.d[t.l]*base:0)+1)/t.d[t.l-1];
                while(le<re)
                {
                    me=re+le+1>>1;
                    if(r<t*me)
                        re=me-1;
                    else
                        le=me;
                }
                res.d[i]=le;
                if(i>0)
                	r=(r-ss*le<<1)+d[i-1];
            }
            if(res.d[res.l-1]==0)
                --res.l;
        }
        return res;
    }

    hugeint operator%(const hugeint &x) const
    {
        hugeint tmp=this->abs()/x*x;
        return this->sig?*this+tmp+x.abs():*this-tmp;
    }

    void print()
    {
        if(l==0)
        {
            puts("0");
            return;
        }
        if(sig)
            putchar('-');
        write(d[l-1],false);
        for(int i=l-2;i>=0;i--)
            write(d[i],true);
        puts("");
    }
}A,B,C,M;

char s1[max_size*L],s2[max_size*L];
int n,tot;

int main()  //some samples.
{
	scanf("%s",s1);
	scanf("%s",s2);
	A=s1;
	B=s2;
	(A/B).print();
	return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值