跳到正文

剪贴板

剪贴板 ppj1rjgl

原文
剪贴板信息

剪贴板信息

收录
更新
C++
/*

cbs制作

说明

1.本人蒟蒻,部分代码是从网上借鉴的。

2.为防止重名,每个函数前都加了2个下划线。

3.基于字符串储存,速度偏慢

4.不支持小数,但支持负数

*/

#ifndef __INT__114514__CPP
#define __INT__114514__CPP

#include<bits/stdc++.h>
using namespace std;

//提前声明有这些模块
struct __int114514;
inline __int114514 __fu__add(__int114514 x,__int114514 y);
inline __int114514 __fu__sub(__int114514 x,__int114514 y);

//数字转字符串
template<typename T>  string __tostring(T n) {
    ostringstream oss;
    oss << fixed << setprecision(0) << n;
    return oss.str() ;
}

//定义结构体
struct __int114514 {
    string data;
    bool f;
    void read() {
        string s;
        cin >> s;
        if(s[0] == '-') {
            s.erase(0,1);
            f=1;
        } else f=0;
        data = s;
    }
    void write() {
        if(f && data!="0") putchar('-');
        cout << data;
    }

    //构造函数
    template<typename T>  __int114514(T b) {
        data=__tostring(abs(b)),f=(b<0);
    }
    __int114514(string b) {
        if(b[0] == '-') {
            b.erase(0,1);
            f=1;
        } else f=0;
        data = b;
    }
    __int114514(string a,bool b) {
        data=a,f=b;
    }
    __int114514() {
        data="0",f=0;
    }

    //赋值
    template<typename T>  __int114514 operator=(T b) {
        data=__tostring(abs(b)),f=(b<0);
        return *this;
    }
    __int114514 operator=(string b) {
        if(b[0] == '-') {
            b.erase(0,1);
            f=1;
        } else f=0;
        data = b;
        return *this;
    }

    __int114514 operator ++() { // 前缀++
        *this = __fu__add(*this, __int114514("1",0));
        return *this;
    }
    __int114514 operator ++(int) { // 后缀++
        __int114514 old = *this;
        *this = __fu__add(*this, __int114514("1",0));
        return old;
    }
    __int114514 operator --() { // 前缀--
        *this = __fu__sub(*this, __int114514("1",0));
        return *this;
    }
    __int114514 operator --(int) { // 后缀--
        __int114514 old = *this;
        *this = __fu__sub(*this, __int114514("1",0));
        return old;
    }
};

//高精度比较代码
bool __cmp(string x,string y) {
    if(x.size() > y.size()) return 1;
    if(x.size() < y.size()) return 0;
    return x >= y;
}
bool __cmp2(string x,string y) {
    if(x.size() > y.size()) return 1;
    if(x.size() < y.size()) return 0;
    return x > y;
}
bool __cmp3(string x,string y) {
    if(x.size() < y.size()) return 1;
    if(x.size() > y.size()) return 0;
    return x <= y;
}
bool __cmp4(string x,string y) {
    if(x.size() < y.size()) return 1;
    if(x.size() > y.size()) return 0;
    return x < y;
}
bool __cmp5(string x,string y) {
    while(x.size()>1 && x[0]=='0') x.erase(0,1);
    while(y.size()>1 && y[0]=='0') y.erase(0,1);
    return x == y;
}

