论422行的AC代码
查看原帖
论422行的AC代码
1592928
lee_liang楼主2025/6/16 21:18

闲的没事拿高精模板干了一个,建议升橙

代码:(仅供观赏)

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

class BigInt {
private:
	vector<int> digits;
	bool isNegative;
	
	void removeLeadingZeros() {
		while (digits.size() > 1 && digits.back() == 0) 
			digits.pop_back();
		if (digits.size() == 1 && digits[0] == 0) 
			isNegative = false;
	}
	
	void add(const BigInt& other) {
		int carry = 0;
		for (size_t i = 0; i < max(digits.size(), other.digits.size()) || carry; ++i) {
			if (i == digits.size()) 
				digits.push_back(0);
			digits[i] += carry + (i < other.digits.size() ? other.digits[i] : 0);
			carry = digits[i] >= 10;
			if (carry) 
				digits[i] -= 10;
		}
	}
	
	void subtract(const BigInt& other) {
		int carry = 0;
		for (size_t i = 0; i < other.digits.size() || carry; ++i) {
			digits[i] -= carry + (i < other.digits.size() ? other.digits[i] : 0);
			carry = digits[i] < 0;
			if (carry) 
				digits[i] += 10;
		}
		removeLeadingZeros();
	}
	
	static bool lessAbs(const BigInt& a, const BigInt& b) {
		if (a.digits.size() != b.digits.size()) 
			return a.digits.size() < b.digits.size();
		for (int i = a.digits.size() - 1; i >= 0; --i) 
			if (a.digits[i] != b.digits[i]) 
				return a.digits[i] < b.digits[i];
		return false;
	}
	
public:
	BigInt() : isNegative(false) {
		digits.push_back(0);
	}
	
	BigInt(long long num) : isNegative(num < 0) {
		if (num == 0) {
			digits.push_back(0);
			return;
		}
		num = std::abs(num);
		while (num > 0) {
			digits.push_back(num % 10);
			num /= 10;
		}
	}
	
	BigInt(const string& s) : isNegative(false) {
		if (s.empty()) {
			digits.push_back(0);
			return;
		}
		size_t start = 0;
		if (s[0] == '-') {
			isNegative = true;
			start = 1;
		}
		for (int i = s.length() - 1; i >= static_cast<int>(start); --i) {
			if (!isdigit(s[i])) {
				throw invalid_argument("Invalid character in BigInt string");
			}
			digits.push_back(s[i] - '0');
		}
		removeLeadingZeros();
	}
	
	BigInt(const BigInt& other) : digits(other.digits), isNegative(other.isNegative) {}
	
	BigInt& operator=(const BigInt& other) {
		if (this != &other) {
			digits = other.digits;
			isNegative = other.isNegative;
		}
		return *this;
	}
	
	BigInt& operator=(long long num) {
		*this = BigInt(num);
		return *this;
	}
	
	BigInt& operator=(const string& s) {
		*this = BigInt(s);
		return *this;
	}
	
	BigInt operator-() const {
		BigInt result(*this);
		result.isNegative = !result.isNegative;
		return result;
	}
	
	BigInt operator+() const {
		return *this;
	}
	
	BigInt operator+(const BigInt& other) const {
		if (isNegative == other.isNegative) {
			BigInt result(*this);
			result.add(other);
			result.isNegative = isNegative;
			return result;
		} else {
			if (lessAbs(*this, other)) {
				BigInt result(other);
				result.subtract(*this);
				if (result.digits.size() == 1 && result.digits[0] == 0) {
					result.isNegative = false;
				} else {
					result.isNegative = other.isNegative;
				}
				return result;
			} else {
				BigInt result(*this);
				result.subtract(other);
				if (result.digits.size() == 1 && result.digits[0] == 0) {
					result.isNegative = false;
				} else {
					result.isNegative = isNegative;
				}
				return result;
			}
		}
	}
	
	BigInt operator-(const BigInt& other) const {
		return *this + (-other);
	}
	
	BigInt operator*(const BigInt& other) const {
		BigInt result;
		result.digits.resize(digits.size() + other.digits.size());
		for (size_t i = 0; i < digits.size(); ++i) {
			int carry = 0;
			for (size_t j = 0; j < other.digits.size() || carry; ++j) {
				long long current = result.digits[i + j] +
				digits[i] * (j < other.digits.size() ? other.digits[j] : 0) +
				carry;
				result.digits[i + j] = current % 10;
				carry = current / 10;
			}
		}
		result.isNegative = isNegative != other.isNegative;
		result.removeLeadingZeros();
		return result;
	}
	
	BigInt operator/(const BigInt& other) const {
		if (other == 0) {
			throw runtime_error("Division by zero");
		}
		BigInt absOther = other.isNegative ? -other : other;
		BigInt current;
		BigInt result;
		result.digits.resize(digits.size());
		for (int i = digits.size() - 1; i >= 0; --i) {
			current.digits.insert(current.digits.begin(), digits[i]);
			current.removeLeadingZeros();
			int quotient = 0;
			while (!lessAbs(current, absOther)) {
				current.subtract(absOther);
				++quotient;
			}
			result.digits[i] = quotient;
		}
		result.isNegative = isNegative != other.isNegative;
		result.removeLeadingZeros();
		return result;
	}
	
