跳到正文

个人记录

a + b problem 大炮打蚊子题解

jsntzth666约 171 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月6日
篇幅
约 171 分钟
获赞
1

版本

  1. v1当前

先给AC 提交记录

闲话

2017 年的管理组:

本题的恶搞题解到此为止。

为防止新人受到误导,不再接受新的此类题解。

以前的保留不会删除,但请不要再提交。

2025 年的管理组:

鼓励全新的大炮(核弹)打蚊子技术。


方法

有人说:“主播主播,别的题解写什么cout << a + b 等方法太吃操作了,有没有一些简单易懂的好方法?” 有的兄弟有的。当然是写一个用FFT实现的高精度来维护线段树呀qwq。

高精度部分

有同学问我:“高精度怎么写?”我不敢相信这么简单都不会,黄埔军校 jsntzth课堂之手搓高精度开课。 高精度就和普通竖式差不对,但我的板子加了优化。 高精度代码:

C++
namespace bigint {
	class ZeroDivisionError : public std::runtime_error {
	public:
		ZeroDivisionError() : std::runtime_error("Division by zero") {}
	};
	class FFTLimitExceededError : public std::runtime_error {
	public:
		FFTLimitExceededError() : std::runtime_error("FFT size limit exceeded") {}
	};
	class NegativeRadicandError : public std::runtime_error {
	public:
		NegativeRadicandError()
		: std::runtime_error("Negative radicand in root operation") {}
	};
	class BigInteger {
	protected:
		static constexpr int WIDTH = 8;
		static constexpr long long BASE = 1e8;
		static constexpr long long FFT_LIMIT = 32;
		static constexpr long long NEWTON_DIV_LIMIT = 64;
		static constexpr long long NEWTON_DIV_MIN_LEVEL = 16;
		static constexpr long long NEWTON_SQRT_LIMIT = 48;
		static constexpr long long NEWTON_SQRT_MIN_LEVEL = 5;
		static constexpr size_t POOL_CHUNK_SIZE = 1024;
		long long *digits;
		int capacity, size;
		bool flag;
		inline void push(const long long &);
		inline void pop();
		inline void resize(const int &);
		inline int compare(const BigInteger &) const;
		BigInteger &build_binary(const std::vector<bool> &);
		static inline BigInteger fft_mul(const BigInteger &, const BigInteger &);
		BigInteger newton_inv(int) const;
		std::pair<BigInteger, BigInteger> newton_div(const BigInteger &) const;
		BigInteger newton_invsqrt() const;
		BigInteger sqrt_normal() const;
		std::vector<long long *> memory_pool;
		inline long long *allocate_digits(size_t);
		inline void deallocate_digits(long long *, size_t);

