/*
BigInteger.h (Version 2.6 Compressed)
stripe-python https://www.luogu.com.cn/user/928879
*/
#ifndef BIGINTEGER_H
#define BIGINTEGER_H
#include <cmath>
#include <bitset>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <algorithm>
class ZeroDivisionError : public std::exception {
public:
const char* what() const throw() {return "BigInteger::divmod";}
};
class FFTLimitExceededError : public std::exception {
public:
const char* what() const throw() {return "BigInteger::fft_mul";}
};
class BigInteger {
protected:
using digit_t = long long;
static constexpr int WIDTH = 8;
static constexpr digit_t BASE = 1e8;
static constexpr long long NEWTON_LIMIT = 128;
static constexpr long long NEWTON_MIN_LEVEL = 16;
digit_t* digits;
int capacity, size;
bool flag;
inline void push(const digit_t&);
inline void pop();
inline int compare(const BigInteger&) const;
BigInteger newton_inv(int) const;
inline std::pair<BigInteger, BigInteger> newton_div(const BigInteger&) const;
public:
inline void reserve(const int&);
protected:
inline void resize(const int&);
public:
BigInteger() : digits(nullptr), flag(true) {*this = 0;}
BigInteger(const BigInteger& x) : digits(nullptr) {*this = x;}
BigInteger(const long long& x) : digits(nullptr) {*this = x;}
BigInteger(const std::string& s) : digits(nullptr) {*this = s;}
BigInteger& operator= (const BigInteger&);
BigInteger& operator= (const long long&);
BigInteger& operator= (const std::string&);
BigInteger& operator= (const std::vector<bool>&);
void clear();
~BigInteger() {clear();}
friend std::ostream& operator<< (std::ostream& out, const BigInteger& x) {
if (!x.flag) out << '-';
out << (long long) x.digits[x.size];
for (int i = x.size - 1; i >= 1; i--)
out << std::setw(WIDTH) << std::setfill('0') << (long long) x.digits[i];
return out;
}
friend std::istream& operator>> (std::istream& in, BigInteger& x) {
std::string s; in >> s; x = s;
return in;
}
std::string to_string() const;
long long to_long_long() const;
BigInteger operator- () const;
BigInteger abs() const;
bool operator== (const BigInteger&) const;
#if __cplusplus >= 202002L
auto operator<=> (const BigInteger&) const;
#else
bool operator< (const BigInteger&) const;
bool operator> (const BigInteger&) const;
bool operator!= (const BigInteger&) const;
bool operator<= (const BigInteger&) const;
bool operator>= (const BigInteger&) const;
#endif //__cplusplus >= 202002L
BigInteger div2() const;
std::pair<BigInteger, BigInteger> divmod(const BigInteger&, bool = false) const;
BigInteger operator+ (const BigInteger&) const;
BigInteger operator- (const BigInteger&) const;
BigInteger operator* (const int&) const;
BigInteger operator* (const BigInteger&) const;
BigInteger operator/ (const long long&) const;
BigInteger operator/ (const BigInteger&) const;
BigInteger operator% (const long long&) const;
BigInteger operator% (const BigInteger&) const;
BigInteger pow(const long long&) const;
BigInteger sqrt() const;
inline BigInteger _move_l(int) const;
inline BigInteger _move_r(int) const;
BigInteger& operator+= (const BigInteger&);
BigInteger& operator-= (const BigInteger&);
BigInteger& operator*= (int);
BigInteger& operator*= (const BigInteger&);
BigInteger& operator/= (const long long&);
BigInteger& operator/= (const BigInteger&);
BigInteger& operator%= (const long long&);
BigInteger& operator%= (const BigInteger&);
};
inline void BigInteger::push(const digit_t& val) {
if (size == capacity) {
int new_capacity = 0;
if (capacity < 1000) new_capacity = capacity << 1;
else new_capacity = (capacity >> 1) * 3;
if (new_capacity < 0) new_capacity = INT_MAX;
digit_t* new_digits = new digit_t[new_capacity + 1];
std::memcpy(new_digits, digits, sizeof(long long) * (capacity + 1));
delete[] digits;
digits = new_digits, capacity = new_capacity;
}
digits[++size] = val;
}
inline void BigInteger::pop() {digits[size--] = 0;}
inline int BigInteger::compare(const BigInteger& x) const {
if (flag && !x.flag) return 1;
if (!flag && x.flag) return -1;
int sgn = (flag && x.flag ? 1 : -1);
if (size > x.size) return sgn;
if (size < x.size) return -sgn;
for (int i = size; i >= 1; i--) {
if (digits[i] > x.digits[i]) return sgn;
if (digits[i] < x.digits[i]) return -sgn;
}
return 0;
}
inline void BigInteger::reserve(const int& sz) {
if (sz < 0) return;
if (digits != nullptr) delete[] digits;
capacity = sz, size = 0;
digits = new digit_t[sz + 1];
std::memset(digits, 0, sizeof(digit_t) * (sz + 1));
}
inline void BigInteger::resize(const int& sz) {reserve(sz), size = sz;}
BigInteger& BigInteger::operator= (const BigInteger& x) {
reserve(x.size + 1);
flag = x.flag, size = x.size;
std::memcpy(digits, x.digits, sizeof(digit_t) * (x.size + 1));
return *this;
}
BigInteger& BigInteger::operator= (const long long& x) {
flag = (x >= 0), reserve(4);
if (x == 0) return size = 1, digits[1] = 0, *this;
if (x == LLONG_MIN) return *this = "-9223372036854775808";
long long n = std::abs(x);
do {push(n % BASE), n /= BASE;} while (n);
return *this;
}
BigInteger& BigInteger::operator= (const std::string& s) {
flag = true, reserve(s.size() / WIDTH + 1);
if (s.empty() || s == "-") return *this = 0;
int i = 0; if (s[0] == '-') flag = false, i++;
for (int j = s.size() - 1; j >= i; j -= WIDTH) {
int start = std::max(i, j - WIDTH + 1), len = j - start + 1;
push(std::stoll(s.substr(start, len)));
}
while (size > 1 && digits[size] == 0) pop();
return *this;
}
void BigInteger::clear() {if (digits != nullptr) delete[] digits, digits = nullptr;}
std::string BigInteger::to_string() const {std::stringstream ss; ss << *this; return ss.str();}
long long BigInteger::to_long_long() const {return std::stoll(to_string());}
BigInteger BigInteger::operator- () const {
if (*this == 0) return 0;
BigInteger res = *this; res.flag = !flag; return res;
}
BigInteger BigInteger::abs() const {BigInteger res = *this; res.flag = true; return res;}
bool BigInteger::operator== (const BigInteger& x) const {return compare(x) == 0;}
#if __cplusplus >= 202002L
auto BigInteger::operator<=> (const BigInteger& x) const {return compare(x);}
#else
bool BigInteger::operator< (const BigInteger& x) const {return compare(x) < 0;}
bool BigInteger::operator> (const BigInteger& x) const {return compare(x) > 0;}
bool BigInteger::operator!= (const BigInteger& x) const {return compare(x) != 0;}
bool BigInteger::operator<= (const BigInteger& x) const {return compare(x) <= 0;}
bool BigInteger::operator>= (const BigInteger& x) const {return compare(x) >= 0;}
#endif //__cplusplus >= 202002L
BigInteger BigInteger::operator+ (const BigInteger& x) const {
if (!x.flag) return *this - x.abs();
if (!flag) return x - abs();
BigInteger res;
res.flag = !(flag ^ x.flag);
int n = std::max(size, x.size) + 1;
res.reserve(n);
digit_t carry = 0;
for (int i = 1; i <= n; i++) {
digit_t d1 = i <= size ? digits[i] : 0, d2 = i <= x.size ? x.digits[i] : 0;
res.push(d1 + d2 + carry);
carry = res.digits[i] / BASE;
res.digits[i] %= BASE;
}
while (res.size > 1 && res.digits[res.size] == 0) res.pop();
return res;
}
BigInteger BigInteger::operator- (const BigInteger& x) const {
if (!x.flag) return *this + x.abs();
if (!flag) return -(abs() + x);
BigInteger res;
if (*this < x) res.flag = false;
digit_t carry = 0;
int n = std::max(size, x.size);
res.reserve(n);
for (int i = 1; i <= n; i++) {
digit_t d1 = i <= size ? digits[i] : 0, d2 = i <= x.size ? x.digits[i] : 0;
if (res.flag) res.push(d1 - d2 - carry);
else res.push(d2 - d1 - carry);
if (res.digits[i] < 0) res.digits[i] += BASE, carry = 1;
else carry = 0;
}
while (res.size > 1 && res.digits[res.size] == 0) res.pop();
return res;
}
BigInteger BigInteger::operator* (const BigInteger& x) const {
BigInteger zero = 0;
if (*this == zero || x == zero) return zero;
int n = size, m = x.size;
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*= (int x) {
if (x == 0 || *this == 0) return *this = 0;
if (x < 0) flag = !flag, x = -x;
digit_t carry = 0;
for (int i = 1; i <= size || carry; i++) {
if (i > size) push(0);
digit_t cur = digits[i] * x + carry;
carry = cur / BigInteger::BASE;
digits[i] = cur % BigInteger::BASE;
}
while (size > 1 && digits[size] == 0) pop();
return *this;
}
BigInteger BigInteger::operator* (const int& x) const {BigInteger t = *this; return t *= x;}
BigInteger BigInteger::div2() const {
BigInteger res = *this;
for (int i = size; i >= 1; i--) {
if ((res.digits[i] & 1) && (i > 1)) res.digits[i - 1] += BASE;
res.digits[i] >>= 1;
}
while (res.size > 1 && res.digits[res.size] == 0) res.pop();
return res;
}
BigInteger BigInteger::operator/ (const long long& x) const {
if (x == 0) throw -1;
if (*this == 0) return 0;
if (x == 2) return div2();
if (x == -2) {BigInteger res = div2(); res.flag = !res.flag; return res;}
BigInteger res;
res.flag = !(flag ^ (x >= 0));
digit_t cur = 0, div = std::abs(x);
res.resize(size);
for (int i = size; i >= 1; i--) {
cur = cur * BASE + digits[i];
res.digits[i] = res.flag ? (cur / div) : (-cur / -div);
cur %= div;
}
while (res.size > 1 && res.digits[res.size] == 0) res.pop();
return res;
}
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_MIN_LEVEL) {
BigInteger a; a.resize(n + 1);
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 std::make_pair(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 std::make_pair(0, flag ? a : -a);
if (!dis_newton && size > NEWTON_LIMIT) return newton_div(x);
//cerr << a << ' ' << b << endl;
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];
digit_t d1 = m < r.size ? r.digits[m + 1] : 0, d2 = m - 1 < r.size ? r.digits[m] : 0;
digit_t d = (d1 * base + d2) / b.digits[m];
r -= b * d;
while (!r.flag) r += b, d--;
q.digits[i] = d;
}
q.flag = !(flag ^ x.flag), r.flag = flag;
while (q.size > 1 && q.digits[q.size] == 0) q.pop();
return std::make_pair(q, r / t);
}
BigInteger BigInteger::operator/ (const BigInteger& x) const {return divmod(x).first;}
BigInteger BigInteger::operator% (const long long& x) const {
if (x == 2) return digits[1] & 1;
if (x == 5) return digits[1] % 5;
return *this - (*this / x * x);
}
BigInteger BigInteger::operator% (const BigInteger& x) const {return divmod(x).second;}
BigInteger BigInteger::sqrt() const {
if (*this <= 1) return *this;
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::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;}
#endif //BIGINTEGER_H
/*
BigDecimal.h (Version 1.0)
Based on BigInteger.h Version 2.6
stripe-python https://www.luogu.com.cn/user/928879
*/
#ifndef BIGDECIMAL_H
#define BIGDECIMAL_H
#include "BigInteger.h"
#include <functional>
class NaNError : public std::exception {
public:
const char* what() const throw() {return "Not a number";}
};
enum __round_types {
ROUND_CEIL, ROUND_FLOOR, ROUND_05, ROUND_04
};
int __global_precision = -65;
int __global_round_type = ROUND_04;
struct __Context {
void precision(int p) {__global_precision = p - 1;}
void round_type(__round_types t) {__global_round_type = t;}
} context;
class BigDecimal {
private:
BigInteger val;
int e; // *this = val * 10^e
static BigInteger move_l(const BigInteger& num, int x) {
std::string s = num.to_string();
for (int i = 1; i <= x; i++) s += '0';
return BigInteger(s);
}
static BigInteger move_r(const BigInteger& num, int x) {
std::string s = num.to_string();
for (int i = 1; i <= x && !s.empty(); i++) s.pop_back();
return BigInteger(s);
}
void trim() {
const int p = __global_precision;
if (e < p) val = move_r(val, p - e);
else if (e > p) val = move_l(val, e - p);
e = p;
}
int len() const {return val.to_string().size();}
BigDecimal(const BigInteger& v, int _e) : val(v), e(_e) {trim();}
std::string to_string_base(std::function<std::string(std::string&)>) const;
std::string round_045(int = INT_MAX, bool = true) const;
inline int compare(const BigDecimal&) const;
public:
BigDecimal() : val(0), e(0) {}
BigDecimal(const BigInteger& x) {*this = x;}
BigDecimal(const BigDecimal& x) {*this = x;}
BigDecimal(const long double& x) {*this = x;}
BigDecimal(const long double& x, int precision) {
std::stringstream ss;
ss << std::setprecision(precision) << x;
*this = ss.str();
}
BigDecimal(const std::string& s) {*this = s;}
BigDecimal& operator= (const BigInteger&);
BigDecimal& operator= (const BigDecimal&);
BigDecimal& operator= (const long double&);
BigDecimal& operator= (const std::string&);
int precision() const {return e;}
void reset() {trim();}
std::string to_string(int = INT_MAX) const;
std::string floor(int = INT_MAX) const;
std::string ceil(int = INT_MAX) const;
std::string round_04(int = INT_MAX) const;
std::string round_05(int = INT_MAX) const;
BigInteger to_bigint() const;
friend std::ostream& operator<< (std::ostream& out, const BigDecimal& x) {return out << x.to_string();}
friend std::istream& operator>> (std::istream& in, BigDecimal& x) {std::string s; in >> s; x = s; return in;}
bool operator== (const BigDecimal&) const;
#if __cplusplus >= 202002L
auto operator<=> (const BigDecimal&) const;
#else
bool operator< (const BigDecimal&) const;
bool operator> (const BigDecimal&) const;
bool operator!= (const BigDecimal&) const;
bool operator<= (const BigDecimal&) const;
bool operator>= (const BigDecimal&) const;
#endif //__cplusplus >= 202002L
BigDecimal operator+ (const BigDecimal&) const;
BigDecimal operator- (const BigDecimal&) const;
BigDecimal operator* (const BigDecimal&) const;
BigDecimal operator/ (const BigDecimal&) const;
BigDecimal operator% (const BigDecimal&) const;
BigDecimal& operator+= (const BigDecimal&);
BigDecimal& operator-= (const BigDecimal&);
BigDecimal& operator*= (const BigDecimal&);
BigDecimal& operator/= (const BigDecimal&);
BigDecimal& operator%= (const BigDecimal&);
bool is_zero() const;
BigDecimal abs() const;
BigDecimal operator- () const;
BigDecimal exp() const;
BigDecimal ln() const;
BigDecimal sqrt() const;
BigDecimal pow(const BigDecimal&) const;
BigDecimal acos() const;
BigDecimal asin() const;
BigDecimal atan() const;
BigDecimal atan2(const BigDecimal&) const;
BigDecimal cos() const;
BigDecimal sin() const;
BigDecimal tan() const;
};
const BigDecimal b_pi = (std::string) "3.1415926535897932384626433832795028841971693993751058209749445923";
const BigDecimal b_ln2 = (std::string) "0.693147180559945309417232121458176568075500134360255254120680009";
const BigDecimal b_sqrt2 = (std::string) "1.4142135623730950488016887242096980785696718753769480731766797";
const BigDecimal b_one = 1;
BigDecimal& BigDecimal::operator= (const BigInteger& x) {
e = 0, val = x, trim();
return *this;
}
BigDecimal& BigDecimal::operator= (const BigDecimal& x) {
e = x.e, val = x.val, trim();
return *this;
}
BigDecimal& BigDecimal::operator= (const long double& x) {
std::stringstream ss; ss << std::setprecision(24) << x;
return *this = ss.str();
}
BigDecimal& BigDecimal::operator= (const std::string& s) {
e = 0, val = 0, trim();
if (s.empty()) return *this;
int n = s.size(), pos = 0, flag = 1;
while (pos < n && s[pos] == '-') flag = !flag, pos++;
std::string a, b;
while (pos < n && std::isdigit(s[pos])) a.push_back(s[pos]), pos++;
if (s[pos] != '.') {
val = a, e = 0;
if (!flag) val = -val;
return trim(), *this;
}
for (pos++; pos < n && std::isdigit(s[pos]); pos++) b.push_back(s[pos]);
e = -static_cast<long long>(b.size()), val = a + b;
if (!flag) val = -val;
return trim(), *this;
}
std::string BigDecimal::to_string_base(std::function<std::string(std::string&)> solve) const {
bool neg = false;
std::string res = val.to_string();
if (val < 0) res = res.substr(1), neg = true;
auto add_neg = [&](const std::string& s) -> std::string {
return neg ? ('-' + s) : s;
};
if (e == 0) return add_neg(solve(res));
if (e > 0) {
for (int i = 1; i <= e; i++) res += '0';
return add_neg(solve(res));
}
int n = res.size(), point = n + e;
if (point >= 0) {
res.insert(res.begin() + point, '.');
if (res[0] == '.') res = '0' + res;
if ((int) res.size() >= 2 && res[0] == '-' && res[1] == '.') res.insert(res.begin() + 1, '0');
return add_neg(solve(res));
}
std::string pre = "0.";
for (int i = 1; i <= -point; i++) pre += '0';
pre += res;
return add_neg(solve(pre));
}
std::string BigDecimal::to_string(int precision) const {
switch (__global_round_type) {
case ROUND_CEIL:
return ceil(precision);
case ROUND_FLOOR:
return floor(precision);
case ROUND_04:
return round_04(precision);
case ROUND_05:
return round_05(precision);
}
return "";
}
std::string BigDecimal::floor(int precision) const {
int p = precision != INT_MAX ? -precision + 1 : -__global_precision;
return to_string_base([&](std::string& s) -> std::string {
int pos = s.find('.');
if (pos == (int) std::string::npos) s += ".0", pos = s.find('.');
int n = s.size(), sep = n - pos;
if (sep == p) return s;
if (sep > p) {
for (int i = 1; i <= sep - p; i++) s.pop_back();
if (s.back() == '.') s.pop_back();
return s;
}
for (int i = 1; i <= p - sep; i++) s += '0';
return s;
});
}
std::string BigDecimal::ceil(int precision) const {
int p = precision != INT_MAX ? -precision + 2 : -__global_precision + 1;
return to_string_base([&](std::string& s) -> std::string {
int pos = s.find('.');
if (pos == (int) std::string::npos) s += ".0", pos = s.find('.');
int n = s.size(), sep = n - pos;
if (sep > p) {
for (int i = 1; i <= sep - p; i++) s.pop_back();
} else if (sep < p) {
for (int i = 1; i <= p - sep; i++) s += '0';
}
int i = s.size() - 1;
for (i--; s[i] == '.' || s[i] == '9'; i--) {
if (s[i] == '.') continue;
s[i] = '0';
if (i == 0) {
s.insert(s.begin(), '0');
break;
}
}
s[i]++, s.pop_back();
if (s.back() == '.') s.pop_back();
return s;
});
}
std::string BigDecimal::round_045(int precision, bool round04) const {
int p = precision != INT_MAX ? -precision + 2 : -__global_precision + 1;
return to_string_base([&](std::string& s) -> std::string {
int pos = s.find('.');
if (pos == (int) std::string::npos) s += ".0", pos = s.find('.');
int n = s.size(), sep = n - pos;
if (sep > p) {
for (int i = 1; i <= sep - p; i++) s.pop_back();
} else if (sep < p) {
for (int i = 1; i <= p - sep; i++) s += '0';
}
int i = s.size() - 1;
bool flag = round04 ? (s[i] >= '5') : (s[i] > '5');
if (flag) {
for (i--; s[i] == '.' || s[i] == '9'; i--) {
if (s[i] == '.') continue;
s[i] = '0';
if (i == 0) {
s.insert(s.begin(), '0');
break;
}
}
s[i]++;
}
s.pop_back();
if (s.back() == '.') s.pop_back();
return s;
});
}
std::string BigDecimal::round_04(int precision) const {return round_045(precision, true);}
std::string BigDecimal::round_05(int precision) const {return round_045(precision, false);}
BigInteger BigDecimal::to_bigint() const {
BigInteger res = val;
if (e < 0) res = move_r(res, -e);
else if (e > 0) res = move_l(res, e);
return res;
}
BigDecimal& BigDecimal::operator+= (const BigDecimal& x) {
return trim(), val += x.val, *this;
}
BigDecimal& BigDecimal::operator-= (const BigDecimal& x) {
return trim(), val -= x.val, *this;
}
BigDecimal& BigDecimal::operator*= (const BigDecimal& x) {
return trim(), val *= x.val, e += x.e, trim(), *this;
}
BigDecimal& BigDecimal::operator/= (const BigDecimal& x) {
if (x.is_zero()) throw NaNError();
int n = x.len();
trim(), val = move_l(val, n), e -= n;
return val /= x.val, e -= x.e, trim(), *this;
}
BigDecimal& BigDecimal::operator%= (const BigDecimal& x) {
if (x.is_zero()) throw NaNError();
return *this -= x * (*this / x).to_bigint();
}
BigDecimal BigDecimal::operator+ (const BigDecimal& x) const {
return BigDecimal(val + x.val, e);
}
BigDecimal BigDecimal::operator- (const BigDecimal& x) const {
return BigDecimal(val - x.val, e);
}
BigDecimal BigDecimal::operator* (const BigDecimal& x) const {
return BigDecimal(val * x.val, e + x.e);
}
BigDecimal BigDecimal::operator/ (const BigDecimal& x) const {
if (x.is_zero()) throw NaNError();
BigDecimal res = *this; res /= x; return res;
}
BigDecimal BigDecimal::operator% (const BigDecimal& x) const {
if (x.is_zero()) throw NaNError();
return *this - x * (*this / x).to_bigint();
}
bool BigDecimal::is_zero() const {return val == 0;}
BigDecimal BigDecimal::abs() const {
BigDecimal res = *this;
res.val = res.val.abs();
return res;
}
BigDecimal BigDecimal::operator- () const {
BigDecimal res = *this;
res.val = -res.val;
return res;
}
int BigDecimal::compare(const BigDecimal& y) const {
if (is_zero() && y.is_zero()) return 0;
if (val < 0 && y.val > 0) return -1;
if (val > 0 && y.val < 0) return 1;
int sgn = 1;
if (val < 0 && y.val < 0) sgn = -1;
if (e != y.e) {
if (e < y.e) return -sgn;
return sgn;
}
if (val < y.val) return -sgn;
if (val > y.val) return sgn;
return 0;
}
bool BigDecimal::operator== (const BigDecimal& x) const {return compare(x) == 0;}
#if __cplusplus >= 202002L
auto BigDecimal::operator<=> (const BigDecimal& x) const {return compare(x);}
#else
bool BigDecimal::operator< (const BigDecimal& x) const {return compare(x) < 0;}
bool BigDecimal::operator> (const BigDecimal& x) const {return compare(x) > 0;}
bool BigDecimal::operator!= (const BigDecimal& x) const {return compare(x) != 0;}
bool BigDecimal::operator<= (const BigDecimal& x) const {return compare(x) <= 0;}
bool BigDecimal::operator>= (const BigDecimal& x) const {return compare(x) >= 0;}
#endif //__cplusplus >= 202002L
BigDecimal BigDecimal::exp() const {
__global_precision -= 2;
BigDecimal a = 1, res = 0, h = *this;
int k = 0;
for (BigInteger fac = 1; ; k++, fac *= k) {
BigDecimal b = a / fac;
if (b.is_zero()) break;
res += b, a *= h;
}
__global_precision += 2, res.reset();
return res;
}
BigDecimal BigDecimal::ln() const {
if (*this < 1) return -((b_one / *this).ln());
// by tiger2005
__global_precision -= 2;
BigDecimal coef = 0, x = *this, ln2 = b_ln2, sqrt2 = b_sqrt2;
while (x > sqrt2) {
x.val = x.val.div2();
coef += ln2;
}
x = (x - 1) / (x + 1);
BigDecimal cnt = x * 2, res = cnt + coef;
x *= x;
for (int i = 3; ; i += 2) {
cnt *= x;
BigDecimal b = cnt / i;
if (b.is_zero()) break;
res += b;
}
__global_precision += 2, res.reset();
return res;
}
BigDecimal BigDecimal::pow(const BigDecimal& x) const {return (ln() * x).exp();}
BigDecimal BigDecimal::sqrt() const {
BigDecimal x = *this;
int n = x.len();
x.val = move_l(x.val, n), x.e -= n;
if (x.e & 1) x.val = move_r(x.val, 1), x.e++;
x.val = x.val.sqrt(), x.e /= 2, x.trim();
return x;
}
BigDecimal BigDecimal::asin() const {
BigDecimal x = *this;
return (x / (b_one - x * x).sqrt()).atan();
}
BigDecimal BigDecimal::acos() const {
return b_pi / 2 - asin();
}
BigDecimal BigDecimal::atan() const {
if (val < 0) return -(abs().atan());
BigDecimal x = *this;
if (x > 1) return b_pi / 2 - (b_one / x).atan();
if (x >= 0.1) return (x / ((x * x + 1).sqrt() + 1)).atan() * 2;
__global_precision -= 2, x = *this;
BigDecimal sgn = x, res = x;
x = -x * x;
for (int i = 3; ; i += 2) {
sgn *= x;
BigDecimal b = sgn / i;
if (b.is_zero()) break;
res += b;
}
__global_precision += 2, res.reset();
return res;
}
BigDecimal BigDecimal::atan2(const BigDecimal& x) const {
BigDecimal y = *this;
if (x > 0) return (y / x).atan();
if (y >= 0 && x < 0) return (y / x).atan() + b_pi;
if (y < 0 && x < 0) return (y / x).atan() - b_pi;
if (y > 0) return b_pi / 2;
if (y < 0) return -b_pi / 2;
throw NaNError();
}
BigDecimal BigDecimal::sin() const {
if (*this < 0) return -(abs().sin());
__global_precision -= 2;
BigDecimal x = *this, y = b_pi * 2;
x = (x % y + y) % y; BigDecimal val = x;
BigDecimal res = x, cur = x;
x *= x;
for (int i = 3; ; i += 2) {
cur = cur * x / (1LL * i * (i - 1));
if (cur.is_zero()) break;
cur = -cur, res += cur;
}
__global_precision += 2, res.reset();
return res;
}
BigDecimal BigDecimal::cos() const {
if (*this < 0) return abs().cos();
__global_precision -= 2;
BigDecimal x = *this, y = b_pi * 2;
x = (x % y + y) % y; BigDecimal val = x;
BigDecimal res = 1, cur = 1;
x *= x;
for (int i = 2; ; i += 2) {
cur = cur * x / (1LL * i * (i - 1));
if (cur.is_zero()) break;
cur = -cur, res += cur;
}
__global_precision += 2, res.reset();
return res;
}
BigDecimal BigDecimal::tan() const {return sin() / cos();}
BigDecimal b_exp(const BigDecimal& x) {return x.exp();}
BigDecimal b_ln(const BigDecimal& x) {return x.ln();}
BigDecimal b_sqrt(const BigDecimal& x) {return x.sqrt();}
BigDecimal b_pow(const BigDecimal& x, const BigDecimal& y) {return x.pow(y);}
BigDecimal b_abs(const BigDecimal& x) {return x.abs();}
BigDecimal b_sin(const BigDecimal& x) {return x.sin();}
BigDecimal b_cos(const BigDecimal& x) {return x.cos();}
BigDecimal b_tan(const BigDecimal& x) {return x.tan();}
BigDecimal b_asin(const BigDecimal& x) {return x.asin();}
BigDecimal b_acos(const BigDecimal& x) {return x.acos();}
BigDecimal b_atan(const BigDecimal& x) {return x.atan();}
BigDecimal b_atan2(const BigDecimal& y, const BigDecimal& x) {return y.atan2(x);}
#endif //BIGDECIMAL_H
#include <bits/stdc++.h>
using namespace std;
int p;
string opt;
BigDecimal x, y;
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
while (cin >> opt) {
cin >> x;
if (opt == "add") cin >> y >> p, cout << (x + y).round_04(-p);
else if (opt == "sub") cin >> y >> p, cout << (x - y).round_04(-p);
else if (opt == "mul") cin >> y >> p, cout << (x * y).round_04(-p);
else if (opt == "div") cin >> y >> p, cout << (x / y).round_04(-p);
else if (opt == "pow") cin >> y >> p, cout << b_pow(x, y).round_04(-p);
else if (opt == "atan2") cin >> y >> p, cout << b_atan2(x, y).round_04(-p);
else if (opt == "exp") cin >> p, cout << b_exp(x).round_04(-p);
else if (opt == "ln") cin >> p, cout << b_ln(x).round_04(-p);
else if (opt == "sqrt") cin >> p, cout << b_sqrt(x).round_04(-p);
else if (opt == "sin") cin >> p, cout << b_sin(x).round_04(-p);
else if (opt == "cos") cin >> p, cout << b_cos(x).round_04(-p);
else if (opt == "tan") cin >> p, cout << b_tan(x).round_04(-p);
else if (opt == "asin") cin >> p, cout << b_asin(x).round_04(-p);
else if (opt == "acos") cin >> p, cout << b_acos(x).round_04(-p);
else if (opt == "atan") cin >> p, cout << b_atan(x).round_04(-p);
else break;
cout << '\n';
}
return 0;
}剪贴板
剪贴板 axjf3eg1
剪贴板信息
剪贴板信息
- 收录
- 更新