//高精度计算代码
string __add(string a,string b) {
    string c="";
    int jw=0;
    if(a.size()>b.size())
        for(int i=0; a.size()!=b.size(); i++)b="0"+b;
    if(b.size()>a.size())
        for(int i=0; a.size()!=b.size(); i++)a="0"+a;
    for(int i=a.size()-1; i>=0; i--) {
        int n = (a[i]-48)+(b[i]-48)+jw;
        if(n > 9) {
            jw=1;
            n-=10;
        } else jw=0;
        c=char(n+48)+c;
    }
    if(jw)return "1"+c;
    return c;
}
string __sub(string a,string b) {
    string c="";
    int tw=0;
    if(a.size()>b.size())
        for(int i=0; a.size()!=b.size(); i++)b="0"+b;
    if(b.size()>a.size())
        for(int i=0; a.size()!=b.size(); i++)a="0"+a;
    for(int i=a.size()-1; i>=0; i--) {
        int n = (a[i]-48)-(b[i]-48)-tw;
        if(n < 0) {
            tw=1;
            n+=10;
        } else tw=0;
        c=char(n+48)+c;
    }
    while(c.size()>1 && c[0]=='0') c.erase(0,1); //去除前导零
    return c;
}
string __cf(string a,string b) {
    int lena=a.size();
    int lenb=b.size();
    short c[lena+lenb+1]; //多开1项,防止数据溢出
    memset(c,0,sizeof c);
    for(int i=0; i<lena; i++) {
        for(int j=0; j<lenb; j++) {
            int n=(lena-i-1)+(lenb-j-1);
            c[n]+=(a[i]-48)*(b[j]-48);
            int k=0;
            while(1) {
                c[n+k+1] += c[n+k]/10;
                c[n+k] %= 10;
                if(c[n+k+1]<10) break;
                k++;
            }
        }
    }
    string s="";
    int i=lena+lenb;
    while(c[i] == 0)i--;
    for(; i>=0; i--)
        s += char(c[i]+48);
    return s;
}
bool __chu_cmp(int* a,int* b,int len) {
    if(a[len]>0)return 1;
    for(int i=len-1; i>=0; i--) {
        if(a[i]>b[i])return 1;
        else if(a[i]<b[i])return 0;
    }
    return 1;
}
string __chu(string x,string y) {
    int len_a=x.size(),len_b=y.size(),len=len_a-len_b;
    int a[len_a+1]= {0},b[len_b+1]= {0},ans[len_a+1]= {0},i,j;
    for(i=0; i<len_a; i++)a[i]=x[len_a-1-i]-'0';
    for(i=0; i<len_b; i++)b[i]=y[len_b-1-i]-'0';
    for(i=len_a-len_b; i>=0; i--) {
        while(__chu_cmp(a+i,b,len_b)) {
            ans[i]++;
            for(j=0; j<=len_b-1; j++) {
                if(a[i+j]<b[j])
                    a[i+j+1]--,a[i+j]+=10;
                a[i+j]-=b[j];
            }
        }
    }
    while(a[len_a]==0&&len_a>0) len_a--;
    while(ans[len]==0&&len>0) len--;
    string retval;
    for(i=len; i>=0; i--)retval += char(ans[i]+'0');
    return retval;
}

string __mod(string x,string y) {
    string xx="";
    for(int i=0; i<int(x.size()); i++) {
        xx+=x[i];
        while(__cmp(xx,y))xx=__sub(xx,y);
    }
    return xx;
}

//负数处理
inline __int114514 __fu__sub(__int114514 x,__int114514 y) {
    if(x.f == y.f) {
        if(__cmp(x.data,y.data)) //判断够不够减
            return __int114514(__sub(x.data,y.data),x.f);
        else
            return __int114514(__sub(y.data,x.data),!x.f);
    } else {
        return __int114514(__add(x.data,y.data),x.f);
    }
}
inline __int114514 __fu__add(__int114514 x,__int114514 y) {
    if(x.f) {
        if(y.f) return __int114514(__add(x.data,y.data),1);
        else return __fu__sub(y, __int114514(x.data,0)); //加一个负数,变成减
    } else {
        if(y.f) return __fu__sub(x, __int114514(y.data,0)); //加一个负数,变成减
        else return __int114514(__add(x.data,y.data),0);
    }
}
inline __int114514 __fu__cf(__int114514 x,__int114514 y) {
    return __int114514(__cf(x.data,y.data),bool(x.f xor y.f));
}
inline __int114514 __fu__chu(__int114514 x,__int114514 y) {
    return __int114514(__chu(x.data,y.data),bool(x.f xor y.f));
}

