【模板】Sirius 的压位高精度数、高精度分数模板结构体版 18.8.4

这个高精度模板经多次调试应该没什么问题

使用说明

参数

  • M a x l :你的高精度数可能达到的长度(压位后)
  • B i t :压了几位( 10 B i t )进制
  • B a s e :进制数,表示这个高精是 B a s e 进制的。
  • a i :高精位。
  • l e n :高精数的长度,初值为1。

请始终保持 10 B i t = B a s e 请注意不要和模板内的变量名重名,尤其是模板中有一个数组 a i

功能

本模板支持:
1、高精 + 高精(单精)
2、高精 高精(单精)(请务必保证非负)
3、高精 × 高精(单精)
5、高精 ÷ 单精
6、高精 g c d (最大公约数)
7、高精度之间的基本比较、高精度与int的基本比较
8、基本输入输出,数字串转高精度数

赠品

现在还赠送“高精度分数”模板一份!
支持输入输出、基本比较、加减乘除(注意不要除0)、取倒数。注意分数初值是 0 / 1
这个高精度分数模板需要建立在我的高精度模板之上。
参数小说明:

  • m o t h e r 分母
  • s o n 分子

在高精度模板的代码下面的“BigFraction”类就是高精分数了。

注意事项

1、高精度乘法没有用 F F T 优化
2、大部分没有定义为友元函数,所以如果要使用堆的话请自行改成友元函数

#define Maxl 200
#define Base 100000000
#define Bit 8

struct Bigint
{
    LL a[Maxl];int len;
    Bigint() {len=1;memset(a,0,sizeof a);};
    void Scan()
    {
        R char str[Maxl];scanf("%s",str);strin(str);
    }
    void strin(R char *p)
    {
        R int tmpl = strlen(p);
        for (R int i=tmpl-1; i>=0; i-=Bit,++len)
        {
            for (R int j=0,rem=1; j<Bit; ++j,rem*=10)
            {
                (i>=j) ? a[len] += ((*(p+i-j))-'0')*rem : 0;
            }
        } 
        --len;
        return ;
    }
    void Cls() 
    { 
        while(!a[len] && len!=1) --len; 
        while(a[len+1]) ++len; 
        return ;
    }
    void Print() 
    {
        Cls(); printf("%lld",a[len]);
        for(int i=len-1;i>0;--i) printf("%08lld",a[i]);
        return;
    }
    int operator = (const int B)
    {
        len=1;
        if(B >= Base) a[len]=Base,a[++len]=B%Base;
        else a[len]=B;
        return B;
    }
    bool operator < (const Bigint & B)
    {
        if(len!=B.len) return len < B.len;
        for (int i=len;i>0;--i) if(a[i] != B.a[i]) return a[i] < B.a[i];
        return 0;
    }
    bool operator == (const Bigint & B)
    {
        if(len!=B.len) return 0;
        for (int i=len;i>0;--i) if(a[i]!=B.a[i]) return 0;
        return 1;
    }
    bool operator <= (const Bigint & B) {return ((*this)<B)||((*this)==B);}
    bool operator > (const Bigint & B) {return !((*this)<=B);}
    bool operator >= (const Bigint & B) {return !((*this)<B);}
    bool operator != (const Bigint & B) {return !((*this)==B);}

    bool operator < (const int & B) {return (a[2]*Base+a[1]<B);}
    bool operator == (const int & B) {return (a[2]*Base+a[1]==B);}
    bool operator <= (const int & B) {return ((*this)<B)||((*this)==B);}
    bool operator > (const int & B) {return !((*this)<=B);}
    bool operator >= (const int & B) {return !((*this)<B);}
    bool operator != (const int & B) {return !((*this)==B);} 

