跳到正文

剪贴板

剪贴板 5xcdh47z

原文
剪贴板信息

剪贴板信息

收录
更新

BigInteger 2.2

C++
/*
  BigInteger.h (Version 2.2)
  stripe-python https://www.luogu.com.cn/user/928879
 */

#ifndef BIGINTEGER_H
#define BIGINTEGER_H

#include <cmath>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <algorithm>

class BigInteger {
protected:
	using digit_t = long long;

	static const int WIDTH = 9;
	static const digit_t BASE = 1e9;
	static const long long FFT_LIMIT = 512;

	digit_t* digits;
	long capacity, size;
	bool flag;

	inline void push(const digit_t&);
	inline void pop();

	inline int compare(const BigInteger&) const;

	static inline BigInteger fft_mul(const BigInteger&, const BigInteger&);

public:
	inline void reserve(const long&);
protected:
	inline void resize(const long&);

public:
	BigInteger() : digits(nullptr), flag(true) {*this = 0;}

	BigInteger(const BigInteger& x) : digits(nullptr) {*this = x;}
	BigInteger(const long long& x) : digits(nullptr) {*this = x;}
	BigInteger(const std::string& s) : digits(nullptr) {*this = s;}

	BigInteger& operator= (const BigInteger&);
	BigInteger& operator= (const long long&);
	BigInteger& operator= (const std::string&);

	void clear();
	~BigInteger() {clear();}

	friend std::ostream& operator<< (std::ostream& out, const BigInteger& x) {
		if (!x.flag) out << '-';
		out << (long long) x.digits[x.size];
		for (int i = x.size - 1; i >= 1; i--)
			out << std::setw(WIDTH) << std::setfill('0') << (long long) x.digits[i];
		return out;
	}
	friend std::istream& operator>> (std::istream& in, BigInteger& x) {
		std::string s; in >> s; x = s;
		return in;
	}

	std::string to_string() const;

	BigInteger operator- () const;
	BigInteger abs() const;

	bool operator== (const BigInteger&) const;
#if __cplusplus >= 202002L
	auto operator<=> (const BigInteger&) const;
#else
	bool operator< (const BigInteger&) const;
	bool operator> (const BigInteger&) const;
	bool operator!= (const BigInteger&) const;
	bool operator<= (const BigInteger&) const;
	bool operator>= (const BigInteger&) const;
#endif //__cplusplus >= 202002L

	BigInteger div2() const;
	std::pair<BigInteger, BigInteger> divmod(const BigInteger&) const;

	BigInteger operator+ (const BigInteger&) const;
	BigInteger operator- (const BigInteger&) const;
	BigInteger operator* (const long&) const;
	BigInteger operator* (const BigInteger&) const;
	BigInteger operator/ (const long long&) const;
	BigInteger operator/ (const BigInteger&) const;
	BigInteger operator% (const long long&) const;
	BigInteger operator% (const BigInteger&) const;

	BigInteger pow(const long long&) const;
	BigInteger pow(const long long&, const BigInteger&) const;

	BigInteger sqrt(const long long& = 2) const;

	BigInteger gcd(const BigInteger&) const;
	BigInteger lcm(const BigInteger&) const;

	BigInteger& operator+= (const BigInteger&);
	BigInteger& operator-= (const BigInteger&);
	BigInteger& operator*= (long);
	BigInteger& operator*= (const BigInteger&);
	BigInteger& operator/= (const long long&);
	BigInteger& operator/= (const BigInteger&);
	BigInteger& operator%= (const long long&);
	BigInteger& operator%= (const BigInteger&);

	BigInteger& operator++ ();
	BigInteger operator++ (int);
	BigInteger& operator-- ();
	BigInteger operator-- (int);
};

inline void BigInteger::push(const digit_t& val) {
	if (capacity == size) {
		long new_capacity = 0;
		if (capacity < 1000) new_capacity = capacity << 1;
		else new_capacity = (capacity >> 1) * 3;
		digit_t* new_digits = new digit_t[new_capacity + 1];
		std::memcpy(new_digits, digits, sizeof(long long) * (capacity + 1));
		delete[] digits;
		digits = new_digits, capacity = new_capacity, ++size;
		return;
	}
	digits[++size] = val;
}
inline void BigInteger::pop() {digits[size--] = 0;}

inline int BigInteger::compare(const BigInteger& x) const {
	if (flag && !x.flag) return 1;
	if (!flag && x.flag) return -1;

	int sgn = (flag && x.flag ? 1 : -1);
	if (size > x.size) return sgn;
	if (size < x.size) return -sgn;

	for (int i = size; i >= 1; i--) {
		if (digits[i] > x.digits[i]) return sgn;
		if (digits[i] < x.digits[i]) return -sgn;
	}
	return 0;
}