	public:
		void reserve(const int &);
		void clear();
		~BigInteger() = default;
		int _digit_len() const;
		friend std::ostream &operator<<(std::ostream &out, const BigInteger &x) {
			if (!x.flag)
				out << '-';
			out << x.digits[x.size];
			for (int i = x.size - 1; i >= 1; i--)
				out << std::setw(WIDTH) << std::setfill('0') << x.digits[i];
			return out;
		}
		friend std::istream &operator>>(std::istream &in, BigInteger &x) {
			std::string s;
			in >> s;
			x = s;
			return in;
		}
		BigInteger() : digits(nullptr), flag(true) {
			*this = 0ll;
		}
		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(const std::vector<bool> &b) : digits(nullptr) {
			*this = b;
		}
		template <class T> BigInteger(const T &begin, const T &end) : digits(nullptr) {
			*this = std::vector<bool>(begin, end);
		}
		BigInteger(const long long *in, int len) : digits(nullptr), flag(true) {
			while (len > 0 && in[len - 1] == 0)
				len--, in++;
			size = len;
			digits = allocate_digits(len + 1);
			for (int i = 0; i < len; i++)
				digits[i + 1] = in[i];
		}
		BigInteger &operator=(const BigInteger &);
		BigInteger &operator=(const long long &);
		BigInteger &operator=(const std::string &);
		BigInteger &operator=(const std::vector<bool> &);
		BigInteger &operator=(const __int128 &);
		std::string to_string() const;
		long long to_long_long() const;
		std::vector<bool> to_binary() const;
		__int128 to_int128() const;
		BigInteger operator-() const;
		BigInteger operator~() const;
		BigInteger abs() const;
		bool operator==(const BigInteger &) const;
		auto operator<=>(const BigInteger &) const;
		BigInteger div2() const;
		std::pair<BigInteger, BigInteger> divmod(const BigInteger &,
			bool = false) const;
		BigInteger operator+(const BigInteger &) 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 operator%(const long long &) const;
		BigInteger operator%(const BigInteger &) const;
		BigInteger pow(const long long &) const;
		template <typename T> BigInteger pow(const long long &, const T &) const;
		BigInteger root(const long long & = 2) const;
		BigInteger sqrt() const;
		BigInteger gcd(const BigInteger &) const;
		BigInteger lcm(const BigInteger &) const;
		inline BigInteger _move_l(int) const;
		inline BigInteger _move_r(int) const;
		BigInteger &operator+=(const BigInteger &);
		BigInteger &operator-=(const BigInteger &);
		BigInteger &operator*=(long 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<<(const long long &);
		BigInteger operator>>(const long long &);
		BigInteger &operator<<=(const long long &);
		BigInteger &operator>>=(const long long &);
		BigInteger operator&(const BigInteger &);
		BigInteger operator|(const BigInteger &);
		BigInteger operator^(const BigInteger &);
		BigInteger &operator&=(const BigInteger &);
		BigInteger &operator|=(const BigInteger &);
		BigInteger &operator^=(const BigInteger &);
		BigInteger &operator++();
		BigInteger operator++(int);
		BigInteger &operator--();
		BigInteger operator--(int);
		static BigInteger random(const int &);
	};
	inline void BigInteger::push(const long long &val) {
		if (size == capacity) {
			int new_capacity = (capacity < 256)
			? capacity * 2
			: static_cast<int>(std::pow(capacity, 1.125));
			long long *new_digits = allocate_digits(new_capacity + 1);
			std::memcpy(new_digits, digits, sizeof(long long) * (capacity + 1));
			deallocate_digits(digits, capacity + 1);
			digits = new_digits;
			capacity = new_capacity;
		}
		digits[++size] = val;
	}
	inline void BigInteger::pop() {
		digits[size--] = 0;
	}
	inline void BigInteger::resize(const int &sz) {
		reserve(sz), size = sz;
	}
	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;
	}
	BigInteger &BigInteger::build_binary(const std::vector<bool> &b) {
		*this = 0ll;
		if (b.empty() || (b.size() == 1 && b[0] == 0))
			return *this;
		BigInteger pw2 = 1;
		for (int i = b.size() - 1; i >= 0; i--, pw2 += pw2)
			if (b[i])
				* this += pw2;
		return *this;
	}
	inline long long *BigInteger::allocate_digits(size_t size) {
		for (auto it = memory_pool.begin(); it != memory_pool.end(); ++it) {
			size_t *block_size = reinterpret_cast<size_t *>(*it) - 1;
			if (*block_size >= size) {
				long long *block = *it;
				memory_pool.erase(it);
				std::memset(block, 0, size * sizeof(long long));
				return block;
			}
		}
		size_t *mem = static_cast<size_t *>(
			::operator new (sizeof(size_t) + size * sizeof(long long)));
		*mem = size;
		long long *ptr = reinterpret_cast<long long *>(mem + 1);
		std::memset(ptr, 0, size * sizeof(long long));
		return ptr;
	}
	inline void BigInteger::deallocate_digits(long long *ptr, size_t size) {
		if (!ptr)
			return;
		size_t *block_size = reinterpret_cast<size_t *>(ptr) - 1;
		if (*block_size >= POOL_CHUNK_SIZE)
			memory_pool.push_back(ptr);
		else
			::operator delete (block_size);
	}
	void BigInteger::reserve(const int &sz) {
		if (sz < 0)
			return;
		if (digits != nullptr)
			deallocate_digits(digits, capacity + 1);
		capacity = sz;
		size = 0;
		digits = allocate_digits(sz + 1);
	}
	void BigInteger::clear() {
		deallocate_digits(digits, capacity + 1);
		digits = nullptr;
		size = capacity = 0;
	}
	int BigInteger::_digit_len() const {
		return size;
	}
	BigInteger &BigInteger::operator=(const BigInteger &x) {
		if (this != &x) {
			long long *new_digits = allocate_digits(x.size + 1);
			deallocate_digits(digits, capacity + 1);
			digits = new_digits;
			capacity = x.size;
			flag = x.flag;
			size = x.size;
			std::memcpy(digits, x.digits, sizeof(long long) * (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 = 0ll;
		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::stoll(s.substr(start, len)));
		}
		while (size > 1 && digits[size] == 0)
			pop();
		return *this;
	}
	BigInteger &BigInteger::operator=(const std::vector<bool> &a) {
		if (a == std::vector<bool> {0ll})
			return *this = 0ll;
		std::vector<bool> res;
		if (a[0] == 0)
			return build_binary(a);
		res.resize(a.size());
		for (int i = 0; i < (int)a.size(); ++i)
			res[i] = !a[i];
		BigInteger x;
		x.build_binary(res);
		return *this = -x - 1;
	}
	BigInteger &BigInteger::operator=(const __int128 &x) {
		flag = (x >= 0), reserve(8);
		if (x == 0)
			return size = 1, digits[1] = 0, *this;
		__int128 n = x >= 0 ? x : -x;
		do {
			push(n % BASE), n /= BASE;
		} while (n);
		return *this;
	}
	std::string BigInteger::to_string() const {
		std::stringstream ss;
		ss << *this;
		return ss.str();
	}
	long long BigInteger::to_long_long() const {
		return std::stoll(to_string());
	}
	std::vector<bool> BigInteger::to_binary() const {
		if (*this == 0)
			return {0, 0};
		std::vector<bool> res;
		if (flag) {
			for (BigInteger x = *this; x != 0; x = x.div2())
				res.emplace_back(x.digits[1] & 1);
			res.emplace_back(0);
			std::reverse(res.begin(), res.end());
			return res;
		} else {
			for (BigInteger x = -*this - 1; x != 0; x = x.div2())
				res.emplace_back(!(x.digits[1] & 1));
			res.emplace_back(1);
			std::reverse(res.begin(), res.end());
			return res;
		}
	};
	__int128 BigInteger::to_int128() const {
		__int128 res = 0;
		for (int i = size; i >= 1; i--)
			res = res * BASE + digits[i];
		return res;
	}
	BigInteger BigInteger::operator-() const {
		if (*this == 0)
			return 0;
		BigInteger res = *this;
		res.flag = !flag;
		return res;
	}
	BigInteger BigInteger::operator~() const {
		return -(*this) - 1;
	}
	BigInteger BigInteger::abs() const {
		BigInteger res = *this;
		res.flag = true;
		return res;
	}
	bool BigInteger::operator==(const BigInteger &x) const {
		return compare(x) == 0;
	}
	auto BigInteger::operator<=>(const BigInteger &x) const {
		return compare(x);
	}
	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);
		long long carry = 0;
		for (int i = 1; i <= n; i++) {
			long long 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;
		long long carry = 0;
		int n = std::max(size, x.size);
		res.reserve(n);
		for (int i = 1; i <= n; i++) {
			long long 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 FastFourierTransformation {
		constexpr long long FFT_BASE = 1e4;
		constexpr double PI2 = 6.283185307179586231995927;
		constexpr double PI6 = 18.84955592153875869598778;
		constexpr int RECALC_WIDTH = 10;
		constexpr int RECALC_BASE = (1 << RECALC_WIDTH) - 1;
		struct complex {
			double real, imag;
			complex(double x = 0.0, 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 &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) {
			delete[] arr;
			arr = new (std::nothrow) complex[n + 1];
			if (!arr)
				throw std::bad_alloc();
		}
		template <const int n> inline void fft(complex *a) {
			const int n2 = n >> 1, n4 = n >> 2;
			complex w(1.0, 0.0), w3(1.0, 0.0);
			const complex wn(std::cos(PI2 / n), std::sin(PI2 / n)),
			wn3(std::cos(PI6 / n), std::sin(PI6 / n));
			for (int i = 0; i < n4; i++, w *= wn, w3 *= wn3) {
				if (!(i & RECALC_BASE))
					w = complex(std::cos(PI2 * i / n), std::sin(PI2 * i / n)), w3 = w * w * w;
				complex x = a[i] - a[i + n2], y = a[i + n4] - a[i + n2 + n4];
				y = complex(y.imag, -y.real);
				a[i] += a[i + n2], a[i + n4] += a[i + n2 + n4];
				a[i + n2] = (x - y) * w, a[i + n2 + n4] = (x + y) * w3;
			}
			fft<n2>(a), fft<n4>(a + n2), fft<n4>(a + n2 + n4);
		}
		template <> inline void fft<0>(complex *) {}
		template <> inline void fft<1>(complex *) {}
		template <> inline void fft<2>(complex *a) {
			complex x = a[0], y = a[1];
			a[0] += y, a[1] = x - y;
		}
		template <> inline void fft<4>(complex *a) {
			complex a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3];
			complex x = a0 - a2, y = a1 - a3;
			y = complex(y.imag, -y.real);
			a[0] += a2, a[1] += a3, a[2] = x - y, a[3] = x + y;
			fft<2>(a);
		}
		template <const int n> inline void ifft(complex *a) {
			const int n2 = n >> 1, n4 = n >> 2;
			ifft<n2>(a), ifft<n4>(a + n2), ifft<n4>(a + n2 + n4);
			complex w(1.0, 0.0), w3(1.0, 0.0);
			const complex wn(std::cos(PI2 / n), -std::sin(PI2 / n)),
			wn3(std::cos(PI6 / n), -std::sin(PI6 / n));
			for (int i = 0; i < n4; i++, w *= wn, w3 *= wn3) {
				if (!(i & RECALC_BASE))
					w = complex(std::cos(PI2 * i / n), -std::sin(PI2 * i / n)),
					w3 = w * w * w;
				complex p = w * a[i + n2], q = w3 * a[i + n2 + n4];
				complex x = a[i], y = p + q, x1 = a[i + n4], y1 = p - q;
				y1 = complex(y1.imag, -y1.real);
				a[i] += y, a[i + n4] += y1, a[i + n2] = x - y, a[i + n2 + n4] = x1 - y1;
			}
		}
		template <> inline void ifft<0>(complex *) {}
		template <> inline void ifft<1>(complex *) {}
		template <> inline void ifft<2>(complex *a) {
			complex x = a[0], y = a[1];
			a[0] += y, a[1] = x - y;
		}
		template <> inline void ifft<4>(complex *a) {
			ifft<2>(a);
			complex p = a[2], q = a[3];
			complex x = a[0], y = p + q, x1 = a[1], y1 = p - q;
			y1 = complex(y1.imag, -y1.real);
			a[0] += y, a[1] += y1, a[2] = x - y, a[3] = x1 - y1;
		}
		inline void dft(complex *a, int n) {
			if (n <= 1)
				return;
			switch (n) {
			case 1 << 2:
				fft < 1 << 2 > (a);
				break;
			case 1 << 3:
				fft < 1 << 3 > (a);
				break;
			case 1 << 4:
				fft < 1 << 4 > (a);
				break;
			case 1 << 5:
				fft < 1 << 5 > (a);
				break;
			case 1 << 6:
				fft < 1 << 6 > (a);
				break;
			case 1 << 7:
				fft < 1 << 7 > (a);
				break;
			case 1 << 8:
				fft < 1 << 8 > (a);
				break;
			case 1 << 9:
				fft < 1 << 9 > (a);
				break;
			case 1 << 10:
				fft < 1 << 10 > (a);
				break;
			case 1 << 11:
				fft < 1 << 11 > (a);
				break;
			case 1 << 12:
				fft < 1 << 12 > (a);
				break;
			case 1 << 13:
				fft < 1 << 13 > (a);
				break;
			case 1 << 14:
				fft < 1 << 14 > (a);
				break;
			case 1 << 15:
				fft < 1 << 15 > (a);
				break;
			case 1 << 16:
				fft < 1 << 16 > (a);
				break;
			case 1 << 17:
				fft < 1 << 17 > (a);
				break;
			case 1 << 18:
				fft < 1 << 18 > (a);
				break;
			case 1 << 19:
				fft < 1 << 19 > (a);
				break;
			case 1 << 20:
				fft < 1 << 20 > (a);
				break;
			case 1 << 21:
				fft < 1 << 21 > (a);
				break;
			case 1 << 22:
				fft < 1 << 22 > (a);
				break;
			case 1 << 23:
				fft < 1 << 23 > (a);
				break;
			case 1 << 24:
				fft < 1 << 24 > (a);
				break;
			case 1 << 25:
				fft < 1 << 25 > (a);
				break;
			case 1 << 26:
				fft < 1 << 26 > (a);
				break;
			case 1 << 27:
				fft < 1 << 27 > (a);
				break;
			case 1 << 28:
				fft < 1 << 28 > (a);
				break;
				throw FFTLimitExceededError();
			}
		}
		inline void idft(complex *a, int n) {
			if (n <= 1)
				return;
			switch (n) {
			case 1 << 2:
				ifft < 1 << 2 > (a);
				break;
			case 1 << 3:
				ifft < 1 << 3 > (a);
				break;
			case 1 << 4:
				ifft < 1 << 4 > (a);
				break;
			case 1 << 5:
				ifft < 1 << 5 > (a);
				break;
			case 1 << 6:
				ifft < 1 << 6 > (a);
				break;
			case 1 << 7:
				ifft < 1 << 7 > (a);
				break;
			case 1 << 8:
				ifft < 1 << 8 > (a);
				break;
			case 1 << 9:
				ifft < 1 << 9 > (a);
				break;
			case 1 << 10:
				ifft < 1 << 10 > (a);
				break;
			case 1 << 11:
				ifft < 1 << 11 > (a);
				break;
			case 1 << 12:
				ifft < 1 << 12 > (a);
				break;
			case 1 << 13:
				ifft < 1 << 13 > (a);
				break;
			case 1 << 14:
				ifft < 1 << 14 > (a);
				break;
			case 1 << 15:
				ifft < 1 << 15 > (a);
				break;
			case 1 << 16:
				ifft < 1 << 16 > (a);
				break;
			case 1 << 17:
				ifft < 1 << 17 > (a);
				break;
			case 1 << 18:
				ifft < 1 << 18 > (a);
				break;
			case 1 << 19:
				ifft < 1 << 19 > (a);
				break;
			case 1 << 20:
				ifft < 1 << 20 > (a);
				break;
			case 1 << 21:
				ifft < 1 << 21 > (a);
				break;
			case 1 << 22:
				ifft < 1 << 22 > (a);
				break;
			case 1 << 23:
				ifft < 1 << 23 > (a);
				break;
			case 1 << 24:
				ifft < 1 << 24 > (a);
				break;
			case 1 << 25:
				ifft < 1 << 25 > (a);
				break;
			case 1 << 26:
				ifft < 1 << 26 > (a);
				break;
			case 1 << 27:
				ifft < 1 << 27 > (a);
				break;
			case 1 << 28:
				ifft < 1 << 28 > (a);
				break;
				throw FFTLimitExceededError();
			}
		}
	} // namespace FastFourierTransformation
	BigInteger BigInteger::fft_mul(const BigInteger &a, const BigInteger &b) {
		int least = (a.size + b.size) << 1, lim = 1 << std::__lg(least);
		if (lim < least)
			lim <<= 1;
		FastFourierTransformation::init(lim);
		for (int i = 0; i < a.size; i++) {
			FastFourierTransformation::arr[i << 1].real =
			a.digits[i + 1] % FastFourierTransformation::FFT_BASE;
			FastFourierTransformation::arr[i << 1 | 1].real =
			a.digits[i + 1] / FastFourierTransformation::FFT_BASE %
			FastFourierTransformation::FFT_BASE;
		}
		for (int i = 0; i < b.size; i++) {
			FastFourierTransformation::arr[i << 1].imag =
			b.digits[i + 1] % FastFourierTransformation::FFT_BASE;
			FastFourierTransformation::arr[i << 1 | 1].imag =
			b.digits[i + 1] / FastFourierTransformation::FFT_BASE %
			FastFourierTransformation::FFT_BASE;
		}
		dft(FastFourierTransformation::arr, lim);
		for (int i = 0; i < lim; i++)
			FastFourierTransformation::arr[i] *= FastFourierTransformation::arr[i];
		idft(FastFourierTransformation::arr, lim);
		BigInteger res;
		res.resize(a.size + b.size + 1);
		long long carry = 0;
		double inv = 0.5 / lim;
		for (int i = 0; i <= a.size + b.size; i++) {
			carry +=
			(long long)(FastFourierTransformation::arr[i << 1].imag * inv + 0.5);
			carry += (long long)(FastFourierTransformation::arr[i << 1 | 1].imag * inv +
				0.5) *
			FastFourierTransformation::FFT_BASE;
			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;
		long long lim = 1LL * n * m;
		if (lim >= 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 long x) {
		if (x == 0 || *this == 0)
			return *this = 0ll;
		if (x < 0)
			flag = !flag, x = -x;
		long long carry = 0;
		for (int i = 1; i <= size || carry; i++) {
			if (i > size)
				push(0);
			long long 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 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 ZeroDivisionError();
		if (*this == 0)
			return 0;
		if (x == 2)
			return div2();
		if (x == -2)
			return -div2();
		BigInteger res;
		res.flag = !(flag ^ (x >= 0));
		long long 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;
	}
	inline BigInteger BigInteger::_move_r(int d) const {
		if (*this == 0 || d >= size)
			return 0;
		if (d == 0)
			return *this;
		BigInteger res;
		res.reserve(size - d + 1);
		for (int i = d + 1; i <= size; i++)
			res.push(digits[i]);
		return res;
	}
	inline BigInteger BigInteger::_move_l(int d) const {
		if (*this == 0)
			return 0;
		if (d == 0)
			return *this;
		BigInteger res;
		res.reserve(size + d + 1);
		for (int i = 1; i <= d; i++)
			res.push(0);
		for (int i = 1; i <= size; i++)
			res.push(digits[i]);
		return res;
	}
	BigInteger BigInteger::newton_inv(int n) const {
		if (*this == 0)
			throw ZeroDivisionError();
		if (std::min(size, n - size) <= NEWTON_DIV_MIN_LEVEL) {
			BigInteger a;
			a.resize(n + 1);
			std::memset(a.digits, 0, sizeof(long long) * a.size);
			a.digits[n + 1] = 1;
			return a.divmod(*this, true).first;
		}
		int k = (n - size + 2) >> 1, k2 = k > size ? 0 : size - k;
		BigInteger x = _move_r(k2);
		int n2 = k + x.size;
		BigInteger y = x.newton_inv(n2), a = y + y, b = (*this) * y * y;
		return a._move_l(n - n2 - k2) - b._move_r(2 * (n2 + k2) - n) - 1;
	}
	std::pair<BigInteger, BigInteger> BigInteger::newton_div(const BigInteger &x) const {
		int k = size - x.size + 2, k2 = k > x.size ? 0 : x.size - k;
		BigInteger x2 = x._move_r(k2);
		if (k2 != 0)
			x2 += 1;
		int n2 = k + x2.size;
		BigInteger u = (*this) * x2.newton_inv(n2);
		BigInteger q = u._move_r(n2 + k2), r = (*this) - q * x;
		while (r >= x)
			q += 1, r -= x;
		return {q, r};
	}
	std::pair<BigInteger, BigInteger> BigInteger::divmod(const BigInteger &x,
		bool dis_newton) const {
			static const int base = BigInteger::BASE;
			BigInteger a = abs(), b = x.abs();
			if (b == 0)
				throw ZeroDivisionError();
			if (a < b)
				return {0, flag ? a : -a};
			if (!dis_newton && size > NEWTON_DIV_LIMIT)
				return newton_div(x);
			int t = base / (x.digits[x.size] + 1);
			a *= t, b *= t;
			int n = a.size, m = b.size;
			BigInteger q = 0, r = 0;
			q.resize(n);
			for (int i = n; i >= 1; i--) {
				r *= base, r += a.digits[i];
				long long d1 = m < r.size ? r.digits[m + 1] : 0,
				d2 = m - 1 < r.size ? r.digits[m] : 0;
				int 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 {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 || x == 4 || x == 5)
			return digits[1] % x;
		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 = res * a;
			a = a * a;
		}
		return res;
	}
	template <typename T>
	BigInteger BigInteger::pow(const long long &x, const T &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::root(const long long &m) const {
		if (!flag && m % 2 == 0)
			throw NegativeRadicandError();
		if (m == 1 || *this == 0)
			return *this;
		if (m == 2)
			return sqrt();
		static constexpr long long base = BigInteger::BASE;
		if (size <= m) {
			long long l = 0, r = base - 1;
			while (l < r) {
				long long mid = (l + r + 1) >> 1;
				if (BigInteger(mid).pow(m) <= *this)
					l = mid;
				else
					r = mid - 1;
			}
			return l;
		}
		if (size <= m * 2) {
			BigInteger res;
			res.resize(2);
			long long l = 0, r = base - 1;
			while (l < r) {
				long long mid = (l + r + 1) >> 1;
				res.digits[2] = mid;
				if (res.pow(m) <= *this)
					l = mid;
				else
					r = mid - 1;
			}
			res.digits[2] = l, l = 0, r = base - 1;
			while (l < r) {
				long long mid = (l + r + 1) >> 1;
				res.digits[1] = mid;
				if (res.pow(m) <= *this)
					l = mid;
				else
					r = mid - 1;
			}
			return res.digits[1] = l, res;
		}
		int n = size, t = n / m / 2;
		BigInteger s = (_move_r(t * m).root(m) + 1)._move_l(t);
		BigInteger res = (s * (m - 1) + *this / s.pow(m - 1)) / m;
		long long l = std::max<long long>(res.digits[1] - 100, 0),
		r = std::min(res.digits[1] + 100, base - 1);
		while (l < r) {
			long long mid = (l + r + 1) >> 1;
			res.digits[1] = mid;
			if (res.pow(m) <= *this)
				l = mid;
			else
				r = mid - 1;
		}
		return res.digits[1] = l, res;
	}
	BigInteger BigInteger::newton_invsqrt() const {
		const BigInteger &x = *this;
		static constexpr long long base = BigInteger::BASE;
		if (x.size <= 2) {
			if (x.size == 0)
				return 0;
			uint64_t t = 0;
			if (x.size == 1)
				t = x.digits[1];
			else
				t = x.digits[1] + x.digits[2] * BASE;
			return uint64_t(base * base / std::sqrt(t));
		}
		if (x.size <= NEWTON_SQRT_MIN_LEVEL) {
			BigInteger b1 = BigInteger(base).pow(x.size / 2);
			return b1 / x.sqrt_normal();
		}
		int n2 = x.size % 2 == 0 ? x.size : x.size + 1, k2 = (n2 + 2) / 4 * 2;
		BigInteger x2k(x.digits + 1 + (n2 - k2), k2 + x.size - n2);
		BigInteger s = x2k.newton_invsqrt()._move_l((n2 - k2) / 2);
		BigInteger x2 = (s * 3).div2() - (s * s * s * x).div2()._move_r(2 * n2);
		BigInteger rx = BigInteger(1)._move_l(2 * n2) - x * x2 * x2, delta = 1;
		if (!rx.flag) {
			for (; !rx.flag; delta *= 2) {
				BigInteger t = (x2 * 2 - delta + delta * delta) * x;
				x2 -= delta, rx += t;
			}
		} else {
			while (true) {
				BigInteger t = (x2 * 2 + delta) * delta * x;
				if (t > rx)
					break;
				x2 += delta, rx -= t;
				delta *= 2;
			}
		}
		for (; delta > 0; delta = delta.div2()) {
			BigInteger t = (x2 * 2 + delta) * delta * x;
			if (t <= rx)
				x2 += delta, rx -= t;
		}
		return x2;
	}
	BigInteger BigInteger::sqrt_normal() const {
		static constexpr long long base = BigInteger::BASE;
		BigInteger n = *this, x0 = BigInteger(base)._move_l((n.size + 2) >> 1);
		BigInteger x = (x0 + n / x0).div2();
		while (x < x0)
			std::swap(x, x0), x = (x0 + n / x0).div2();
		return x0;
	}
	BigInteger BigInteger::sqrt() const {
		if (!flag)
			throw NegativeRadicandError();
		if (*this <= 1)
			return *this;
		if (size <= NEWTON_SQRT_LIMIT)
			return sqrt_normal();
		const BigInteger &x = *this;
		int n2 = x.size % 2 == 0 ? x.size : x.size + 1;
		BigInteger res = (x * x.newton_invsqrt())._move_r(n2);
		BigInteger r = x - res * res, delta = 1;
		while (true) {
			BigInteger dr = (res * 2 + delta) * delta;
			if (dr > r)
				break;
			r -= dr, res += delta, delta *= 2;
		}
		for (; delta > 0; delta = delta.div2()) {
			BigInteger dr = (res * 2 + delta) * delta;
			if (dr <= r)
				r -= dr, res += delta;
		}
		return res;
	}
	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<<(const long long &x) {
		return *this * BigInteger(2).pow(x);
	}
	BigInteger BigInteger::operator>>(const long long &x) {
		return *this / BigInteger(2).pow(x);
	}
	BigInteger &BigInteger::operator<<=(const long long &x) {
		return *this = *this << x;
	}
	BigInteger &BigInteger::operator>>=(const long long &x) {
		return *this = *this >> x;
	}
	BigInteger BigInteger::operator&(const BigInteger &x) {
		std::vector<bool> a = to_binary(), b = x.to_binary();
		int n = a.size(), m = b.size(), lim = std::max(n, m);
		std::vector<bool> res(lim), temp(lim);
		if (m == lim) {
			a.resize(lim);
			for (int i = 0; i < n; ++i)
				temp[lim - n + i] = a[i];
			a = temp;
		} else {
			b.resize(lim);
			for (int i = 0; i < m; ++i)
				temp[lim - m + i] = b[i];
			b = temp;
		}
		for (int i = 0; i < lim; ++i)
			res[i] = a[i] & b[i];
		return res;
	}
	BigInteger BigInteger::operator|(const BigInteger &x) {
		std::vector<bool> a = to_binary(), b = x.to_binary();
		int n = a.size(), m = b.size(), lim = std::max(n, m);
		std::vector<bool> res(lim), temp(lim);
		if (m == lim) {
			a.resize(lim);
			for (int i = 0; i < n; ++i)
				temp[lim - n + i] = a[i];
			a = temp;
		} else {
			b.resize(lim);
			for (int i = 0; i < m; ++i)
				temp[lim - m + i] = b[i];
			b = temp;
		}
		for (int i = 0; i < lim; ++i)
			res[i] = a[i] | b[i];
		return res;
	}
	BigInteger BigInteger::operator^(const BigInteger &x) {
		std::vector<bool> a = to_binary(), b = x.to_binary();
		int n = a.size(), m = b.size(), lim = std::max(n, m);
		std::vector<bool> res(lim), temp(lim);
		if (m == lim) {
			a.resize(lim);
			for (int i = 0; i < n; ++i)
				temp[lim - n + i] = a[i];
			a = temp;
		} else {
			b.resize(lim);
			for (int i = 0; i < m; ++i)
				temp[lim - m + i] = b[i];
			b = temp;
		}
		for (int i = 0; i < lim; ++i)
			res[i] = a[i] ^ b[i];
		return res;
	}
	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++() {
		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;
	}
	BigInteger BigInteger::random(const int &num_digits) {
		std::random_device rd;
		std::mt19937 gen(rd());
		BigInteger res;
		res.reserve(num_digits / 8 + 8);
		res.size = 0;
		std::uniform_int_distribution<long long> last_digits(
			1, std::pow(10, (num_digits - 1) % 8 + 1) - 1);
		if (num_digits % 8)
			res.push(last_digits(gen));
		std::uniform_int_distribution<long long> other_digits(0, 99999999);
		for (int i = 1; i <= num_digits / 8; ++i)
			res.push(other_digits(gen));
		return res;
	}
}
using namespace bigint;

接下来开始 打广告,啊呸,是复杂度分析:

操作

记 n=⌊lg⁡max⁡(a,b)+1⌋,w=8n=\lfloor\lg\max(a,b)+1\rfloor,w=8。

表达式含义时间复杂度空间复杂度备注
a.to_string()求 aa 的string表示O(nw)O(\dfrac{n}{w})O(n)O(n)
a.to_long_long()求 aa 的long long表示O(nw)O(\dfrac{n}{w})O(n)O(n)
a.to_binary()求 aa 的二进制表示O(n2w)O(\dfrac{n^2}{w})O(n2)O(n^2)
a.to_int128()求 aa 的__int128表示O(nw)O(\dfrac{n}{w})O(n)O(n)
a._digit_len()求 ⌊nw⌋\lfloor \dfrac{n}{w} \rfloorO(1)O(1)O(1)O(1)
-a求 −a-aO(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
~a求 −a−1-a-1O(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a.abs()求 ∣a∣\mid a\midO(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a <=> b求大小关系O(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a.div2()求 ⌊a2⌋\lfloor \dfrac{a}{2} \rfloorO(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a.divmod(b)除法 + 取余(普通)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a.divmod(b)除法 + 取余(牛迭)O(nlog⁡nw)O(\dfrac{n\log n}{w})O(nlog⁡nw)O(\dfrac{n\log n}{w})
a + b加法O(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a - b减法O(nw)O(\dfrac{n}{w})O(nw)O(\dfrac{n}{w})
a * b乘法(普通)O(n2w2)O(\dfrac{n^2}{w^2})O(nw)O(\dfrac{n}{w})
a * b乘法(FFT)O(nlog⁡nw)O(\dfrac{n\log n}{w})O(n)O(n)
a / b除法(普通)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a / b除法(牛迭)O(nlog⁡nw)O(\dfrac{n \log n}{w})O(nlog⁡nw)O(\dfrac{n \log n}{w})
a % b取余(普通)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a % b取余(牛迭)O(nlog⁡nw)O(\dfrac{n \log n}{w})O(nlog⁡nw)O(\dfrac{n\log n}{w})
a.pow(x)乘方(普通)O(n2log⁡nlog⁡xw)O(\dfrac{n^2\log n\log x}{w})O(nx)O(nx)
a.pow(x,p)乘方(取余)O(n2log⁡nlog⁡xw)O(\dfrac{n^2\log n\log x}{w})O(n)O(n)
a.root(x)求 ⌊ax⌋\lfloor \sqrt[x]{a} \rfloorO(nlog⁡nw)O(\dfrac{n \log n}{w})O(nw)O(\dfrac{n}{w})
a.sqrt()求 ⌊a⌋\lfloor \sqrt{a} \rfloorO(nlog⁡nw)O(\dfrac{n \log n}{w})O(nw)O(\dfrac{n}{w})
a.gcd(b)求 gcd⁡(a,b)\gcd(a,b)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a.lcm(b)求 lcm⁡(a,b)\operatorname{lcm}(a,b)O(n2w)O(\dfrac{n^2}{w})O(nw)O(\dfrac{n}{w})
a & b按位与O(n2)O(n^2)O(n)O(n)
a | b按位或O(n2)O(n^2)O(n)O(n)
a ^ b按位异或O(n2)O(n^2)O(n)O(n)
a << x求 a⋅2xa\cdot 2^xO(nxw)O(\dfrac{nx}{w})O(n)O(n)
a >> x求 ⌊a2x⌋\lfloor \dfrac{a}{2^x} \rfloorO(nxw)O(\dfrac{nx}{w})O(n)O(n)
a._move_l(x)求 a⋅10wxa\cdot 10^{wx}O(nw+x)O(\dfrac{n}{w}+x)O(nw+x)O(\dfrac{n}{w}+x)
a._move_r(x)求 ⌊a10wx⌋\lfloor \dfrac{a}{10^{wx}} \rfloorO(nw−x)O(\dfrac{n}{w}-x)O(nw−x)O(\dfrac{n}{w}-x)
random_big(x)生成位数为 xx 的随机正数O(n)O(n)O(n)O(n)

优点

  • 高度封装,支持许多操作;
  • 速度较快,大部分操作已优化到较优秀的复杂度;
  • 代码较短(约 30KB)。

缺点

  • 位运算速度慢;
  • 整数位数较大时,FFT 会出现误差;
  • 其他偶然错误。

性能测试

测试信息
HTML
CPU:12th Gen Intel(R) Core(TM) i3-12100   3.30 GHz
RAM:24.0 GB
系统类型:64 位操作系统,基于 x64 的处理器
版本:Windows 11 专业版
编译器:TDM-GCC 14.2.0 64-bit Release
编译语言:C++ 23
软件:Dev-C++ 6.7.5
备注:采用文件读写的方式,运用 Windows 系统函数测速,多次测速取算数平均数,最坏情况
单位:毫秒
测试结果
10410^45×1045\times 10^410510^55×1055\times 10^510610^65×1065\times 10^610710^7
加法0.00870.00870.05460.05460.10750.10750.53580.53581.40621.40625.31545.31549.40039.4003
减法0.00540.00540.03530.03530.08510.08510.46040.46041.36051.36054.06914.06918.43798.4379
乘法0.11830.11830.86810.86811.93431.93439.17529.175218.820318.8203207.892207.892413.864413.864
除法0.45510.45511.57091.57094.60364.603610.165810.165823.342723.3427226.032226.032515.148515.148
取余0.68640.68641.01691.01692.15622.156212.08812.08831.830431.8304265.982265.982491.209491.209
开平方3.36513.365118.737118.737138.37838.378189.588189.588352.961352.961
GCD152.062152.0624930.94930.9
LCM157.421157.4214962.434962.43
位运算191.679191.6795605.275605.27

OK啊,接下来是线段树部分。

又双叒叕有同学问我线段树是什么,我不明白这么简单都不会。

一、简介线段树

ps: 此处以询问区间和为例。实际上线段树可以处理很多符合结合律的操作。

比如说加法,a[1]+a[2]+a[3]+a[4]=(a[1]+a[2])+(a[3]+a[4]) 。

线段树之所以称为“树”,是因为其具有树的结构特性。线段树由于本身是专门用来处理区间问题的(包括 RMQ、RSQ 问题等)。

对于每一个子节点而言,都表示整个序列中的一段子区间;对于每个叶子节点而言,都表示序列中的单个元素信息;子节点不断向自己的父亲节点传递信息,而父节点存储的信息则是他的每一个子节点信息的整合。

有没有觉得很熟悉?对,线段树就是分块思想的树化,或者说是对于信息处理的二进制化——用于达到 O(log⁡n)O(\log n) 级别的处理速度,log⁡\log 以 22 为底。(其实以几为底都只不过是个常数,可忽略)。而分块的思想,则是可以用一句话总结为:通过将整个序列分为有穷个小块,对于要查询的一段区间,总是可以整合成 kk 个所分块与 mm 个单个元素的信息的并 (0≤k,m≤n)(0\leq k,m\leq \sqrt{n})。但普通的分块不能高效率地解决很多问题,所以作为 log⁡\log 级别的数据结构,线段树应运而生。


Extra  Tips\mathcal{Extra \ \ Tips}

其实,虽然线段树的时间效率要高于分块但是实际上分块的总合并次数不会超过 n\sqrt{n} 但是线段树在最坏情况下的合并次数显然是要大于这个时间效率的。

但是毕竟也只是一个很大的常数而已。

However,虽说如此,分块的应用范围还是要广于线段树的,因为虽然线段树好像很快,但是它只能维护带有结合律的信息,比如区间 max⁡/min⁡\max/\min、∑\sum、xor\rm xor 之类的,但是不带有结合律的信息就不能维护(且看下文分解);而分块则灵活得多,可以维护很多别的东西,因为实际上分块的本质就是优雅的暴力。

其实越暴力的算法可以支持的操作就越多、功能性就越强!你看 n2n^2 的暴力几乎什么都可以维护。


二、逐步分析线段树的构造实现

1、建树与维护

由于二叉树的自身特性,对于每个父亲节点的编号 ii,他的两个儿子的编号分别是 2i2i 和 2i+12i+1,所以我们考虑写两个 O(1)O(1) 的取儿子函数。 Extra  Tips\mathcal{Extra \ \ Tips} 那么根据线段树的服务对象,可以得到线段树的维护。 此处一定要注意,push up 操作的目的是为了维护父子节点之间的逻辑关系。当我们递归建树时,对于每一个节点我们都需要遍历一遍,并且电脑中的递归实际意义是先向底层递归,然后从底层向上回溯,所以开始递归之后必然是先去整合子节点的信息,再向它们的祖先回溯整合之后的信息。(这其实是正确性的证明啦)。 呐,我们在这儿就能看出来,实际上 push_up 是在合并两个子节点的信息,所以需要信息满足结合律! 那么对于建树,由于二叉树自身的父子节点之间的可传递关系,所以可以考虑递归建树(emmmm 之前好像不小心剧透了 qwq),并且在建树的同时,我们应该维护父子节点的关系。

2、接下来谈区间修改

为什么不讨论单点修改呢 ?因为其实很显然,单点修改就是区间修改的一个子问题而已,即区间长度为11时进行的区间修改操作罢了 qwq。

那么对于区间操作,我们考虑引入一个名叫“ lazy tag\rm lazy~tag ”(懒标记)的东西——之所以称其“lazy\rm lazy”,是因为原本区间修改需要通过先改变叶子节点的值,然后不断地向上递归修改祖先节点直至到达根节点,时间复杂度最高可以到达 O(nlog⁡n)O(n\log n) 的级别。但当我们引入了懒标记之后,区间更新的期望复杂度就降到了 O(log⁡n)O(\log n) 的级别且甚至会更低.

(1)首先先来从分块思想上解释如何区间修改:

分块的思想是通过将整个序列分为有穷个小块,对于要查询的一段区间,总是可以整合成 kk 个所分块与 mm 个单个元素的信息的并 (0≤k,m≤log⁡n)(0\leq k,m\leq \log n)(小小修改了一下的上面的前言 qwq )。

那么我们可以反过来思考这个问题:对于一个要修改的、长度为 ll 的区间来说,总是可以看做由一个长度为 2log⁡(⌊n⌋)2^{\log(\lfloor{n}\rfloor)} 和剩下的元素(或者小区间组成)。那么我们就可以先将其拆分成线段树上节点所示的区间,之后分开处理:

如果单个元素被包含就只改变自己,如果整个区间被包含就修改整个区间。

其实好像这个在分块里不是特别简单地实现,但是在线段树里,无论是元素还是区间都是线段树上的一个节点,所以我们不需要区分区间还是元素,加个判断就好。

(2)懒标记的正确打开方式

首先,懒标记的作用是记录每次、每个节点要更新的值,也就是 Δ\Delta。但线段树的优点不在于全记录(全记录依然很慢 qwq),而在于传递式记录:

整个区间都被操作,记录在公共祖先节点上;只修改了一部分,那么就记录在这部分的公共祖先上;如果四环以内只修改了自己的话,那就只改变自己。

After that,如果我们采用上述的优化方式的话,我们就需要在每次区间的查询修改时 push_down 一次,以免重复或者冲突或者爆炸 qwq。

那么对于 push_down 而言,其实就是纯粹的 push_up 的逆向思维(但不是逆向操作): 因为修改信息存在父节点上,所以要由父节点向下传导 lazy tag\rm lazy~tag 。

那么问题来了:怎么传导 push_down 呢?这里很有意思,开始回溯时执行 push_up,因为是向上传导信息;那我们如果要让它向下更新,就调整顺序,在向下递归的时候 push_down 不就好惹~ qwq。 对于复杂度而言,由于完全二叉树的深度不超过 log⁡n\log n,那么单点修改显然是 O(log⁡n)O(\log n) 的,区间修改的话,由于我们的这个区间至多分 log⁡n\log n 个子区间,对于每个子区间的查询是 O(1)O(1) 的,所以复杂度自然是 O(log⁡n)O(\log n)不过带一点常数。

3、那么对于区间查询

没什么好说的,由于是信息的整合,所以还是要用到分块思想,我实在是不想再码一遍了 qwq。

最后贴高清无码的标程:

C++
namespace Segtree {
	struct SegTree {
		int l, r;
		int sum, min_val, max_val, mul;
		int add_lt, mul_lt;
	} tree[4 * N + 1];
	inline void build(int l, int r, int idx, int arr[]) {
		tree[idx].l = l;
		tree[idx].r = r;
		tree[idx].add_lt = 0;
		tree[idx].mul_lt = 1;
		if (l == r) {
			tree[idx].sum = arr[l];
			tree[idx].min_val = arr[l];
			tree[idx].max_val = arr[l];
			tree[idx].mul = arr[l];
			return;
		}
		int mid = (l + r) / 2;
		build(l, mid, idx * 2, arr);
		build(mid + 1, r, idx * 2 + 1, arr);
		tree[idx].sum = (tree[idx * 2].sum + tree[idx * 2 + 1].sum) % MOD;
		tree[idx].min_val = min(tree[idx * 2].min_val, tree[idx * 2 + 1].min_val);
		tree[idx].max_val = max(tree[idx * 2].max_val, tree[idx * 2 + 1].max_val);
		tree[idx].mul = (tree[idx * 2].mul * tree[idx * 2 + 1].mul) % MOD;
	}
	inline void push_down(int idx) {
		if (tree[idx].mul_lt != 1) {
			tree[idx * 2].mul_lt = (tree[idx * 2].mul_lt * tree[idx].mul_lt) % MOD;
			tree[idx * 2].add_lt = (tree[idx * 2].add_lt * tree[idx].mul_lt) % MOD;
			tree[idx * 2].sum = (tree[idx * 2].sum * tree[idx].mul_lt) % MOD;
			tree[idx * 2].min_val = (tree[idx * 2].min_val * tree[idx].mul_lt) % MOD;
			tree[idx * 2].max_val = (tree[idx * 2].max_val * tree[idx].mul_lt) % MOD;
			tree[idx * 2].mul = (tree[idx * 2].mul * tree[idx].mul_lt) % MOD;
			tree[idx * 2 + 1].mul_lt = (tree[idx * 2 + 1].mul_lt * tree[idx].mul_lt) % MOD;
			tree[idx * 2 + 1].add_lt = (tree[idx * 2 + 1].add_lt * tree[idx].mul_lt) % MOD;
			tree[idx * 2 + 1].sum = (tree[idx * 2 + 1].sum * tree[idx].mul_lt) % MOD;
			tree[idx * 2 + 1].min_val = (tree[idx * 2 + 1].min_val * tree[idx].mul_lt) % MOD;
			tree[idx * 2 + 1].max_val = (tree[idx * 2 + 1].max_val * tree[idx].mul_lt) % MOD;
			tree[idx * 2 + 1].mul = (tree[idx * 2 + 1].mul * tree[idx].mul_lt) % MOD;
			tree[idx].mul_lt = 1;
		}
		if (tree[idx].add_lt != 0) {
			int len1 = tree[idx * 2].r - tree[idx * 2].l + 1;
			int len2 = tree[idx * 2 + 1].r - tree[idx * 2 + 1].l + 1;
			tree[idx * 2].add_lt = (tree[idx * 2].add_lt + tree[idx].add_lt) % MOD;
			tree[idx * 2].sum = (tree[idx * 2].sum + tree[idx].add_lt * len1) % MOD;
			tree[idx * 2].min_val = (tree[idx * 2].min_val + tree[idx].add_lt) % MOD;
			tree[idx * 2].max_val = (tree[idx * 2].max_val + tree[idx].add_lt) % MOD;
			tree[idx * 2 + 1].add_lt = (tree[idx * 2 + 1].add_lt + tree[idx].add_lt) % MOD;
			tree[idx * 2 + 1].sum = (tree[idx * 2 + 1].sum + tree[idx].add_lt * len2) % MOD;
			tree[idx * 2 + 1].min_val = (tree[idx * 2 + 1].min_val + tree[idx].add_lt) % MOD;
			tree[idx * 2 + 1].max_val = (tree[idx * 2 + 1].max_val + tree[idx].add_lt) % MOD;
			tree[idx].add_lt = 0;
		}
	}
	inline void updrandge_mul(int idx, int l, int r, int k) {
		if (tree[idx].l >= l && tree[idx].r <= r) {
			tree[idx].sum = (tree[idx].sum * k) % MOD;
			tree[idx].min_val = (tree[idx].min_val * k) % MOD;
			tree[idx].max_val = (tree[idx].max_val * k) % MOD;
			tree[idx].mul = (tree[idx].mul * k) % MOD;
			tree[idx].mul_lt = (tree[idx].mul_lt * k) % MOD;
			tree[idx].add_lt = (tree[idx].add_lt * k) % MOD;
			return;
		}
		push_down(idx);
		if (tree[idx * 2].r >= l) updrandge_mul(idx * 2, l, r, k);
		if (tree[idx * 2 + 1].l <= r) updrandge_mul(idx * 2 + 1, l, r, k);
		tree[idx].sum = (tree[idx * 2].sum + tree[idx * 2 + 1].sum) % MOD;
		tree[idx].min_val = min(tree[idx * 2].min_val, tree[idx * 2 + 1].min_val);
		tree[idx].max_val = max(tree[idx * 2].max_val, tree[idx * 2 + 1].max_val);
		tree[idx].mul = (tree[idx * 2].mul * tree[idx * 2 + 1].mul) % MOD;
	}
	inline void updrandge_add(int idx, int l, int r, int k) {
		if (tree[idx].l >= l && tree[idx].r <= r) {
			int len = tree[idx].r - tree[idx].l + 1;
			tree[idx].sum = (tree[idx].sum + k * len) % MOD;
			tree[idx].min_val = (tree[idx].min_val + k) % MOD;
			tree[idx].max_val = (tree[idx].max_val + k) % MOD;
			tree[idx].add_lt = (tree[idx].add_lt + k) % MOD;
			if (tree[idx].l == tree[idx].r)
				tree[idx].mul = tree[idx].sum;
			return;
		}
		push_down(idx);
		if (tree[idx * 2].r >= l) updrandge_add(idx * 2, l, r, k);
		if (tree[idx * 2 + 1].l <= r) updrandge_add(idx * 2 + 1, l, r, k);
		tree[idx].sum = (tree[idx * 2].sum + tree[idx * 2 + 1].sum) % MOD;
		tree[idx].min_val = min(tree[idx * 2].min_val, tree[idx * 2 + 1].min_val);
		tree[idx].max_val = max(tree[idx * 2].max_val, tree[idx * 2 + 1].max_val);
		tree[idx].mul = (tree[idx * 2].mul * tree[idx * 2 + 1].mul) % MOD;
	}
	int query_sum(int idx, int l, int r) {
		if (tree[idx].l >= l && tree[idx].r <= r)
			return tree[idx].sum;
		push_down(idx);
		int ans = 0;
		if (tree[idx * 2].r >= l)
			ans = (ans + query_sum(idx * 2, l, r)) % MOD;
		if (tree[idx * 2 + 1].l <= r)
			ans = (ans + query_sum(idx * 2 + 1, l, r)) % MOD;
		return ans;
	}
	int query_min(int idx, int l, int r) {
		if (tree[idx].l >= l && tree[idx].r <= r)
			return tree[idx].min_val;
		push_down(idx);
		int ans = INT_MAX;
		if (tree[idx * 2].r >= l)
			ans = min(ans, query_min(idx * 2, l, r));
		if (tree[idx * 2 + 1].l <= r)
			ans = min(ans, query_min(idx * 2 + 1, l, r));
		return ans;
	}
	int query_max(int idx, int l, int r) {
		if (tree[idx].l >= l && tree[idx].r <= r)
			return tree[idx].max_val;
		push_down(idx);
		int ans = INT_MIN;
		if (tree[idx * 2].r >= l)
			ans = max(ans, query_max(idx * 2, l, r));
		if (tree[idx * 2 + 1].l <= r)
			ans = max(ans, query_max(idx * 2 + 1, l, r));
		return ans;
	}
	int query_mul(int idx, int l, int r) {
		if (tree[idx].l >= l && tree[idx].r <= r)
			return tree[idx].mul;
		push_down(idx);
		int ans = 1;
		if (tree[idx * 2].r >= l)
			ans = (ans * query_mul(idx * 2, l, r)) % MOD;
		if (tree[idx * 2 + 1].l <= r)
			ans = (ans * query_mul(idx * 2 + 1, l, r)) % MOD;
		return ans;
	}
} using namespace Segtree;

细节和代码部分

接下来很简单qwq。我们可以使用本蒟蒻教你们的高精度模板维护本蒟蒻教你们的线段树。再将 2 个 0 插入可爱线段树qwq,然后把 0 号位置 + 第一个数 numanuma,再把 1 号位置 + 第二个数 numbnumb,此时用线段树维护区间查询即可通过。(一共 1500 行)。

接下来是完整代码qwq,Look:

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

// 简单高精度代码,使用了 FFT
namespace bigint {
	// 报错程序
	class ZeroDivisionError : public std::runtime_error {
	public:
		ZeroDivisionError() : std::runtime_error("Division by zero") {}
	};

	class FFTLimitExceededError : public std::runtime_error {
	public:
		FFTLimitExceededError() : std::runtime_error("FFT size limit exceeded") {}
	};

	class NegativeRadicandError : public std::runtime_error {
	public:
		NegativeRadicandError()
		: std::runtime_error("Negative radicand in root operation") {}
	};

	//封装高精度
	class BigInteger {
	protected:
		static constexpr int WIDTH = 8;
		static constexpr long long BASE = 1e8;
		static constexpr long long FFT_LIMIT = 32;
		static constexpr long long NEWTON_DIV_LIMIT = 64;
		static constexpr long long NEWTON_DIV_MIN_LEVEL = 16;
		static constexpr long long NEWTON_SQRT_LIMIT = 48;
		static constexpr long long NEWTON_SQRT_MIN_LEVEL = 5;
		static constexpr size_t POOL_CHUNK_SIZE = 1024;
		long long *digits;
		int capacity, size;
		bool flag;
		inline void push(const long long &);
		inline void pop();
		inline void resize(const int &);
		inline int compare(const BigInteger &) const;
		BigInteger &build_binary(const std::vector<bool> &);
		static inline BigInteger fft_mul(const BigInteger &, const BigInteger &);
		BigInteger newton_inv(int) const;
		std::pair<BigInteger, BigInteger> newton_div(const BigInteger &) const;
		BigInteger newton_invsqrt() const;
		BigInteger sqrt_normal() const;
		std::vector<long long *> memory_pool;
		inline long long *allocate_digits(size_t);
		inline void deallocate_digits(long long *, size_t);

	public:
		inline void reserve(const int &);
		inline void clear();
		~BigInteger() = default;
		int _digit_len() const;
		//读写
		friend std::ostream &operator<<(std::ostream &out, const BigInteger &x) {
			if (!x.flag)
				out << '-';
			out << x.digits[x.size];
			for (int i = x.size - 1; i >= 1; i--)
				out << std::setw(WIDTH) << std::setfill('0') << x.digits[i];
			return out;
		}

		friend std::istream &operator>>(std::istream &in, BigInteger &x) {
			std::string s;
			in >> s;
			x = s;
			return in;
		}

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

		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(const std::vector<bool> &b) : digits(nullptr) {
			*this = b;
		}

		template <class T> BigInteger(const T &begin, const T &end) : digits(nullptr) {
			*this = std::vector<bool>(begin, end);
		}

		BigInteger(const long long *in, int len) : digits(nullptr), flag(true) {
			while (len > 0 && in[len - 1] == 0)
				len--, in++;
			size = len;
			digits = allocate_digits(len + 1);
			for (int i = 0; i < len; i++)
				digits[i + 1] = in[i];
		}

		BigInteger &operator=(const BigInteger &);
		BigInteger &operator=(const long long &);
		BigInteger &operator=(const std::string &);
		BigInteger &operator=(const std::vector<bool> &);
		BigInteger &operator=(const __int128 &);
		std::string to_string() const;
		long long to_long_long() const;
		std::vector<bool> to_binary() const;
		__int128 to_int128() const;
		BigInteger operator-() const;
		BigInteger operator~() const;
		BigInteger abs() const;
		bool operator==(const BigInteger &) const;
		auto operator<=>(const BigInteger &) const;
		BigInteger div2() const;
		std::pair<BigInteger, BigInteger> divmod(const BigInteger &,
			bool = false) const;
		BigInteger operator+(const BigInteger &) 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 operator%(const long long &) const;
		BigInteger operator%(const BigInteger &) const;
		BigInteger pow(const long long &) const;
		template <typename T> BigInteger pow(const long long &, const T &) const;
		BigInteger root(const long long & = 2) const;
		BigInteger sqrt() const;
		BigInteger gcd(const BigInteger &) const;
		BigInteger lcm(const BigInteger &) const;
		inline BigInteger _move_l(int) const;
		inline BigInteger _move_r(int) const;
		BigInteger &operator+=(const BigInteger &);
		BigInteger &operator-=(const BigInteger &);
		BigInteger &operator*=(long 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<<(const long long &);
		BigInteger operator>>(const long long &);
		BigInteger &operator<<=(const long long &);
		BigInteger &operator>>=(const long long &);
		BigInteger operator&(const BigInteger &);
		BigInteger operator|(const BigInteger &);
		BigInteger operator^(const BigInteger &);
		BigInteger &operator&=(const BigInteger &);
		BigInteger &operator|=(const BigInteger &);
		BigInteger &operator^=(const BigInteger &);
		BigInteger &operator++();
		BigInteger operator++(int);
		BigInteger &operator--();
		BigInteger operator--(int);
		static BigInteger random(const int &);
	};

	inline void BigInteger::push(const long long &val) {
		if (size == capacity) {
			int new_capacity = (capacity < 256)
			? capacity * 2
			: static_cast<int>(std::pow(capacity, 1.125));
			long long *new_digits = allocate_digits(new_capacity + 1);
			std::memcpy(new_digits, digits, sizeof(long long) * (capacity + 1));
			deallocate_digits(digits, capacity + 1);
			digits = new_digits;
			capacity = new_capacity;
		}
		digits[++size] = val;
	}

	inline void BigInteger::pop() {
		digits[size--] = 0;
	}

	inline void BigInteger::resize(const int &sz) {
		reserve(sz), size = sz;
	}

	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;
	}

	BigInteger &BigInteger::build_binary(const std::vector<bool> &b) {
		*this = 0ll;
		if (b.empty() || (b.size() == 1 && b[0] == 0))
			return *this;
		BigInteger pw2 = 1;
		for (int i = b.size() - 1; i >= 0; i--, pw2 += pw2)
			if (b[i])
				* this += pw2;
		return *this;
	}

	inline long long *BigInteger::allocate_digits(size_t size) {
		for (auto it = memory_pool.begin(); it != memory_pool.end(); ++it) {
			size_t *block_size = reinterpret_cast<size_t *>(*it) - 1;
			if (*block_size >= size) {
				long long *block = *it;
				memory_pool.erase(it);
				std::memset(block, 0, size * sizeof(long long));
				return block;
			}
		}
		size_t *mem = static_cast<size_t *>(
			::operator new (sizeof(size_t) + size * sizeof(long long)));
		*mem = size;
		long long *ptr = reinterpret_cast<long long *>(mem + 1);
		std::memset(ptr, 0, size * sizeof(long long));
		return ptr;
	}

	inline void BigInteger::deallocate_digits(long long *ptr, size_t size) {
		if (!ptr)
			return;
		size_t *block_size = reinterpret_cast<size_t *>(ptr) - 1;
		if (*block_size >= POOL_CHUNK_SIZE)
			memory_pool.push_back(ptr);
		else
			::operator delete (block_size);
	}

	inline void BigInteger::reserve(const int &sz) {
		if (sz < 0)
			return;
		if (digits != nullptr)
			deallocate_digits(digits, capacity + 1);
		capacity = sz;
		size = 0;
		digits = allocate_digits(sz + 1);
	}

	inline void BigInteger::clear() {
		deallocate_digits(digits, capacity + 1);
		digits = nullptr;
		size = capacity = 0;
	}
	int BigInteger::_digit_len() const {
		return size;
	}

	BigInteger &BigInteger::operator=(const BigInteger &x) {
		if (this != &x) {
			long long *new_digits = allocate_digits(x.size + 1);
			deallocate_digits(digits, capacity + 1);
			digits = new_digits;
			capacity = x.size;
			flag = x.flag;
			size = x.size;
			std::memcpy(digits, x.digits, sizeof(long long) * (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 = 0ll;
		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::stoll(s.substr(start, len)));
		}
		while (size > 1 && digits[size] == 0)
			pop();
		return *this;
	}

	BigInteger &BigInteger::operator=(const std::vector<bool> &a) {
		if (a == std::vector<bool> {0ll})
			return *this = 0ll;
		std::vector<bool> res;
		if (a[0] == 0)
			return build_binary(a);
		res.resize(a.size());
		for (int i = 0; i < (int)a.size(); ++i)
			res[i] = !a[i];
		BigInteger x;
		x.build_binary(res);
		return *this = -x - 1;
	}

	BigInteger &BigInteger::operator=(const __int128 &x) {
		flag = (x >= 0), reserve(8);
		if (x == 0)
			return size = 1, digits[1] = 0, *this;
		__int128 n = x >= 0 ? x : -x;
		do {
			push(n % BASE), n /= BASE;
		} while (n);
		return *this;
	}

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

	long long BigInteger::to_long_long() const {
		return std::stoll(to_string());
	}

	std::vector<bool> BigInteger::to_binary() const {
		if (*this == 0)
			return {0, 0};
		std::vector<bool> res;
		if (flag) {
			for (BigInteger x = *this; x != 0; x = x.div2())
				res.emplace_back(x.digits[1] & 1);
			res.emplace_back(0);
			std::reverse(res.begin(), res.end());
			return res;
		} else {
			for (BigInteger x = -*this - 1; x != 0; x = x.div2())
				res.emplace_back(!(x.digits[1] & 1));
			res.emplace_back(1);
			std::reverse(res.begin(), res.end());
			return res;
		}
	};

	__int128 BigInteger::to_int128() const {
		__int128 res = 0;
		for (int i = size; i >= 1; i--)
			res = res * BASE + digits[i];
		return res;
	}

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

	BigInteger BigInteger::operator~() const {
		return -(*this) - 1;
	}

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

	bool BigInteger::operator==(const BigInteger &x) const {
		return compare(x) == 0;
	}

	auto BigInteger::operator<=>(const BigInteger &x) const {
		return compare(x);
	}

	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);
		long long carry = 0;
		for (int i = 1; i <= n; i++) {
			long long 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;
		long long carry = 0;
		int n = std::max(size, x.size);
		res.reserve(n);
		for (int i = 1; i <= n; i++) {
			long long 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 FastFourierTransformation {
		constexpr long long FFT_BASE = 1e4;
		constexpr double PI2 = 6.283185307179586231995927;
		constexpr double PI6 = 18.84955592153875869598778;
		constexpr int RECALC_WIDTH = 10;
		constexpr int RECALC_BASE = (1 << RECALC_WIDTH) - 1;
		struct complex {
			double real, imag;
			complex(double x = 0.0, 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 &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) {
			delete[] arr;
			arr = new (std::nothrow) complex[n + 1];
			if (!arr)
				throw std::bad_alloc();
		}

		template <const int n> inline void fft(complex *a) {
			const int n2 = n >> 1, n4 = n >> 2;
			complex w(1.0, 0.0), w3(1.0, 0.0);
			const complex wn(std::cos(PI2 / n), std::sin(PI2 / n)),
			wn3(std::cos(PI6 / n), std::sin(PI6 / n));
			for (int i = 0; i < n4; i++, w *= wn, w3 *= wn3) {
				if (!(i & RECALC_BASE))
					w = complex(std::cos(PI2 * i / n), std::sin(PI2 * i / n)), w3 = w * w * w;
				complex x = a[i] - a[i + n2], y = a[i + n4] - a[i + n2 + n4];
				y = complex(y.imag, -y.real);
				a[i] += a[i + n2], a[i + n4] += a[i + n2 + n4];
				a[i + n2] = (x - y) * w, a[i + n2 + n4] = (x + y) * w3;
			}
			fft<n2>(a), fft<n4>(a + n2), fft<n4>(a + n2 + n4);
		}
		template <> inline void fft<0>(complex *) {

		}

		template <> inline void fft<1>(complex *) {

		}

		template <> inline void fft<2>(complex *a) {
			complex x = a[0], y = a[1];
			a[0] += y, a[1] = x - y;
		}

		template <> inline void fft<4>(complex *a) {
			complex a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3];
			complex x = a0 - a2, y = a1 - a3;
			y = complex(y.imag, -y.real);
			a[0] += a2, a[1] += a3, a[2] = x - y, a[3] = x + y;
			fft<2>(a);
		}

		template <const int n> inline void ifft(complex *a) {
			const int n2 = n >> 1, n4 = n >> 2;
			ifft<n2>(a), ifft<n4>(a + n2), ifft<n4>(a + n2 + n4);
			complex w(1.0, 0.0), w3(1.0, 0.0);
			const complex wn(std::cos(PI2 / n), -std::sin(PI2 / n)),
			wn3(std::cos(PI6 / n), -std::sin(PI6 / n));
			for (int i = 0; i < n4; i++, w *= wn, w3 *= wn3) {
				if (!(i & RECALC_BASE))
					w = complex(std::cos(PI2 * i / n), -std::sin(PI2 * i / n)),
					w3 = w * w * w;
				complex p = w * a[i + n2], q = w3 * a[i + n2 + n4];
				complex x = a[i], y = p + q, x1 = a[i + n4], y1 = p - q;
				y1 = complex(y1.imag, -y1.real);
				a[i] += y, a[i + n4] += y1, a[i + n2] = x - y, a[i + n2 + n4] = x1 - y1;
			}
		}

		template <> inline void ifft<0>(complex *) {

		}

		template <> inline void ifft<1>(complex *) {

		}

		template <> inline void ifft<2>(complex *a) {
			complex x = a[0], y = a[1];
			a[0] += y, a[1] = x - y;
		}

		template <> inline void ifft<4>(complex *a) {
			ifft<2>(a);
			complex p = a[2], q = a[3];
			complex x = a[0], y = p + q, x1 = a[1], y1 = p - q;
			y1 = complex(y1.imag, -y1.real);
			a[0] += y, a[1] += y1, a[2] = x - y, a[3] = x1 - y1;
		}

		inline void dft(complex *a, int n) {
			if (n <= 1)
				return;
			switch (n) {
			case 1 << 2:
				fft < 1 << 2 > (a);
				break;
			case 1 << 3:
				fft < 1 << 3 > (a);
				break;
			case 1 << 4:
				fft < 1 << 4 > (a);
				break;
			case 1 << 5:
				fft < 1 << 5 > (a);
				break;
			case 1 << 6:
				fft < 1 << 6 > (a);
				break;
			case 1 << 7:
				fft < 1 << 7 > (a);
				break;
			case 1 << 8:
				fft < 1 << 8 > (a);
				break;
			case 1 << 9:
				fft < 1 << 9 > (a);
				break;
			case 1 << 10:
				fft < 1 << 10 > (a);
				break;
			case 1 << 11:
				fft < 1 << 11 > (a);
				break;
			case 1 << 12:
				fft < 1 << 12 > (a);
				break;
			case 1 << 13:
				fft < 1 << 13 > (a);
				break;
			case 1 << 14:
				fft < 1 << 14 > (a);
				break;
			case 1 << 15:
				fft < 1 << 15 > (a);
				break;
			case 1 << 16:
				fft < 1 << 16 > (a);
				break;
			case 1 << 17:
				fft < 1 << 17 > (a);
				break;
			case 1 << 18:
				fft < 1 << 18 > (a);
				break;
			case 1 << 19:
				fft < 1 << 19 > (a);
				break;
			case 1 << 20:
				fft < 1 << 20 > (a);
				break;
			case 1 << 21:
				fft < 1 << 21 > (a);
				break;
			case 1 << 22:
				fft < 1 << 22 > (a);
				break;
			case 1 << 23:
				fft < 1 << 23 > (a);
				break;
			case 1 << 24:
				fft < 1 << 24 > (a);
				break;
			case 1 << 25:
				fft < 1 << 25 > (a);
				break;
			case 1 << 26:
				fft < 1 << 26 > (a);
				break;
			case 1 << 27:
				fft < 1 << 27 > (a);
				break;
			case 1 << 28:
				fft < 1 << 28 > (a);
				break;
				throw FFTLimitExceededError();
			}
		}

		inline void idft(complex *a, int n) {
			if (n <= 1)
				return;
			switch (n) {
			case 1 << 2:
				ifft < 1 << 2 > (a);
				break;
			case 1 << 3:
				ifft < 1 << 3 > (a);
				break;
			case 1 << 4:
				ifft < 1 << 4 > (a);
				break;
			case 1 << 5:
				ifft < 1 << 5 > (a);
				break;
			case 1 << 6:
				ifft < 1 << 6 > (a);
				break;
			case 1 << 7:
				ifft < 1 << 7 > (a);
				break;
			case 1 << 8:
				ifft < 1 << 8 > (a);
				break;
			case 1 << 9:
				ifft < 1 << 9 > (a);
				break;
			case 1 << 10:
				ifft < 1 << 10 > (a);
				break;
			case 1 << 11:
				ifft < 1 << 11 > (a);
				break;
			case 1 << 12:
				ifft < 1 << 12 > (a);
				break;
			case 1 << 13:
				ifft < 1 << 13 > (a);
				break;
			case 1 << 14:
				ifft < 1 << 14 > (a);
				break;
			case 1 << 15:
				ifft < 1 << 15 > (a);
				break;
			case 1 << 16:
				ifft < 1 << 16 > (a);
				break;
			case 1 << 17:
				ifft < 1 << 17 > (a);
				break;
			case 1 << 18:
				ifft < 1 << 18 > (a);
				break;
			case 1 << 19:
				ifft < 1 << 19 > (a);
				break;
			case 1 << 20:
				ifft < 1 << 20 > (a);
				break;
			case 1 << 21:
				ifft < 1 << 21 > (a);
				break;
			case 1 << 22:
				ifft < 1 << 22 > (a);
				break;
			case 1 << 23:
				ifft < 1 << 23 > (a);
				break;
			case 1 << 24:
				ifft < 1 << 24 > (a);
				break;
			case 1 << 25:
				ifft < 1 << 25 > (a);
				break;
			case 1 << 26:
				ifft < 1 << 26 > (a);
				break;
			case 1 << 27:
				ifft < 1 << 27 > (a);
				break;
			case 1 << 28:
				ifft < 1 << 28 > (a);
				break;
				throw FFTLimitExceededError();
			}
		}
	}


	BigInteger BigInteger::fft_mul(const BigInteger &a, const BigInteger &b) {
		int least = (a.size + b.size) << 1, lim = 1 << std::__lg(least);
		if (lim < least)
			lim <<= 1;
		FastFourierTransformation::init(lim);
		for (int i = 0; i < a.size; i++) {
			FastFourierTransformation::arr[i << 1].real =
			a.digits[i + 1] % FastFourierTransformation::FFT_BASE;
			FastFourierTransformation::arr[i << 1 | 1].real =
			a.digits[i + 1] / FastFourierTransformation::FFT_BASE %
			FastFourierTransformation::FFT_BASE;
		}
		for (int i = 0; i < b.size; i++) {
			FastFourierTransformation::arr[i << 1].imag =
			b.digits[i + 1] % FastFourierTransformation::FFT_BASE;
			FastFourierTransformation::arr[i << 1 | 1].imag =
			b.digits[i + 1] / FastFourierTransformation::FFT_BASE %
			FastFourierTransformation::FFT_BASE;
		}
		dft(FastFourierTransformation::arr, lim);
		for (int i = 0; i < lim; i++)
			FastFourierTransformation::arr[i] *= FastFourierTransformation::arr[i];
		idft(FastFourierTransformation::arr, lim);
		BigInteger res;
		res.resize(a.size + b.size + 1);
		long long carry = 0;
		double inv = 0.5 / lim;
		for (int i = 0; i <= a.size + b.size; i++) {
			carry +=
			(long long)(FastFourierTransformation::arr[i << 1].imag * inv + 0.5);
			carry += (long long)(FastFourierTransformation::arr[i << 1 | 1].imag * inv +
				0.5) *
			FastFourierTransformation::FFT_BASE;
			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;
		long long lim = 1LL * n * m;
		if (lim >= 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 long x) {
		if (x == 0 || *this == 0)
			return *this = 0ll;
		if (x < 0)
			flag = !flag, x = -x;
		long long carry = 0;
		for (int i = 1; i <= size || carry; i++) {
			if (i > size)
				push(0);
			long long 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 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 ZeroDivisionError();
		if (*this == 0)
			return 0;
		if (x == 2)
			return div2();
		if (x == -2)
			return -div2();
		BigInteger res;
		res.flag = !(flag ^ (x >= 0));
		long long 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;
	}

	inline BigInteger BigInteger::_move_r(int d) const {
		if (*this == 0 || d >= size)
			return 0;
		if (d == 0)
			return *this;
		BigInteger res;
		res.reserve(size - d + 1);
		for (int i = d + 1; i <= size; i++)
			res.push(digits[i]);
		return res;
	}

	inline BigInteger BigInteger::_move_l(int d) const {
		if (*this == 0)
			return 0;
		if (d == 0)
			return *this;
		BigInteger res;
		res.reserve(size + d + 1);
		for (int i = 1; i <= d; i++)
			res.push(0);
		for (int i = 1; i <= size; i++)
			res.push(digits[i]);
		return res;
	}

	BigInteger BigInteger::newton_inv(int n) const {
		if (*this == 0)
			throw ZeroDivisionError();
		if (std::min(size, n - size) <= NEWTON_DIV_MIN_LEVEL) {
			BigInteger a;
			a.resize(n + 1);
			std::memset(a.digits, 0, sizeof(long long) * a.size);
			a.digits[n + 1] = 1;
			return a.divmod(*this, true).first;
		}
		int k = (n - size + 2) >> 1, k2 = k > size ? 0 : size - k;
		BigInteger x = _move_r(k2);
		int n2 = k + x.size;
		BigInteger y = x.newton_inv(n2), a = y + y, b = (*this) * y * y;
		return a._move_l(n - n2 - k2) - b._move_r(2 * (n2 + k2) - n) - 1;
	}

	std::pair<BigInteger, BigInteger> BigInteger::newton_div(const BigInteger &x) const {
		int k = size - x.size + 2, k2 = k > x.size ? 0 : x.size - k;
		BigInteger x2 = x._move_r(k2);
		if (k2 != 0)
			x2 += 1;
		int n2 = k + x2.size;
		BigInteger u = (*this) * x2.newton_inv(n2);
		BigInteger q = u._move_r(n2 + k2), r = (*this) - q * x;
		while (r >= x)
			q += 1, r -= x;
		return {q, r};
	}

	std::pair<BigInteger, BigInteger> BigInteger::divmod(const BigInteger &x,
		bool dis_newton) const {
			static const int base = BigInteger::BASE;
			BigInteger a = abs(), b = x.abs();
			if (b == 0)
				throw ZeroDivisionError();
			if (a < b)
				return {0, flag ? a : -a};
			if (!dis_newton && size > NEWTON_DIV_LIMIT)
				return newton_div(x);
			int t = base / (x.digits[x.size] + 1);
			a *= t, b *= t;
			int n = a.size, m = b.size;
			BigInteger q = 0, r = 0;
			q.resize(n);
			for (int i = n; i >= 1; i--) {
				r *= base, r += a.digits[i];
				long long d1 = m < r.size ? r.digits[m + 1] : 0,
				d2 = m - 1 < r.size ? r.digits[m] : 0;
				int 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 {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 || x == 4 || x == 5)
			return digits[1] % x;
		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 = res * a;
			a = a * a;
		}
		return res;
	}

	template <typename T>
	BigInteger BigInteger::pow(const long long &x, const T &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::root(const long long &m) const {
		if (!flag && m % 2 == 0)
			throw NegativeRadicandError();
		if (m == 1 || *this == 0)
			return *this;
		if (m == 2)
			return sqrt();
		static constexpr long long base = BigInteger::BASE;
		if (size <= m) {
			long long l = 0, r = base - 1;
			while (l < r) {
				long long mid = (l + r + 1) >> 1;
				if (BigInteger(mid).pow(m) <= *this)
					l = mid;
				else
					r = mid - 1;
			}
			return l;
		}
		if (size <= m * 2) {
			BigInteger res;
			res.resize(2);
			long long l = 0, r = base - 1;
			while (l < r) {
				long long mid = (l + r + 1) >> 1;
				res.digits[2] = mid;
				if (res.pow(m) <= *this)
					l = mid;
				else
					r = mid - 1;
			}
			res.digits[2] = l, l = 0, r = base - 1;
			while (l < r) {
				long long mid = (l + r + 1) >> 1;
				res.digits[1] = mid;
				if (res.pow(m) <= *this)
					l = mid;
				else
					r = mid - 1;
			}
			return res.digits[1] = l, res;
		}
		int n = size, t = n / m / 2;
		BigInteger s = (_move_r(t * m).root(m) + 1)._move_l(t);
		BigInteger res = (s * (m - 1) + *this / s.pow(m - 1)) / m;
		long long l = std::max<long long>(res.digits[1] - 100, 0),
		r = std::min(res.digits[1] + 100, base - 1);
		while (l < r) {
			long long mid = (l + r + 1) >> 1;
			res.digits[1] = mid;
			if (res.pow(m) <= *this)
				l = mid;
			else
				r = mid - 1;
		}
		return res.digits[1] = l, res;
	}

	BigInteger BigInteger::newton_invsqrt() const {
		const BigInteger &x = *this;
		static constexpr long long base = BigInteger::BASE;
		if (x.size <= 2) {
			if (x.size == 0)
				return 0;
			uint64_t t = 0;
			if (x.size == 1)
				t = x.digits[1];
			else
				t = x.digits[1] + x.digits[2] * BASE;
			return uint64_t(base * base / std::sqrt(t));
		}
		if (x.size <= NEWTON_SQRT_MIN_LEVEL) {
			BigInteger b1 = BigInteger(base).pow(x.size / 2);
			return b1 / x.sqrt_normal();
		}
		int n2 = x.size % 2 == 0 ? x.size : x.size + 1, k2 = (n2 + 2) / 4 * 2;
		BigInteger x2k(x.digits + 1 + (n2 - k2), k2 + x.size - n2);
		BigInteger s = x2k.newton_invsqrt()._move_l((n2 - k2) / 2);
		BigInteger x2 = (s * 3).div2() - (s * s * s * x).div2()._move_r(2 * n2);
		BigInteger rx = BigInteger(1)._move_l(2 * n2) - x * x2 * x2, delta = 1;
		if (!rx.flag) {
			for (; !rx.flag; delta *= 2) {
				BigInteger t = (x2 * 2 - delta + delta * delta) * x;
				x2 -= delta, rx += t;
			}
		} else {
			while (true) {
				BigInteger t = (x2 * 2 + delta) * delta * x;
				if (t > rx)
					break;
				x2 += delta, rx -= t;
				delta *= 2;
			}
		}
		for (; delta > 0; delta = delta.div2()) {
			BigInteger t = (x2 * 2 + delta) * delta * x;
			if (t <= rx)
				x2 += delta, rx -= t;
		}
		return x2;
	}

	BigInteger BigInteger::sqrt_normal() const {
		static constexpr long long base = BigInteger::BASE;
		BigInteger n = *this, x0 = BigInteger(base)._move_l((n.size + 2) >> 1);
		BigInteger x = (x0 + n / x0).div2();
		while (x < x0)
			std::swap(x, x0), x = (x0 + n / x0).div2();
		return x0;
	}

	BigInteger BigInteger::sqrt() const {
		if (!flag)
			throw NegativeRadicandError();
		if (*this <= 1)
			return *this;
		if (size <= NEWTON_SQRT_LIMIT)
			return sqrt_normal();
		const BigInteger &x = *this;
		int n2 = x.size % 2 == 0 ? x.size : x.size + 1;
		BigInteger res = (x * x.newton_invsqrt())._move_r(n2);
		BigInteger r = x - res * res, delta = 1;
		while (true) {
			BigInteger dr = (res * 2 + delta) * delta;
			if (dr > r)
				break;
			r -= dr, res += delta, delta *= 2;
		}
		for (; delta > 0; delta = delta.div2()) {
			BigInteger dr = (res * 2 + delta) * delta;
			if (dr <= r)
				r -= dr, res += delta;
		}
		return res;
	}

	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<<(const long long &x) {
		return *this * BigInteger(2).pow(x);
	}

	BigInteger BigInteger::operator>>(const long long &x) {
		return *this / BigInteger(2).pow(x);
	}

	BigInteger &BigInteger::operator<<=(const long long &x) {
		return *this = *this << x;
	}

	BigInteger &BigInteger::operator>>=(const long long &x) {
		return *this = *this >> x;
	}

	BigInteger BigInteger::operator&(const BigInteger &x) {
		std::vector<bool> a = to_binary(), b = x.to_binary();
		int n = a.size(), m = b.size(), lim = std::max(n, m);
		std::vector<bool> res(lim), temp(lim);
		if (m == lim) {
			a.resize(lim);
			for (int i = 0; i < n; ++i)
				temp[lim - n + i] = a[i];
			a = temp;
		} else {
			b.resize(lim);
			for (int i = 0; i < m; ++i)
				temp[lim - m + i] = b[i];
			b = temp;
		}
		for (int i = 0; i < lim; ++i)
			res[i] = a[i] & b[i];
		return res;
	}

	BigInteger BigInteger::operator|(const BigInteger &x) {
		std::vector<bool> a = to_binary(), b = x.to_binary();
		int n = a.size(), m = b.size(), lim = std::max(n, m);
		std::vector<bool> res(lim), temp(lim);
		if (m == lim) {
			a.resize(lim);
			for (int i = 0; i < n; ++i)
				temp[lim - n + i] = a[i];
			a = temp;
		} else {
			b.resize(lim);
			for (int i = 0; i < m; ++i)
				temp[lim - m + i] = b[i];
			b = temp;
		}
		for (int i = 0; i < lim; ++i)
			res[i] = a[i] | b[i];
		return res;
	}

	BigInteger BigInteger::operator^(const BigInteger &x) {
		std::vector<bool> a = to_binary(), b = x.to_binary();
		int n = a.size(), m = b.size(), lim = std::max(n, m);
		std::vector<bool> res(lim), temp(lim);
		if (m == lim) {
			a.resize(lim);
			for (int i = 0; i < n; ++i)
				temp[lim - n + i] = a[i];
			a = temp;
		} else {
			b.resize(lim);
			for (int i = 0; i < m; ++i)
				temp[lim - m + i] = b[i];
			b = temp;
		}
		for (int i = 0; i < lim; ++i)
			res[i] = a[i] ^ b[i];
		return res;
	}

	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++() {
		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;
	}

	BigInteger BigInteger::random(const int &num_digits) {
		std::random_device rd;
		std::mt19937 gen(rd());
		BigInteger res;
		res.reserve(num_digits / 8 + 8);
		res.size = 0;
		std::uniform_int_distribution<long long> last_digits(
			1, std::pow(10, (num_digits - 1) % 8 + 1) - 1);
		if (num_digits % 8)
			res.push(last_digits(gen));
		std::uniform_int_distribution<long long> other_digits(0, 99999999);
		for (int i = 1; i <= num_digits / 8; ++i)
			res.push(other_digits(gen));
		return res;
	}
}
using namespace bigint;

const int N = 1e2 + 5;// 范围

// 线段树的代码
namespace Segtree {
	struct SegNode {
		int l, r;
		BigInteger sum, min_val, max_val, mul;
		BigInteger add_lt, mul_lt; // 加法和乘法懒标记
	};
	SegNode tree[4 * N + 1]; // 线段树节点数组
	//记住一定要 * 4 否则你会后悔的

	// 初始化线段树节点
	inline void init_node(int idx) {
		tree[idx].mul_lt = BigInteger(1); // 乘法懒标记初始化为1
		tree[idx].add_lt = BigInteger(0); // 加法懒标记初始化为0
	}

	// 建树函数,接收BigInteger数组
	inline void build(int l, int r, int idx, const vector<BigInteger>& arr) {
		tree[idx].l = l;
		tree[idx].r = r;
		init_node(idx); // 初始化懒标记

		if (l == r) {
			tree[idx].sum = tree[idx].mul = tree[idx].min_val = tree[idx].max_val = arr[l];
			return;
		}

		int mid = (l + r) / 2;
		build(l, mid, 2 * idx, arr);
		build(mid + 1, r, 2 * idx + 1, arr);

		// 合并子节点信息
		tree[idx].sum = tree[2 * idx].sum + tree[2 * idx + 1].sum;
		tree[idx].min_val = min(tree[2 * idx].min_val, tree[2 * idx + 1].min_val);
		tree[idx].max_val = max(tree[2 * idx].max_val, tree[2 * idx + 1].max_val);
		tree[idx].mul = tree[2 * idx].mul * tree[2 * idx + 1].mul;
	}

	// 下传懒标记
	inline void push_down(int idx) {
		if (tree[idx].l == tree[idx].r)
			return; // 叶子节点无需下传

		// 处理乘法懒标记
		if (tree[idx].mul_lt != 1) {
			SegNode& left = tree[2 * idx];
			SegNode& right = tree[2 * idx + 1];

			// 子节点乘法懒标记更新
			left.mul_lt = left.mul_lt * tree[idx].mul_lt;
			right.mul_lt = right.mul_lt * tree[idx].mul_lt;
			// 子节点加法懒标记更新(加法标记随乘法放大)
			left.add_lt = left.add_lt * tree[idx].mul_lt;
			right.add_lt = right.add_lt * tree[idx].mul_lt;
			// 子节点数据更新
			left.sum = left.sum * tree[idx].mul_lt;
			left.min_val = left.min_val * tree[idx].mul_lt;
			left.max_val = left.max_val * tree[idx].mul_lt;
			left.mul = left.mul * tree[idx].mul_lt;

			right.sum = right.sum * tree[idx].mul_lt;
			right.min_val = right.min_val * tree[idx].mul_lt;
			right.max_val = right.max_val * tree[idx].mul_lt;
			right.mul = right.mul * tree[idx].mul_lt;

			tree[idx].mul_lt = BigInteger(1); // 清除当前节点乘法标记
		}

		// 处理加法懒标记
		if (tree[idx].add_lt != 0) {
			SegNode& left = tree[2 * idx];
			SegNode& right = tree[2 * idx + 1];
			int len_left = left.r - left.l + 1;
			int len_right = right.r - right.l + 1;

			// 子节点加法懒标记累加
			left.add_lt = left.add_lt + tree[idx].add_lt;
			right.add_lt = right.add_lt + tree[idx].add_lt;
			// 子节点数据更新
			left.sum = left.sum + tree[idx].add_lt * len_left;
			left.min_val = left.min_val + tree[idx].add_lt;
			left.max_val = left.max_val + tree[idx].add_lt;

			right.sum = right.sum + tree[idx].add_lt * len_right;
			right.min_val = right.min_val + tree[idx].add_lt;
			right.max_val = right.max_val + tree[idx].add_lt;

			tree[idx].add_lt = BigInteger(0); // 清除当前节点加法标记
		}
	}

	// 区间乘法更新
	inline void range_mul(int idx, int l, int r, const BigInteger& k) {
		if (tree[idx].r < l || tree[idx].l > r)
			return;
		if (l <= tree[idx].l && tree[idx].r <= r) {
			// 直接更新当前节点和懒标记
			tree[idx].sum = tree[idx].sum * k;
			tree[idx].min_val = tree[idx].min_val * k;
			tree[idx].max_val = tree[idx].max_val * k;
			tree[idx].mul = tree[idx].mul * k;
			tree[idx].mul_lt = tree[idx].mul_lt * k;
			tree[idx].add_lt = tree[idx].add_lt * k; // 加法标记随乘法缩放
			return;
		}
		push_down(idx); // 先下传懒标记
		range_mul(2 * idx, l, r, k);
		range_mul(2 * idx + 1, l, r, k);
		// 合并子节点信息
		tree[idx].sum = tree[2 * idx].sum + tree[2 * idx + 1].sum;
		tree[idx].min_val = min(tree[2 * idx].min_val, tree[2 * idx + 1].min_val);
		tree[idx].max_val = max(tree[2 * idx].max_val, tree[2 * idx + 1].max_val);
		tree[idx].mul = tree[2 * idx].mul * tree[2 * idx + 1].mul;
	}

	// 区间加法更新
	inline void range_add(int idx, int l, int r, const BigInteger& k) {
		if (tree[idx].r < l || tree[idx].l > r)
			return;
		if (l <= tree[idx].l && tree[idx].r <= r) {
			// 直接更新当前节点和懒标记
			int len = tree[idx].r - tree[idx].l + 1;
			tree[idx].sum = tree[idx].sum + k * len;
			tree[idx].min_val = tree[idx].min_val + k;
			tree[idx].max_val = tree[idx].max_val + k;
			tree[idx].add_lt = tree[idx].add_lt + k;
			return;
		}
		push_down(idx); // 先下传懒标记
		range_add(2 * idx, l, r, k);
		range_add(2 * idx + 1, l, r, k);
		// 合并子节点信息
		tree[idx].sum = tree[2 * idx].sum + tree[2 * idx + 1].sum;
		tree[idx].min_val = min(tree[2 * idx].min_val, tree[2 * idx + 1].min_val);
		tree[idx].max_val = max(tree[2 * idx].max_val, tree[2 * idx + 1].max_val);
		tree[idx].mul = tree[2 * idx].mul * tree[2 * idx + 1].mul;
	}

	// 区间和查询
	BigInteger query_sum(int idx, int l, int r) {
		if (tree[idx].r < l || tree[idx].l > r)
			return 0;
		if (l <= tree[idx].l && tree[idx].r <= r)
			return tree[idx].sum;
		push_down(idx); // 下传懒标记确保数据正确
		return query_sum(2 * idx, l, r) + query_sum(2 * idx + 1, l, r);
	}

	// 区间最小值查询
	BigInteger query_min(int idx, int l, int r) {
		if (tree[idx].r < l || tree[idx].l > r)
			return BigInteger(INT_MAX); // 初始化为极大值
		if (l <= tree[idx].l && tree[idx].r <= r)
			return tree[idx].min_val;
		push_down(idx);
		return min(query_min(2 * idx, l, r), query_min(2 * idx + 1, l, r));
	}

	// 区间最大值查询
	BigInteger query_max(int idx, int l, int r) {
		if (tree[idx].r < l || tree[idx].l > r)
			return BigInteger(INT_MIN); // 初始化为极小值
		if (l <= tree[idx].l && tree[idx].r <= r)
			return tree[idx].max_val;
		push_down(idx);
		return max(query_max(2 * idx, l, r), query_max(2 * idx + 1, l, r));
	}

	// 区间乘积查询
	BigInteger query_mul(int idx, int l, int r) {
		if (tree[idx].r < l || tree[idx].l > r)
			return 1; // 乘法单位元
		if (l <= tree[idx].l && tree[idx].r <= r)
			return tree[idx].mul;
		push_down(idx);
		return query_mul(2 * idx, l, r) * query_mul(2 * idx + 1, l, r);
	}
} using namespace Segtree;

vector <BigInteger> a(2);
// 记录初始线段树的值

signed main() {
	build(0, 1, 1, a);
	BigInteger numa, numb;
	cin >> numa >> numb;
	range_add(1, 0, 0, numa);
	range_add(1, 1, 1, numb);
	BigInteger ans = query_sum(1, 0, 1);
	// 求答案
	cout << ans;
}

最后

没想到写了这么多字符,快点个赞吧qwq(最好点个关注,有粉福)。

原文