vector高精度

 2024/08/13 20:20 更新
更新内容:改为封装
下次目标:争取重载输入流输出流


 2024/08/14 9:48 更新
更新内容:重载输入流输出流,修复了封装导致的一系列bug
下次目标:完善带符号高精度

老版

#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
ll read(){
    ll res=0;int op=1;char ch=getchar();
    while(ch<'0'||'9'<ch){if(ch=='-')op=-op;ch=getchar();}
    while('0'<=ch&&ch<='9'){res=res*10+ch-'0';ch=getchar();}
    return res;
}
void write(ll x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
struct lll{
    vector<int>dat;
    int& operator[](const int &i){
        return dat[i];
    }
    void resize(ll x){
        dat.resize(x);
    }
    ll size(){
        return dat.size();
    }
    void push_back(const int x){
        dat.push_back(x);
    }
    int back(){
        return dat.back();
    }
    void _(ll x){
        if(x==0)dat.push_back(0);
        else while(x){
            dat.push_back(x%10);
            x/=10;
        }
    }
    vector<int>::iterator begin(){
        return dat.begin();
    }
    vector<int>::iterator end(){
        return dat.end();
    }
    friend bool operator==(const lll &x,const lll &y){
        lll a=x,b=y;
        if(a.size()==b.size()){
            bool flag=1;
            ll l=a.size();
            for(int i=l-1;i>=0;i--){
                if(a[i]!=b[i]){
                    flag=0;
                    break;
                }
            }
            return flag;
        }
        return false;
    }
    friend bool operator!=(const lll &a,const lll &b){
        return !(a==b);
    }
    friend bool operator<(const lll &x,const lll &y){
        lll a=x,b=y;
        if(a.size()==b.size()){
            bool flag=0;
            ll l=a.size();
            for(ll i=l-1;i>=0;i--){
                if(a[i]<b[i]){
                    flag=1;
                    break;
                }
                else if(a[i]==b[i])continue;
                else break;
            }
            return flag;
        }
        return a.size()<b.size();
    }
    friend bool operator>(const lll &a,const lll &b){
        return b<a;
    }
    friend bool operator>=(const lll &a,const lll &b){
        return a==b||b<a;
    }
    friend bool operator<=(const lll &a,const lll &b){
        return a==b||a<b;
    }
    friend bool check(lll x,lll y,ll begin,ll len){
        lll a,b=y;
        vector<int>res(x.begin()+begin,x.begin()+begin+len);
        a.dat=res;
        while(a.back()==0)a.resize(a.size()-1);
        return a>=b;
    }
    void read(){
        dat.resize(0);
        char ch=getchar();
        while(ch<'0'||'9'<ch)ch=getchar();
        while('0'<=ch&&ch<='9'){dat.push_back(ch-'0');ch=getchar();}
        reverse(dat.begin(),dat.end());
        ll cnt=0;
        while(dat[dat.size()-cnt-1]==0&&dat.size()-cnt>1)cnt++;
        dat.resize(dat.size()-cnt);
    }
    void put(){
        for(ll i=dat.size()-1;i>=0;i--)putchar(dat[i]+'0');
    }
    friend lll operator-(const lll &x,const lll &y){//a>=b
        lll a=x,b=y,c;
        ll l=max(a.size(),b.size());
        c.resize(l);
        a.resize(l);
        b.resize(l);
        for(ll i=0;i<l;i++){
            c[i]+=a[i]-b[i];
            if(c[i]<0){
                c[i+1]-=1;
                c[i]+=10;
            }
        }
        ll cnt=0;
        while(c.size()-cnt>1&&c[c.size()-1-cnt]==0)cnt++;
        c.resize(c.size()-cnt);
        return c;
    }
    friend lll operator+(const lll &x,const lll &y){
        lll a=x,b=y,c;
        ll l=max(a.size(),b.size());
        c.resize(l);
        a.resize(l);
        b.resize(l);
        ll m=0;
        for(ll i=0;i<l;i++){
            c[i]=(a[i]+b[i]+m)%10;
            m=(a[i]+b[i]+m)/10;
        }
        if(m>0)c.push_back(m);
        return c;
    }
    friend lll operator*(const lll &x,const lll &y){
        lll a=x,b=y,c;
        if((a[0]==0&&a.size()==1)||(b[0]==0&&b.size()==1))c.push_back(0);
        else{
            if(a<b)swap(a,b);
            c.resize(a.size()+b.size()-1);
            for(ll i=0;i<a.size();i++){
                for(ll j=0;j<b.size();j++)c[i+j]+=a[i]*b[j];
            }
            for(ll i=0;i<c.size()-1;i++){
                c[i+1]+=c[i]/10;
                c[i]%=10;
            }
            while(c[c.size()-1]/10){
                c.push_back(c.back()/10);
                c[c.size()-2]%=10;
            }
        }
        return c;
    }
    friend lll operator*(const lll &x,const ll &y){
        lll a=x,c;
        ll b=y;
        if((a[0]==0&&a.size()==1)||b==0)c.push_back(0);
        else{
            ll cnt=0,d=b;
            while(d){
                cnt++;
                d/=10;
            }
            c.resize(a.size()+cnt-1);
            for(ll i=0;i<a.size();i++)c[i]=a[i]*b;
            for(ll i=0;i<c.size()-1;i++){
                c[i+1]+=c[i]/10;
                c[i]%=10;
            }
            while(c.back()/10){
                c.push_back(c.back()/10);
                c[c.size()-2]%=10;
            }
        }
        return c;
    }
    friend lll operator/(const lll &x,const lll &y){//朴素版高精除 
        lll a=x,b=y,c;
        if(a.size()==1&&b[0]==0)return c;
        if(a.size()==1&&b[0]==0)c.push_back(0);
        else{
            c.resize(max(a.size(),b.size()));
            for(ll i=a.size()-b.size();i>=0;i--){
                while(check(a,b,i,a.size()-i)){
                    for(ll j=0;j<b.size();j++){
                        a[i+j]-=b[j];
                        if(a[i+j]<0){
                            a[i+j+1]--;
                            a[i+j]+=10;
                        }
                    }
                    c[i]++;
                }
            }
        }
        ll cnt=0;
        while(c[c.size()-1-cnt]==0&&c.size()-cnt>1)cnt++;
        c.resize(c.size()-cnt);
        return c;
    }
    friend lll operator/(const lll &x,const ll &y){
        lll a=x,c;
        if(y==0)return c;
        if(y==1)return x;
        c.resize(a.size());
        ll r=0;
        for(ll i=a.size()-1;i>=0;i--){
            r=r*10+a[i];
            c.push_back(r/y);
            r%=y;
        }
        reverse(c.begin(),c.end());
        ll cnt=0;
        while(c.size()-cnt>1&&c[c.size()-cnt-1]==0)cnt++;
        c.resize(c.size()-cnt);
        return c;
    }
    friend ll operator%(const lll &x,const ll &y){
        lll a=x;
        ll r=0;
        for(ll i=a.size()-1;i>=0;i--){
            r=r*10+a[i];
            r%=y;
        }
        return r;
    }
    friend lll operator%(const lll &x,const lll &y){
        lll a=x,b=y,c;
        for(ll i=a.size()-b.size();i>=0;i--){
            while(check(a,b,i,a.size()-i)){
                for(ll j=0;j<b.size();j++){
                    a[i+j]-=b[j];
                    if(a[i+j]<0){
                        a[i+j+1]--;
                        a[i+j]+=10;
                    }
                }
            }
        }
        ll cnt=0;
        while(a[a.size()-cnt-1]==0&&a.size()-cnt>1)cnt++;
        a.resize(a.size()-cnt);
        return a;
    }
    friend ostream &operator<<(ostream &out,const lll &x){
        for(ll i=x.dat.size()-1;i>=0;i--)putchar(x.dat[i]+'0');
        return out;
    }
    friend istream &operator>>(istream &in,lll &x){
        x.read();
        return in;
    }
};
struct sll{
    bool op;//1为负 
    lll dat;
    friend bool operator<(const sll&a,const sll&b){
        if(a.op==1&&b.op==0)return true;
        if(a.op==0&&b.op==1)return false;
        if(a.op==0)return a.dat<b.dat;
        if(a.op==1)return b.dat<a.dat;
        return a.dat.dat.size()<b.dat.dat.size();
    }
    friend bool operator>(const sll&a,const sll&b){
        return b<a;
    }
    friend bool operator==(const sll&a,const sll&b){
        return (a.op==b.op)&&a.dat==b.dat;
    }
    friend bool operator<=(const sll&a,const sll&b){
        return a<b||b==a;
    }
    friend bool operator>=(const sll&a,const sll&b){
        return b<a||b==a;
    }
    friend bool operator!=(const sll&a,const sll &b){
        return !(a==b);
    }
    friend sll operator-(const sll &x,const sll &y){
        sll a=x,b=y,c;
        if(a.op==b.op&&a.dat==b.dat){
            c.op=0;
            c.dat.push_back(0);
        }
        else if(a.op==0&&b.op==1){
            c.op=0;
            c.dat=a.dat+b.dat;
        }
        else if(a.op==1&&b.op==0){
            c.op=1;
            c.dat=a.dat+b.dat;
        }
        else if(a.op==1&&b.op==1){
            if(a.dat<b.dat){
                c.op=0;
                c.dat=b.dat-a.dat;
            }
            else{
                c.op=1;
                c.dat=a.dat-b.dat;
            }
        }
        else if(a.op==0&&b.op==0){
            if(a<b){
                c.op=1;
                c.dat=b.dat-a.dat;
            }
            else{
                c.op=0;
                c.dat=a.dat-b.dat;
            }
        }
        return c;
    }
    friend sll operator+(const sll &x,const sll &y){
        sll a=x,b=y,c;
        if(a.op==b.op){
            c.op=a.op;
            c.dat=a.dat+b.dat;
        }
        else if(a.op){
            bool flag=a.dat>b.dat;
            c.op=flag;
            c.dat=flag?a.dat-b.dat:b.dat-a.dat;
        }
        else{
            bool flag=b.dat>a.dat;
            c.op=flag;
            c.dat=flag?b.dat-a.dat:a.dat-b.dat;
        }
        return c;
    }
    friend sll operator*(const sll &x,const sll &y){
        sll a=x,b=y,c;
        c.op=a.op^b.op;
        c.dat=a.dat*b.dat;
        return c;
    }
    friend sll operator/(const sll &x,const sll &y){
        sll a=x,b=y,c;
        c.op=a.op^b.op;
        c.dat=a.dat/b.dat;
        return c;
    }
    void read(){//原来的读入实现,不习惯用重载可以用这个
        /*
        例如:
        sll x;
        x.read(); 
        */ 
        op=0;
        dat.resize(0);
        char ch=getchar();
        while(ch<'0'||'9'<ch){if(ch=='-')op^=1;ch=getchar();}
        while('0'<=ch&&ch<='9'){dat.push_back(ch-'0');ch=getchar();}
        reverse(dat.begin(),dat.end());
        ll cnt=0;
        while(dat[dat.size()-cnt-1]==0&&dat.size()-cnt>1)cnt++;
        dat.resize(dat.size()-cnt);
    }
    void put(){
        if(op==1)putchar('-');
        for(ll i=dat.size()-1;i>=0;i--)putchar(dat[i]+'0');
    }
    void __(ll x){//提供整型直接转换高精度 
        /*
        例如:
        sll x;
        x.__(-114514); 
        */ 
        op=0;
           if(x<0){
               op=1;
            x=-x;
        }
           dat._(x);
    }
    friend ostream &operator<<(ostream &out,const sll &x){//输入输出流重载,之后可以直接用cin,cout
        if(x.op==1)putchar('-');
        for(int i=x.dat.dat.size()-1;i>=0;i--)putchar(x.dat.dat[i]+'0');
        return out;
    }
    friend istream &operator>>(istream &in,sll &x){
        x.read();
        return in;
    }
    /*
    例如:
    sll x;
    cin>>x;
    cout<<x; 
    */ 
};
int main(){
    //用法举例:
    lll a,b;//无符号高精度,注意减法要求a>=b 
    cin>>a>>b;
    cout<<(a+b)<<" "<<(a-b)<<" "<<(a*b)<<" "<<(a/b)<<" "<<(a%b)<<" ";
    cout<<a[3]<<endl;//重载了,a[i]存的是a的第a.size()-i位 
    sll c,d;
    cin>>c>>d;
    //.......
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值