// This is free and unencumbered software released into the public domain. // Anyone is free to copy, modify, publish, use, compile, sell, or // distribute this software, either in source code form or as a compiled // binary, for any purpose, commercial or non-commercial, and by any // means. // In jurisdictions that recognize copyright laws, the author or authors // of this software dedicate any and all copyright interest in the // software to the public domain. We make this dedication for the benefit // of the public at large and to the detriment of our heirs and // successors. We intend this dedication to be an overt act of // relinquishment in perpetuity of all present and future rights to this // software under copyright law. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // For more information, please refer to /****************/ /* template.hpp */ /****************/ #include #include #include #include #include #include using std::cerr; using std::cout; using std::endl; using std::max; using std::min; using std::swap; struct BoolName : std::numpunct { std::string t, f; BoolName(std::string t, std::string f) : t(t), f(f) {} std::string do_truename() const { return t; } std::string do_falsename() const { return f; } }; void setBoolName(std::string t, std::string f) { cout.imbue(std::locale(cout.getloc(), new BoolName(t, f))); } struct Initializer { Initializer() { cout << std::fixed << std::setprecision(15) << std::boolalpha; setBoolName("Yes", "No"); } } initializer; struct Input { bool eof; Input() : eof(false) {} operator char() { char v; this->eof = (std::scanf("%c", &v) != 1); return v; } operator int() { int v; this->eof = (std::scanf("%d", &v) != 1); return v; } operator long() { long v; this->eof = (std::scanf("%ld", &v) != 1); return v; } operator long long() { long long v; this->eof = (std::scanf("%lld", &v) != 1); return v; } operator unsigned int() { unsigned int v; this->eof = (std::scanf("%u", &v) != 1); return v; } operator unsigned long() { unsigned long v; this->eof = (std::scanf("%lu", &v) != 1); return v; } operator unsigned long long() { unsigned long long v; this->eof = (std::scanf("%llu", &v) != 1); return v; } operator double() { double v; this->eof = (std::scanf("%lf", &v) != 1); return v; } operator long double() { long double v; this->eof = (std::scanf("%Lf", &v) != 1); return v; } void ignore() const { getchar(); } } in; template T abs(T a) { return a >= 0 ? a : -a; } template bool chmin(T &a, const S &b) { return a > b ? a = b, true : false; } template bool chmax(T &a, const S &b) { return a < b ? a = b, true : false; } template std::function cast() { return [](const T &t) { return static_cast(t); }; } template T copy(const T &a) { return T(a); } class ZeroPadding { public: ZeroPadding(int n) : n(n) {} int n; }; std::ostream &operator<<(std::ostream &os, const ZeroPadding &z) { os << std::setw(z.n) << std::setfill('0'); return os; } template constexpr T inf() { return std::numeric_limits::max() / 2 - 1; } /******************/ /* arithmetic.hpp */ /******************/ template class Addition { public: template T operator+(const V &v) const { return T(static_cast(*this)) += v; } T operator++() { return static_cast(*this) += 1; } }; template class Subtraction { public: template T operator-(const V &v) const { return T(static_cast(*this)) -= v; } }; template class Multiplication { public: template T operator*(const V &v) const { return T(static_cast(*this)) *= v; } }; template class Division { public: template T operator/(const V &v) const { return T(static_cast(*this)) /= v; } }; template class Modulus { public: template T operator%(const V &v) const { return T(static_cast(*this)) %= v; } }; template class IndivisibleArithmetic : public Addition, public Subtraction, public Multiplication {}; template class Arithmetic : public IndivisibleArithmetic, public Division {}; /*************/ /* tuple.hpp */ /*************/ #include template class Tuple : public std::tuple { public: Tuple(Input &in) : std::tuple() { (void)in; } }; template class Tuple : public std::tuple { public: Tuple() : std::tuple() {} Tuple(T t, S... s) : std::tuple(t, s...) {} Tuple(const std::tuple &t) : std::tuple(t) {} Tuple(Input &in) { auto a = std::tuple(in); std::tuple b = Tuple(in); std::tuple c = std::tuple_cat(a, b); *this = c; } template auto &get() { return std::get(*this); } template const auto &get() const { return std::get(*this); } }; template Tuple makeTuple(const T &... args) { return Tuple(args...); } namespace std { template class tuple_size> : public std::integral_constant {}; template class tuple_element> { public: using type = tuple_element_t>; }; } // namespace std /*****************/ /* container.hpp */ /*****************/ #include template class Container : public T { private: using S = typename T::value_type; using Itr = typename T::iterator; public: Container() : T() {} Container(int n) : T(n) {} Container(int n, S s) : T(n, s) {} template Container(Itr first, Itr last) : T(first, last) {} Container(const std::initializer_list &v) : T(v) {} Container(int n, Input &in) { std::vector v(n); for (auto &i : v) { i = in; } *this = Container(v.begin(), v.end()); } S max() const { return *std::max_element(this->begin(), this->end()); } template auto max(Function func) const { std::vector> res; for (const auto &i : *this) { res.emplace_back(func(i), i); } return std::max_element(res.begin(), res.end())->second; } S min() const { return *std::min_element(this->begin(), this->end()); } Tuple minmax() const { auto itrs = std::minmax_element(this->begin(), this->end()); return Tuple(*itrs.first, *itrs.second); } template auto min(Function func) const { std::vector> res; for (const auto &i : *this) { res.emplace_back(func(i), i); } return std::min_element(res.begin(), res.end())->second; } int argmax() const { return std::distance(std::max_element(this->begin(), this->end()), this->begin()); } int argmin() const { return std::distance(std::min_element(this->begin(), this->end()), this->begin()); } int find(const S &a) const { return std::distance(std::find(this->begin(), this->end(), a), this->begin()); } bool contains(const S &a) const { return std::find(this->begin(), this->end(), a) != this->end(); } int size() const { return T::size(); } std::pair equal_range(const S &a) { return std::equal_range(this->begin(), this->end(), a); } template bool all_of(Function func) const { return std::all_of(this->begin(), this->end(), func); } template bool any_of(Function func) const { return std::any_of(this->begin(), this->end(), func); } template bool none_of(Function func) const { return std::none_of(this->begin(), this->end(), func); } int count(const S &s) const { return std::count(this->begin(), this->end(), s); } bool is_sorted() const { return std::is_sorted(this->begin(), this->end()); } void output(std::string sep = "\n", std::string end = "\n") const { bool first = true; for (const auto &i : *this) { if (!first) { cout << sep; } first = false; cout << i; } cout << end; } }; /***********/ /* map.hpp */ /***********/ #include template class Map : public Container> { public: Map() : Container>() {} bool contains(const T &a) const { return this->count(a) != 0; } int count(const T &t) const { return this->count(t); } }; /***************/ /* ordered.hpp */ /***************/ template class Ordered { public: template bool operator==(const V &v) const { return !(static_cast(v) < static_cast(*this) || static_cast(*this) < static_cast(v)); } template bool operator!=(const V &v) const { return static_cast(v) < static_cast(*this) || static_cast(*this) < static_cast(v); } template bool operator>(const V &v) const { return static_cast(v) < static_cast(*this); } template bool operator<=(const V &v) const { return !(static_cast(v) < static_cast(*this)); } template bool operator>=(const V &v) const { return !(static_cast(*this) < static_cast(v)); } }; /**************/ /* vector.hpp */ /**************/ #include template class Vector : public Container>, public Ordered> { public: Vector() = default; Vector(const Vector &v) = default; Vector(int n) : Container>(n) {} Vector(int n, T t) : Container>(n, t) {} template Vector(Itr first, Itr last) : Container>(first, last) {} Vector(const std::initializer_list &v) : Container>(v) {} Vector(int n, Input &in) : Container>(n, in) {} Vector &operator+=(const Vector &v) { if (this->size() < v.size()) { this->resize(v.size()); } for (int i = 0; i < v.size(); ++i) { (*this)[i] += v[i]; } return *this; } Vector &operator+=(const T &v) { for (auto &i : *this) { i += v; } return *this; } Vector &operator-=(const Vector &v) { if (this->size() < v.size()) { this->resize(v.size()); } for (int i = 0; i < v.size(); ++i) { (*this)[i] -= v[i]; } return *this; } Vector &operator-=(const T &v) { for (auto &i : *this) { i -= v; } return *this; } Vector &operator*=(const Vector &v) { for (int i = 0; i < this->size(); ++i) { (*this)[i] *= v[i]; } return *this; } Vector &operator*=(const T &v) { for (auto &i : *this) { i *= v; } return *this; } Vector &operator/=(const Vector &v) { for (int i = 0; i < this->size(); ++i) { (*this)[i] /= v[i]; } return *this; } Vector &operator/=(const T &v) { for (auto &i : *this) { i /= v; } return *this; } Vector &operator%=(const Vector &v) { for (int i = 0; i < this->size(); ++i) { (*this)[i] %= v[i]; } return *this; } Vector &operator%=(const T &v) { for (auto &i : *this) { i %= v; } return *this; } Vector operator+(const Vector &v) const { return Vector(*this) += v; } Vector operator+(const T &v) const { return Vector(*this) += v; } Vector operator-(const Vector &v) const { return Vector(*this) -= v; } Vector operator-(const T &v) const { return Vector(*this) -= v; } Vector operator*(const Vector &v) const { return Vector(*this) *= v; } Vector operator*(const T &v) const { return Vector(*this) *= v; } Vector operator/(const Vector &v) const { return Vector(*this) /= v; } Vector operator/(const T &v) const { return Vector(*this) /= v; } Vector operator%(const Vector &v) const { return Vector(*this) %= v; } Vector operator%(const T &v) const { return Vector(*this) %= v; } bool operator<(const Vector &v) const { if (this->size() != v.size()) { return this->size() < v.size(); } for (int i = 0; i < this->size(); ++i) { if ((*this)[i] != v[i]) { return (*this)[i] < v[i]; } } return false; } Vector operator-() const { return *this * -1; } T inner_product(const Vector &v) const { return std::inner_product(this->begin(), this->end(), v.begin(), T(0)); } Vector &partial_sort(int k, bool reverse = false) { if (!reverse) { std::partial_sort(this->begin(), this->begin() + k, this->end()); } else { std::partial_sort(this->begin(), this->begin() + k, this->end(), std::greater()); } return *this; } Vector &sort() { std::sort(this->begin(), this->end()); return *this; } template Vector &sort(Function func) { std::sort(this->begin(), this->end(), func); return *this; } Vector &rsort() { std::sort(this->rbegin(), this->rend()); return *this; } Vector argsort() const { Vector> v; for (int i = 0; i < this->size(); ++i) { v.emplace_back((*this)[i], i); } v.sort(); auto f = [](const Tuple &t) { return t.template get<1>(); }; return v.transform(f); } Vector &nth_element(int n, bool reverse = false) { if (!reverse) { std::nth_element(this->begin(), this->begin() + n, this->end()); } else { std::nth_element(this->begin(), this->begin() + n, this->end(), std::greater()); } return *this; } Vector subvector(int a) const { return Vector(this->begin(), this->begin() + a); } Vector subvector(int a, int b) const { return Vector(this->begin() + a, this->begin() + b); } template auto transform(Function func) const { Vector res; std::transform(this->begin(), this->end(), std::back_inserter(res), func); return res; } Vector partial_sum() const { Vector res; std::partial_sum(this->begin(), this->end(), std::back_inserter(res)); return res; } template Vector partial_sum(Function func) const { Vector res; std::partial_sum(this->begin(), this->end(), std::back_inserter(res), func); return res; } Vector &reverse() { std::reverse(this->begin(), this->end()); return *this; } template int count_if(Function func) const { return std::count_if(this->begin(), this->end(), func); } Vector adjacent_difference() const { Vector res; std::adjacent_difference(this->begin(), this->end(), std::back_inserter(res)); return res; } T lower_bound(T t) const { return std::lower_bound(this->begin(), this->end(), t) - this->begin(); } T upper_bound(T t) const { return std::upper_bound(this->begin(), this->end(), t) - this->begin(); } T accumulate() const { return std::accumulate(this->begin(), this->end(), T()); } template S accumulate(S n, Function func) const { return std::accumulate(this->begin(), this->end(), n, func); } template static Vector makeVector(Int n) { return Vector(n); } template static Vector makeVector(Input &in, Int n) { return Vector(n, in); } template static auto makeVector(Input &in, Int n, Ints... ints) { Vector res; for (int i = 0; i < n; ++i) { res.emplace_back(makeVector(in, ints...)); } return res; } template static auto makeVector(Int n, Ints... ints) { Vector res; for (int i = 0; i < n; ++i) { res.emplace_back(makeVector(ints...)); } return res; } Vector &unique() { this->erase(std::unique(this->begin(), this->end()), this->end()); return *this; } bool next_permutation() { return std::next_permutation(this->begin(), this->end()); } Vector &rotate(int n) { std::rotate(this->begin(), this->begin() + n, this->end()); return *this; } Map countAll() const { Map res; for (const auto &i : *this) { ++res[i]; } return res; } T matmul(const T &a) const { return this->transform([&](const T &i) { return i.inner_product(a); }); } }; template Vector iota(int n, T m = 0) { Vector v(n); std::iota(v.begin(), v.end(), m); return v; } template void read(Vector &t, Vector &s) { for (int i = 0; i < t.size(); ++i) { t[i] = T(in); s[i] = S(in); } } template void read(Vector &t, Vector &s, Vector &u) { for (int i = 0; i < t.size(); ++i) { t[i] = T(in); s[i] = S(in); u[i] = U(in); } } template Vector operator+(const T &a, const Vector &b) { return b + a; } template Vector operator-(const T &a, const Vector &b) { return -b + a; } template Vector operator*(const T &a, const Vector &b) { return b * a; } /**************/ /* string.hpp */ /**************/ #include class String : public std::string { public: constexpr static auto npos = std::string::npos; String() : std::string() {} String(char c) : std::string(1, c) {} String(int n) : std::string(std::to_string(n)) {} String(long n) : std::string(std::to_string(n)) {} String(long long n) : std::string(std::to_string(n)) {} String(const char *s) : std::string(s) {} String(const std::string &s) : std::string(s) {} String(int n, char c) : std::string(n, c) {} String(Input &in) { std::cin >> *this; in.eof = std::cin.eof(); } String &operator+=(const String &s) { return *this = *this + s; } String operator+(const String &s) const { return std::operator+(*this, s); } String &operator+=(const char *s) { return *this = *this + s; } String operator+(const char *s) const { return std::operator+(*this, s); } String &operator+=(char s) { return *this = *this + s; } String operator+(char s) const { return std::operator+(*this, s); } static String getline() { String v; std::getline(std::cin, v); in.eof = std::cin.eof(); return v; } String substr(size_t pos, size_t n_pos = std::string::npos) { return std::string::substr(pos, n_pos); } String &reverse() { std::reverse(this->begin(), this->end()); return *this; } Vector split(char delim = ' ') const { std::stringstream ss(*this); Vector res; for (std::string tmp; std::getline(ss, tmp, delim);) { if (tmp != "") { res.emplace_back(tmp); } } return res; } String &toupper() { for (auto &c : *this) { c = std::toupper(c); } return *this; } String &tolower() { for (auto &c : *this) { c = std::tolower(c); } return *this; } template String transform(Function func) const { String res; std::transform(this->begin(), this->end(), std::back_inserter(res), func); return res; } bool contains(const String &s) const { return this->find(s) != std::string::npos; } String &erase(char c) { String res; for (char i : *this) { if (i != c) { res += i; } } return *this = res; } int count(char c) const { return std::count(this->begin(), this->end(), c); } template String &replaceAll(const String &from, const String &to) { for (size_t pos = 0; (pos = this->find(from, pos)) != std::string::npos;) { this->replace(pos, from.size(), to); if (!Repeat) { pos += to.size(); } } return *this; } String &sort() { std::sort(this->begin(), this->end()); return *this; } String &rsort() { std::sort(this->rbegin(), this->rend()); return *this; } String &unique() { std::string::erase(std::unique(this->begin(), this->end()), this->end()); return *this; } String &rotate(int n) { std::rotate(this->begin(), this->begin() + n, this->end()); return *this; } bool next_permutation() { return std::next_permutation(this->begin(), this->end()); } template T inner_product(const String &v, T init, Function1 func1, Function2 func2) const { return std::inner_product(this->begin(), this->end(), v.begin(), init, func1, func2); } template int find_if(Function func) const { return std::find_if(this->begin(), this->end(), func) - this->begin(); } template bool all_of(Function func) const { return std::all_of(this->begin(), this->end(), func); } template bool any_of(Function func) const { return std::any_of(this->begin(), this->end(), func); } template bool none_of(Function func) const { return std::none_of(this->begin(), this->end(), func); } int size() const { return std::string::size(); } operator int() const { return std::stoi(*this); } operator long() const { return std::stol(*this); } operator long long() const { return std::stoll(*this); } operator float() const { return std::stof(*this); } operator double() const { return std::stod(*this); } operator long double() const { return std::stold(*this); } }; /*******************/ /* big_decimal.hpp */ /*******************/ #include #include template class BigDecimal : public Arithmetic>, public Ordered> { private: constexpr static int BitSize = 31; constexpr static bool PLUS = false; constexpr static bool MINUS = true; const static long double EPSILON; bool sign; std::array d; public: BigDecimal() { *this = BigDecimal(0); } BigDecimal(int n) { sign = PLUS; for (auto &i : d) { i = 0; } d[DecimalSize] = n; normal(); } BigDecimal(int64_t n) { sign = PLUS; for (auto &i : d) { i = 0; } d[DecimalSize] = n; normal(); } BigDecimal(String str) { *this = BigDecimal(0); bool minus = false; if (str[0] == '-') { minus = true; str = str.substr(1); } BigDecimal t = 1; bool decimal = false; for (int i = 0; i < str.size(); ++i) { if (str[i] == '.') { decimal = true; } else { if (decimal) { *this += (t /= 10) * (str[i] - '0'); } else { *this = *this * 10 + (str[i] - '0'); } } } if (minus) { sign = MINUS; } else { sign = PLUS; } } BigDecimal(double r) { *this = BigDecimal(0); int n; BigDecimal b = (r >= 0 ? BigDecimal(1) : BigDecimal(-1)); r = 2 * abs(std::frexp(abs(r), &n)); b <<= n - 1; while (r) { if (r >= 1) { *this += b; r -= 1; } r *= 2; b >>= 1; } } BigDecimal(long double r) { *this = BigDecimal(0); int n; BigDecimal b = (r >= 0 ? BigDecimal(1) : BigDecimal(-1)); r = 2 * abs(std::frexp(abs(r), &n)); b <<= n - 1; while (r) { if (r >= 1) { *this += b; r -= 1; } r *= 2; b >>= 1; } } BigDecimal(Input &in) { *this = BigDecimal(String(in)); } BigDecimal &normal() { for (int i = 0; i < IntegerSize + DecimalSize - 1; ++i) { d[i + 1] += d[i] >> BitSize; d[i] &= (1ll << BitSize) - 1; } if (d.back() < 0) { sign = !sign; for (auto &i : d) { i = -i; } normal(); } if (d.back() >= (1ll << BitSize)) { throw "overflow"; } if (std::all_of(d.begin(), d.end(), [](int64_t i) { return i == 0; })) { sign = PLUS; } return *this; } BigDecimal operator-() const { BigDecimal bd(*this); bd.sign = !bd.sign; return bd; } BigDecimal operator<<(int a) const { return BigDecimal(*this) <<= a; } BigDecimal operator>>(int a) const { return BigDecimal(*this) >>= a; } BigDecimal operator%(const BigDecimal &a) const { return BigDecimal(*this) %= a; } BigDecimal &operator<<=(int a) { if (a < 0) { return *this >>= -a; } while (a >= BitSize) { if (d.back()) { throw "overflow"; } for (int i = IntegerSize + DecimalSize; --i > 0;) { d[i] = d[i - 1]; } d[0] = 0; a -= BitSize; } if (d.back() >= (1ll << (BitSize - a))) { throw "overflow"; } for (auto &i : d) { i <<= a; } return normal(); } BigDecimal &operator>>=(int a) { if (a < 0) { return *this <<= -a; } while (a >= BitSize) { for (int i = 0; i < IntegerSize + DecimalSize - 1; ++i) { d[i] = d[i + 1]; } d.back() >>= BitSize; a -= BitSize; } for (int i = 0; i < IntegerSize + DecimalSize - 1; ++i) { d[i] |= d[i + 1] << BitSize; d[i + 1] = 0; } for (auto &i : d) { i >>= a; } return normal(); } BigDecimal &operator+=(const BigDecimal &a) { if (sign == a.sign) { for (int i = 0; i < IntegerSize + DecimalSize; ++i) { d[i] += a.d[i]; } } else { for (int i = 0; i < IntegerSize + DecimalSize; ++i) { d[i] -= a.d[i]; } } return normal(); } BigDecimal &operator-=(const BigDecimal &a) { return *this += -a; } BigDecimal &operator*=(const BigDecimal &a) { BigDecimal res = 0; for (int i = 0; i < IntegerSize + DecimalSize; ++i) { if (i < DecimalSize) { res = (res + *this * a.d[i]) >> BitSize; } else { res += *this * a.d[i] << (i - DecimalSize) * BitSize; } } res.sign = (sign == a.sign ? PLUS : MINUS); return *this = res.normal(); } BigDecimal &operator*=(const unsigned int &a) { for (auto &i : d) { i *= a; } return normal(); } BigDecimal &operator/=(const BigDecimal &a) { if (a == 0) { throw "divide by zero"; } BigDecimal rev = 1. / a.toDouble(); for (int i = 0; i < 7; ++i) { rev = (rev << 1) - a * rev * rev; } rev.sign = a.sign; return *this *= rev; } BigDecimal &operator%=(const BigDecimal &a) { if (a == 0) { throw "modulo by zero"; } return *this -= floor(*this / a) * a; } BigDecimal operator-(const BigDecimal &v) const { return BigDecimal(*this) -= v; } BigDecimal &operator++() { return *this += 1; } BigDecimal &operator++(int) { BigDecimal bd = *this; *this += 1; return bd; } BigDecimal &operator--() { return *this -= 1; } BigDecimal &operator--(int) { BigDecimal bd = *this; *this -= 1; return bd; } bool operator<(const BigDecimal &a) const { BigDecimal aa = a - EPSILON; if (sign == MINUS) { if (aa.sign == MINUS) { if (a.sign == MINUS) { return -a < -*this; } else { return *this + EPSILON < a + EPSILON; } } else { return true; } } if (aa.sign == MINUS) { return false; } for (int i = IntegerSize + DecimalSize; i-- > 0;) { if (d[i] != aa.d[i]) { return d[i] < aa.d[i]; } } return false; } bool equals(const BigDecimal &a) const { return sign == a.sign && d == a.d; } int toInt() const { int res = 0; for (int i = 0; i < IntegerSize; ++i) { res += d[DecimalSize + i] << BitSize * i; } if (sign == MINUS) { return -res; } return res; } int64_t toLongLong() const { int64_t res = 0; for (int i = 0; i < IntegerSize; ++i) { res += d[DecimalSize + i] << BitSize * i; } if (sign == MINUS) { return -res; } return res; } String toString(int digit = 100, String mode = "near") const { String str; BigDecimal a = *this, bd = 1; if (a.sign == MINUS) { str += "-"; a.sign = PLUS; } if (mode == "near") { BigDecimal round = BigDecimal(0.5); for (int i = 0; i < digit; ++i) { round /= 10; } a += round + EPSILON; } if (mode == "ceil") { BigDecimal round = 1; for (int i = 0; i < digit; ++i) { round /= 10; } a += round - EPSILON; } for (; bd <= a; bd *= 10) { ++digit; } if (bd > 1) { bd /= 10; --digit; } for (int i = 0; i < digit + 1; ++i) { if (bd == 0) { str += "0"; continue; } if (bd * 10 == 1) { str += "."; } int d = 0; while (bd < a) { a -= bd; ++d; } if (d > 9) { d -= 10; auto itr = str.end(); while (true) { if (itr == str.begin()) { str = "1" + str; break; } --itr; if (*itr == '.') { continue; } ++*itr; if (*itr > '9') { *itr = '0'; } else { break; } } } str += '0' + d; bd /= 10; } return str; } double toDouble() const { double res = 0; for (int i = 0; i < IntegerSize + DecimalSize; ++i) { res += d[i] * pow(2, (i - DecimalSize) * BitSize); } if (sign == MINUS) { return -res; } return res; } bool isPlus() const { return sign == PLUS; } bool isMinus() const { return sign == MINUS; } template friend BigDecimal pow(const BigDecimal &x, const BigDecimal &y); template friend BigDecimal floor(BigDecimal x); }; template const long double BigDecimal::EPSILON = std::pow(2, -(BitSize - 4) * DecimalSize); template BigDecimal pi() { const static BigDecimal PI = (atan(BigDecimal(1) / 5) << 4) - (atan(BigDecimal(1) / 239) << 2); return PI; } template BigDecimal operator+(const int &a, BigDecimal b) { return BigDecimal(a) + b; } template BigDecimal operator-(const int &a, BigDecimal b) { return BigDecimal(a) - b; } template BigDecimal operator*(const int &a, BigDecimal b) { return BigDecimal(a) * b; } template BigDecimal operator/(const int &a, BigDecimal b) { return BigDecimal(a) / b; } template std::ostream &operator<<(std::ostream &os, BigDecimal a) { os << a.toString(os.precision()); return os; } template BigDecimal sin(BigDecimal x) { BigDecimal res = 0, xx = -x * x; for (int i = 1;; i += 2) { x /= max(i * (i - 1), 1); if (x.equals(0)) { break; } res += x; x *= xx; } return res; } template BigDecimal cos(BigDecimal x) { BigDecimal res = 0, xx = -x * x; x = 1; for (int i = 0;; i += 2) { x /= max(i * (i - 1), 1); if (x.equals(0)) { break; } res += x; x *= xx; } return res; } template BigDecimal tan(const BigDecimal &x) { return sin(x) / cos(x); } template BigDecimal asin(BigDecimal x) { if (abs(x) > 1) { throw "out of domain"; } if (x > 1 / sqrt(BigDecimal(2))) { return (pi() >> 1) - asin(sqrt(1 - x * x)); } if (x < -1 / sqrt(BigDecimal(2))) { return -(pi() >> 1) + asin(sqrt(1 - x * x)); } BigDecimal res = 0, xx = x * x >> 2; for (int i = 0;; ++i) { x *= max(i * 2 * (i * 2 - 1), 1); x /= max(i * i, 1); auto add = x / (i * 2 + 1); if (add.equals(0)) { break; } res += add; x *= xx; } return res; } template BigDecimal acos(const BigDecimal &x) { return (pi() >> 1) - asin(x); } template BigDecimal atan(BigDecimal x) { if (x.isMinus()) { return -atan(-x); } if (abs(x) > sqrt(2) + 1) { return (pi() >> 1) - atan(1 / x); } if (abs(x) > sqrt(2) - 1) { return (pi() >> 2) + atan((x - 1) / (x + 1)); } BigDecimal res = 0, xx = -x * x; for (int i = 1;; i += 2) { auto add = x / i; if (add.equals(0)) { break; } res += add; x *= xx; } return res; } template BigDecimal atan2(const BigDecimal &y, const BigDecimal &x) { if (x == 0) { if (y > 0) { return pi() / 2; } if (y < 0) { return -pi() / 2; } throw "origin can't define argument"; } if (x.isPlus()) { return atan(y / x); } if (y.isPlus()) { return atan(y / x) + pi(); } return atan(y / x) - pi(); } template BigDecimal sinh(BigDecimal x) { BigDecimal res = 0, xx = x * x; for (int i = 1;; i += 2) { x /= max(i * (i - 1), 1); if (x.equals(0)) { break; } res += x; x *= xx; } return res; } template BigDecimal cosh(BigDecimal x) { BigDecimal res = 0, xx = x * x; x = 1; for (int i = 0;; i += 2) { x /= max(i * (i - 1), 1); if (x.equals(0)) { break; } res += x; x *= xx; } return res; } template BigDecimal tanh(const BigDecimal &x) { return sinh(x) / cosh(x); } template BigDecimal exp(const BigDecimal &x) { BigDecimal res = 0, xx = 1; for (int i = 0;; ++i) { xx /= max(i, 1); if (xx.equals(0)) { break; } res += xx; xx *= x; } return res; } template BigDecimal log(const BigDecimal &x) { BigDecimal y = log(x.toDouble()); BigDecimal z = x / exp(y); BigDecimal a = (z - 1) / (z + 1); BigDecimal res = 0, b = a, aa = a * a; for (int i = 1;; i += 2) { if (b.equals(0)) { break; } res += b / i; b *= aa; } return y + res * 2; } template BigDecimal log10(const BigDecimal &x) { return log(x) / log(BigDecimal(10)); } template BigDecimal pow(const BigDecimal &x, const BigDecimal &y) { if (x.isMinus()) { if (floor(y) == y) { return floor(y).d[DecimalSize] % 2 ? -pow(-x, floor(y)) : pow(-x, floor(y)); } throw "power of negative number"; } return exp(y * log(x)); } template BigDecimal sqrt(const BigDecimal &x) { BigDecimal r = 1 / sqrt(x.toDouble()); for (int i = 0; i < 7; ++i) { r *= (3 - x * r * r) >> 1; } return BigDecimal(1) / r; } template BigDecimal abs(const BigDecimal &x) { return x.isPlus() ? x : -x; } template BigDecimal ceil(const BigDecimal &x) { if (x.isMinus()) { return -floor(-x); } auto f = floor(x); return f == x ? f : x + 1; } template BigDecimal floor(BigDecimal x) { if (x.isMinus()) { return -ceil(-x); } x += BigDecimal::EPSILON; for (int i = 0; i < DecimalSize; ++i) { x.d[i] = 0; } return x; } /************/ /* main.cpp */ /************/ int main() { BigDecimal<> v(in), t(in); cout << int((v * t).toDouble()) << endl; }