//重载运算符
__int114514 operator+(__int114514 x,__int114514 y) {
    return __fu__add(x,y);
}
__int114514 operator-(__int114514 x,__int114514 y) {
    return __fu__sub(x,y);
}
__int114514 operator*(__int114514 x,__int114514 y) {
    return __fu__cf(x,y);
}
__int114514 operator/(__int114514 x,__int114514 y) {
    return __fu__chu(x,y);
}
__int114514 operator%(__int114514 x,__int114514 y) {
    return __int114514(__mod(x.data,y.data),x.f);
}

//重载操作符
__int114514 operator+=(__int114514 &a,__int114514 b) {
    a = __fu__add(a,b);
    return a;
}
__int114514 operator-=(__int114514 &a,__int114514 b) {
    a = __fu__sub(a,b);
    return a;
}
__int114514 operator*=(__int114514 &a,__int114514 b) {
    a = __fu__cf(a,b);
    return a;
}
__int114514 operator/=(__int114514 &a, __int114514 b) {
    a = __fu__chu(a,b);
    return a;
}
__int114514 operator%=(__int114514 &a,__int114514 b) {
    a.data = __mod(a.data,b.data);
    return a;
}

//比较
bool operator>=(__int114514 x,__int114514 y) {
    if(x.data == "0" && y.data == "0") return 1;
    if(x.f) {
        if(y.f) return __cmp3(x.data,y.data);
        else return 0;
    } else {
        if(y.f) return 1;
        else return __cmp(x.data,y.data);
    }
}
bool operator>(__int114514 x,__int114514 y) {
    if(x.data == "0" && y.data == "0") return 0;
    if(x.f) {
        if(y.f) return __cmp4(x.data,y.data);
        else return 0;
    } else {
        if(y.f) return 1;
        else return __cmp2(x.data,y.data);
    }
}
bool operator<=(__int114514 x,__int114514 y) {
    if(x.data == "0" && y.data == "0") return 1;
    if(x.f) {
        if(y.f) return __cmp(x.data,y.data);
        else return 1;
    } else {
        if(y.f) return 0;
        else return __cmp3(x.data,y.data);
    }
}
bool operator<(__int114514 x,__int114514 y) {
    if(x.data == "0" && y.data == "0") return 0;
    if(x.f) {
        if(y.f) return __cmp2(x.data,y.data);
        else return 1;
    } else {
        if(y.f) return 0;
        else return __cmp4(x.data,y.data);
    }
}
bool operator==(__int114514 x,__int114514 y) {
    if(x.data == "0" && y.data == "0") return 1;
    if(x.f == y.f) return __cmp5(x.data,y.data);
    return 0;
}

//重载输入输出
istream & operator>>(istream &in, __int114514 &a) {
    a.read();
    return in;
}
ostream & operator<<(ostream &out, __int114514 a) {
    a.write();
    return out;
}

//与short、int、longlong保持兼容
template<typename T>  T __mod__int(string x,T y) {
    int ans;
    for(int i=0; i<x.size(); i++) ans = (ans*10+(x[i]-'0'))%y;
    return ans;
}
template<typename T>  string __chu__int(string x,T y) {
    string ans;
    T t=0;
    for(int i=0; i<x.size(); i++) {
        t=(t*10+(x[i]-'0'));
        if(ans != "" || t/y > 0)
            ans += char(t/y+'0');
        t%=y;
    }
    return ans;
}