    Bigint operator - (const Bigint & B)
    {
        Bigint ret=*this;
        for (int i=1;i<=len && i<=B.len;++i)
        {
            if(ret.a[i] < B.a[i]) ret.a[i] += Base,--ret.a[i+1];    
            ret.a[i] -= B.a[i];
        }
        ret.Cls();return ret;       
    }
    Bigint operator + (const Bigint & B)
    {
        Bigint ret=*this;
        for (int i=1;i<=len && i<=B.len;++i)
        {
            if(ret.a[i]+B.a[i] >= Base) ret.a[i] -= Base,++ret.a[i+1];
            ret.a[i] += B.a[i];
        }
        ret.Cls();return ret;
    }
    Bigint operator * (const int & B)
    {
        Bigint ret=*this;
        for (int i=1;i<=len;++i) ret.a[i] *= B;
        for (int i=1;i<=len;++i) ret.a[i+1] += ret.a[i] / Base,ret.a[i] %= Base;
        ret.Cls(); return ret;
    }
    Bigint operator * (const Bigint & B)
    {
        Bigint ret;
        for (int i=1;i<=len;++i)
         for (int j=1;j<=B.len;++j)
          ret.a[i+j-1] += a[i] * B.a[j];
        for (int i=1;i<len+B.len-1;++i) ret.a[i+1] += ret.a[i] / Base,ret.a[i] %= Base;
        for (ret.len=len+B.len-1;ret.a[ret.len] > Base; ++ret.len)
        {
            ret.a[ret.len+1] += ret.a[ret.len] / Base,ret.a[ret.len] %= Base;
        }
        ret.Cls(); return ret;
    }
    Bigint operator / (const int & B)
    {
        Bigint ret=*this;
        for (int i=ret.len;i>0;--i) ret.a[i-1] += (ret.a[i] % B) * Base,ret.a[i] /= B;
        ret.Cls(); return ret; 
    }
    friend Bigint gcd(R Bigint xx,R Bigint yy)
    {
        R Bigint ret;ret=1;
        while(xx != yy)
        {
            while(!(xx.a[1]&1) && !(yy.a[1]&1)) xx=xx/2,yy=yy/2,ret=ret*2;
            while(!(xx.a[1]&1)) xx=xx/2;
            while(!(yy.a[1]&1)) yy=yy/2;
            if(xx < yy) {swap(xx,yy);}
            if(xx == yy) break ;
            xx=xx-yy;
        }
        ret = ret*xx;
        return ret;
    }
};
struct BigFraction
{
    Bigint son,mother;
    BigFraction(){son=0;mother=1;}
    void Scan()
    {
        R int tmpm,tmps;
        scanf("%d %d",&tmps,&tmpm);
        son=tmps,mother=tmpm;
    }
    void Print()
    {
        son.Print();putchar('/');mother.Print();
    }
    BigFraction inv()
    {
        R BigFraction ret=(*this);swap(ret.mother,ret.son);return ret;
    }
    BigFraction operator + (const BigFraction &B)
    {
        R BigFraction ret;
        ret.mother=mother*B.mother;
        R Bigint tmp1,tmp2;
        tmp1=son*B.mother,tmp2=mother*B.son;
        ret.son=tmp1+tmp2;
        return ret;
    }
    BigFraction operator - (const BigFraction &B)
    {
        R BigFraction ret;
        ret.mother=mother*B.mother;
        R Bigint tmp1,tmp2;
        tmp1=son*B.mother,tmp2 = mother*B.son;
        ret.son=tmp1-tmp2;
        return ret;
    }
    BigFraction operator * (const int &B)
    {
        R BigFraction ret= *this;
        ret.son=ret.son*B; 
        return ret;
    }
    BigFraction operator * (const BigFraction &B)
    {
        R BigFraction ret= *this;
        ret.son=ret.son*B.son;ret.mother=ret.mother*B.mother; 
        return ret;
    }
    BigFraction operator / (BigFraction &B)
    {
        R BigFraction ret= *this;return ret*B.inv();
    }
    bool operator == (const BigFraction &B)
    {
        R Bigint tmp1,tmp2;
        tmp1=son*B.mother,tmp2 = mother*B.son;
        return tmp1==tmp2;
    }
    bool operator < (const BigFraction &B)
    {
        R Bigint tmp1,tmp2;
        tmp1=son*B.mother,tmp2 = mother*B.son;
        return tmp1<tmp2;
    }
    bool operator <= (const BigFraction & B) {return ((*this)<B)||((*this)==B);}
    bool operator > (const BigFraction & B) {return !((*this)<=B);}
    bool operator >= (const BigFraction & B) {return !((*this)<B);}
    bool operator != (const BigFraction & B) {return !((*this)==B);}
};

猜你喜欢

转载自blog.csdn.net/qq_42814118/article/details/81413598