高精度模板(Big_Int)

你还在为刷题时看到的高精度预警而苦恼吗?

你还在因为高精度难写而放弃宝贵的分数吗?

不用担心!让高精度模板 来帮助你!

目前已知bug:

长度最大值只能开到54724,超过这个值会爆炸(无法调试),目前原因未知

2018.09.07更新:

1、优化了高精乘以低精的速度。
2、输出优化,可使用.output()函数输出。(基于输出挂)@Jesse666
ps:那个长度最大值的bug有可能是栈空间的问题,可以试试更改栈空间有没有效果

2018.09.01更新:

1、鉴于@Jesse666同学的建议,本次更新增加了基于牛顿迭代法的高精度开平方运算。
2、我还没有掌握好关于迭代次数的问题,所以在超大整数(过100位)可能会出现精度不正确的bug,请见谅。
3、目前迭代次数为长度length*10,有更好的迭代次数计算方法可以在我的blog下留言。

ps:下面那个更新实际上是8.31晚上11点更新的,然而更新完已经9.1了,所以标号为9.1。

pps:潜在bug:该高精度模板长度最大值只能设置为54724(或者更少),目前还没有找到明确原因,有人知道的可以私信我。

2018.09.01更新:

1、更改了代码顺序,并调整了格式,阅读代码以及调试代码更加方便。
2、四则运算现在可以参与int类型(即可以Big_Int+int。
3、完善了四则运算的+=、-=等操作。

2018.08.31更新:
在集训的最后一天,我学会了高精度除法,所以顺便做出来了除法的模板。本次更新内容:

1、增加++、--等递增递减运算,平均速度比+1要快。
2、增加高精度除法/运算。
3、增加高精度取模%运算。
4、增加了高精幂(请使用a.pow(b))运算。
5、增加了基于十进制的左移<<、右移>>运算(是基于十进制,所以等于*10和/10。比乘除法快)。
6、增加了使用int与char数组初始化的功能
7、修复了清空big_int的潜在正负性bug

如果以后发现bug,还会再更新的。喜欢请点个赞哦~

链接:https://pan.baidu.com/s/1axc75pHjtxD33U6V_6HBOg 密码: p0w7

转载请注明出处及作者。

#pragma GCC optinize(3)
#pragma comment(linker, "/STACK:102400000,10240000")
#include <bits/stdc++.h>
#define LEN 35663//100001
using namespace std;
class Big_Int{
    //54724
    private:
        int a[LEN];
        int len;
        bool f,p;
    public:
        //初始化构造函数运算 
        void clear()
        {
            memset(a,0,sizeof a);
            len=1;
           f=0;
            p=0;
        }        
        Big_Int()
        {
            clear();
        }
        Big_Int(int x)
        {
            clear();
            (*this)=x;
        }
        Big_Int(const Big_Int &x)
        {
            clear();
            (*this)=x;
        }
    
    //赋值运算 
     
        Big_Int operator =(const char *ch)
        {
            len=strlen(ch);
            if(ch[0]=='-') 
            {
                f=1;len--;
                for(int i=1;i<=len;i++) a[i]=ch[len-i+1]-'0';
                return *this;
            }
            for(int i=1;i<=len;i++) a[i]=ch[len-i]-'0';
            return *this;
        }
        Big_Int operator =(const int d)
        {
            char ch[LEN];
            sprintf(ch,"%d",d);
            *this=ch;
            return *this;
        }
    
    //四则基本运算 

        Big_Int operator +(Big_Int b)
        {
            if(f==1&&b.f==0)
            {
                f=0;
                return b-(*this);
            }
            else if(f==0&&b.f==1)
            {
                b.f=0;
                return (*this)-b;
            }
            Big_Int c;
            c.clear();
            int l=max(len,b.len);
            for(int i=1;i<=l;i++)
            {
                c.a[i]+=a[i]+b.a[i];
                c.a[i+1]=c.a[i]/10;
                c.a[i]%=10;
            }
            if(c.a[l+1]) l++;
            c.len=l;
            c.f=f&&b.f;
            return c;
        }
        Big_Int operator -(Big_Int b)
        {

            if(f==1&&b.f==0)
            {
                b.f=1;
                return b+(*this);
            }
            if(f==0&&b.f==1)
            {
                b.f=0;
                return b+(*this);
            }
            if(f==1&&b.f==1)
            {
                b.f=0;
                return b+(*this); 
            }
            Big_Int c;
            c.clear();
            if((*this)<b)
            {
                swap(*this,b);
                c.f=1;
            }
            int l=max(len,b.len);
            for(int i=1;i<=l;i++)
            {
                c.a[i]+=a[i]-b.a[i];
                if(c.a[i]<0) c.a[i+1]--,c.a[i]+=10;
            }
            while(c.a[l]==0&&l>0) l--;
            if(l==0) l=1;
            c.len=l;
            return c;
        }       
        Big_Int operator *(Big_Int b)
        {
            Big_Int c;
            c.clear();
            int l=len+b.len-1;
            for(int i=1;i<=len;i++)
            {
                for(int j=1;j<=b.len;j++)
                {
                    c.a[i+j-1]+=a[i]*b.a[j];
                    c.a[i+j]+=c.a[i+j-1]/10;
                    c.a[i+j-1]%=10;
                }
            }
            while(c.a[l+1]!=0) l++;
            c.len=l;
            if((f==0&&b.f==1)||(f==1&&b.f==0)) c.f=1;
            return c;
        }       
        Big_Int operator /(Big_Int b)
        {
            if(b==0)
            {
                throw 0x7f;
            }
            Big_Int a=(*this),c,t;
            c.len=len-b.len+1;
            for(int i=c.len;i>=0;i--)
            {
                if(a==0) break;
                t.clear();
                t=b<<i;
                while(t<=a) 
                {c.a[i+1]++;a-=t;}
            }
            while(!c.a[c.len]&&c.len>0) c.len--;
            if(c.len==0) c.len=1;
            if(f==0&&b.f==1) c.f=1;
            else if(f==1&&b.f==0) c.f=1;
            return c;
        }       
        Big_Int operator %(Big_Int b)
        {
            Big_Int c=(*this)/b;
            b=b*c;
            return (*this)-b;
        }       
        
        //比较运算 
        
        bool operator <=(Big_Int b)
        {
            return ((*this)<b||(*this)==b);
        }
        bool operator >=(Big_Int b)
        {
            return ((*this)>b||(*this)==b);
        }
        bool operator <(Big_Int b)
        {
            if((*this)==b) return 0;
            if(f&&!b.f) return 1;
            else if(!f&&b.f) return 0;
            else if(f==b.f&&f==0&&len<b.len) return 1;
            else if(f==b.f&&f==0&&len>b.len) return 0;
            else if(f==b.f&&f==1&&len<b.len) return 0;
            else if(f==b.f&&f==1&&len>b.len) return 1;
            else
            {
                int flag=-1;
                for(int i=len;i>=1;i--)
                {
                    if(a[i]>b.a[i]) 
                    {
                        flag=0;
                        break;
                    }
                    else if(a[i]<b.a[i])
                    {
                        break;
                    }
                }
                if(f==1) flag=!flag;
                else if(f==-1) return 0;
                return flag;
            }
        }   
        bool operator ==(Big_Int b)
        {
            if(f!=b.f) return 0;
            if(len!=b.len) return 0;
            for(int i=len;i>=1;i--)
            {
                if(a[i]!=b.a[i]) return 0;
            }
            return 1;
        }
        bool operator !=(Big_Int b)
        {
            return !((*this)==b);
        }
        bool operator !=(int b)
        {
            return !((*this)==b);
        }
        bool operator ==(int b)
        {
            Big_Int a=b;
            return a==(*this);
        }
        bool operator >(Big_Int b)
        {
            if(!((*this)<b)&&!((*this)==b)) return 1;
            else return 0;
        }

        //递增(减)运算 

        Big_Int operator ++(int)
        {
            if(f==1&&!p) 
            {
                p=1;
                (*this)--;
                return *this;
            }
            a[1]++;
            int t=1;
            while(a[t]>9) 
            {
                a[t+1]++;
                a[t]%=10;
                t++;
            }
            while(a[len+1]) len++;
            if(len==1&&a[len]==0) f=0;
            p=0;
            return *this;
        }
        Big_Int operator --(int)
        {
            if(f==1&&!p)
            {
                (*this)++;
                p=1;
                return *this;
            }
            int t=1;
            a[t]--;
            while(a[t]<0)
            {
                a[t+1]--;
                a[t]+=10;
                t++;
            }
            while(a[len+1]) len++;
            if(len==1&&a[len]==0) f=0;
            p=0;
            return *this;
        }
        
        //四则运算扩展 
        
            //加法相关 
            
        Big_Int operator +(int b)
        {
            Big_Int c=b;
            return (*this)+c;
        }       
        void operator +=(Big_Int b)
        {
            (*this)=(*this)+b;
        }
        void operator +=(int b)
        {
            Big_Int c=b;
            (*this)=(*this)+c; 
        }
            //减法相关 
                
        Big_Int operator -(int b)
        {
            Big_Int c=b;
            return (*this)-c;
        }
        void operator -=(Big_Int b)
        {
            (*this)=(*this)-b;
        }
        void operator -=(int b)
        {
            Big_Int c=b;
            (*this)=(*this)-b;
        }       
        
            //乘法相关 
        inline Big_Int operator *(int b)
        {
            /*Big_Int c=b;
            return (*this)*c;*/
            Big_Int c;
            for(int i=1;i<=len;i++)
            {
                c.a[i]+=a[i]*b;
                c.a[i+1]=c.a[i]/10;
                c.a[i]%=10;
            }
            c.len=len;
            while(c.a[c.len+1]!=0)
            {
                c.len++;
                c.a[c.len+1]=c.a[c.len]/10;
                c.a[c.len]%=10;
            }
            return c;
        }
        void operator *=(Big_Int b)
        {
            (*this)=(*this)*b;
        }
        void operator *=(int b)
        {
            (*this)=(*this)*b;
        }
        
            //除法相关 
            
        Big_Int operator /(int b)
        {
            Big_Int c=b;
            return (*this)/c;
        }
        void operator /=(int b)
        {
            Big_Int c=b;
            (*this)=(*this)/c;          
        }       
        void operator /=(Big_Int b)
        {
            (*this)=(*this)/b;
        }
        
            //取模相关 
                
        Big_Int operator %(int b)
        {
            Big_Int c=b;
            return (*this)%c;
        }       
        void operator %=(int b)
        {
            Big_Int c=b;
            (*this)=(*this)%c;
        }
        void operator %=(Big_Int b)
        {
            (*this)=(*this)%b;
        }          
           
        //特殊运算   
        Big_Int abs()
        {
            Big_Int c=(*this);
            c.f=0;return c;
        }
        Big_Int operator <<(int b)  //基于十进制的左移,所以是等同于*10 
        {
            Big_Int c;
            for(int i=b+1;i<=len+b;i++) 
              c.a[i]=a[i-b];
            c.len=len+b;
            return c;
        }
        Big_Int operator >>(int b)
        {
            Big_Int c;
            for(int i=b+1;i<=len;i++)
            {
                c.a[i-b]=a[i];
            }
            c.len=len-b;
            return c;
        }
        Big_Int pow(Big_Int b)
        {
            Big_Int a=(*this),ans=1;
            while(b!=0)
            {
                if(b.a[1]&1) ans*=a;
                a*=a;
                b/=2;
            }
            return ans;
        }
        Big_Int pow(int b)
        {
            Big_Int a=(*this),ans=1;
            while(b!=0)
            {
                if(b&1) ans*=a;
                a*=a;
                b/=2;
            }
            return ans; 
        }
        Big_Int sqrt()
        {
            Big_Int x=(*this),a=(*this);
            for(int i=1;i<=len*10;i++) x=(x+a/x)/2;
            return x;
        }
        //转换函数 
        void output()
        {
            while(a[len]==0) --len;
            for(register int i=len;i>=1;--i) putchar(a[i]+'0');
        }
        string cpp_str()
        {
            string str;
            if(len==0)
            {
                str="0";
                return str;
            }
            if(f==1&&!(len==1&&a[1]==0)) str.push_back('-');
            for(int i=len;i>=1;i--) str.push_back(a[i]+'0');
           return str;
        }
}a;
istream& operator >>(istream &in,Big_Int &a)
{
    string str;
    in>>str;
    a=str.c_str();
    return in;
}
ostream& operator <<(ostream &out,Big_Int &a)
{
    out<<a.cpp_str();
    return out;
}
int main()
{
}

转载于:https://www.cnblogs.com/MaxDYF/p/9792688.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值