跳到正文

剪贴板

剪贴板 hpeasi2j

原文
剪贴板信息

剪贴板信息

收录
更新

大整型(bigint)

版本:1.1

用位压缩技术,将八位压成一位,增加有效位数(感谢 Su_xu\color{Black}\textup{Su\_xu} 的宝贵意见)

先看前情提要,再看这个

注:高精除有点问题,请暂时不要使用

Code:

C++
#ifndef BIGINT_H  //bigint.h 1.1
#define BIGINT_H
#include "algorithm"
#include "iostream"
#include "cstring"
#define SIZE int(st.size())
#define BIT int(1e8)
using namespace std;
struct bigint{
    long long s[1010];
    int f=1,l=1,tl=1;
    inline bool zero(){
        return (l<2&&!s[1]);
    }
    inline void tzero(){
        memset(s,0,sizeof(s));
        f=l=tl=1;
    }
    inline void resize(){
        l=1000;
        while (!s[l]&&l>1) l--;
        tl=(l-1)<<3;
        for (int i=8;i;i--)
            if ((s[l]%BIT)/pow(10,i)){
                tl+=i;
                break;
            }
    }
    inline void get(){
        string st;
        tzero();
        cin>>st;
        if (st[0]=='-'){
            f=-1;
            st.erase(0,1);
        }
        if (st[0]=='0'){
            f=1;
            return;
        }
        tl=SIZE;
        while (SIZE>8){
            for (int j=SIZE-8;j<SIZE;j++)
                s[l]=(s[l]<<3)+(s[l]<<1)+(st[j]-'0');
            st.erase(SIZE-8,8);
            l++;
        }
        for (int i=0;i<SIZE;i++)
        	s[l]=(s[l]<<3)+(s[l]<<1)+(st[i]-'0');
    }
    inline void put(){
        if (f==-1) printf("-");
        printf("%llu",s[l]);
        for (int i=l-1;i;i--) printf("%08llu",s[i]);
        printf("\n");
	}
    inline void push(long long x){
        string st;
        tzero();
        if (x<0) f=-1;
        x*=f;
        while (x){
            st=char(x%10+'0')+st;
            x/=10;
        }
        tl=SIZE;
        while (SIZE>8){
            for (int j=SIZE-8;j<SIZE;j++)
                s[l]=(s[l]<<3)+(s[l]<<1)+(st[j]-'0');
            st.erase(SIZE-8,8);
            l++;
        }
        for (int i=0;i<SIZE;i++)
            s[l]=(s[l]<<3)+(s[l]<<1)+(st[i]-'0');
    }
};
inline bool operator==(bigint a,bigint b){
    if (a.f!=b.f||a.tl!=b.tl) return false;
    for (int i=a.l;i;i--)
        if (a.s[i]!=b.s[i]) return false;
    return true;
}
inline bool operator==(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a==c);
}
inline bool operator==(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c==b);
}
inline bool operator<(bigint a,bigint b){
    if (a.f!=b.f) return (a.f<b.f);
    if (a.tl<b.tl) return (a.f==1);
    if (a.tl>b.tl) return (a.f==-1);
	for (int i=a.l;i;i--)
        if (a.s[i]!=b.s[i]) return (a.s[i]<b.s[i]);
    return false;
}
inline bool operator<(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a<c);
}
inline bool operator<(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c<b);
}
inline bool operator>(bigint a,bigint b){
    if (a.f!=b.f) return (a.f>b.f);
    if (a.tl<b.tl) return (a.f==-1);
    if (a.tl>b.tl) return (a.f==1);
	for (int i=a.l;i;i--)
        if (a.s[i]!=b.s[i]) return (a.s[i]>b.s[i]);
    return false;
}
inline bool operator>(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a>c);
}
inline bool operator>(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c>b);
}
inline bool operator!=(bigint a,bigint b){
    if (a.f!=b.f||a.tl!=b.tl) return true;
    for (int i=a.l;i;i--)
        if (a.s[i]!=b.s[i]) return true;
    return false;
}
inline bool operator!=(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a!=c);
}
inline bool operator!=(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c!=b);
}
inline bool operator<=(bigint a,bigint b){
    if (a.f!=b.f) return (a.f<b.f);
    if (a.tl<b.tl) return (a.f==1);
    if (a.tl>b.tl) return (a.f==-1);
    for (int i=a.l;i;i--)
        if (a.s[i]>b.s[i]) return false;
    return true;
}
inline bool operator<=(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a<=c);
}
inline bool operator<=(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c<=b);
}
inline bool operator>=(bigint a,bigint b){
    if (a.f!=b.f) return (a.f>b.f);
    if (a.tl<b.tl) return (a.f==-1);
    if (a.tl>b.tl) return (a.f==1);
    for (int i=a.l;i;i--)
        if (a.s[i]<b.s[i]) return false;
    return true;
}
inline bool operator>=(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a>=c);
}
inline bool operator>=(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c>=b);
}
inline bigint operator+(bigint a,bigint b){
    bigint c;
    c.tzero();
    if (a.f==1&&b.f==1){
        for (int i=1;i<1001;i++) c.s[i]=a.s[i]+b.s[i];
        for (int i=1;i<1001;i++){
            c.s[i+1]+=c.s[i]/BIT;
            c.s[i]%=BIT;
        }
        c.resize();
        return c;
    }
    if (a.f==1&&b.f==-1){
        b.f=1;
        if (a<b){
            swap(a,b);
            a.f=-1;
        }
        for (int i=1;i<1001;i++){
            if (a.s[i]<b.s[i]){
                a.s[i+1]--;
                a.s[i]+=BIT;
            }
            a.s[i]-=b.s[i];
        }
        a.resize();
        return a;
    }
    if (a.f==-1&&b.f==1){
        swap(a,b);
        b.f=1;
        if (a<b){
            swap(a,b);
            a.f=-1;
        }
        for (int i=1;i<1001;i++){
            if (a.s[i]<b.s[i]){
                a.s[i+1]--;
                a.s[i]+=BIT;
            }
            a.s[i]-=b.s[i];
        }
        a.resize();
        return a;
    }
    a.f=b.f=1;
    c=a+b;
    c.f=-1;
    return c;
}
inline bigint operator+(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a+c);
}
inline bigint operator+(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c+b);
}
inline void operator+=(bigint &a,bigint b){
    a=a+b;
}
inline void operator+=(bigint &a,long long b){
    a=a+b;
}
inline bigint operator-(bigint a,bigint b){
    bigint c;
    c.tzero();
	if (a.f!=b.f){
        int bo=(a.f<b.f?-1:1);
        a.f=b.f=1;
        c=a+b;
        c.f=bo;
        return c;
    }
    if (a.f==1){
        if (a<b){
            swap(a,b);
            a.f=-1;
        }
        for (int i=1;i<1001;i++){
            if (a.s[i]<b.s[i]){
                a.s[i+1]--;
                a.s[i]+=BIT;
            }
            a.s[i]-=b.s[i];
        }
        a.resize();
        return a;
    }
    a.f=b.f=1;
    swap(a,b);
    if (a<b){
        swap(a,b);
        a.f=-1;
    }
    for (int i=1;i<1001;i++){
        if (a.s[i]<b.s[i]){
            a.s[i+1]--;
            a.s[i]+=BIT;
        }
        a.s[i]-=b.s[i];
    }
    a.resize();
    return a;
}
inline bigint operator-(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a-c);
}
inline bigint operator-(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c-b);
}
inline void operator-=(bigint &a,bigint b){
    a=a-b;
}
inline void operator-=(bigint &a,long long b){
    a=a-b;
}
inline bigint operator*(bigint a,bigint b){
    bigint c;
    c.tzero();
    c.f=(a.f==b.f?1:-1);
    for (int i=1;i<=a.l;i++){
        for (int j=1;j<=b.l;j++)
            c.s[i+j-1]+=a.s[i]*b.s[j];
    }
    for (int i=1;i<1001;i++){
        c.s[i+1]+=c.s[i]/BIT;
        c.s[i]%=BIT;
    }
    c.resize();
    return c;
}
inline bigint operator*(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a*c);
}
inline bigint operator*(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c*b);
}
inline void operator*=(bigint &a,bigint b){
    a=a*b;
}
inline void operator*=(bigint &a,long long b){
    a=a*b;
}
inline bigint operator/(bigint a,bigint b){
    bigint c=b,f;
    f.f=a.f*b.f;
    a.f=b.f=1;
    if (b.zero()){
        printf("Division by zero\n");
        exit(1);
    }
    while (b*BIT<=a) b*=BIT;
    int cnt,k=1;
    while (!c.s[k]) k++;
    while (1){
        cnt=b.l-c.l+1;
        while (a>=b){
            a-=b;
            f.s[cnt]++;
        }
        if (b==c||a.zero()) break;
        for (int i=1;i<=b.l;i++) b.s[i]=b.s[i+1];
        b.l--;
    }
    f.resize();
    return f;
}
inline bigint operator/(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a/c);
}
inline bigint operator/(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c/b);
}
inline void operator/=(bigint &a,bigint b){
    a=a/b;
}
inline void operator/=(bigint &a,long long b){
    a=a/b;
}
inline bigint operator%(bigint a,bigint b){
    return a-a/b*b;
}
inline bigint operator%(bigint a,long long b){
    bigint c;
    c.push(b);
    return (a%c);
}
inline bigint operator%(long long a,bigint b){
    bigint c;
    c.push(a);
    return (c%b);
}
inline void operator%=(bigint &a,bigint b){
    a=a%b;
}
inline void operator%=(bigint &a,long long b){
    a=a%b;
}
inline bigint operator++(bigint &a){
    a+=1;
    return a;
}
inline bigint operator--(bigint &a){
    a-=1;
    return a;
}
inline bigint operator<<(bigint a,bigint b){
    while (!b.zero()){
        --b;
        a*=2;
    }
    return a;
}
inline bigint operator<<(bigint a,long long b){
    while (b--) a*=2;
    return a;
}
inline void operator<<=(bigint &a,bigint b){
    a=a<<b;
}
inline void operator<<=(bigint &a,long long b){
    a=a<<b;
}
inline bigint operator>>(bigint a,bigint b){
    while (!b.zero()){
        --b;
        a/=2;
    }
    return a;
}
inline bigint operator>>(bigint a,long long b){
    while (b--) a/=2;
    return a;
}
inline void operator>>=(bigint &a,bigint b){
    a=a>>b;
}
inline void operator>>=(bigint &a,long long b){
    a=a>>b;
}
/*
可直接使用:
1.max(a,b);
2.min(a,b);
3.sort(a+1,a+n+1,cmp);
4.stable_sort(a+1,a+n+1,cmp);
5.__gcd(a,b);(暂时无法使用)
*/
#endif  //BIGINT_H

更新日志:

24.08.28:添加get,put,push,resize,zero,tzero函数

24.08.29:添加比较运算符与加/减/乘法与输入’-0’特判

24.08.30:修复位数异常Bug

24.08.31:修复输入异常Bug

24.09.01:修复乘法计算异常Bug

24.09.02-06:备战GESP,断更

24.09.07:添加除法(含’除数为0异常’特判)/模/’++’/’—’/’<<’/’>>‘及其赋值运算符

24.09.08- ? :备战CSP/鄞州区区赛,断更