emmmmmm
https://www.luogu.com/article/nnn0rfvd
题解机
cpret.online
我的MC存档
↑点击这里得到我的mc存档
《诈骗专家》
我的团队
ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็ด้้้้้็้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็็็็้้้้้็็็็็้้้้้้็็
高精度:
struct BigInt {
std::string num;
bool isNegative;
// 构造函数
BigInt(const std::string& s = "0") {
if (s[0] == '-') {
isNegative = true;
num = s.substr(1);
} else {
isNegative = false;
num = s;
}
// 去除前导零
while (num.length() > 1 && num[0] == '0') {
num.erase(0, 1);
}
if (num == "0") {
isNegative = false;
}
}
// 重载输入运算符
friend std::istream& operator>>(std::istream& is, BigInt& bi) {
std::string s;
is >> s;
bi = BigInt(s);
return is;
}
// 重载输出运算符
friend std::ostream& operator<<(std::ostream& os, const BigInt& bi) {
if (bi.isNegative && bi.num != "0") {
os << '-';
}
os << bi.num;
return os;
}
// 比较绝对值大小
bool absGreater(const BigInt& other) const {
if (num.length() != other.num.length()) {
return num.length() > other.num.length();
}
return num > other.num;
}
// 重载比较运算符
bool operator>(const BigInt& other) const {
if (isNegative != other.isNegative) {
return !isNegative;
}
if (isNegative) {
return absGreater(other) ? false : true;
}
return absGreater(other);
}
bool operator<(const BigInt& other) const {
return other > *this;
}
bool operator>=(const BigInt& other) const {
return !(*this < other);
}
bool operator<=(const BigInt& other) const {
return !(*this > other);
}
bool operator==(const BigInt& other) const {
return isNegative == other.isNegative && num == other.num;
}
bool operator!=(const BigInt& other) const {
return !(*this == other);
}
// 高精度加法
BigInt operator+(const BigInt& other) const {
if (isNegative == other.isNegative) {
BigInt result;
result.isNegative = isNegative;
std::string a = num, b = other.num;
std::reverse(a.begin(), a.end());
std::reverse(b.begin(), b.end());
int carry = 0;
for (int i = 0; i < std::max(a.length(), b.length()) || carry; ++i) {
if (i < a.length()) carry += a[i] - '0';
if (i < b.length()) carry += b[i] - '0';
result.num.push_back(carry % 10 + '0');
carry /= 10;
}
std::reverse(result.num.begin(), result.num.end());
return result;
}
if (isNegative) {
return other - BigInt(num);
}
return *this - BigInt(other.num);
}
// 高精度减法
BigInt operator-(const BigInt& other) const {
if (isNegative == other.isNegative) {
if (absGreater(other)) {
BigInt result;
result.isNegative = isNegative;
std::string a = num, b = other.num;
std::reverse(a.begin(), a.end());
std::reverse(b.begin(), b.end());
int borrow = 0;
for (int i = 0; i < a.length(); ++i) {
int x = a[i] - '0' - borrow;
if (i < b.length()) x -= b[i] - '0';
if (x < 0) {
x += 10;
borrow = 1;
} else {
borrow = 0;
}
result.num.push_back(x + '0');
}
while (result.num.length() > 1 && result.num.back() == '0') {
result.num.pop_back();
}
std::reverse(result.num.begin(), result.num.end());
return result;
}
BigInt result = other - *this;
result.isNegative = !isNegative;
return result;
}
if (isNegative) {
BigInt result = BigInt(num) + other;
result.isNegative = true;
return result;
}
return *this + BigInt(other.num);
}
// 高精度乘法
BigInt operator*(const BigInt& other) const {
BigInt result("0");
for (int i = other.num.length() - 1; i >= 0; --i) {
BigInt temp;
int carry = 0;
for (int j = 0; j < other.num.length() - 1 - i; ++j) {
temp.num.push_back('0');
}
for (int j = num.length() - 1; j >= 0; --j) {
int mul = (num[j] - '0') * (other.num[i] - '0') + carry;
temp.num.push_back(mul % 10 + '0');
carry = mul / 10;
}
if (carry) {
temp.num.push_back(carry + '0');
}
std::reverse(temp.num.begin(), temp.num.end());
result = result + temp;
}
result.isNegative = isNegative != other.isNegative;
return result;
}
// 高精度除法
BigInt operator/(const BigInt& other) const {
if (other == BigInt("0")) {
throw std::runtime_error("Division by zero");
}
BigInt dividend = BigInt(num);
BigInt divisor = BigInt(other.num);
BigInt quotient("0");
BigInt remainder("0");
for (int i = 0; i < dividend.num.length(); ++i) {
remainder = remainder * BigInt("10") + BigInt(std::string(1, dividend.num[i]));
int count = 0;
while (remainder >= divisor) {
remainder = remainder - divisor;
++count;
}
quotient.num.push_back(count + '0');
}
// 去除前导零
while (quotient.num.length() > 1 && quotient.num[0] == '0') {
quotient.num.erase(0, 1);
}
quotient.isNegative = isNegative != other.isNegative;
return quotient;
}
};



