This documentation is automatically generated by online-judge-tools/verification-helper
#include "my_template.hpp"
#include "geo/incircle.hpp"
// 非退化の整数座標三角形に対し、中心が三辺の内側にあり、各辺までの
// 符号付き距離が半径と一致することを検査する。辺長加重平均は使わない。
// 3-4-5三角形の閉形式、6通りの順序、行列式1の細長い三角形も検証。
// 整数の辺差と外積はllで表せる範囲に限定。乱数seedは固定。
namespace {
using P = Point<ll>;
using Tri = array<P, 3>;
int triangles = 0, calls = 0;
long double cross(P a, P b, P c) {
return (long double)(b.x-a.x)*(c.y-a.y) - (long double)(b.y-a.y)*(c.x-a.x);
}
template<class Real>
void verify(const Tri& base, optional<array<long double, 3>> exact = nullopt) {
long double orient = cross(base[0],base[1],base[2]);
long double scale = 1;
for (P p : base) scale = max({scale, fabsl(p.x), fabsl(p.y)});
const long double eps = 32 * numeric_limits<Real>::epsilon() * scale;
auto require = [&](bool ok, const char* label, long double observed, long double expected) {
if (ok) return;
cerr << "incircle " << label << " real_bytes=" << sizeof(Real)
<< " case=" << triangles << " got=" << setprecision(18) << observed
<< " want=" << expected << " tolerance=" << eps;
for (P p : base) cerr << " (" << p.x << ',' << p.y << ')';
cerr << '\n';
abort();
};
array<int,3> ids{0,1,2};
array<long double,3> baseline{};
do {
auto c = incircle<Real>(base[ids[0]],base[ids[1]],base[ids[2]]);
array<long double,3> result{c.O.x,c.O.y,c.r};
require(c.r > 0, "positive radius", c.r, 0);
for (int j=0;j<3;++j) {
P a=base[j], b=base[(j+1)%3];
long double dx=b.x-a.x, dy=b.y-a.y;
long double d=(dx*(result[1]-a.y) - dy*(result[0]-a.x))/hypotl(dx,dy);
if (orient<0) d=-d;
require(d>0 && fabsl(d-result[2])<=eps, "side distance", d,result[2]);
}
if (ids == array<int,3>{0,1,2}) baseline=result;
for(int j=0;j<3;++j) {
require(fabsl(result[j]-baseline[j])<=eps, "permutation", result[j],baseline[j]);
if(exact) require(fabsl(result[j]-(*exact)[j])<=eps, "3-4-5", result[j],(*exact)[j]);
}
++calls;
} while(next_permutation(ids.begin(),ids.end()));
}
void test(const Tri& t, optional<array<long double,3>> exact = nullopt) {
++triangles;
verify<double>(t,exact);
verify<long double>(t,exact);
}
} // namespace
int main() {
for (ll s : {1LL, 19LL, 100000LL}) {
for (ll y : {-1000000LL, 0LL, 1000000LL}) {
test({P{0,y},P{3*s,y},P{0,y+4*s}},array<long double,3>{1.L*s,1.L*(y+s),1.L*s});
test({P{0,y},P{-4*s,y},P{0,y+3*s}},array<long double,3>{-1.L*s,1.L*(y+s),1.L*s});
}
}
for (ll n : {10LL, 1000LL, 100000LL}) test({P{0,0},P{n,1},P{n-1,1}});
mt19937 rng(0x14c1c1);
for (int i=0;i<3000;) {
Tri t{};
for (auto& p : t) p=P{int(rng()%2001)-1000,int(rng()%2001)-1000};
if (cross(t[0],t[1],t[2])==0) continue;
test(t);
++i;
}
cout << "incircle: " << triangles << " triangles, " << calls << " calls passed\n";
}#line 1 "my_template.hpp"
// Source: https://github.com/maspypy/library/blob/main/my_template.hpp
#if defined(USE_PCH)
#include <my_template_compiled.hpp>
#if defined(__GNUC__)
#pragma GCC optimize("Ofast,unroll-loops")
#if !defined(MASPY_NON_AVX2)
#pragma GCC target("avx2,popcnt")
#endif
#endif
#else // MASPY_TEMPLATE_BODY
#include <bits/stdc++.h>
#include <cassert>
#if defined(__GNUC__)
#pragma GCC optimize("Ofast,unroll-loops")
#endif
using namespace std;
using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
template <class>
constexpr bool dependent_false = false;
template <class T>
constexpr T infty = [] {
static_assert(dependent_false<T>, "infty<T> is not defined");
return T{};
}();
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 4'040'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 4'000'000'000'000'000'000;
template <>
constexpr double infty<double> = infty<i128>;
template <>
constexpr long double infty<long double> = infty<i128>;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using pq_max = priority_queue<T>;
template <class T>
using pq_min = priority_queue<T, vector<T>, greater<T>>;
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name( \
a, vector<vector<vector<type>>>( \
b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR1_R(a) for (ll i = ll(a) - 1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = ll(a) - 1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); --i)
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload3(__VA_ARGS__, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define len(x) ll(x.size())
#define elif else if
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define stoi stoll
// require y > 0
template <typename T>
T floor(T x, T y) {
return x / y - (x % y < 0);
}
// require y > 0
template <typename T>
T ceil(T x, T y) {
return (x / y) + (x % y > 0);
}
// require y > 0
template <typename T>
T bmod(T x, T y) {
T r = x % y;
return (r < 0 ? r + y : r);
}
// require y > 0
template <typename T>
pair<T, T> divmod(T x, T y) {
T q = x / y, r = x % y;
if (r < 0) --q, r += y;
return {q, r};
}
constexpr auto TEN = [] {
array<u64, 20> A{};
A[0] = 1;
for (int i = 1; i < 20; ++i) A[i] = 10 * A[i - 1];
return A;
}();
template <typename T, typename U>
T SUM(const U &A) {
return std::accumulate(A.begin(), A.end(), T{});
}
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
template <class C, class T>
inline long long LB(const C &c, const T &x) {
return lower_bound(c.begin(), c.end(), x) - c.begin();
}
template <class C, class T>
inline long long UB(const C &c, const T &x) {
return upper_bound(c.begin(), c.end(), x) - c.begin();
}
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
template <typename T>
T POP(deque<T> &que) {
T a = que.front();
que.pop_front();
return a;
}
template <class T, class Container, class Compare>
T POP(priority_queue<T, Container, Compare> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
i128 binary_search(F check, i128 ok, i128 ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (1) {
i128 x = (ok + ng) / 2;
if (x == ok || x == ng) break;
(check(x) ? ok : ng) = x;
}
return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
FOR(iter) {
double x = (ok + ng) / 2;
(check(x) ? ok : ng) = x;
}
return (ok + ng) / 2;
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
T c = max<T>(a, b);
bool changed = (c != a);
a = c;
return changed;
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
T c = min<T>(a, b);
bool changed = (c != a);
a = c;
return changed;
}
// bit operations
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T>
T kth_bit(int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
assert(0 <= k && k < int(8 * sizeof(T)));
return x >> k & 1;
}
template <typename T>
struct all_bit {
static_assert(is_integral<T>::value);
T s;
all_bit(T s) : s(s) { assert(s >= 0); }
struct iter {
T s;
int operator*() const { return lowbit(s); }
void operator++() { s &= s - 1; }
bool operator!=(nullptr_t) const { return s; }
};
iter begin() const { return {s}; }
nullptr_t end() const { return nullptr; }
};
template <typename T>
struct all_subset {
static_assert(is_integral<T>::value);
T s;
all_subset(T s) : s(s) { assert(s >= 0); }
struct iter {
T s, t;
bool done = false;
T operator*() const { return t; }
void operator++() {
done = (t == 0);
t = (t - 1) & s;
}
bool operator!=(nullptr_t) const { return !done; }
};
iter begin() const { return {s, s}; }
nullptr_t end() const { return nullptr; }
};
constexpr u64 full_mask(int n) {
assert(0 <= n && n <= 64);
return n == 64 ? -1ULL : (1ULL << n) - 1;
}
u64 bit_reverse(u64 x) {
x = ((x & 0x5555555555555555ULL) << 1) | ((x >> 1) & 0x5555555555555555ULL);
x = ((x & 0x3333333333333333ULL) << 2) | ((x >> 2) & 0x3333333333333333ULL);
x = ((x & 0x0f0f0f0f0f0f0f0fULL) << 4) | ((x >> 4) & 0x0f0f0f0f0f0f0f0fULL);
x = ((x & 0x00ff00ff00ff00ffULL) << 8) | ((x >> 8) & 0x00ff00ff00ff00ffULL);
x = ((x & 0x0000ffff0000ffffULL) << 16) | ((x >> 16) & 0x0000ffff0000ffffULL);
x = (x << 32) | (x >> 32);
return x;
}
// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
vc<int> A(S.size());
FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
return A;
}
template <typename T, typename U>
vc<T> cumsum(const vc<U> &A, int off = 1) {
int N = A.size();
vc<T> B(N + 1);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
if (off == 0) B.erase(B.begin());
return B;
}
// stable sort
template <typename T>
vc<int> argsort(const vc<T> &A) {
vc<int> ids(len(A));
iota(all(ids), 0);
sort(all(ids),
[&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
return ids;
}
// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
vc<T> B(len(I));
FOR(i, len(I)) B[i] = A[I[i]];
return B;
}
template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &...others) {
size_t n = first.size() + (others.size() + ... + 0);
if (n > first.capacity()) {
first.reserve(max(n, 2 * first.capacity()));
}
(first.insert(first.end(), others.begin(), others.end()), ...);
}
// i128
template <class T, enable_if_t<is_same_v<T, i128>, int> = 0>
constexpr i128 abs(T x) {
return x < 0 ? -x : x;
}
constexpr i128 gcd(i128 a, i128 b) {
while (b != 0) {
i128 c = a % b;
a = b, b = c;
}
return abs(a);
}
#endif // MASPY_TEMPLATE_BODY
#line 1 "geo/incircle.hpp"
// Source: https://github.com/maspypy/library/blob/main/geo/incircle.hpp
#line 1 "geo/base.hpp"
// Source: https://github.com/maspypy/library/blob/main/geo/base.hpp
template <typename T>
struct Point {
T x, y;
Point() : x(0), y(0) {}
template <typename A, typename B>
Point(A x, B y) : x(x), y(y) {}
template <typename A, typename B>
Point(pair<A, B> p) : x(p.fi), y(p.se) {}
template <typename U>
Point(Point<U> p) : x(p.x), y(p.y) {
static_assert(!is_integral_v<T> || is_integral_v<U>);
}
Point operator+=(const Point p) {
x += p.x, y += p.y;
return *this;
}
Point operator-=(const Point p) {
x -= p.x, y -= p.y;
return *this;
}
Point operator+(Point p) const { return {x + p.x, y + p.y}; }
Point operator-(Point p) const { return {x - p.x, y - p.y}; }
bool operator==(Point p) const { return x == p.x && y == p.y; }
bool operator!=(Point p) const { return x != p.x || y != p.y; }
Point operator-() const { return {-x, -y}; }
Point operator*(T t) const { return {x * t, y * t}; }
Point operator/(T t) const { return {x / t, y / t}; }
bool operator<(Point p) const {
if (x != p.x) return x < p.x;
return y < p.y;
}
T dot(const Point& other) const { return x * other.x + y * other.y; }
T det(const Point& other) const { return x * other.y - y * other.x; }
double norm() { return sqrtl(x * x + y * y); }
double angle() { return atan2(y, x); }
Point rotate(double theta) {
static_assert(!is_integral<T>::value);
double c = cos(theta), s = sin(theta);
return Point{c * x - s * y, s * x + c * y};
}
Point rot90(bool ccw) { return (ccw ? Point{-y, x} : Point{y, -x}); }
};
#ifdef FASTIO
template <typename T>
void rd(Point<T>& p) {
fastio::rd(p.x), fastio::rd(p.y);
}
template <typename T>
void wt(Point<T>& p) {
fastio::wt(p.x);
fastio::wt(' ');
fastio::wt(p.y);
}
#endif
// A -> B -> C と進むときに、左に曲がるならば +1、右に曲がるならば -1
template <typename T>
int ccw(Point<T> A, Point<T> B, Point<T> C) {
T x = (B - A).det(C - A);
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
template <typename REAL, typename T, typename U>
REAL distance(Point<T> A, Point<U> B) {
REAL dx = REAL(A.x) - REAL(B.x);
REAL dy = REAL(A.y) - REAL(B.y);
return sqrt(dx * dx + dy * dy);
}
// ax+by+c
template <typename T>
struct Line {
T a, b, c;
Line(T a, T b, T c) : a(a), b(b), c(c) { assert(a != 0 || b != 0); }
Line(Point<T> A, Point<T> B) {
a = A.y - B.y, b = B.x - A.x, c = A.x * B.y - A.y * B.x;
assert(a != 0 || b != 0);
}
Line(T x1, T y1, T x2, T y2) : Line(Point<T>(x1, y1), Point<T>(x2, y2)) {}
template <typename U>
auto eval(Point<U> P) const {
return eval(P.x, P.y);
}
template <typename U>
auto eval(U x, U y) const {
return a * x + b * y + c;
}
// 同じ直線が同じ a,b,c で表現されるようにする
void normalize() {
static_assert(is_same_v<T, int> || is_same_v<T, long long>);
T g = gcd(gcd(abs(a), abs(b)), abs(c));
a /= g, b /= g, c /= g;
if (b < 0) {
a = -a, b = -b, c = -c;
}
if (b == 0 && a < 0) {
a = -a, b = -b, c = -c;
}
}
bool is_parallel(Line other) { return a * other.b - b * other.a == 0; }
bool is_orthogonal(Line other) { return a * other.a + b * other.b == 0; }
bool is_same(Line other) {
if (a * other.b != b * other.a) return 0;
if (a * other.c != c * other.a) return 0;
if (b * other.c != c * other.b) return 0;
return 1;
}
};
template <typename T>
struct Segment {
Point<T> A, B;
Segment(Point<T> A, Point<T> B) : A(A), B(B) { assert(A != B); }
Segment(T x1, T y1, T x2, T y2)
: Segment(Point<T>(x1, y1), Point<T>(x2, y2)) {}
bool contain(Point<T> C) {
T det = (C - A).det(B - A);
if (det != 0) return 0;
return (C - A).dot(B - A) >= 0 && (C - B).dot(A - B) >= 0;
}
Line<T> to_line() { return Line(A, B); }
};
template <typename REAL>
struct Circle {
Point<REAL> O;
REAL r;
Circle() {}
Circle(Point<REAL> O, REAL r) : O(O), r(r) {}
Circle(REAL x, REAL y, REAL r) : O(x, y), r(r) {}
template <typename T>
bool contain(Point<T> p) {
REAL dx = p.x - O.x, dy = p.y - O.y;
return dx * dx + dy * dy <= r * r;
}
};
#line 1 "geo/triangle_area.hpp"
// Source: https://github.com/maspypy/library/blob/main/geo/triangle_area.hpp
template <typename REAL, typename T>
REAL triangle_area(Point<T> A, Point<T> B, Point<T> C) {
return abs((B - A).det(C - A)) * 0.5;
}
#line 4 "geo/incircle.hpp"
template <typename REAL, typename T>
Circle<REAL> incircle(Point<T> A, Point<T> B, Point<T> C) {
REAL a = distance<REAL, T, T>(B, C);
REAL b = distance<REAL, T, T>(C, A);
REAL c = distance<REAL, T, T>(A, B);
REAL x = (a * A.x + b * B.x + c * C.x) / (a + b + c);
REAL y = (a * A.y + b * B.y + c * C.y) / (a + b + c);
REAL r = 2 * triangle_area<REAL>(A, B, C) / (a + b + c);
return Circle<REAL>(x, y, r);
}
#line 3 "test/5_ai_generated_test/incircle_tangency_permutations.test.cpp"
// 非退化の整数座標三角形に対し、中心が三辺の内側にあり、各辺までの
// 符号付き距離が半径と一致することを検査する。辺長加重平均は使わない。
// 3-4-5三角形の閉形式、6通りの順序、行列式1の細長い三角形も検証。
// 整数の辺差と外積はllで表せる範囲に限定。乱数seedは固定。
namespace {
using P = Point<ll>;
using Tri = array<P, 3>;
int triangles = 0, calls = 0;
long double cross(P a, P b, P c) {
return (long double)(b.x-a.x)*(c.y-a.y) - (long double)(b.y-a.y)*(c.x-a.x);
}
template<class Real>
void verify(const Tri& base, optional<array<long double, 3>> exact = nullopt) {
long double orient = cross(base[0],base[1],base[2]);
long double scale = 1;
for (P p : base) scale = max({scale, fabsl(p.x), fabsl(p.y)});
const long double eps = 32 * numeric_limits<Real>::epsilon() * scale;
auto require = [&](bool ok, const char* label, long double observed, long double expected) {
if (ok) return;
cerr << "incircle " << label << " real_bytes=" << sizeof(Real)
<< " case=" << triangles << " got=" << setprecision(18) << observed
<< " want=" << expected << " tolerance=" << eps;
for (P p : base) cerr << " (" << p.x << ',' << p.y << ')';
cerr << '\n';
abort();
};
array<int,3> ids{0,1,2};
array<long double,3> baseline{};
do {
auto c = incircle<Real>(base[ids[0]],base[ids[1]],base[ids[2]]);
array<long double,3> result{c.O.x,c.O.y,c.r};
require(c.r > 0, "positive radius", c.r, 0);
for (int j=0;j<3;++j) {
P a=base[j], b=base[(j+1)%3];
long double dx=b.x-a.x, dy=b.y-a.y;
long double d=(dx*(result[1]-a.y) - dy*(result[0]-a.x))/hypotl(dx,dy);
if (orient<0) d=-d;
require(d>0 && fabsl(d-result[2])<=eps, "side distance", d,result[2]);
}
if (ids == array<int,3>{0,1,2}) baseline=result;
for(int j=0;j<3;++j) {
require(fabsl(result[j]-baseline[j])<=eps, "permutation", result[j],baseline[j]);
if(exact) require(fabsl(result[j]-(*exact)[j])<=eps, "3-4-5", result[j],(*exact)[j]);
}
++calls;
} while(next_permutation(ids.begin(),ids.end()));
}
void test(const Tri& t, optional<array<long double,3>> exact = nullopt) {
++triangles;
verify<double>(t,exact);
verify<long double>(t,exact);
}
} // namespace
int main() {
for (ll s : {1LL, 19LL, 100000LL}) {
for (ll y : {-1000000LL, 0LL, 1000000LL}) {
test({P{0,y},P{3*s,y},P{0,y+4*s}},array<long double,3>{1.L*s,1.L*(y+s),1.L*s});
test({P{0,y},P{-4*s,y},P{0,y+3*s}},array<long double,3>{-1.L*s,1.L*(y+s),1.L*s});
}
}
for (ll n : {10LL, 1000LL, 100000LL}) test({P{0,0},P{n,1},P{n-1,1}});
mt19937 rng(0x14c1c1);
for (int i=0;i<3000;) {
Tri t{};
for (auto& p : t) p=P{int(rng()%2001)-1000,int(rng()%2001)-1000};
if (cross(t[0],t[1],t[2])==0) continue;
test(t);
++i;
}
cout << "incircle: " << triangles << " triangles, " << calls << " calls passed\n";
}