inline void BigInteger::reserve(const long& sz) {
	if (digits != nullptr) delete[] digits;
	capacity = sz, size = 0;
	digits = new digit_t[sz + 1];
	std::memset(digits, 0, sizeof(digit_t) * (sz + 1));
}
inline void BigInteger::resize(const long& sz) {
	reserve(sz), size = sz;
}

BigInteger& BigInteger::operator= (const BigInteger& x) {
	reserve(x.size + 1);
	flag = x.flag, size = x.size;
	std::memcpy(digits, x.digits, sizeof(digit_t) * (x.size + 1));
	return *this;
}
BigInteger& BigInteger::operator= (const long long& x) {
	flag = (x >= 0);
	reserve(4);
	if (x == 0) return size = 1, digits[1] = 0, *this;
	if (x == LLONG_MIN) return *this = "-9223372036854775808";
	long long n = std::abs(x);
	do {
		push(n % BASE);
		n /= BASE;
	} while (n);
	return *this;
}
BigInteger& BigInteger::operator= (const std::string& s) {
	flag = true, reserve(s.size() / WIDTH + 1);
	if (s.empty() || s == "-") return *this = 0;
	int i = 0;
	if (s[0] == '-') flag = false, i++;
	for (int j = s.size() - 1; j >= i; j -= WIDTH) {
		int start = std::max(i, j - WIDTH + 1), len = j - start + 1;
		push(std::stoi(s.substr(start, len)));
	}
	return *this;
}

void BigInteger::clear() {
	if (digits != nullptr) delete[] digits, digits = nullptr;
}

std::string BigInteger::to_string() const {
	std::stringstream ss; ss << *this;
	return ss.str();
}

BigInteger BigInteger::operator- () const {
	BigInteger res = *this;
	res.flag = !flag;
	return res;
}
BigInteger BigInteger::abs() const {
	BigInteger res = *this;
	res.flag = true;
	return res;
}

bool BigInteger::operator== (const BigInteger& x) const {return compare(x) == 0;}
#if __cplusplus >= 202002L
auto BigInteger::operator<=> (const BigInteger& x) const {return compare(x);}
#else
bool BigInteger::operator< (const BigInteger& x) const {return compare(x) < 0;}
bool BigInteger::operator> (const BigInteger& x) const {return compare(x) > 0;}
bool BigInteger::operator!= (const BigInteger& x) const {return compare(x) != 0;}
bool BigInteger::operator<= (const BigInteger& x) const {return compare(x) <= 0;}
bool BigInteger::operator>= (const BigInteger& x) const {return compare(x) >= 0;}
#endif //__cplusplus >= 202002L

