模板 int128

发布时间 2023-07-30 01:32:30作者: wuyoudexian
struct i128 {
	__int128 x;
	constexpr i128() : x(0) {};
	constexpr i128(__int128 v) : x(v) {};

	i128 &operator+=(i128 rhs) {
		x = x + rhs.x;
		return *this;
	}
	i128 &operator-=(i128 rhs) {
		x = x - rhs.x;
		return *this;
	}
	i128 &operator*=(i128 rhs) {
		x = x * rhs.x;
		return *this;
	}
	i128 &operator/=(i128 rhs) {
		x = x / rhs.x;
		return *this;
	}
	i128 &operator%=(i128 rhs) {
		x = x % rhs.x;
		return *this;
	}
	friend i128 operator+(i128 lhs, i128 rhs) {
		return lhs += rhs;
	}
	friend i128 operator-(i128 lhs, i128 rhs) {
		return lhs -= rhs;
	}
	friend i128 operator*(i128 lhs, i128 rhs) {
		return lhs *= rhs;
	}
	friend i128 operator/(i128 lhs, i128 rhs) {
		return lhs /= rhs;
	}
	friend i128 operator%(i128 lhs, i128 rhs) {
		return lhs %= rhs;
	}
	friend istream &operator>>(istream &is, i128 &a) {
		ll x = 0; bool f = false;
		char ch = is.get();
    	while(!isdigit(ch)) {
        	if(ch == '-') f = true;
        	is.get(ch);
    	}
    	while(isdigit(ch)) {
        	x = (x << 1) + (x << 3) + (ch ^ '0');
        	is.get(ch);
    	}
    	a.x = f ? -x : x;
    	return is;
    }
    friend ostream &operator<<(ostream &os, i128 a) {
    	if(a.x == 0) {
    		return os << 0;
    	}
    	if(a.x < 0) {
    		os << '-';
    		a.x = -a.x;
    	}
    	string s;
    	while(a.x > 0) {
    		s.push_back(char(a.x % 10 + '0'));
    		a.x /= 10;
    	}
    	reverse(s.begin(), s.end());
    	return os << s;
    }
};