跳到正文

剪贴板

剪贴板 47gujppj

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#ifndef BIGINT_HPP
#define BIGINT_HPP

#include<cstddef>   // size_t
#include<cstdint>   // uint32_t, uint64_t
#include<cstring>   // memset
#include<string>    // std::string
#include<vector>    // std::vector
#include<iostream>  // std::istream, std::ostream
#include<cassert>   // assert

using std::string;
using std::vector;
using std::istream;
using std::ostream;

template <size_t N>
class BigInt {
private :
	static constexpr size_t n = N;
	uint32_t bit[N];
	uint32_t& operator [](size_t index) { return bit[index]; }
	const uint32_t& operator [](size_t index)const { return bit[index]; }
public :
	static bool sign(const BigInt &x) { return x[n - 1] & (1 << 31); }
	static BigInt StringToBigInt(const string &str) {
		BigInt res;
		size_t len = str.size(), start = 0;
		bool sgn = (str[0] == '-'); if(sgn) start = 1;
		for(size_t i = start; i < len; i++) {
			uint32_t carry = str[i] ^ 48;
			for(size_t j = 0; j < n; j++) {
				uint64_t temp = static_cast<uint64_t>(res[j]) * 10 + carry;
				res[j] = static_cast<uint32_t>(temp);
				carry = temp >> 32;
			}
		}
		return sgn ? -res : res;
	}
	static string BigIntToString(BigInt num) {
		bool sgn = sign(num);
		if(sgn) num = -num;
		vector<uint8_t> s;
		for(int i = n - 1; i >= 0; i--) {
			size_t m = s.size();
			uint32_t carry = num[i];
			for(size_t j = 0; j < m; j++) {
				uint64_t temp = (static_cast<uint64_t>(s[j]) << 32) + carry;
				s[j] = temp % 10;
				carry = temp / 10;
			}
			while(carry) s.emplace_back(carry % 10), carry /= 10;
		}
		string res;
	    for(auto it = s.rbegin(); it != s.rend(); ++it) res += (*it) ^ 48;
	    if(res.empty()) res = "0";
		return sgn ? '-' + res : res;
	}
	BigInt() { memset(bit, 0, n * sizeof(uint32_t)); }
	BigInt operator ~() const { BigInt res; for(size_t i = 0; i < n; i++) res[i] = ~bit[i]; return res; }
	BigInt operator -() const {
		BigInt res = ~(*this);
		uint32_t carry = 1;
		for(size_t i = 0; i < n; i++) {
			uint64_t temp = static_cast<uint64_t>(res[i]) + carry;
			res[i] = static_cast<uint32_t>(temp);
			carry = temp >> 32;
		}
		return res;
	}
	BigInt(int x) {
		bool sgn = (x < 0); if(sgn) x = -x;
		bit[0] = x; memset(bit + 1, 0, (n - 1) * sizeof(uint32_t));
		if(sgn) *this = -(*this);
	}
	BigInt(long long x) {
		bool sgn = (x < 0); if(sgn) x = -x;
		bit[0] = x & 0xFFFFFFFF, bit[1] = x >> 32; memset(bit + 2, 0, (n - 2) * sizeof(uint32_t));
		if(sgn) *this = -(*this);
	}
	BigInt(const string &str) { *this = StringToBigInt(str); }
	friend istream& operator >> (istream &in, BigInt &x) { string str; in >> str; x = StringToBigInt(str); return in; }
	friend ostream& operator << (ostream &out, const BigInt &x) { out << BigIntToString(x); return out; }
	BigInt& operator &= (const BigInt &o) { for (size_t i = 0; i < n; i++) bit[i] &= o[i]; return *this; }
	BigInt operator & (const BigInt &o) const { BigInt res = *this; return res &= o; }
	BigInt& operator |= (const BigInt &o) { for (size_t i = 0; i < n; i++) bit[i] |= o[i]; return *this; }
	BigInt operator | (const BigInt &o) const { BigInt res = *this; return res |= o; }
	BigInt& operator ^= (const BigInt &o) { for (size_t i = 0; i < n; i++) bit[i] ^= o[i]; return *this; }
	BigInt operator ^ (const BigInt &o) const { BigInt res = *this; return res ^= o; }
	BigInt& operator <<= (size_t shift) {
	    if(shift == 0) return *this;
	    size_t wordShift = shift >> 5, bitShift = shift & 31;
	    if(wordShift >= n) { memset(bit, 0, sizeof(bit)); return *this; }
	    if(wordShift > 0) {
	        for(size_t i = n - 1; i >= wordShift; --i)
	            bit[i] = bit[i - wordShift];
	        memset(bit, 0, wordShift * sizeof(uint32_t));
	    }
	    if(bitShift > 0) {
	        for(size_t i = n - 1; i > 0; --i)
	            bit[i] = (bit[i] << bitShift) | (bit[i - 1] >> (32 - bitShift));
	        bit[0] <<= bitShift;
	    }
	    return *this;
	}
    BigInt& operator >>= (size_t shift) {
        if(shift == 0) return *this;
        size_t wordShift = shift >> 5, bitShift = shift & 31;
        if(wordShift >= n) { memset(bit, 0, sizeof(bit)); return *this; }
        if(wordShift > 0) {
            for(size_t i = 0; i < n - wordShift; ++i)
                bit[i] = bit[i + wordShift];
            memset(bit + n - wordShift, 0, wordShift * sizeof(uint32_t));
        }
        if(bitShift > 0) {
            for(size_t i = 0; i < n - 1; ++i)
                bit[i] = (bit[i] >> bitShift) | (bit[i + 1] << (32 - bitShift));
            bit[n - 1] >>= bitShift;
        }
        return *this;
    }
	BigInt operator << (const size_t &o) const { BigInt res = *this; return res <<= o; }
	BigInt operator >> (const size_t &o) const { BigInt res = *this; return res >>= o; }
	bool operator == (const BigInt &o) const {
		for(size_t i = 0; i < n; i++)
			if(bit[i] != o[i]) return false;
		return true;
	}
	bool operator != (const BigInt &o) const { return !(*this == o); }
    bool operator < (const BigInt &o) const {
		bool signT = sign(*this), signO = sign(o);
		if(signT != signO) return signT;
		for(int i = n - 1; i >= 0; i--)
			if(bit[i] != o[i]) return signT ? bit[i] > o[i] : bit[i] < o[i];
		return false;
	}
	bool operator > (const BigInt &o) const { return o < *this; }
	bool operator <= (const BigInt &o) const { return !(o < *this); }
	bool operator >= (const BigInt &o) const { return !(*this < o); }