BigInteger BigInteger::operator+ (const BigInteger& x) const {
	if (!x.flag) return *this - x.abs();
	if (!flag) return x - abs();

	BigInteger res;
	res.flag = !(flag ^ x.flag);
	int n = std::max(size, x.size) + 1;
	res.reserve(n);
	digit_t carry = 0;
	for (int i = 1; i <= n; i++) {
		digit_t d1 = i <= size ? digits[i] : 0, d2 = i <= x.size ? x.digits[i] : 0;
		res.push(d1 + d2 + carry);
		carry = res.digits[i] / BASE;
		res.digits[i] %= BASE;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}
BigInteger BigInteger::operator- (const BigInteger& x) const {
	if (!x.flag) return *this + x.abs();
	if (!flag) return -(abs() + x);
	BigInteger res;
	if (*this < x) res.flag = false;
	digit_t carry = 0;
	int n = std::max(size, x.size);
	res.reserve(n);
	for (int i = 1; i <= n; i++) {
		digit_t d1 = i <= size ? digits[i] : 0, d2 = i <= x.size ? x.digits[i] : 0;
		if (res.flag) res.push(d1 - d2 - carry);
		else res.push(d2 - d1 - carry);
		if (res.digits[i] < 0) res.digits[i] += BASE, carry = 1;
		else carry = 0;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}

namespace __FFT {
	constexpr long double PI = 3.14159265358979323846L;

	struct complex {
		long double real, imag;

		complex(long double x = 0.0, long double y = 0.0) : real(x), imag(y) {}

		complex operator+ (const complex& other) const {return complex(real + other.real, imag + other.imag);}
		complex operator- (const complex& other) const {return complex(real - other.real, imag - other.imag);}
		complex operator* (const complex& other) const {return complex(real * other.real - imag * other.imag, real * other.imag + other.real * imag);}
		complex conj() const {return complex(real, -imag);}

		complex& operator+= (const complex& other) {return real += other.real, imag += other.imag, *this;}
		complex& operator-= (const complex& other) {return real -= other.real, imag -= other.imag, *this;}
		complex& operator*= (const complex& other) {return *this = *this * other;}
	};

	complex* arr = nullptr;

	inline void init(int n) {
		if (arr != nullptr) delete[] arr, arr = nullptr;
		arr = new complex[n + 1];
		for (int i = 0; i <= n; i++) arr[i] = complex(0.0, 0.0);
	}

	inline void dft(complex* a, long len) {
		for (long i = len; i >= 2; i >>= 1) {
			complex wn(std::cos(2.0L * PI / i), std::sin(2.0L * PI / i));
			for (long j = 0; j < len; j += i) {
				complex w(1.0, 0.0);
				for (long k = j; k < j + (i >> 1); k++, w *= wn) {
					complex u = a[k], t = a[k + (i >> 1)];
					a[k] = u + t, a[k + (i >> 1)] = w * (u - t);
				}
			}
		}
	}
	inline void idft(complex* a, long len) {
		for (long i = 2; i <= len; i <<= 1) {
			complex wn(std::cos(2.0L * PI / i), std::sin(2.0L * PI / i));
			for (long j = 0; j < len; j += i) {
				complex w(1.0, 0.0);
				for (long k = j; k < j + (i >> 1); k++, w *= wn) {
					complex u = a[k], t = w * a[k + (i >> 1)];
					a[k] = u + t, a[k + (i >> 1)] = u - t;
				}
			}
		}
	}
}

BigInteger BigInteger::fft_mul(const BigInteger& a, const BigInteger& b) {
	long lim = 1;
	while (lim <= 3 * (a.size + b.size)) lim <<= 1;
	__FFT::init(lim);
	using __FFT::arr;
	for (int i = 0; i < a.size; i++) {
		arr[i * 3].real = a.digits[i + 1] % 1000;
		arr[i * 3 + 1].real = a.digits[i + 1] / 1000 % 1000;
		arr[i * 3 + 2].real = a.digits[i + 1] / (1000 * 1000);
	}
	for (int i = 0; i < b.size; i++) {
		arr[i * 3].imag = b.digits[i + 1] % 1000;
		arr[i * 3 + 1].imag = b.digits[i + 1] / 1000 % 1000;
		arr[i * 3 + 2].imag = b.digits[i + 1] / (1000 * 1000);
	}
	__FFT::dft(arr, lim);
	for (int i = 0; i < lim; i++) arr[i] = (arr[i] * arr[i]).conj();
	__FFT::idft(arr, lim);
	BigInteger res;
	res.resize(a.size + b.size + 1);
	digit_t carry = 0;
	for (int i = 0; i <= a.size + b.size; i++) {
		carry += (digit_t)(arr[i * 3].imag * -0.5 / lim + 0.5);
		carry += (digit_t)(arr[i * 3 + 1].imag * -0.5 / lim + 0.5) * 1000LL;
		carry += (digit_t)(arr[i * 3 + 2].imag * -0.5 / lim + 0.5) * 1000LL * 1000LL;
		res.digits[i + 1] += carry % BASE, carry /= BASE;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}

BigInteger BigInteger::operator* (const BigInteger& x) const {
	BigInteger zero = 0;
	if (*this == zero || x == zero) return zero;
	int n = size, m = x.size;
	if (1LL * n * m >= FFT_LIMIT) {
		BigInteger res = fft_mul(*this, x);
		res.flag = !(flag ^ x.flag);
		return res;
	}

	BigInteger res;
	res.flag = !(flag ^ x.flag);
	res.resize(n + m + 2);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			res.digits[i + j - 1] += digits[i] * x.digits[j];
			res.digits[i + j] += res.digits[i + j - 1] / BASE;
			res.digits[i + j - 1] %= BASE;
		}
	}
	for (int i = 1; i <= n + m + 1; i++) {
		res.digits[i + 1] += res.digits[i] / BASE;
		res.digits[i] %= BASE;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}

BigInteger& BigInteger::operator*= (long x) {
	if (x == 0 || *this == 0) return *this = 0;
	if (x < 0) flag = !flag, x = -x;
	digit_t carry = 0;
	for (long i = 1; i <= size || carry; i++) {
		if (i > size) push(0);
		digit_t cur = digits[i] * x + carry;
		carry = cur / BigInteger::BASE;
		digits[i] = cur % BigInteger::BASE;
	}
	while (size > 1 && digits[size] == 0) pop();
	return *this;
}
BigInteger BigInteger::operator* (const long& x) const {
	BigInteger t = *this;
	return t *= x;
}

BigInteger BigInteger::div2() const {
	BigInteger res = *this;
	for (int i = size; i >= 1; i--) {
		if ((res.digits[i] & 1) && (i > 1)) res.digits[i - 1] += BASE;
		res.digits[i] >>= 1;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}
BigInteger BigInteger::operator/ (const long long& x) const {
	if (x == 0) throw -1;
	if (*this == 0) return 0;
	if (x == 2) return div2();
	if (x == -2) {BigInteger res = div2(); res.flag = !res.flag; return res;}

	BigInteger res;
	res.flag = !(flag ^ (x >= 0));

	digit_t cur = 0, div = std::abs(x);
	res.resize(size);

	for (int i = size; i >= 1; i--) {
		cur = cur * BASE + digits[i];
		res.digits[i] = res.flag ? (cur / div) : (-cur / -div);
		cur %= div;
	}
	while (res.size > 1 && res.digits[res.size] == 0) res.pop();
	return res;
}

class ZeroDivisionError : public std::exception {
public:
	const char* what() const throw() {return "BigInteger::divmod";}
};

std::pair<BigInteger, BigInteger> BigInteger::divmod(const BigInteger& x) const {
	static const long base = BigInteger::BASE;
	BigInteger a = abs(), b = x.abs();
	if (b == 0) throw ZeroDivisionError();
	if (a < b) return std::make_pair(0, flag ? a : -a);

	long t = base / (x.digits[x.size] + 1);
	a *= t, b *= t;
	long n = a.size, m = b.size;
	BigInteger q = 0, r = 0;
	q.resize(n);
	for (long i = n; i >= 1; i--) {
		r *= base, r += a.digits[i];
		digit_t d1 = m < r.size ? r.digits[m + 1] : 0, d2 = m - 1 < r.size ? r.digits[m] : 0;
		long d = (d1 * base + d2) / b.digits[m];
		r -= b * d;
		while (!r.flag) r += b, d--;
		q.digits[i] = d;
	}
	q.flag = !(flag ^ x.flag), r.flag = flag;
	while (q.size > 1 && q.digits[q.size] == 0) q.pop();
	return std::make_pair(q, r / t);
}
BigInteger BigInteger::operator/ (const BigInteger& x) const {
	return divmod(x).first;
}

BigInteger BigInteger::operator% (const long long& x) const {
	if (x == 2) return digits[1] & 1;
	if (x == 5) return digits[1] % 5;
	return *this - (*this / x * x);
}
BigInteger BigInteger::operator% (const BigInteger& x) const {
	return divmod(x).second;
}
BigInteger BigInteger::pow(const long long& x) const {
	BigInteger res = 1, a = *this;
	for (long long t = x; t != 0; t >>= 1) {
		if (t & 1) res *= a;
		a *= a;
	}
	return res;
}
BigInteger BigInteger::pow(const long long& x, const BigInteger& p) const {
	BigInteger res = 1, a = *this % p;
	for (long long t = x; t != 0; t >>= 1) {
		if (t & 1) res = res * a % p;
		a = a * a % p;
	}
	return res;
}

BigInteger BigInteger::sqrt(const long long& x) const {
	BigInteger l = 0, r = *this;
	long long len = size + x - 1;
	while (l + 1 < r) {
		BigInteger mid = (l + r) / 2;
		if (x * mid.size <= len && mid.pow(x) <= *this) l = mid;
		else r = mid;
	}
	return l.pow(x) <= *this ? l : r;
}

BigInteger BigInteger::gcd(const BigInteger& x) const {
	BigInteger a = *this, b = x;
	if (a < b) std::swap(a, b);
	if (b == 0) return a;
	int t = 0;
	while (a % 2 == 0 && b % 2 == 0) a = a.div2(), b = b.div2(), t++;
	while (b > 0) {
		if (a % 2 == 0) a = a.div2();
		else if (b % 2 == 0) b = b.div2();
		else a -= b;
		if (a < b) std::swap(a, b);
	}
	while (t--) a += a;
	return a;
}
BigInteger BigInteger::lcm(const BigInteger& x) const {
	return *this / gcd(x) * x;
}

BigInteger& BigInteger::operator+= (const BigInteger& x) {return *this = *this + x;}
BigInteger& BigInteger::operator-= (const BigInteger& x) {return *this = *this - x;}
BigInteger& BigInteger::operator*= (const BigInteger& x) {return *this = *this * x;}
BigInteger& BigInteger::operator/= (const long long& x) {return *this = *this / x;}
BigInteger& BigInteger::operator/= (const BigInteger& x) {return *this = *this / x;}
BigInteger& BigInteger::operator%= (const long long& x) {return *this = *this / x;}
BigInteger& BigInteger::operator%= (const BigInteger& x) {return *this = *this % x;}

BigInteger& BigInteger::operator++ () {return *this += 1;}
BigInteger BigInteger::operator++ (int) {BigInteger t = *this; return *this += 1, t;}
BigInteger& BigInteger::operator-- () {return *this -= 1;}
BigInteger BigInteger::operator-- (int) {BigInteger t = *this; return *this -= 1, t;}

#endif //BIGINTEGER_H