	BigInt operator%(const BigInt& other) const {
		BigInt quotient = *this / other;
		return *this - quotient * other;
	}
	
	BigInt operator+(long long num) const {
		return *this + BigInt(num);
	}
	
	BigInt operator-(long long num) const {
		return *this - BigInt(num);
	}
	
	BigInt operator*(long long num) const {
		return *this * BigInt(num);
	}
	
	BigInt operator/(long long num) const {
		return *this / BigInt(num);
	}
	
	BigInt operator%(long long num) const {
		return *this % BigInt(num);
	}
	
	BigInt& operator+=(const BigInt& other) {
		*this = *this + other;
		return *this;
	}
	
	BigInt& operator-=(const BigInt& other) {
		*this = *this - other;
		return *this;
	}
	
	BigInt& operator*=(const BigInt& other) {
		*this = *this * other;
		return *this;
	}
	
	BigInt& operator/=(const BigInt& other) {
		*this = *this / other;
		return *this;
	}
	
	BigInt& operator%=(const BigInt& other) {
		*this = *this % other;
		return *this;
	}
	
	BigInt& operator+=(long long num) {
		*this = *this + num;
		return *this;
	}
	
	BigInt& operator-=(long long num) {
		*this = *this - num;
		return *this;
	}
	
	BigInt& operator*=(long long num) {
		*this = *this * num;
		return *this;
	}
	
	BigInt& operator/=(long long num) {
		*this = *this / num;
		return *this;
	}
	
	BigInt& operator%=(long long num) {
		*this = *this % num;
		return *this;
	}
	BigInt& operator++() {
		*this += 1;
		return *this;
	}
	BigInt operator++(int) {
		BigInt temp = *this;
		*this += 1;
		return temp;
	}
	BigInt& operator--() {
		*this -= 1;
		return *this;
	}
	BigInt operator--(int) {
		BigInt temp = *this;
		*this -= 1;
		return temp;
	}
	
	bool operator==(const BigInt& other) const {
		return isNegative == other.isNegative && digits == other.digits;
	}
	
	bool operator!=(const BigInt& other) const {
		return !(*this == other);
	}
	
	bool operator<(const BigInt& other) const {
		if (isNegative != other.isNegative) {
			return isNegative;
		}
		if (isNegative) {
			return lessAbs(other, *this);
		}
		return lessAbs(*this, other);
	}
	
	bool operator<=(const BigInt& other) const {
		return *this < other || *this == other;
	}
	
	bool operator>(const BigInt& other) const {
		return !(*this <= other);
	}
	
	bool operator>=(const BigInt& other) const {
		return !(*this < other);
	}
	
	bool operator==(long long num) const {
		return *this == BigInt(num);
	}
	
	bool operator!=(long long num) const {
		return *this != BigInt(num);
	}
	
	bool operator<(long long num) const {
		return *this < BigInt(num);
	}
	
	bool operator<=(long long num) const {
		return *this <= BigInt(num);
	}
	
	bool operator>(long long num) const {
		return *this > BigInt(num);
	}
	
	bool operator>=(long long num) const {
		return *this >= BigInt(num);
	}
	
	friend ostream& operator<<(ostream& os, const BigInt& num) {
		if (num.isNegative) {
			os << '-';
		}
		for (auto it = num.digits.rbegin(); it != num.digits.rend(); ++it) {
			os << *it;
		}
		return os;
	}
	
	friend istream& operator>>(istream& is, BigInt& num) {
		string s;
		is >> s;
		num = BigInt(s);
		return is;
	}
	
	explicit operator bool() const {
		return !(digits.size() == 1 && digits[0] == 0);
	}
	
	BigInt abs() const {
		BigInt result(*this);
		result.isNegative = false;
		return result;
	}
	
	string toString() const {
		string s;
		if (isNegative) {
			s += '-';
		}
		for (auto it = digits.rbegin(); it != digits.rend(); ++it) {
			s += to_string(*it);
		}
		return s;
	}
};
BigInt operator+(long long num, const BigInt& bigNum) {
	return bigNum + num;
}

BigInt operator-(long long num, const BigInt& bigNum) {
	return BigInt(num) - bigNum;
}

BigInt operator*(long long num, const BigInt& bigNum) {
	return bigNum * num;
}

BigInt operator/(long long num, const BigInt& bigNum) {
	return BigInt(num) / bigNum;
}

BigInt operator%(long long num, const BigInt& bigNum) {
	return BigInt(num) % bigNum;
}
bool operator==(long long num, const BigInt& bigNum) {
	return bigNum == num;
}

bool operator!=(long long num, const BigInt& bigNum) {
	return !(num == bigNum);
}

bool operator<(long long num, const BigInt& bigNum) {
	return BigInt(num) < bigNum;
}

bool operator<=(long long num, const BigInt& bigNum) {
	return BigInt(num) <= bigNum;
}

bool operator>(long long num, const BigInt& bigNum) {
	return BigInt(num) > bigNum;
}

bool operator>=(long long num, const BigInt& bigNum) {
	return BigInt(num) >= bigNum;
}
int main() {
	BigInt a,b,c;
	cin>>a>>b;
	c=a+b;
	cout<<c;
	return 0;
}
2025/6/16 21:18
加载中...