	BigInt& operator += (const BigInt &o) {
		uint32_t carry = 0;
		for(size_t i = 0; i < n; i++) {
			uint64_t temp = static_cast<uint64_t>(bit[i]) + o[i] + carry;
			bit[i] = static_cast<uint32_t>(temp);
			carry = temp >> 32;
		}
		return *this;
	}
	BigInt operator + (const BigInt &o) const { BigInt res = *this; return res += o; }
	BigInt& operator -= (const BigInt &o) { *this += -o; return *this; }
	BigInt operator - (const BigInt &o) const { BigInt res = *this; return res += -o; }
    BigInt& operator ++ () { return *this += 1; }
	BigInt operator ++ (int) { BigInt res = *this; *this += 1; return res; }
	BigInt& operator -- () { return *this += -1; }
	BigInt operator -- (int) { BigInt res = *this; *this += -1; return res; }
	BigInt& operator *= (const BigInt &o) {
	    BigInt res;
	    for(size_t i = 0; i < n; i++) {
	        uint32_t carry = 0;
	        for(size_t j = 0; j + i < n; j++) {
	            uint64_t temp = static_cast<uint64_t>(bit[i]) * o[j] + res[i + j] + carry;
	            res[i + j] = static_cast<uint32_t>(temp);
	            carry = temp >> 32;
	        }
	    }
	    return *this = res;
	}
	BigInt operator * (const BigInt &o) const { BigInt res = *this; return res *= o; }
    BigInt& operator *= (const uint32_t &o) {
	    uint32_t carry = 0;
	    for(size_t i = 0; i < n; i++) {
	        uint64_t temp = static_cast<uint64_t>(bit[i]) * o + carry;
	        bit[i] = static_cast<uint32_t>(temp);
	        carry = temp >> 32;
	    }
	    return *this;
	}
	BigInt operator * (const uint32_t &o) const { BigInt res = *this; return res *= o; }
	BigInt& operator *= (int o) {
	    bool sgn = (o < 0); if(sgn) o = -o;
	    *this *= static_cast<uint32_t>(o);
	    if(sgn) *this = -(*this);
	    return *this;
	}
	BigInt operator * (const int &o) const { BigInt res = *this; return res *= o; }
    BigInt& operator /= (const BigInt &o) {
//		if(o == BigInt(0)) assert(false);
        bool sgn = sign(*this) ^ sign(o);
        if(sign(*this)) *this = -(*this);
        BigInt divisor = sign(o) ? -o : o;
        BigInt quotient, remainder;
        for(int i = (n << 5) - 1; i >= 0; --i) {
            remainder <<= 1;
            remainder[0] |= (bit[i >> 5] >> (i & 31)) & 1;
            if (remainder >= divisor) {
                remainder -= divisor;
                quotient[i >> 5] |= (1u << (i & 31));
            }
        }
        return *this = sgn ? -quotient : quotient;
    }
    BigInt operator / (const BigInt &o) const { BigInt res = *this; return res /= o; }
	BigInt& operator /= (const uint32_t &o) {
		bool sgn = sign(*this); if(sgn) *this = -(*this);
	    uint64_t carry = 0;
	    for(int i = n - 1; i >= 0; i--) {
	        uint64_t current = (carry << 32) | bit[i];
	        bit[i] = static_cast<uint32_t>(current / o);
	        carry = current % o;
	    }
	    if(sgn) *this = -(*this);
	    return *this;
	}
	BigInt operator / (const uint32_t &o) const { BigInt res = *this; return res /= o; }
	BigInt& operator /= (int o) {
	    bool sgn = (o < 0); if(sgn) o = -o;
	    *this /= static_cast<uint32_t>(o);
	    if(sgn) *this = -(*this);
	    return *this;
	}
	BigInt operator / (const int &o) const { BigInt res = *this; return res /= o; }
    BigInt& operator %= (const BigInt &o) {
//		if(o == BigInt(0)) assert(false);
        bool sgn = sign(*this);
        if(sign(*this)) *this = -(*this);
        BigInt divisor = sign(o) ? -o : o;
        BigInt quotient, remainder;
        for(int i = (n << 5) - 1; i >= 0; --i) {
            remainder <<= 1;
            remainder[0] |= (bit[i >> 5] >> (i & 31)) & 1;
            if (remainder >= divisor) remainder -= divisor;
        }
        return *this = sgn ? -remainder : remainder;
    }
    BigInt operator % (const BigInt &o) const { BigInt res = *this; return res %= o; }
	BigInt& operator %= (const uint32_t &o) {
		bool sgn = sign(*this); if(sgn) *this = -(*this);
	    uint32_t carry = 0;
	    for(int i = n - 1; i >= 0; i--) {
	        uint64_t current = (static_cast<uint64_t>(carry) << 32) | bit[i];
	        bit[i] = 0;
	        carry = current % o;
	    }
	    bit[0] = carry;
	    if(sgn) *this = -(*this);
	    return *this;
	}
	BigInt operator % (const uint32_t &o) const { BigInt res = *this; return res %= o; }
	BigInt& operator %= (int o) {
	    if (o < 0) o = -o;
	    *this %= static_cast<uint32_t>(o);
	    return *this;
	}
	BigInt operator % (const int &o) const { BigInt res = *this; return res %= o; }
};

#endif