#define SKIP_RUNTIME_CHECKS // Verify: https://yukicoder.me/problems/no/132 // Library: Geometry/Integer_Geometry_3D.cpp // SPDX-License-Identifier: CC0-1.0 // ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/integer_geometry_3d.md // // API一覧 // Point3i{x,y,z}: int64_tの3次元整数点。各座標の絶対値は10^9以下。 // orientation_3d(a,b,c,p): (b-a)×(c-a)と(p-a)の内積の符号。正1、負-1、零0。整数演算で厳密。 // triangular_prism_position(a,b,c,p): pの平面への垂直射影を三角形と比較。内部・境界・外部・退化。 // signed_plane_distance(a,b,c,p): 法線方向の符号付き距離をdoubleで返す。3点が共線ならnullopt。 // 判定は整数で厳密。符号付き距離は法線の長さと除算に浮動小数点を使う。各操作はO(1)時間・領域。 // // [同梱の既約分数と固定幅整数] // [Fraction] // Fraction(n=0,d=1): int64_tの分子・分母から既約分数を作る。分母は正、0は0/1。O(log M)。 // numerator()/denominator()/sign()/is_integer(): 正規化済みの分子・分母・符号・整数判定。O(1)。 // 四則演算・複合代入・単項+/-: 厳密な既約結果を返す。整数との演算も可。O(log M)。 // ==, <=>: 厳密な等値・大小比較。O(1)。最終結果が表現できても中間積が大きい場合に対応。 // inverse()/abs(): 逆数・絶対値を返す。O(1)。0の逆数はdomain_error。 // floor()/ceil()/trunc(): 床・天井・0方向への整数丸めをint64_tで返す。O(1)。 // explicit浮動小数点変換: 分子/分母を対象の浮動小数点型で計算する。O(1)。 // <<, >>: n/dを出力。入力は整数またはn/d(/は分子の直後)。失敗時は値を保持。 // 0分母・0除算はdomain_error、既約結果の分子/正分母がint64_t範囲外ならoverflow_error。 // これらの数学的・表現上の保護はSKIP_RUNTIME_CHECKS時も有効。Mは整数の絶対値の上限。 // SIRKIT_FRACTION_DISABLE_INT128: GCC/Clangの128ビット拡張を使わず標準C++経路を選ぶ。 // SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。 #include #include #include #include #include #include #include #include #include #include #include #include #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { #ifndef SIRKIT_FRACTION_DETAIL_DEFINED #define SIRKIT_FRACTION_DETAIL_DEFINED namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ { // 引数は0以上2^63以下。奇数化した正の値の差は絶対値が2^63未満。 constexpr std::uint64_t fraction_gcd(std::uint64_t a, std::uint64_t b) { if (a == 1 || b == 1) return 1; if (a == 0) return b; if (b == 0) return a; const auto common = std::countr_zero(a | b); a >>= std::countr_zero(a); b >>= std::countr_zero(b); while (a != b) { const auto diff = a - b; const auto shift = std::countr_zero(diff); const auto low = a < b ? a : b; const auto mag = diff < (std::uint64_t{1} << 63) ? diff : 0 - diff; a = mag >> shift; b = low; } return a << common; } struct FractionWide { #if defined(__SIZEOF_INT128__) && !defined(SIRKIT_FRACTION_DISABLE_INT128) __extension__ using Storage = unsigned __int128; Storage value = 0; constexpr FractionWide(std::uint64_t x = 0) : value(x) {} static constexpr FractionWide product(std::uint64_t a, std::uint64_t b) { FractionWide r; r.value = Storage(a) * b; return r; } friend constexpr FractionWide operator+(FractionWide a, FractionWide b) { a.value += b.value; return a; } friend constexpr FractionWide operator-(FractionWide a, FractionWide b) { a.value -= b.value; return a; } constexpr auto operator<=>(const FractionWide&) const = default; constexpr std::uint64_t low() const { return static_cast(value); } constexpr bool fits(std::uint64_t bound) const { return value <= bound; } constexpr std::pair divmod(std::uint64_t d) const { FractionWide q; q.value = value / d; return {q, static_cast(value % d)}; } #else std::uint64_t hi = 0, lo = 0; constexpr FractionWide(std::uint64_t x = 0) : lo(x) {} static constexpr FractionWide product(std::uint64_t a, std::uint64_t b) { constexpr std::uint64_t mask = 0xffffffff; const auto a0 = a & mask, a1 = a >> 32; const auto b0 = b & mask, b1 = b >> 32; const auto t0 = a0 * b0; const auto t1 = a1 * b0 + (t0 >> 32); const auto t2 = a0 * b1 + (t1 & mask); FractionWide r; r.hi = a1 * b1 + (t1 >> 32) + (t2 >> 32); r.lo = (t2 << 32) | (t0 & mask); return r; } friend constexpr FractionWide operator+(FractionWide a, FractionWide b) { const auto old = a.lo; a.lo += b.lo; a.hi += b.hi + (a.lo < old); return a; } friend constexpr FractionWide operator-(FractionWide a, FractionWide b) { const auto old = a.lo; a.lo -= b.lo; a.hi -= b.hi + (old < b.lo); return a; } constexpr auto operator<=>(const FractionWide&) const = default; constexpr std::uint64_t low() const { return lo; } constexpr bool fits(std::uint64_t bound) const { return hi == 0 && lo <= bound; } constexpr std::pair divmod(std::uint64_t d) const { if (hi == 0) return {FractionWide(lo / d), lo % d}; FractionWide q; q.hi = hi / d; auto rem = hi % d; // 呼び出し側では1 <= d <= 2^63。余りを1bit伸ばしてもuint64_tに収まる。 for (int bit = 63; bit >= 0; --bit) { rem = (rem << 1) | ((lo >> bit) & 1); if (rem >= d) { rem -= d; q.lo |= std::uint64_t{1} << bit; } } return {q, rem}; } #endif }; constexpr std::uint64_t fraction_magnitude(std::int64_t x) { const auto u = static_cast(x); return x < 0 ? std::uint64_t{0} - u : u; } } // namespace reinsirk::detail #endif #ifndef SIRKIT_FRACTION_DEFINED #define SIRKIT_FRACTION_DEFINED class Fraction { using Wide = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::FractionWide; std::int64_t num_ = 0, den_ = 1; static constexpr auto max_ = static_cast(std::numeric_limits::max()); static constexpr std::uint64_t magnitude(std::int64_t n) { return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fraction_magnitude(n); } static constexpr Fraction result(Wide n, Wide d, bool negative) { if (!n.fits(max_ + static_cast(negative)) || !d.fits(max_)) throw std::overflow_error("Fraction: result out of int64_t range"); Fraction r; const auto u = n.low(); // 負号を指定する呼出しは、分子が0の場合を処理済み。 r.num_ = negative ? -static_cast(u - 1) - 1 : static_cast(u); r.den_ = static_cast(d.low()); return r; } static constexpr Fraction combine(const Fraction& a, const Fraction& b, bool subtract) { const auto da = static_cast(a.den_); const auto db = static_cast(b.den_); std::uint64_t g; Wide left, right; if (da == db) { g = da; left = Wide(magnitude(a.num_)); right = Wide(magnitude(b.num_)); } else { g = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fraction_gcd(da, db); left = Wide::product(magnitude(a.num_), db / g); right = Wide::product(magnitude(b.num_), da / g); } bool negative = a.num_ < 0; const bool right_negative = (b.num_ < 0) != subtract; Wide n; if (negative == right_negative) n = left + right; else if (left >= right) n = left - right; else { n = right - left; negative = right_negative; } if (n == Wide{}) return {}; // 各分数は既約なので、和の分子との追加の共通因子はgの約数だけ。 const auto h = g == 1 ? 1 : /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fraction_gcd(n.divmod(g).second, g); if (h != 1) n = n.divmod(h).first; return result(n, Wide::product(da / g, db / h), negative); } public: constexpr Fraction() = default; constexpr Fraction(std::int64_t n, std::int64_t d = 1) { if (d == 0) throw std::domain_error("Fraction: zero denominator"); if (d == 1) { num_ = n; return; } if (n == 0) return; auto u = magnitude(n), v = magnitude(d); const auto g = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fraction_gcd(u, v); if (g == 1 && d > 0) { num_ = n; den_ = d; return; } if (g != 1) { u /= g; v /= g; } *this = result(Wide(u), Wide(v), (n < 0) != (d < 0)); } constexpr std::int64_t numerator() const noexcept { return num_; } constexpr std::int64_t denominator() const noexcept { return den_; } constexpr int sign() const noexcept { return (num_ > 0) - (num_ < 0); } constexpr bool is_integer() const noexcept { return den_ == 1; } constexpr std::int64_t trunc() const noexcept { return num_ / den_; } constexpr std::int64_t floor() const noexcept { return trunc() - (num_ % den_ < 0); } constexpr std::int64_t ceil() const noexcept { return trunc() + (num_ % den_ > 0); } constexpr Fraction inverse() const { if (num_ == 0) throw std::domain_error("Fraction: inverse of zero"); return result(Wide(static_cast(den_)), Wide(magnitude(num_)), num_ < 0); } constexpr Fraction abs() const { return result(Wide(magnitude(num_)), Wide(static_cast(den_)), false); } template explicit constexpr operator T() const noexcept { return static_cast(num_) / static_cast(den_); } constexpr Fraction operator+() const noexcept { return *this; } constexpr Fraction operator-() const { return result(Wide(magnitude(num_)), Wide(static_cast(den_)), num_ > 0); } friend constexpr Fraction operator+(const Fraction& a, const Fraction& b) { return combine(a, b, false); } friend constexpr Fraction operator-(const Fraction& a, const Fraction& b) { return combine(a, b, true); } friend constexpr Fraction operator*(const Fraction& a, const Fraction& b) { if (a.num_ == 0 || b.num_ == 0) return {}; const auto na = magnitude(a.num_), nb = magnitude(b.num_); const auto da = static_cast(a.den_), db = static_cast(b.den_); const auto g = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fraction_gcd(na, db), h = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fraction_gcd(nb, da); return result(Wide::product(na / g, nb / h), Wide::product(da / h, db / g), (a.num_ < 0) != (b.num_ < 0)); } friend constexpr Fraction operator/(const Fraction& a, const Fraction& b) { if (b.num_ == 0) throw std::domain_error("Fraction: division by zero"); if (a.num_ == 0) return {}; const auto na = magnitude(a.num_), nb = magnitude(b.num_); const auto da = static_cast(a.den_), db = static_cast(b.den_); const auto g = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fraction_gcd(na, nb), h = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::fraction_gcd(da, db); return result(Wide::product(na / g, db / h), Wide::product(da / h, nb / g), (a.num_ < 0) != (b.num_ < 0)); } constexpr Fraction& operator+=(const Fraction& b) { return *this = *this + b; } constexpr Fraction& operator-=(const Fraction& b) { return *this = *this - b; } constexpr Fraction& operator*=(const Fraction& b) { return *this = *this * b; } constexpr Fraction& operator/=(const Fraction& b) { return *this = *this / b; } constexpr bool operator==(const Fraction&) const = default; constexpr std::strong_ordering operator<=>(const Fraction& b) const { #if defined(__SIZEOF_INT128__) && !defined(SIRKIT_FRACTION_DISABLE_INT128) if (den_ == b.den_) return num_ <=> b.num_; __extension__ using SignedWide = __int128; const auto left = static_cast(num_) * b.den_; const auto right = static_cast(b.num_) * den_; return left <=> right; #else if (den_ == b.den_) return num_ <=> b.num_; if ((num_ < 0) != (b.num_ < 0)) return num_ <=> b.num_; const auto left = Wide::product(magnitude(num_), static_cast(b.den_)); const auto right = Wide::product(magnitude(b.num_), static_cast(den_)); return num_ < 0 ? right <=> left : left <=> right; #endif } friend std::ostream& operator<<(std::ostream& out, const Fraction& x) { return out << x.num_ << '/' << x.den_; } friend std::istream& operator>>(std::istream& in, Fraction& x) { std::int64_t n = 0, d = 1; if (!(in >> n)) return in; if (!in.eof() && in.peek() == '/') { in.get(); if (!(in >> d)) return in; } try { x = Fraction(n, d); } catch (const std::domain_error&) { in.setstate(std::ios::failbit); } catch (const std::overflow_error&) { in.setstate(std::ios::failbit); } return in; } }; #endif #ifndef SIRKIT_INTEGER_GEOMETRY_3D_DEFINED #define SIRKIT_INTEGER_GEOMETRY_3D_DEFINED struct Point3i { std::int64_t x, y, z; }; enum class PrismPosition { kOutside, kBoundary, kInside, kDegenerate }; namespace detail { using Point3Vector = std::array; inline void CheckPoint3([[maybe_unused]] Point3i a) { #ifndef SKIP_RUNTIME_CHECKS constexpr std::int64_t kLimit = 1000000000; for (const auto coordinate : {a.x, a.y, a.z}) { if (coordinate < -kLimit || coordinate > kLimit) { throw std::out_of_range("Point3i: coordinate out of range"); } } #endif } [[nodiscard]] inline Point3Vector Point3Diff(Point3i a, Point3i b) noexcept { return {a.x - b.x, a.y - b.y, a.z - b.z}; } [[nodiscard]] inline Point3Vector Point3Cross(const Point3Vector &a, const Point3Vector &b) noexcept { return {a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]}; } struct Point3Dot { FractionWide positive, negative; [[nodiscard]] int sign() const noexcept { return (positive > negative) - (positive < negative); } [[nodiscard]] double to_double() const noexcept { const auto convert = [](FractionWide magnitude) -> double { #if defined(__SIZEOF_INT128__) && !defined(SIRKIT_FRACTION_DISABLE_INT128) return static_cast(magnitude.value); #else return std::ldexp(static_cast(magnitude.hi), 64) + static_cast(magnitude.lo); #endif }; return positive < negative ? -convert(negative - positive) : convert(positive - negative); } }; [[nodiscard]] inline Point3Dot WidePoint3Dot(const Point3Vector &a, const Point3Vector &b) noexcept { Point3Dot result; for (std::size_t i = 0; i < a.size(); ++i) { const auto product = FractionWide::product(fraction_magnitude(a[i]), fraction_magnitude(b[i])); auto &sum = (a[i] < 0) == (b[i] < 0) ? result.positive : result.negative; sum = sum + product; } return result; } [[nodiscard]] inline Point3Vector Point3Normal(Point3i a, Point3i b, Point3i c, Point3i p) { CheckPoint3(a); CheckPoint3(b); CheckPoint3(c); CheckPoint3(p); return Point3Cross(Point3Diff(b, a), Point3Diff(c, a)); } } [[nodiscard]] inline int orientation_3d(Point3i a, Point3i b, Point3i c, Point3i p) { const auto normal = detail::Point3Normal(a, b, c, p); return detail::WidePoint3Dot(normal, detail::Point3Diff(p, a)).sign(); } [[nodiscard]] inline PrismPosition triangular_prism_position(Point3i a, Point3i b, Point3i c, Point3i p) { const auto normal = detail::Point3Normal(a, b, c, p); if (normal == detail::Point3Vector{}) { return PrismPosition::kDegenerate; } const std::array vertices{a, b, c}; bool boundary = false; for (std::size_t i = 0; i < vertices.size(); ++i) { const auto start = vertices[i], end = vertices[(i + 1) % vertices.size()]; const auto edge_cross = detail::Point3Cross(detail::Point3Diff(end, start), detail::Point3Diff(p, start)); const auto side = detail::WidePoint3Dot(normal, edge_cross).sign(); if (side < 0) { return PrismPosition::kOutside; } boundary |= side == 0; } return boundary ? PrismPosition::kBoundary : PrismPosition::kInside; } [[nodiscard]] inline std::optional signed_plane_distance(Point3i a, Point3i b, Point3i c, Point3i p) { const auto normal = detail::Point3Normal(a, b, c, p); if (normal == detail::Point3Vector{}) { return std::nullopt; } const auto signed_numerator = detail::WidePoint3Dot(normal, detail::Point3Diff(p, a)); const double x = static_cast(normal[0]), y = static_cast(normal[1]), z = static_cast(normal[2]); return signed_numerator.to_double() / std::sqrt(x * x + y * y + z * z); } #endif } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 // SIRKIT_NAMESPACE_END_V1 namespace sirkit {} using namespace sirkit; // SPDX-License-Identifier: CC0-1.0 #include #include // Verify I/O: Verify/current/common/fast_io.hpp // SPDX-License-Identifier: CC0-1.0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef SIRKIT_VERIFY_FAST_IO_DEFINED #define SIRKIT_VERIFY_FAST_IO_DEFINED // VerifyInput(file=stdin): 非所有のFILEから読む。別の入力方式と混用しない。 // >> 整数: 64bit以下の十進整数を読む。検査省略時は値域内の正しい入力を前提とする。 // >> char/string: 空白を飛ばし、1文字/空白までの文字列を読む。 // >> 浮動小数点: 十進文字列を変換する。通常のCロケールで使う。 // bool / !: 直前までの読み取りが成功しているか/失敗したかを返す。 class VerifyInput { static constexpr std::size_t buffer_size = 1 << 17; std::FILE* file_; std::array buffer_; std::size_t position_ = 0, size_ = 0; bool good_ = true; int peek() { if (position_ == size_) { size_ = std::fread(buffer_.data(), 1, buffer_size, file_); buffer_[size_] = 0; // 補充後の末尾は常に数字でない番兵。 position_ = 0; if (size_ == 0) return EOF; } return buffer_[position_]; } int skip_space() { int c; while ((c = peek()) != EOF && c <= ' ') ++position_; if (c == EOF) good_ = false; return c; } public: explicit VerifyInput(std::FILE* file = stdin) : file_(file) {} VerifyInput(const VerifyInput&) = delete; VerifyInput& operator=(const VerifyInput&) = delete; explicit operator bool() const noexcept { return good_; } bool operator!() const noexcept { return !good_; } template requires (std::is_integral_v && sizeof(T) <= sizeof(std::uint64_t) && !std::is_same_v) VerifyInput& operator>>(T& result) { if (!good_) return *this; int c = skip_space(); if (c == EOF) return *this; bool negative = false; if (c == '-' || c == '+') { negative = c == '-'; ++position_; c = peek(); } if (c == EOF) { good_ = false; return *this; } #ifndef SKIP_RUNTIME_CHECKS if (c < '0' || c > '9') { good_ = false; return *this; } // 先頭の0を除けば、20桁以下の末尾直前の値はuint64_tに収まる。 while (c == '0') { ++position_; c = peek(); } std::uint64_t previous = 0; unsigned last_digit = 0; std::size_t digits = 0; #endif std::uint64_t value = 0; #ifdef SKIP_RUNTIME_CHECKS // バッファ末尾の番兵で内側の範囲判定を省き、数字が境界をまたぐときだけ補充する。 for (;;) { const unsigned char* first = buffer_.data() + position_; const unsigned char* cursor = first; while (static_cast(*cursor - '0') < 10) { value = value * 10 + static_cast(*cursor - '0'); ++cursor; } position_ += static_cast(cursor - first); if (position_ != size_) break; c = peek(); if (c == EOF) break; } #else while (c >= '0' && c <= '9') { previous = value; last_digit = static_cast(c - '0'); if (digits <= 20) ++digits; value = value * 10 + static_cast(c - '0'); ++position_; c = peek(); } #endif #ifndef SKIP_RUNTIME_CHECKS constexpr auto maximum = std::numeric_limits::max(); if ((c != EOF && c > ' ') || digits > 20 || (digits == 20 && (previous > maximum / 10 || (previous == maximum / 10 && last_digit > maximum % 10)))) { good_ = false; return *this; } std::uint64_t limit = static_cast(std::numeric_limits::max()); if constexpr (std::is_signed_v) limit += negative; else if (negative && value != 0) good_ = false; if (value > limit) good_ = false; if (!good_) return *this; #endif if constexpr (std::is_signed_v) { constexpr std::uint64_t minimum_magnitude = static_cast(std::numeric_limits::max()) + 1; // -(最小値)は評価せず、絶対値が最大値+1のときだけ最小値を使う。 result = negative ? (value == minimum_magnitude ? std::numeric_limits::min() : static_cast(-static_cast(value))) : static_cast(value); } else result = static_cast(value); return *this; } VerifyInput& operator>>(char& result) { if (!good_) return *this; const int c = skip_space(); if (c != EOF) { result = static_cast(c); ++position_; } return *this; } VerifyInput& operator>>(std::string& result) { if (!good_) return *this; int c = skip_space(); if (c == EOF) return *this; result.clear(); for (;;) { const std::size_t first = position_; while (position_ < size_ && buffer_[position_] > ' ') ++position_; result.append(reinterpret_cast(buffer_.data() + first), position_ - first); if (position_ != size_ || peek() == EOF) break; } return *this; } template requires std::is_floating_point_v VerifyInput& operator>>(T& result) { std::string token; *this >> token; if (!good_) return *this; #ifndef SKIP_RUNTIME_CHECKS char* end = nullptr; const long double value = std::strtold(token.c_str(), &end); if (end != token.c_str() + token.size()) good_ = false; else result = static_cast(value); #else result = static_cast(std::strtold(token.c_str(), nullptr)); #endif return *this; } }; // VerifyOutput(file=stdout): 非所有のFILEへ書く。破棄時にflushし、FILEは閉じない。 // << 整数/char/文字列/浮動小数点: 値を出力する。整数は64bit以下に対応する。 // fixed(digits): 浮動小数点を小数点以下digits桁で出力する(digits>=0)。 // flush(): バッファの内容をFILEへ渡す。bool / !で書き込み失敗を確認できる。 class VerifyOutput { static constexpr std::size_t buffer_size = 1 << 17; std::FILE* file_; std::array buffer_; std::size_t size_ = 0; int precision_ = 6; bool fixed_ = false, good_ = true; void write(std::string_view text) { while (!text.empty() && good_) { if (size_ == buffer_.size()) flush(); const auto count = std::min(text.size(), buffer_.size() - size_); std::memcpy(buffer_.data() + size_, text.data(), count); size_ += count; text.remove_prefix(count); } } public: explicit VerifyOutput(std::FILE* file = stdout) : file_(file) {} VerifyOutput(const VerifyOutput&) = delete; VerifyOutput& operator=(const VerifyOutput&) = delete; ~VerifyOutput() { flush(); } explicit operator bool() const noexcept { return good_; } bool operator!() const noexcept { return !good_; } VerifyOutput& fixed(int digits) { if (digits < 0) throw std::invalid_argument("negative output precision"); fixed_ = true; precision_ = digits; return *this; } void flush() noexcept { std::size_t written = 0; while (written < size_ && good_) { const auto count = std::fwrite(buffer_.data() + written, 1, size_ - written, file_); if (count == 0) good_ = false; written += count; } size_ = 0; } template requires (std::is_integral_v && sizeof(T) <= sizeof(std::uint64_t) && !std::is_same_v) VerifyOutput& operator<<(T value) { if constexpr (std::is_same_v) return *this << static_cast('0' + value); else { constexpr std::size_t capacity = std::numeric_limits::digits10 + 3; if (buffer_.size() - size_ < capacity) flush(); if (!good_) return *this; const auto end = std::to_chars(buffer_.data() + size_, buffer_.data() + size_ + capacity, value).ptr; size_ = static_cast(end - buffer_.data()); return *this; } } VerifyOutput& operator<<(char value) { if (size_ == buffer_.size()) flush(); if (good_) buffer_[size_++] = value; return *this; } VerifyOutput& operator<<(std::string_view value) { write(value); return *this; } VerifyOutput& operator<<(const std::string& value) { write(value); return *this; } VerifyOutput& operator<<(const char* value) { write(value); return *this; } template requires std::is_floating_point_v VerifyOutput& operator<<(T value) { char local[128]; const char* format = fixed_ ? "%.*Lf" : "%.*Lg"; const int count = std::snprintf(local, sizeof(local), format, precision_, static_cast(value)); if (count < 0) good_ = false; else if (static_cast(count) < sizeof(local)) write(std::string_view(local, static_cast(count))); else { std::vector text(static_cast(count) + 1); std::snprintf(text.data(), text.size(), format, precision_, static_cast(value)); write(std::string_view(text.data(), static_cast(count))); } return *this; } }; inline VerifyInput verify_in; inline VerifyOutput verify_out; #endif int main() { int n = 0; verify_in >> n; constexpr double kScale = 1000000; const auto read_point = [] { double x = 0, y = 0, z = 0; verify_in >> x >> y >> z; return Point3i{std::llround(x * kScale), std::llround(y * kScale), std::llround(z * kScale)}; }; const auto p = read_point(); std::vector points(n); for (auto &point : points) { point = read_point(); } double total = 0, rounding = 0; for (int k = 2; k < n; ++k) { for (int j = 1; j < k; ++j) { for (int i = 0; i < j; ++i) { const auto term = std::abs(*signed_plane_distance(points[i], points[j], points[k], p)) - rounding; const auto sum = total + term; rounding = (sum - total) - term; total = sum; } } } verify_out.fixed(12) << total / kScale << '\n'; }