#ifdef __LOCAL #include // プリコンパイル済みヘッダ ~/local/include/mytemplate.hpp.gch #else #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #endif using namespace std; // #pragma GCC target "no-avx" // gcc12.2のバグ回避用。boost使用時はコメントアウトの事 // #include // using bint = boost::multiprecision::cpp_int; using ll = long long; using vl = vector; using vvl = vector; using vb = vector; using vvb = vector; using vd = vector; using vvd = vector; using vc = vector; using vvc = vector; using pll = pair; template using min_priority_queue = priority_queue, greater>; #define REP1(i, n) REP3(i, 0, n, 1) #define REP2(i, s, n) REP3(i, s, n, 1) #define REP3(i, s, n, d) for (ll i = (ll)(s); i < (ll)(n); i += (d)) #define REP_OVERLOAD(e1, e2, e3, e4, NAME, ...) NAME #define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__) #define DEP1(i, n) DEP3(i, n, -1, 1) #define DEP2(i, n, s) DEP3(i, n, s, 1) #define DEP3(i, n, s, d) for (ll i = (ll)(n); (ll)(s) < i; i -= (d)) #define DEP_OVERLOAD(e1, e2, e3, e4, NAME, ...) NAME #define dep(...) DEP_OVERLOAD(__VA_ARGS__, DEP3, DEP2, DEP1)(__VA_ARGS__) #define fore(e, a) for (auto&& e : (a)) #define len(a) (ll)(a).size() #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define pb push_back #define eb emplace_back #define fi first #define se second #define th third #define fo fourth /********* C O N S T A N T **********/ constexpr ll INF = LONG_LONG_MAX / 2 - 10000LL; // 4,611,686,018,427,377,903 ~= 4.6e+18, 19桁 const double PI = acos(-1); /********* 1 2 8 b i t 整 数 **********/ using int128 = __int128_t; istream& operator>>(istream& stream, int128& val); ostream& operator<<(ostream& stream, const int128& val_); /********* P A I R **********/ // 単項演算子(+ -) template pair operator+(pair a); template pair operator-(pair a); // 複合代入演算子 template pair& operator+=(pair& a, const pair b); template pair& operator-=(pair& a, const pair b); template pair& operator*=(pair& a, const pair b); template pair& operator/=(pair& a, const pair b); template pair& operator%=(pair& a, const pair b); template pair& operator+=(pair& a, const V b); template pair& operator-=(pair& a, const V b); template pair& operator*=(pair& a, const V b); template pair& operator/=(pair& a, const V b); template pair& operator%=(pair& a, const V b); // 算術演算子 template pair operator+(pair a, const V& b); template pair operator-(pair a, const V& b); template pair operator*(pair a, const V& b); template pair operator/(pair a, const V& b); template pair operator%(pair a, const V& b); // 入出力 template istream& operator>>(istream& stream, pair& a); template ostream& operator<<(ostream& stream, const pair& a); /********* V E C T O R **********/ // 単項演算子(+ -) template vector operator+(vector a); template vector operator-(vector a); // 複合代入演算子 template vector& operator+=(vector& a, const vector b); template vector& operator-=(vector& a, const vector b); template vector& operator*=(vector& a, const vector b); template vector& operator/=(vector& a, const vector b); template vector& operator%=(vector& a, const vector b); template vector& operator+=(vector& a, const U b); template vector& operator-=(vector& a, const U b); template vector& operator*=(vector& a, const U b); template vector& operator/=(vector& a, const U b); template vector& operator%=(vector& a, const U b); // 算術演算子 template vector operator+(vector a, const U& b); template vector operator-(vector a, const U& b); template vector operator*(vector a, const U& b); template vector operator/(vector a, const U& b); template vector operator%(vector a, const U& b); // 入出力 template istream& operator>>(istream& stream, vector& a); template ostream& operator<<(ostream& stream, const vector& v); template ostream& operator<<(ostream& stream, const vector>& vv); // 計算 template T Sum(const vector& v); template T Max(const vector& v); template T Min(const vector& v); template ll Argmax(const vector& v); template ll Argmin(const vector& v); template bool Contains(const vector& v, const U& a); template void Unique(vector& v); // 生成 template vector make_vector(int n, U v); template auto make_vector(int n, Args... args) { auto val = make_vector(args...); return make_vector(n, move(val)); } /********* S E T **********/ template istream& operator>>(istream& stream, set& st); template ostream& operator<<(ostream& stream, const set& st); template T Max(const set& st); template T Min(const set& st); /********* M U L T I S E T **********/ template istream& operator>>(istream& stream, multiset& st); template ostream& operator<<(ostream& stream, const multiset& st); template T Max(const multiset& st); template T Min(const multiset& st); template bool EraseOne(multiset& st, const T& x); /********* C H A R **********/ bool IsUpper(const char& c); bool IsLower(const char& c); char ToUpper(const char& c); char ToLower(const char& c); /********* S T R I N G **********/ string ToUpper(const string& s); string ToLower(const string& s); bool IsPalindrome(const string& S); /********* T R I O **********/ template struct trio { T1 first; T2 second; T3 third; // コンストラクタ・代入演算子 trio(); trio(const T1& x); trio(const T1& x, const T2& y, const T3& z); trio(const trio& t); trio& operator=(const trio& t); // 比較演算子 bool operator<(const trio& t) const; bool operator==(const trio& t) const; bool operator!=(const trio& t) const; bool operator>(const trio& t) const; bool operator<=(const trio& t) const; bool operator>=(const trio& t) const; // 単項演算子(+ -) trio operator+() const; trio operator-() const; // 入出力 template friend istream& operator>>(istream&, trio&); template friend ostream& operator<<(ostream&, const trio&); // `tie(a, b, c) = tll{3, 5, 7}`をするのに必要(コンパイラが警告?を出すのでここで定義) operator tuple() { return tie(first, second, third); } }; // 複合代入演算子 template trio& operator+=(trio& a, const trio b); template trio& operator-=(trio& a, const trio b); template trio& operator*=(trio& a, const trio b); template trio& operator/=(trio& a, const trio b); template trio& operator%=(trio& a, const trio b); template trio& operator+=(trio& a, const U b); template trio& operator-=(trio& a, const U b); template trio& operator*=(trio& a, const U b); template trio& operator/=(trio& a, const U b); template trio& operator%=(trio& a, const U b); // 算術演算子 template trio operator+(trio a, const U& b); template trio operator-(trio a, const U& b); template trio operator*(trio a, const U& b); template trio operator/(trio a, const U& b); template trio operator%(trio a, const U& b); using tll = trio; /********* Q U A R T E T **********/ template struct quartet { T1 first; T2 second; T3 third; T4 fourth; // コンストラクタ・代入演算子 quartet(); quartet(const T1& x); quartet(const T1& x, const T2& y, const T3& z, const T4& w); quartet(const quartet& t); quartet& operator=(const quartet& t); // 比較演算子 bool operator<(const quartet& t) const; bool operator==(const quartet& t) const; bool operator!=(const quartet& other) const; bool operator>(const quartet& other) const; bool operator<=(const quartet& other) const; bool operator>=(const quartet& other) const; // 単項演算子(+ -) quartet operator+() const; quartet operator-() const; // 入出力 template friend istream& operator>>(istream&, quartet&); template friend ostream& operator<<(ostream&, const quartet&); // `tie(a, b, c, d) = qll{3, 5, 7, 9}`をするのに必要(コンパイラが警告?を出すのでここで定義) operator tuple() { return tie(first, second, third, fourth); } }; // 複合代入演算子 template quartet& operator+=(quartet& a, const quartet b); template quartet& operator-=(quartet& a, const quartet b); template quartet& operator*=(quartet& a, const quartet b); template quartet& operator/=(quartet& a, const quartet b); template quartet& operator%=(quartet& a, const quartet b); template quartet& operator+=(quartet& a, const U b); template quartet& operator-=(quartet& a, const U b); template quartet& operator*=(quartet& a, const U b); template quartet& operator/=(quartet& a, const U b); template quartet& operator%=(quartet& a, const U b); // 算術演算子 template quartet operator+(quartet a, const U& b); template quartet operator-(quartet a, const U& b); template quartet operator*(quartet a, const U& b); template quartet operator/(quartet a, const U& b); template quartet operator%(quartet a, const U& b); using qll = quartet; /********* R A N D O M **********/ struct Random { mt19937_64 rnd; Random(); Random(ll seed); ll randint(ll a, ll b); double randreal(double a, double b); char randchar(char a, char b); template void shuffle(vector& vec); }; /********* T I M E R **********/ struct Timer { int time_limit; int start_time; Timer(double time_limit_sec); bool is_time_up() const; double get_elapsed_ratio() const; }; /********* P R I N T **********/ template void print(const T& e); template void print(const H& h, const T&... t); template void End(const T&... t); /********* D E B U G **********/ #ifdef __LOCAL #define debug(...) \ if (DEBUG) do { \ cout << '[' << #__VA_ARGS__ << "] "; \ debug_(__VA_ARGS__); \ } while (0) #else #define debug(...) #endif void dbg_(const long long& e); template void dbg_(const T& e); template void dbg_(const pair& p); template void dbg_(const trio& t); template void dbg_(const quartet& t); template void debug_(const T& e); template void debug_(const vector& v); template void debug_(const vector>& vv); template void debug_(const map& mp); template void debug_(const vector>& vm); template void debug_(const set& st); template void debug_(const multiset& st); template void debug_(const vector>& vs); template void debug_(const vector>& vs); template void debug_(const H& h, const T&... t); /********* O T H E R S **********/ template bool chmin(T& a, U b); template bool chmax(T& a, U b); template T Mod(const T& a, const U& m); template T Ceil(const T& x, const U& y); template T Floor(const T& x, const U& y); template ll Sign(const T& x); ll Isqrt(ll n); ll Comb(ll n, ll r); template T Pow(T x, ll n); template T Pow(T x, ll n, T mod); template T Aseries(T a, T d, ll n); template T Gseries(T a, T r, ll n); bool Bit(ll b, int i); ll Popcount(ll b); ll Mask(ll n); ll Mask(ll n, ll m); pll GetOverlap(ll l1, ll r1, ll l2, ll r2); bool IsOverlap(ll l1, ll r1, ll l2, ll r2); void PrintYesNo(bool b); /********* M O D **********/ // #include // https://atcoder.github.io/ac-library/production/document_ja/ // using namespace atcoder; // using mint = modint998244353; // modint1000000007; // istream& operator >>(istream& stream, mint& e) {ll n; stream >> n; e = n; return stream; } // ostream& operator <<(ostream& stream, const mint& e) { stream << e.val(); return stream; } // using vm = vector; // using vvm = vector; /***************************************/ constexpr bool DEBUG = true; /************** デ カ ル ト 座 標 構 造 体 **************/ template struct Point { T x, y; // コンストラクタ・代入演算子 Point() { x = T(); y = T(); } Point(const T& x_) : x(x_), y(x_) {} // 整数型との四則演算で必要 Point(const T& x_, const T& y_) : x(x_), y(y_) {} Point(const pair p) : x(p.first), y(p.second) {} Point(const Point& p) { x = p.x; y = p.y; } // コピーコンストラクタ Point& operator=(const Point& p) { x = p.x; y = p.y; return *this; } // // 比較演算子(Pairと同じ) bool operator<(const Point& p) const { return tie(x, y) < tie(p.x, p.y); } bool operator==(const Point& p) const { return tie(x, y) == tie(p.x, p.y); } bool operator!=(const Point& p) const { return !(*this == p); } bool operator>(const Point& p) const { return p < *this; } bool operator<=(const Point& p) const { return !(*this > p); } bool operator>=(const Point& p) const { return !(*this < p); } // 単項演算子(+ -) Point operator+() const { return *this; } Point operator-() const { return Point(-x, -y); } // 複合代入演算子 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*=(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%=(const Point& p) { x %= p.x; y %= p.y; return *this; } // 算術演算子(+ - * /) friend Point operator+(const Point& lhs, const Point& rhs) { return Point(lhs) += rhs; } friend Point operator-(const Point& lhs, const Point& rhs) { return Point(lhs) -= rhs; } friend Point operator*(const Point& lhs, const Point& rhs) { return Point(lhs) *= rhs; } friend Point operator/(const Point& lhs, const Point& rhs) { return Point(lhs) /= rhs; } friend Point operator%(const Point& lhs, const Point& rhs) { return Point(lhs) %= rhs; } // // 二乗 (x^2, y^2) Point sq() const { return Point(x * x, y * y); } // 絶対値 (|x|, |y|) Point abs() const { return Point(std::abs(x), std::abs(y)); } // 大きさの二乗 x^2 + y^2 T mag2() const { return x * x + y * y; } // // 入出力、型変換 friend istream& operator>>(istream& stream, Point& p) { return stream >> p.x >> p.y; } friend ostream& operator<<(ostream& stream, const Point& p) { return stream << p.x << ' ' << p.y; } template operator pair() const { pair p{x, y}; return p; } }; // 内積 x1x2 + y1y2 template T Dot(const Point& p1, const Point& p2) { return p1.x * p2.x + p1.y * p2.y; } // クロス積 x1y2 - x2y1 template T Cross(const Point& p1, const Point& p2) { return p1.x * p2.y - p2.x * p1.y; } // 距離の二乗 (x2-x1)^2 + (y2-y1)^2 template T Dist2(const Point& p1, const Point& p2) { return (p2 - p1).mag2(); } // p1ベクトルに対するp2ベクトルのなす角[0, 2π) template T Angle(const Point& p1, const Point& p2) { T cos = Dot(p1, p2) / sqrt(p1.mag2() * p2.mag2()); T theta = acos(cos); if (Cross(p1, p2) >= T(0)) return theta; else return 2 * acos((T)(-1)) - theta; } // pベクトルの偏角 [0, 2π) template T Angle(const Point& p) { return Angle(Point(1, 0), p); } // pベクトルを theta [rad]回転 template Point Rotate(const Point& p, T theta) { T x = cos(theta) * p.x - sin(theta) * p.y; T y = sin(theta) * p.x + cos(theta) * p.y; return Point(x, y); } // rad -> deg template T Degrees(T rad) { return rad / acos((T)(-1)) * 180; } // deg -> rad template T Radians(T deg) { return acos((T)(-1)) * deg / 180; } // ベクトル`p1`に対して`p2`のなす角θが 0° < θ < 90° を満たすか template bool IsAngleAcute(const Point& p1, const Point& p2) { auto zero_vector = Point(); assert(p1 != zero_vector && p2 != zero_vector); return (Dot(p1, p2) > T(0)) && (Cross(p1, p2) > T(0)); } // ベクトル`p1`に対して`p2`のなす角θが θ = 90° を満たすか template bool IsAngle90(const Point& p1, const Point& p2) { auto zero_vector = Point(); assert(p1 != zero_vector && p2 != zero_vector); return (Dot(p1, p2) == T(0)) && (Cross(p1, p2) > T(0)); } // ベクトルのなす角のうち小さい方(0: 0°, 1: 鋭角, 2: 90°, 3: 鈍角, 4: 180°) template ll ClassifyVectorAngle(const Point& p1, const Point& p2) { auto zero_vector = Point(); assert(p1 != zero_vector && p2 != zero_vector); if (Dot(p1, p2) > T(0)) return Cross(p1, p2) == T(0) ? 0 : 1; else if (Dot(p1, p2) == T(0)) return 2; else return Cross(p1, p2) == T(0) ? 4 : 3; } // ベクトルpがベクトルp1からp2へCCWで回る角のどこにいるか(-1: 外側、0: 境界, 1: 内側) // 参考:JAG2016国内模擬E, JAG2013春F template ll ClassifyVectorPositionInAngle(const Point& p, const Point& p1, const Point& p2) { auto zero_vector = Point(); assert(p != zero_vector && p1 != zero_vector && p2 != zero_vector); if (ClassifyVectorAngle(p1, p2) == 0) { // p1とp2のなす角が0の時(コーナーケース) return ClassifyVectorAngle(p, p1) == 0 ? 0 : -1; } else { ll res = Sign(Cross(p1, p)) - Sign(Cross(p2, p)) - Sign(Cross(p1, p2)); return Sign(res); } } // 偏角による比較関数. 原点(0, 0)は不可. Arg(p1) < Arg(p2)ならtrue. template bool CompareByArg(const Point& p1, const Point& p2) { assert((p1.x != T(0) || p1.y != T(0)) && (p2.x != T(0) || p2.y != T(0))); if (Cross(p1, p2) == T(0) && Dot(p1, p2) > T(0)) { // なす角が0°のとき、ソート結果を一意にするためベクトルの大きさで比較する return p1.mag2() < p2.mag2(); } else { auto& [x1, y1] = p1; auto& [x2, y2] = p2; if (y1 == T(0) && y2 == T(0)) return x1 > T(0) && x2 < T(0); // p1が0°, p2が180°でtrue else if (y1 >= T(0) && y2 < T(0)) return true; // p1 <= 180° < p2 else if (y1 < T(0) && y2 >= T(0)) return false; // p2 <= 180° < p1 else return x1 * y2 > x2 * y1; // p1, p2 <= 180° or 180° < p1, p2 } } // 凸多角形かどうか template bool IsConvex(const vector>& vec) { ll n = vec.size(); for (ll i = 0; i < n; i++) { ll j = (i + 1) % n; ll k = (i - 1 + n) % n; if (Cross(vec[j] - vec[i], vec[k] - vec[i]) <= T(0)) return false; } return true; } int main() { cin.tie(nullptr); cout << fixed << setprecision(10); ll T; cin >> T; rep(t, T) { ll D; Point p; cin >> D >> p; ll ans = 0; { Point e = {-p.y, p.x}; ll g = gcd(p.y, p.x); e /= g; ll left = 0; ll right = Pow(10LL, 9) + 5; while (right - left > 1) { ll mid = (left + right) / 2; auto vec = p + e * mid; if (0 <= vec.x && vec.x <= D && 0 <= vec.y && vec.y <= D) left = mid; else right = mid; } debug(left); debug(left * e); debug(p + left * e); chmax(ans, abs(Cross(p, p + left * e))); } { Point e = {-p.y, p.x}; ll left = -Pow(10LL, 9) - 5; ll right = 0; while (right - left > 1) { ll mid = (left + right) / 2; auto vec = p + e * mid; if (0 <= vec.x && vec.x <= D && 0 <= vec.y && vec.y <= D) right = mid; else left = mid; } chmax(ans, abs(Cross(p, p + right * e))); } print(ans); } } /********* 1 2 8 b i t 整 数 *********/ // 128bit整数を入力. LONG_LONG_MAX超過/LONG_LONG_MIN未満もOK. istream& operator>>(istream& stream, int128& val) { string s; stream >> s; assert(s.size() > 0); val = 0; bool is_minus = false; for (int i = 0; i < (int)s.size(); i++) { if (i == 0 && s[i] == '-') { assert(s.size() > 1); is_minus = true; } else { assert('0' <= s[i] && s[i] <= '9'); val *= 10; val += s[i] - '0'; } } if (is_minus) val *= -1; return stream; } // 128bit整数を出力. ostream& operator<<(ostream& stream, const int128& val_) { auto val(val_); if (val == 0) return stream << 0; if (val < 0) { stream << '-'; val *= -1; } string s; while (val > 0) { s += (val % 10) + '0'; val /= 10; } reverse(all(s)); return stream << s; } /********* P A I R **********/ /* 単項演算子(+ -) */ template pair operator+(pair a) { return a; } template pair operator-(pair a) { return a * (-1); } /* 複合代入演算子 (pair) */ template pair& operator+=(pair& a, const pair b) { a.fi += b.fi; a.se += b.se; return a; } template pair& operator-=(pair& a, const pair b) { a.fi -= b.fi; a.se -= b.se; return a; } template pair& operator*=(pair& a, const pair b) { a.fi *= b.fi; a.se *= b.se; return a; } template pair& operator/=(pair& a, const pair b) { a.fi /= b.fi; a.se /= b.se; return a; } template pair& operator%=(pair& a, const pair b) { a.fi %= b.fi; a.se %= b.se; return a; } template pair& operator+=(pair& a, const V b) { a.fi += b; a.se += b; return a; } template pair& operator-=(pair& a, const V b) { a.fi -= b; a.se -= b; return a; } template pair& operator*=(pair& a, const V b) { a.fi *= b; a.se *= b; return a; } template pair& operator/=(pair& a, const V b) { a.fi /= b; a.se /= b; return a; } template pair& operator%=(pair& a, const V b) { a.fi %= b; a.se %= b; return a; } /* 算術演算子 (pair) */ template pair operator+(pair a, const V& b) { return a += b; } template pair operator-(pair a, const V& b) { return a -= b; } template pair operator*(pair a, const V& b) { return a *= b; } template pair operator/(pair a, const V& b) { return a /= b; } template pair operator%(pair a, const V& b) { return a %= b; } // 入出力 template istream& operator>>(istream& stream, pair& a) { stream >> a.fi >> a.se; return stream; } template ostream& operator<<(ostream& stream, const pair& a) { stream << a.fi << " " << a.se; return stream; } /********* V E C T O R **********/ /* 単項演算子(+ -) */ template vector operator+(vector a) { return a; } template vector operator-(vector a) { return a * (-1); } /* 複合代入演算子 (vector) */ template vector& operator+=(vector& a, const vector b) { assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] += b[i]; return a; } template vector& operator-=(vector& a, const vector b) { assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] -= b[i]; return a; } template vector& operator*=(vector& a, const vector b) { assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] *= b[i]; return a; } template vector& operator/=(vector& a, const vector b) { assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] /= b[i]; return a; } template vector& operator%=(vector& a, const vector b) { assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] %= b[i]; return a; } template vector& operator+=(vector& a, const U b) { fore(e, a) e += b; return a; } template vector& operator-=(vector& a, const U b) { fore(e, a) e -= b; return a; } template vector& operator*=(vector& a, const U b) { fore(e, a) e *= b; return a; } template vector& operator/=(vector& a, const U b) { fore(e, a) e /= b; return a; } template vector& operator%=(vector& a, const U b) { fore(e, a) e %= b; return a; } /* 算術演算子 (vector) */ template vector operator+(vector a, const U& b) { return a += b; } template vector operator-(vector a, const U& b) { return a -= b; } template vector operator*(vector a, const U& b) { return a *= b; } template vector operator/(vector a, const U& b) { return a /= b; } template vector operator%(vector a, const U& b) { return a %= b; } /* 入出力 (vector) */ // ベクトルを入力 template istream& operator>>(istream& stream, vector& a) { fore(e, a) stream >> e; return stream; } // ベクトルを空白区切りで出力 template ostream& operator<<(ostream& stream, const vector& v) { if (v.size()) { stream << v[0]; rep(i, 1, v.size()) cout << " " << v[i]; } return stream; } // 2次元ベクトルを空白/改行区切りで出力 template ostream& operator<<(ostream& stream, const vector>& vv) { if (vv.size()) { stream << vv[0]; rep(i, 1, vv.size()) cout << '\n' << vv[i]; } return stream; } /* 計算 (vector) */ // 合計。v.size() == 0 のとき T() を返す template T Sum(const vector& v) { return reduce(all(v)); } template T Max(const vector& v) { assert(v.size()); return *max_element(all(v)); } template T Min(const vector& v) { assert(v.size()); return *min_element(all(v)); } // 最大要素を指す最初の位置を返す(0-origin), O(N). template ll Argmax(const vector& v) { assert(v.size()); return max_element(all(v)) - v.begin(); } // 最小要素を指す最初の位置を返す(0-origin), O(N). template ll Argmin(const vector& v) { assert(v.size()); return min_element(all(v)) - v.begin(); } // 要素`a`がベクトル`v`に1つ以上含まれるかどうか template bool Contains(const vector& v, const U& a) { return find(all(v), a) != v.end(); } // ソートされたユニーク値のベクトルにinplaceで書き換える, O(NlogN). template void Unique(vector& v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } /* 生成 (vector) */ template vector make_vector(int n, U v) { return vector(n, v); } /********* S E T **********/ // 一要素を入力 template istream& operator>>(istream& stream, set& st) { T e; stream >> e; st.insert(e); return stream; } // 空白区切りで出力 template ostream& operator<<(ostream& stream, const set& st) { if (st.size()) { auto it = st.begin(); stream << *it++; for (; it != st.end(); it++) cout << " " << *it; } return stream; } template T Max(const set& st) { assert(st.size()); return *prev(st.end()); } template T Min(const set& st) { assert(st.size()); return *st.begin(); } /********* M U L T I S E T **********/ // 一要素を入力 template istream& operator>>(istream& stream, multiset& st) { T e; stream >> e; st.insert(e); return stream; } // 空白区切りで出力 template ostream& operator<<(ostream& stream, const multiset& st) { if (st.size()) { auto it = st.begin(); stream << *it++; for (; it != st.end(); it++) cout << " " << *it; } return stream; } template T Max(const multiset& st) { assert(st.size()); return *prev(st.end()); } template T Min(const multiset& st) { assert(st.size()); return *st.begin(); } // 要素xを1つ削除. 消せたらtrueを返す. template bool EraseOne(multiset& st, const T& x) { auto it = st.find(x); if (it != st.end()) { st.erase(it); return true; } else return false; } /********* C H A R **********/ // 大文字かどうか bool IsUpper(const char& c) { return isupper(c) > 0; } // 小文字かどうか bool IsLower(const char& c) { return islower(c) > 0; } // 大文字へ変換 char ToUpper(const char& c) { string s{c}, t; t.resize(s.size()); std::transform(all(s), t.begin(), ::toupper); return t[0]; } // 小文字へ変換 char ToLower(const char& c) { string s{c}, t; t.resize(s.size()); std::transform(all(s), t.begin(), ::tolower); return t[0]; } /********* S T R I N G **********/ // 大文字へ変換 string ToUpper(const string& s) { string t; t.resize(s.size()); std::transform(all(s), t.begin(), ::toupper); return t; } // 小文字へ変換 string ToLower(const string& s) { string t; t.resize(s.size()); std::transform(all(s), t.begin(), ::tolower); return t; } // 回文判定, O(N). 空文字の返り値はTrue. bool IsPalindrome(const string& S) { ll N = len(S); for (ll i = 0; 2 * i < N; i++) if (S[i] != S[N - i - 1]) return false; return true; } /********* T R I O **********/ // コンストラクタ・代入演算子 template trio::trio() { first = T1(); second = T2(); third = T3(); } template trio::trio(const T1& x) : first(x), second(x), third(x) {} template trio::trio(const T1& x, const T2& y, const T3& z) : first(x), second(y), third(z) {} template trio::trio(const trio& t) { first = t.first; second = t.second; third = t.third; } template trio& trio::operator=(const trio& t) { first = t.first; second = t.second; third = t.third; return *this; } // 比較演算子 (trio) template bool trio::operator<(const trio& t) const { return tie(first, second, third) < tie(t.first, t.second, t.third); } template bool trio::operator==(const trio& t) const { return tie(first, second, third) == tie(t.first, t.second, t.third); } template bool trio::operator!=(const trio& t) const { return !(*this == t); } template bool trio::operator>(const trio& t) const { return t < *this; } template bool trio::operator<=(const trio& t) const { return !(*this > t); } template bool trio::operator>=(const trio& t) const { return !(*this < t); } // 単項演算子(+ -) (trio) template trio trio::operator+() const { return *this; } template trio trio::operator-() const { return (*this) * (-1); } // 入出力 (trio) template istream& operator>>(istream& stream, trio& t) { return stream >> t.first >> t.second >> t.third; } template ostream& operator<<(ostream& stream, const trio& t) { return stream << t.first << " " << t.second << " " << t.third; } /* 複合代入演算子 (trio) */ template trio& operator+=(trio& a, const trio b) { a.fi += b.fi; a.se += b.se; a.th += b.th; return a; } template trio& operator-=(trio& a, const trio b) { a.fi -= b.fi; a.se -= b.se; a.th -= b.th; return a; } template trio& operator*=(trio& a, const trio b) { a.fi *= b.fi; a.se *= b.se; a.th *= b.th; return a; } template trio& operator/=(trio& a, const trio b) { a.fi /= b.fi; a.se /= b.se; a.th /= b.th; return a; } template trio& operator%=(trio& a, const trio b) { a.fi %= b.fi; a.se %= b.se; a.th %= b.th; return a; } template trio& operator+=(trio& a, const U b) { a.fi += b; a.se += b; a.th += b; return a; } template trio& operator-=(trio& a, const U b) { a.fi -= b; a.se -= b; a.th -= b; return a; } template trio& operator*=(trio& a, const U b) { a.fi *= b; a.se *= b; a.th *= b; return a; } template trio& operator/=(trio& a, const U b) { a.fi /= b; a.se /= b; a.th /= b; return a; } template trio& operator%=(trio& a, const U b) { a.fi %= b; a.se %= b; a.th %= b; return a; } /* 算術演算子 (trio) */ template trio operator+(trio a, const U& b) { return a += b; } template trio operator-(trio a, const U& b) { return a -= b; } template trio operator*(trio a, const U& b) { return a *= b; } template trio operator/(trio a, const U& b) { return a /= b; } template trio operator%(trio a, const U& b) { return a %= b; } /********* Q U A R T E T **********/ // コンストラクタ・代入演算子 template quartet::quartet() { first = T1(); second = T2(); third = T3(); fourth = T4(); } template quartet::quartet(const T1& x) : first(x), second(x), third(x), fourth(x) {} template quartet::quartet(const T1& x, const T2& y, const T3& z, const T4& w) : first(x), second(y), third(z), fourth(w) {} template quartet::quartet(const quartet& t) { first = t.first; second = t.second; third = t.third; fourth = t.fourth; } template quartet& quartet::operator=(const quartet& t) { first = t.first; second = t.second; third = t.third; fourth = t.fourth; return *this; } // 比較演算子 (quartet) template bool quartet::operator<(const quartet& t) const { return tie(first, second, third, fourth) < tie(t.first, t.second, t.third, t.fourth); } template bool quartet::operator==(const quartet& t) const { return tie(first, second, third, fourth) == tie(t.first, t.second, t.third, t.fourth); } template bool quartet::operator!=(const quartet& t) const { return !(*this == t); } template bool quartet::operator>(const quartet& t) const { return t < *this; } template bool quartet::operator<=(const quartet& t) const { return !(*this > t); } template bool quartet::operator>=(const quartet& t) const { return !(*this < t); } // 単項演算子(+ -) (quartet) template quartet quartet::operator+() const { return *this; } template quartet quartet::operator-() const { return (*this) * (-1); } // 入出力 (quartet) template istream& operator>>(istream& stream, quartet& t) { return stream >> t.first >> t.second >> t.third >> t.fourth; } template ostream& operator<<(ostream& stream, const quartet& t) { return stream << t.first << " " << t.second << " " << t.third << " " << t.fourth; } /* 複合代入演算子 (quartet) */ template quartet& operator+=(quartet& a, const quartet b) { a.fi += b.fi; a.se += b.se; a.th += b.th; a.fo += b.fo; return a; } template quartet& operator-=(quartet& a, const quartet b) { a.fi -= b.fi; a.se -= b.se; a.th -= b.th; a.fo -= b.fo; return a; } template quartet& operator*=(quartet& a, const quartet b) { a.fi *= b.fi; a.se *= b.se; a.th *= b.th; a.fo *= b.fo; return a; } template quartet& operator/=(quartet& a, const quartet b) { a.fi /= b.fi; a.se /= b.se; a.th /= b.th; a.fo /= b.fo; return a; } template quartet& operator%=(quartet& a, const quartet b) { a.fi %= b.fi; a.se %= b.se; a.th %= b.th; a.fo %= b.fo; return a; } template quartet& operator+=(quartet& a, const U b) { a.fi += b; a.se += b; a.th += b; a.fo += b; return a; } template quartet& operator-=(quartet& a, const U b) { a.fi -= b; a.se -= b; a.th -= b; a.fo -= b; return a; } template quartet& operator*=(quartet& a, const U b) { a.fi *= b; a.se *= b; a.th *= b; a.fo *= b; return a; } template quartet& operator/=(quartet& a, const U b) { a.fi /= b; a.se /= b; a.th /= b; a.fo /= b; return a; } template quartet& operator%=(quartet& a, const U b) { a.fi %= b; a.se %= b; a.th %= b; a.fo %= b; return a; } /* 算術演算子 (quartet) */ template quartet operator+(quartet a, const U& b) { return a += b; } template quartet operator-(quartet a, const U& b) { return a -= b; } template quartet operator*(quartet a, const U& b) { return a *= b; } template quartet operator/(quartet a, const U& b) { return a /= b; } template quartet operator%(quartet a, const U& b) { return a %= b; } /********* R A N D O M **********/ // 引数なし: seedはランダム Random::Random() { random_device seed_gen; rnd.seed(seed_gen()); } // 引数: seed Random::Random(ll seed) { rnd.seed(seed); } // [a, b]からランダムに整数を返す ll Random::randint(ll a, ll b) { uniform_int_distribution dist(a, b); return dist(rnd); } // [a, b]からランダムに浮動小数点数を返す double Random::randreal(double a, double b) { uniform_real_distribution dist(a, b); return dist(rnd); } // [a, b]からランダムに文字を返す char Random::randchar(char a, char b) { uniform_int_distribution dist(a, b); return dist(rnd); } // `vec`をinplaceで並び替える template void Random::shuffle(vector& vec) { std::shuffle(all(vec), rnd); } /********* T I M E R **********/ // 引数: タイムリミット(秒) Timer::Timer(double time_limit_sec) { assert(time_limit_sec > 0); this->time_limit = time_limit_sec * CLOCKS_PER_SEC; this->start_time = clock(); } // タイムリミットに到達したかどうか bool Timer::is_time_up() const { return clock() - this->start_time >= this->time_limit; } // 経過時間の割合を[0.0, 1.0]で返す double Timer::get_elapsed_ratio() const { double ratio = 1.0 * (clock() - this->start_time) / (this->time_limit - this->start_time); return min(1.0, ratio); } /********* P R I N T **********/ template void print(const T& e) { cout << e << '\n'; } template void print(const H& h, const T&... t) { cout << h << ' '; print(t...); } template void End(const T&... t) { print(t...); exit(0); } /********* D E B U G *********/ void dbg_(const long long& e) { if (e == INF) cout << "INF"; else if (e == -INF) cout << "-INF"; else cout << e; } template void dbg_(const T& e) { cout << e; } template void dbg_(const pair& p) { cout << '('; dbg_(p.first); cout << ' '; dbg_(p.second); cout << ')'; } template void dbg_(const trio& t) { cout << '('; dbg_(t.first); cout << ' '; dbg_(t.second); cout << ' '; dbg_(t.third); cout << ')'; } template void dbg_(const quartet& t) { cout << '('; dbg_(t.first); cout << ' '; dbg_(t.second); cout << ' '; dbg_(t.third); cout << ' '; dbg_(t.fourth); cout << ')'; } template void debug_(const T& e) { dbg_(e); cout << '\n'; } template void debug_(const vector& v) { if (v.size()) { auto it = v.begin(); dbg_(*it++); for (; it != v.end(); ++it) { cout << ' '; dbg_(*it); } } cout << '\n'; } template void debug_(const vector>& vv) { cout << '\n'; ll cnt = 0; for (auto&& v : vv) { cout << cnt++ << ": "; debug_(v); } } template void debug_(const map& mp) { if (mp.size()) { auto it = mp.begin(); dbg_(*it++); for (; it != mp.end(); ++it) { cout << ' '; dbg_(*it); } } cout << '\n'; } template void debug_(const vector>& vm) { cout << '\n'; ll cnt = 0; for (auto&& mp : vm) { cout << cnt++ << ": "; debug_(mp); } } template void debug_(const set& st) { if (st.size()) { auto it = st.begin(); dbg_(*it++); for (; it != st.end(); ++it) { cout << ' '; dbg_(*it); } } cout << '\n'; } template void debug_(const multiset& st) { if (st.size()) { auto it = st.begin(); dbg_(*it++); for (; it != st.end(); ++it) { cout << ' '; dbg_(*it); } } cout << '\n'; } template void debug_(const vector>& vs) { cout << '\n'; ll cnt = 0; for (auto&& st : vs) { cout << cnt++ << ": "; debug_(st); } } template void debug_(const vector>& vs) { cout << '\n'; ll cnt = 0; for (auto&& st : vs) { cout << cnt++ << ": "; debug_(st); } } template void debug_(const H& h, const T&... t) { dbg_(h); cout << ", "; debug_(t...); } /********* O T H E R S **********/ // a > b なら a に b を代入. template bool chmin(T& a, U b) { if (a > b) { a = b; return true; } return false; } // a < b なら a に b を代入. template bool chmax(T& a, U b) { if (a < b) { a = b; return true; } return false; } // a % mを返す. aは負もOK. template T Mod(const T& a, const U& m) { assert(m > 0); return (a % m + m) % m; } // 天井関数. xは負もOK template T Ceil(const T& x, const U& y) { assert(y > 0); return x < 0 ? x / y : (x + y - 1) / y; } // 床関数. xは負もOK template T Floor(const T& x, const U& y) { assert(y > 0); return -(Ceil(-x, y)); } // 符号関数. 返り値: -1, 0, +1のいずれか template ll Sign(const T& x) { return (x > T(0)) - (x < T(0)); } // x * x <= nを満たす最大の整数`x`を返す ll Isqrt(ll n) { assert(n >= 0); ll x = round(sqrt(n)); while (x * x > n) --x; return x; } // nCr. n=60, r=30までOK. ll Comb(ll n, ll r) { if (r < 0 || n < r) return 0; r = min(r, n - r); ll ret = 1; rep(i, r) { ret *= n - i; ret /= i + 1; } return ret; } // x^n. mintもOK. template T Pow(T x, ll n) { assert(n >= 0); T ret = 1; while (1) { if (n % 2) ret *= x; n /= 2; if (!n) return ret; x = x * x; } } // (x^n) % mod. template T Pow(T x, ll n, T mod) { assert(n >= 0 && mod > 0); T ret = 1; while (1) { if (n % 2) ret = (ret * x) % mod; n /= 2; if (!n) return ret; x = (x * x) % mod; } } // 等差級数. mintもOK. template T Aseries(T a, T d, ll n) { assert(n >= 0); return a * n + n * (n - 1) / 2 * d; } // 等比級数. mintもOK. template T Gseries(T a, T r, ll n) { assert(n >= 0); if (r == 1) return a * n; else return a * (1 - Pow(r, n)) / (1 - r); } // `b`の`i`番目(0 origin)のビットが1かどうか bool Bit(ll b, int i) { assert(0 <= i && i < 64); return (b >> i) & 1; } // `b`の立っているビット数 ll Popcount(ll b) { return __builtin_popcountll(b); } // [0, n)番目のビットが立った64bit整数を返す. ll Mask(ll n) { assert(0 <= n && n < 63); return (1LL << n) - 1LL; } // [m, n)番目のビットが立った64bit整数を返す. ll Mask(ll n, ll m) { assert(n >= m); return Mask(n) ^ Mask(m); } // [l1, r1)と[l2, r2)の積区間を返す. 例: [3, 6) x [5, 7) = [5, 6) pll GetOverlap(ll l1, ll r1, ll l2, ll r2) { return {max(l1, l2), min(r1, r2)}; } // [l1, r1)と[l2, r2)で重なりがあるかを返す. 引数が片方でも空区間ならfalse; bool IsOverlap(ll l1, ll r1, ll l2, ll r2) { return max(l1, l2) < min(r1, r2); } // b=trueなら"Yes\n", falseなら"No\n"を出力 void PrintYesNo(bool b) { b ? print("Yes") : print("No"); }