template<typename T>  bool operator>=(__int114514 x,T y) {
    if(x.data == "0" && y == 0) return 1;
    if(x.f) {
        if(y < 0) return __cmp3(x.data,__tostring(abs(y)));
        else return 0;
    } else {
        if(y < 0) return 1;
        else return __cmp(x.data,__tostring(abs(y)));
    }
}
template<typename T>  bool operator>(__int114514 x,T y) {
    if(x.data == "0" && y == 0) return 0;
    if(x.f) {
        if(y < 0) return __cmp4(x.data,__tostring(abs(y)));
        else return 0;
    } else {
        if(y < 0) return 1;
        else return __cmp2(x.data,__tostring(abs(y)));
    }
}
template<typename T>  bool operator<=(__int114514 x,T y) {
    if(x.data == "0" && y == 0) return 1;
    if(x.f) {
        if(y < 0)   return __cmp(x.data,__tostring(abs(y)));
        else return 1;
    } else {
        if(y < 0) return 0;
        else return __cmp3(x.data,__tostring(abs(y)));
    }
}
template<typename T>  bool operator<(__int114514 x,T y) {
    if(x.data == "0" && y == 0) return 0;
    if(x.f) {
        if(y < 0) return __cmp2(x.data,__tostring(abs(y)));
        else return 1;
    } else {
        if(y < 0) return 0;
        else return __cmp4(x.data,__tostring(abs(y)));
    }
}
template<typename T>  bool operator==(__int114514 x,T y) {
    if(y == 0) {
        return x.data == "0";
    } else if(y < 0) {
        if(x.f == 1) {
            while(x.data.size()>1 && x.data[0]=='0') x.data.erase(0,1); // 删除前导零,防止bug
            return x.data == __tostring(y);
        } else {
            return 0;
        }
    } else {
        if(x.f == 0) {
            while(x.data.size()>1 && x.data[0]=='0') x.data.erase(0,1); // 删除前导零,防止bug
            return x.data == __tostring(y);
        } else {
            return 0;
        }
    }
    return 0;
}

template<typename T>  __int114514 operator+(__int114514 x,T y) {
    return __fu__add(x, __int114514(y));
}
template<typename T>  __int114514 operator-(__int114514 x,T y) {
    return __fu__sub(x, __int114514(y));
}
template<typename T>  __int114514 operator*(__int114514 x,T y) {
    return __fu__cf(x, __int114514(y));
}
template<typename T>  __int114514 operator/(__int114514 x,T y) {
    return __int114514(__chu__int(x.data,abs(y)),bool((x.f)^(y<0)));
}
template<typename T>  T operator%(__int114514 x,T y) {
    return __mod__int(x.data,y)*(x.f ? -1 : 1);
}
template<typename T>  __int114514 operator+=(__int114514 &x,T y) {
    x = __fu__add(x, __int114514(y));
    return x;
}
template<typename T>  __int114514 operator-=(__int114514 &x,T y) {
    x = __fu__sub(x, __int114514(y));
    return x;
}
template<typename T>  __int114514 operator*=(__int114514 &x,T y) {
    x = __fu__cf(x, __int114514(y));
    return x;
}
template<typename T>  __int114514 operator/=(__int114514 &x,T y) {
    x = __int114514(__chu__int(x.data,abs(y)),bool((x.f)^(y<0)));
    return x;
}
template<typename T>  __int114514 operator%=(__int114514 x,T y) {
    x.data = __tostring(__mod__int(x.data,y));
    return x;
}

template<typename T>  __int114514 operator+(T y,__int114514 x) {
    return __fu__add(__int114514(y), x);
}
template<typename T>  __int114514 operator-(T y,__int114514 x) {
    return __fu__sub(__int114514(y), x);
}
template<typename T>  __int114514 operator*(T y,__int114514 x) {
    return __fu__cf(__int114514(y), x);
}
template<typename T>  __int114514 operator/(T y,__int114514 x) {
    return __fu__chu(__int114514(y), x);
}
template<typename T>  __int114514 operator%(T y,__int114514 x) {
    return __int114514(__mod(__tostring(abs(y)),x.data),y<0);
}
template<typename T>  T operator+=(T &y,__int114514 x) {
    y += (atoi((x.data).c_str())*(x.f?-1:1));
    return y;
}
template<typename T>  T operator-=(T &y,__int114514 x) {
    y -= (atoi((x.data).c_str())*(x.f?-1:1));
    return y;
}
template<typename T>  T operator*=(T &y,__int114514 x) {
    y *= (atoi((x.data).c_str())*(x.f?-1:1));
    return y;
}
template<typename T>  T operator/=(T &y,__int114514 x) {
    y /= (atoi((x.data).c_str())*(x.f?-1:1));
    return y;
}
template<typename T>  T operator%=(T &y,__int114514 x) {
    y %= (atoi((x.data).c_str())*(x.f?-1:1));
    return y;
}

#endif