結果
問題 | No.1775 Love Triangle 2 |
ユーザー | Pachicobue |
提出日時 | 2021-12-05 16:41:20 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 935 ms / 8,000 ms |
コード長 | 27,429 bytes |
コンパイル時間 | 3,157 ms |
コンパイル使用メモリ | 246,252 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-07 07:53:28 |
合計ジャッジ時間 | 36,247 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 32 ms
5,248 KB |
testcase_01 | AC | 31 ms
5,376 KB |
testcase_02 | AC | 32 ms
5,376 KB |
testcase_03 | AC | 31 ms
5,376 KB |
testcase_04 | AC | 935 ms
5,376 KB |
testcase_05 | AC | 898 ms
5,376 KB |
testcase_06 | AC | 903 ms
5,376 KB |
testcase_07 | AC | 835 ms
5,376 KB |
testcase_08 | AC | 889 ms
5,376 KB |
testcase_09 | AC | 213 ms
5,376 KB |
testcase_10 | AC | 245 ms
5,376 KB |
testcase_11 | AC | 168 ms
5,376 KB |
testcase_12 | AC | 150 ms
5,376 KB |
testcase_13 | AC | 121 ms
5,376 KB |
testcase_14 | AC | 105 ms
5,376 KB |
testcase_15 | AC | 95 ms
5,376 KB |
testcase_16 | AC | 85 ms
5,376 KB |
testcase_17 | AC | 81 ms
5,376 KB |
testcase_18 | AC | 84 ms
5,376 KB |
testcase_19 | AC | 208 ms
5,376 KB |
testcase_20 | AC | 192 ms
5,376 KB |
testcase_21 | AC | 167 ms
5,376 KB |
testcase_22 | AC | 148 ms
5,376 KB |
testcase_23 | AC | 123 ms
5,376 KB |
testcase_24 | AC | 168 ms
5,376 KB |
testcase_25 | AC | 145 ms
5,376 KB |
testcase_26 | AC | 125 ms
5,376 KB |
testcase_27 | AC | 144 ms
5,376 KB |
testcase_28 | AC | 116 ms
5,376 KB |
testcase_29 | AC | 85 ms
5,376 KB |
testcase_30 | AC | 138 ms
5,376 KB |
testcase_31 | AC | 229 ms
5,376 KB |
testcase_32 | AC | 323 ms
5,376 KB |
testcase_33 | AC | 418 ms
5,376 KB |
testcase_34 | AC | 425 ms
5,376 KB |
testcase_35 | AC | 488 ms
5,376 KB |
testcase_36 | AC | 574 ms
5,376 KB |
testcase_37 | AC | 643 ms
5,376 KB |
testcase_38 | AC | 779 ms
5,376 KB |
testcase_39 | AC | 109 ms
5,376 KB |
testcase_40 | AC | 181 ms
5,376 KB |
testcase_41 | AC | 245 ms
5,376 KB |
testcase_42 | AC | 258 ms
5,376 KB |
testcase_43 | AC | 275 ms
5,376 KB |
testcase_44 | AC | 196 ms
5,376 KB |
testcase_45 | AC | 457 ms
5,376 KB |
testcase_46 | AC | 289 ms
5,376 KB |
testcase_47 | AC | 138 ms
5,376 KB |
testcase_48 | AC | 331 ms
5,376 KB |
testcase_49 | AC | 87 ms
5,376 KB |
testcase_50 | AC | 136 ms
5,376 KB |
testcase_51 | AC | 230 ms
5,376 KB |
testcase_52 | AC | 327 ms
5,376 KB |
testcase_53 | AC | 417 ms
5,376 KB |
testcase_54 | AC | 418 ms
5,376 KB |
testcase_55 | AC | 504 ms
5,376 KB |
testcase_56 | AC | 572 ms
5,376 KB |
testcase_57 | AC | 639 ms
5,376 KB |
testcase_58 | AC | 752 ms
5,376 KB |
testcase_59 | AC | 116 ms
5,376 KB |
testcase_60 | AC | 162 ms
5,376 KB |
testcase_61 | AC | 203 ms
5,376 KB |
testcase_62 | AC | 144 ms
5,376 KB |
testcase_63 | AC | 172 ms
5,376 KB |
testcase_64 | AC | 219 ms
5,376 KB |
testcase_65 | AC | 126 ms
5,376 KB |
testcase_66 | AC | 205 ms
5,376 KB |
testcase_67 | AC | 281 ms
5,376 KB |
testcase_68 | AC | 166 ms
5,376 KB |
testcase_69 | AC | 306 ms
5,376 KB |
testcase_70 | AC | 589 ms
5,376 KB |
testcase_71 | AC | 694 ms
5,376 KB |
testcase_72 | AC | 747 ms
5,376 KB |
testcase_73 | AC | 698 ms
5,376 KB |
testcase_74 | AC | 774 ms
5,376 KB |
testcase_75 | AC | 770 ms
5,376 KB |
testcase_76 | AC | 669 ms
5,376 KB |
testcase_77 | AC | 807 ms
5,376 KB |
testcase_78 | AC | 766 ms
5,376 KB |
testcase_79 | AC | 234 ms
5,376 KB |
testcase_80 | AC | 264 ms
5,376 KB |
testcase_81 | AC | 369 ms
5,376 KB |
testcase_82 | AC | 333 ms
5,376 KB |
testcase_83 | AC | 419 ms
5,376 KB |
testcase_84 | AC | 307 ms
5,376 KB |
testcase_85 | AC | 401 ms
5,376 KB |
testcase_86 | AC | 271 ms
5,376 KB |
testcase_87 | AC | 378 ms
5,376 KB |
testcase_88 | AC | 577 ms
5,376 KB |
testcase_89 | AC | 502 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> constexpr const char* URLs[] = { "https://hitonanode.github.io/cplib-cpp/number/nimber.hpp", "https://www.slideshare.net/wata_orz/ss-12208032", }; #pragma region Header using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; using f64 = double; using f80 = long double; using f128 = __float128; constexpr i32 operator"" _i32(u64 v) { return v; } constexpr i32 operator"" _u32(u64 v) { return v; } constexpr i64 operator"" _i64(u64 v) { return v; } constexpr u64 operator"" _u64(u64 v) { return v; } constexpr f64 operator"" _f64(f80 v) { return v; } constexpr f80 operator"" _f80(f80 v) { return v; } using Istream = std::istream; using Ostream = std::ostream; using Str = std::string; template<typename T> using Lt = std::less<T>; template<typename T> using Gt = std::greater<T>; template<typename T> using IList = std::initializer_list<T>; template<int n> using BSet = std::bitset<n>; template<typename T1, typename T2> using Pair = std::pair<T1, T2>; template<typename... Ts> using Tup = std::tuple<Ts...>; template<typename T, int N> using Arr = std::array<T, N>; template<typename... Ts> using Deq = std::deque<Ts...>; template<typename... Ts> using Set = std::set<Ts...>; template<typename... Ts> using MSet = std::multiset<Ts...>; template<typename... Ts> using USet = std::unordered_set<Ts...>; template<typename... Ts> using UMSet = std::unordered_multiset<Ts...>; template<typename... Ts> using Map = std::map<Ts...>; template<typename... Ts> using MMap = std::multimap<Ts...>; template<typename... Ts> using UMap = std::unordered_map<Ts...>; template<typename... Ts> using UMMap = std::unordered_multimap<Ts...>; template<typename... Ts> using Vec = std::vector<Ts...>; template<typename... Ts> using Stack = std::stack<Ts...>; template<typename... Ts> using Queue = std::queue<Ts...>; template<typename T> using MaxHeap = std::priority_queue<T>; template<typename T> using MinHeap = std::priority_queue<T, Vec<T>, Gt<T>>; using NSec = std::chrono::nanoseconds; using USec = std::chrono::microseconds; using MSec = std::chrono::milliseconds; using Sec = std::chrono::seconds; template<typename T> constexpr T LIMMIN = std::numeric_limits<T>::min(); template<typename T> constexpr T LIMMAX = std::numeric_limits<T>::max(); template<typename T> constexpr T INF = (LIMMAX<T> - 1) / 2; template<typename T> constexpr T PI = T{3.141592653589793238462643383279502884}; template<typename T = u64> constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN<T>(n - 1) * T{10}; } Ostream& operator<<(Ostream& os, i128 v) { bool minus = false; if (v < 0) { minus = true, v = -v; } Str ans; if (v == 0) { ans = "0"; } while (v) { ans.push_back('0' + v % 10), v /= 10; } std::reverse(ans.begin(), ans.end()); return os << (minus ? "-" : "") << ans; } Ostream& operator<<(Ostream& os, u128 v) { Str ans; if (v == 0) { ans = "0"; } while (v) { ans.push_back('0' + v % 10), v /= 10; } std::reverse(ans.begin(), ans.end()); return os << ans; } template<typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else { return false; } } template<typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else { return false; } } template<typename T> constexpr T floorDiv(T x, T y) { if (y < T{}) { x = -x, y = -y; } return x >= T{} ? x / y : (x - y + 1) / y; } template<typename T> constexpr T ceilDiv(T x, T y) { if (y < T{}) { x = -x, y = -y; } return x >= T{} ? (x + y - 1) / y : x / y; } template<typename T, typename I> constexpr T modPower(T v, I n, T mod) { T ans = 1 % mod; for (; n > 0; n >>= 1, (v *= v) %= mod) { if (n % 2 == 1) { (ans *= v) %= mod; } } return ans; } template<typename T, typename I> constexpr T power(T v, I n) { T ans = 1; for (; n > 0; n >>= 1, v *= v) { if (n % 2 == 1) { ans *= v; } } return ans; } template<typename T, typename I> constexpr T power(T v, I n, const T& e) { T ans = e; for (; n > 0; n >>= 1, v *= v) { if (n % 2 == 1) { ans *= v; } } return ans; } template<typename T> Vec<T> operator+=(Vec<T>& vs1, const Vec<T>& vs2) { vs1.insert(vs1.end(), vs2.begin(), vs2.end()); return vs1; } template<typename T> Vec<T> operator+(const Vec<T>& vs1, const Vec<T>& vs2) { auto vs = vs1; vs += vs2; return vs; } template<typename Vs, typename V> void fillAll(Vs& arr, const V& v) { if constexpr (std::is_convertible<V, Vs>::value) { arr = v; } else { for (auto& subarr : arr) { fillAll(subarr, v); } } } template<typename Vs> void sortAll(Vs& vs) { std::sort(std::begin(vs), std::end(vs)); } template<typename Vs, typename C> void sortAll(Vs& vs, C comp) { std::sort(std::begin(vs), std::end(vs), comp); } template<typename Vs> void reverseAll(Vs& vs) { std::reverse(std::begin(vs), std::end(vs)); } template<typename V, typename Vs> V sumAll(const Vs& vs) { if constexpr (std::is_convertible<Vs, V>::value) { return static_cast<V>(vs); } else { V ans = 0; for (const auto& v : vs) { ans += sumAll<V>(v); } return ans; } } template<typename Vs> int minInd(const Vs& vs) { return std::min_element(std::begin(vs), std::end(vs)) - std::begin(vs); } template<typename Vs> int maxInd(const Vs& vs) { return std::max_element(std::begin(vs), std::end(vs)) - std::begin(vs); } template<typename Vs, typename V> int lbInd(const Vs& vs, const V& v) { return std::lower_bound(std::begin(vs), std::end(vs), v) - std::begin(vs); } template<typename Vs, typename V> int ubInd(const Vs& vs, const V& v) { return std::upper_bound(std::begin(vs), std::end(vs), v) - std::begin(vs); } template<typename T, typename F> Vec<T> genVec(int n, F gen) { Vec<T> ans; std::generate_n(std::back_insert_iterator(ans), n, gen); return ans; } Vec<int> iotaVec(int n, int offset = 0) { Vec<int> ans(n); std::iota(ans.begin(), ans.end(), offset); return ans; } constexpr int popcount(const u64 v) { return v ? __builtin_popcountll(v) : 0; } constexpr int log2p1(const u64 v) { return v ? 64 - __builtin_clzll(v) : 0; } constexpr int lsbp1(const u64 v) { return __builtin_ffsll(v); } constexpr int clog(const u64 v) { return v ? log2p1(v - 1) : 0; } constexpr u64 ceil2(const u64 v) { const int l = clog(v); return (l == 64) ? 0_u64 : (1_u64 << l); } constexpr u64 floor2(const u64 v) { return v ? (1_u64 << (log2p1(v) - 1)) : 0_u64; } constexpr bool ispow2(const u64 v) { return (v > 0) and ((v & (v - 1)) == 0); } constexpr bool btest(const u64 mask, const int ind) { return (mask >> ind) & 1_u64; } template<typename F> struct Fix : F { Fix(F&& f) : F{std::forward<F>(f)} {} template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); } }; class irange { private: struct itr { itr(i64 start = 0, i64 step = 1) : m_cnt{start}, m_step{step} {} bool operator!=(const itr& it) const { return m_cnt != it.m_cnt; } int operator*() { return m_cnt; } itr& operator++() { m_cnt += m_step; return *this; } i64 m_cnt, m_step; }; i64 m_start, m_end, m_step; public: irange(i64 start, i64 end, i64 step = 1) { assert(step != 0); const i64 d = std::abs(step); const i64 l = (step > 0 ? start : end); const i64 r = (step > 0 ? end : start); int n = (r - l) / d + ((r - l) % d ? 1 : 0); if (l >= r) { n = 0; } m_start = start; m_end = start + step * n; m_step = step; } itr begin() const { return itr{m_start, m_step}; } itr end() const { return itr{m_end, m_step}; } }; irange rep(int end) { return irange(0, end, 1); } irange per(int rend) { return irange(rend - 1, -1, -1); } #pragma COMMENT("[REFS] Xoshiro: https://prng.di.unimi.it") namespace xoshiro_impl { u64 x; u64 next() { uint64_t z = (x += 0x9e3779b97f4a7c15); z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; z = (z ^ (z >> 27)) * 0x94d049bb133111eb; return z ^ (z >> 31); } } class Xoshiro32 { public: using result_type = u32; using T = result_type; Xoshiro32(T seed = 0) { xoshiro_impl::x = seed; s[0] = xoshiro_impl::next(); s[1] = xoshiro_impl::next(); s[2] = xoshiro_impl::next(); s[3] = xoshiro_impl::next(); } static constexpr T min() { return LIMMIN<T>; } static constexpr T max() { return LIMMAX<T>; } T operator()() { return next(); } private: static constexpr T rotl(const T x, int k) { return (x << k) | (x >> (32 - k)); } T next() { const T ans = rotl(s[1] * 5, 7) * 9; const T t = s[1] << 9; s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; s[2] ^= t; s[3] = rotl(s[3], 11); return ans; } T s[4]; }; class Xoshiro64 { public: using result_type = u64; using T = result_type; Xoshiro64(T seed = 0) { xoshiro_impl::x = seed; s[0] = xoshiro_impl::next(); s[1] = xoshiro_impl::next(); s[2] = xoshiro_impl::next(); s[3] = xoshiro_impl::next(); } static constexpr T min() { return LIMMIN<T>; } static constexpr T max() { return LIMMAX<T>; } T operator()() { return next(); } private: static constexpr T rotl(const T x, int k) { return (x << k) | (x >> (64 - k)); } T next() { const T ans = rotl(s[1] * 5, 7) * 9; const T t = s[1] << 17; s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; s[2] ^= t; s[3] = rotl(s[3], 45); return ans; } T s[4]; }; template<typename Rng> class RNG { public: using result_type = typename Rng::result_type; using T = result_type; static constexpr T min() { return Rng::min(); } static constexpr T max() { return Rng::max(); } RNG() : RNG(std::random_device{}()) {} RNG(T seed) : m_rng(seed) {} T operator()() { return m_rng(); } template<typename T> T val(T min, T max) { return std::uniform_int_distribution<T>(min, max)(m_rng); } template<typename T> Pair<T, T> pair(T min, T max) { return std::minmax({val<T>(min, max), val<T>(min, max)}); } template<typename T> Vec<T> vec(int n, T min, T max) { return genVec<T>(n, [&]() { return val<T>(min, max); }); } template<typename T> Vec<Vec<T>> vvec(int n, int m, T min, T max) { return genVec<Vec<T>>(n, [&]() { return vec(m, min, max); }); } private: Rng m_rng; }; RNG<std::mt19937> rng; RNG<std::mt19937_64> rng64; RNG<Xoshiro32> rng_xo; RNG<Xoshiro64> rng_xo64; class Scanner { public: Scanner(Istream& is = std::cin) : m_is{is} { m_is.tie(nullptr)->sync_with_stdio(false); } template<typename T> T val() { T v; return m_is >> v, v; } template<typename T> T val(T offset) { return val<T>() - offset; } template<typename T> Vec<T> vec(int n) { return genVec<T>(n, [&]() { return val<T>(); }); } template<typename T> Vec<T> vec(int n, T offset) { return genVec<T>(n, [&]() { return val<T>(offset); }); } template<typename T> Vec<Vec<T>> vvec(int n, int m) { return genVec<Vec<T>>(n, [&]() { return vec<T>(m); }); } template<typename T> Vec<Vec<T>> vvec(int n, int m, const T offset) { return genVec<Vec<T>>(n, [&]() { return vec<T>(m, offset); }); } template<typename... Args> auto tup() { return Tup<Args...>{val<Args>()...}; } template<typename... Args> auto tup(const Args&... offsets) { return Tup<Args...>{val<Args>(offsets)...}; } private: Istream& m_is; }; Scanner in; class Printer { public: Printer(Ostream& os = std::cout) : m_os{os} { m_os << std::fixed << std::setprecision(15); } template<typename... Args> int operator()(const Args&... args) { dump(args...); return 0; } template<typename... Args> int ln(const Args&... args) { dump(args...), m_os << '\n'; return 0; } template<typename... Args> int el(const Args&... args) { dump(args...), m_os << std::endl; return 0; } private: template<typename T> void dump(const T& v) { m_os << v; } template<typename T> void dump(const Vec<T>& vs) { for (const int i : rep(vs.size())) { m_os << (i ? " " : ""), dump(vs[i]); } } template<typename T> void dump(const Vec<Vec<T>>& vss) { for (const int i : rep(vss.size())) { m_os << (i ? "\n" : ""), dump(vss[i]); } } template<typename T, typename... Ts> int dump(const T& v, const Ts&... args) { dump(v), m_os << ' ', dump(args...); return 0; } Ostream& m_os; }; Printer out; template<typename T, int n, int i = 0> auto ndVec(int const (&szs)[n], const T x = T{}) { if constexpr (i == n) { return x; } else { return std::vector(szs[i], ndVec<T, n, i + 1>(szs, x)); } } template<typename T, typename F> T binSearch(T ng, T ok, F check) { while (std::abs(ok - ng) > 1) { const T mid = (ok + ng) / 2; (check(mid) ? ok : ng) = mid; } return ok; } template<u32 mod_, u32 root_, u32 max2p_> class modint { template<typename U = u32&> static U modRef() { static u32 s_mod = 0; return s_mod; } template<typename U = u32&> static U rootRef() { static u32 s_root = 0; return s_root; } template<typename U = u32&> static U max2pRef() { static u32 s_max2p = 0; return s_max2p; } public: template<typename U = const u32> static constexpr std::enable_if_t<mod_ != 0, U> mod() { return mod_; } template<typename U = const u32> static std::enable_if_t<mod_ == 0, U> mod() { return modRef(); } template<typename U = const u32> static constexpr std::enable_if_t<mod_ != 0, U> root() { return root_; } template<typename U = const u32> static std::enable_if_t<mod_ == 0, U> root() { return rootRef(); } template<typename U = const u32> static constexpr std::enable_if_t<mod_ != 0, U> max2p() { return max2p_; } template<typename U = const u32> static std::enable_if_t<mod_ == 0, U> max2p() { return max2pRef(); } template<typename U = u32> static void setMod(std::enable_if_t<mod_ == 0, U> m) { modRef() = m; } template<typename U = u32> static void setRoot(std::enable_if_t<mod_ == 0, U> r) { rootRef() = r; } template<typename U = u32> static void setMax2p(std::enable_if_t<mod_ == 0, U> m) { max2pRef() = m; } constexpr modint() : m_val{0} {} constexpr modint(i64 v) : m_val{normll(v)} {} constexpr void setRaw(u32 v) { m_val = v; } constexpr modint operator-() const { return modint{0} - (*this); } constexpr modint& operator+=(const modint& m) { m_val = norm(m_val + m.val()); return *this; } constexpr modint& operator-=(const modint& m) { m_val = norm(m_val + mod() - m.val()); return *this; } constexpr modint& operator*=(const modint& m) { m_val = normll((i64)m_val * (i64)m.val() % (i64)mod()); return *this; } constexpr modint& operator/=(const modint& m) { return *this *= m.inv(); } constexpr modint operator+(const modint& m) const { auto v = *this; return v += m; } constexpr modint operator-(const modint& m) const { auto v = *this; return v -= m; } constexpr modint operator*(const modint& m) const { auto v = *this; return v *= m; } constexpr modint operator/(const modint& m) const { auto v = *this; return v /= m; } constexpr bool operator==(const modint& m) const { return m_val == m.val(); } constexpr bool operator!=(const modint& m) const { return not(*this == m); } friend Istream& operator>>(Istream& is, modint& m) { i64 v; return is >> v, m = v, is; } friend Ostream& operator<<(Ostream& os, const modint& m) { return os << m.val(); } constexpr u32 val() const { return m_val; } template<typename I> constexpr modint pow(I n) const { return power(*this, n); } constexpr modint inv() const { return pow(mod() - 2); } static modint sinv(u32 n) { static Vec<modint> is{1, 1}; for (u32 i = (u32)is.size(); i <= n; i++) { is.push_back(-is[mod() % i] * (mod() / i)); } return is[n]; } static modint fact(u32 n) { static Vec<modint> fs{1, 1}; for (u32 i = (u32)fs.size(); i <= n; i++) { fs.push_back(fs.back() * i); } return fs[n]; } static modint ifact(u32 n) { static Vec<modint> ifs{1, 1}; for (u32 i = (u32)ifs.size(); i <= n; i++) { ifs.push_back(ifs.back() * sinv(i)); } return ifs[n]; } static modint comb(int n, int k) { return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k) * ifact(k); } private: static constexpr u32 norm(u32 x) { return x < mod() ? x : x - mod(); } static constexpr u32 normll(i64 x) { return norm(u32(x % (i64)mod() + (i64)mod())); } u32 m_val; }; using modint_1000000007 = modint<1000000007, 5, 1>; using modint_998244353 = modint<998244353, 3, 23>; template<int id> using modint_dynamic = modint<0, 0, id>; template<typename T = int> class Graph { struct Edge { Edge() = default; Edge(int i, int t, T c) : id{i}, to{t}, cost{c} {} int id; int to; T cost; operator int() const { return to; } }; public: Graph(int n) : m_v{n}, m_edges(n) {} void addEdge(int u, int v, bool bi = false) { assert(0 <= u and u < m_v); assert(0 <= v and v < m_v); m_edges[u].emplace_back(m_e, v, 1); if (bi) { m_edges[v].emplace_back(m_e, u, 1); } m_e++; } void addEdge(int u, int v, const T& c, bool bi = false) { assert(0 <= u and u < m_v); assert(0 <= v and v < m_v); m_edges[u].emplace_back(m_e, v, c); if (bi) { m_edges[v].emplace_back(m_e, u, c); } m_e++; } const Vec<Edge>& operator[](const int u) const { assert(0 <= u and u < m_v); return m_edges[u]; } Vec<Edge>& operator[](const int u) { assert(0 <= u and u < m_v); return m_edges[u]; } int v() const { return m_v; } int e() const { return m_e; } friend Ostream& operator<<(Ostream& os, const Graph& g) { for (int u : rep(g.v())) { for (const auto& [id, v, c] : g[u]) { os << "[" << id << "]: "; os << u << "->" << v << "(" << c << ")\n"; } } return os; } Vec<T> sizes(int root = 0) const { const int N = v(); assert(0 <= root and root < N); Vec<T> ss(N, 1); Fix([&](auto dfs, int u, int p) -> void { for (const auto& [id, v, c] : m_edges[u]) { static_cast<void>(id); if (v == p) { continue; } dfs(v, u); ss[u] += ss[v]; } })(root, -1); return ss; } Vec<T> depths(int root = 0) const { const int N = v(); assert(0 <= root and root < N); Vec<T> ds(N, 0); Fix([&](auto dfs, int u, int p) -> void { for (const auto& [id, v, c] : m_edges[u]) { static_cast<void>(id); if (v == p) { continue; } ds[v] = ds[u] + c; dfs(v, u); } })(root, -1); return ds; } Vec<int> parents(int root = 0) const { const int N = v(); assert(0 <= root and root < N); Vec<int> ps(N, -1); Fix([&](auto dfs, int u, int p) -> void { for (const auto& [id, v, c] : m_edges[u]) { static_cast<void>(id); if (v == p) { continue; } ps[v] = u; dfs(v, u); } })(root, -1); return ps; } private: int m_v; int m_e = 0; Vec<Vec<Edge>> m_edges; }; #pragma endregion struct Nimber { using ull = unsigned long long; ull v; const static std::array<std::array<unsigned, 256>, 256> small_table; const static std::array<std::array<std::array<ull, 256>, 8>, 8> precalc; explicit operator bool() const { return v != 0; } Nimber(ull val = 0) : v(val) {} Nimber operator+(const Nimber& x) const { return Nimber(v ^ x.v); } Nimber operator-(const Nimber& x) const { return Nimber(v ^ x.v); } Nimber operator-() const { return *this; } Nimber& operator+=(const Nimber& x) { return *this = *this + x; } Nimber& operator-=(const Nimber& x) { return *this = *this + x; } template<class IStream> friend IStream& operator>>(IStream& is, Nimber& x) { ull v; return is >> v, x = Nimber(v), is; } template<class OStream> friend OStream& operator<<(OStream& os, const Nimber& x) { return os << x.v; } bool operator==(const Nimber& x) const { return v == x.v; } bool operator!=(const Nimber& x) const { return v != x.v; } bool operator<(const Nimber& x) const { return v < x.v; } static ull _rec(ull x, ull y) { if (x == 0 or y == 0) return 0; if (x < y) x ^= y, y ^= x, x ^= y; if (y == 1) return x; for (int shift = 64 / 2;; shift >>= 1) { ull mask = (1ULL << shift) - 1; if (y >> shift) { ull v00 = _rec(x & mask, y & mask); ull v01 = _rec(x & mask, y >> shift); ull v10 = _rec(x >> shift, y & mask); ull v11 = _rec(x >> shift, y >> shift); return v00 ^ ((v01 ^ v10 ^ v11) << shift) ^ _rec(v11, 1ULL << (shift - 1)); } else if (x >> shift) { return (_rec(x >> shift, y) << shift) ^ _rec(x & mask, y); } } } Nimber operator*(const Nimber& x) const { ull ret = 0; for (int d = 0; d < 8; ++d) { for (int e = 0; e < 8; ++e) { ret ^= precalc[d][e][small_table[(v >> (d * 8)) & 255] [(x.v >> (e * 8)) & 255]]; } } return Nimber(ret); } Nimber& operator*=(const Nimber& x) { return *this = *this * x; } }; const std::array<std::array<unsigned, 256>, 256> Nimber::small_table = []() { std::array<std::array<unsigned, 256>, 256> ret; for (int i = 0; i < 256; ++i) { for (int j = 0; j < 256; ++j) ret[i][j] = _rec(i, j); } return ret; }(); const std::array<std::array<std::array<unsigned long long, 256>, 8>, 8> Nimber::precalc = []() { std::array<std::array<std::array<unsigned long long, 256>, 8>, 8> ret; for (int d = 0; d < 8; ++d) { for (int e = 0; e < 8; ++e) { ull p = _rec(1ULL << (8 * d), 1ULL << (8 * e)); for (int i = 0; i < 256; ++i) ret[d][e][i] = _rec(p, i); } } return ret; }(); int main() { using mint = Nimber; const auto [N, M] = in.tup<int, int>(); const auto [X, Y, Z] = in.tup<int, int, int>(1, 1, 1); auto G = ndVec({N, N}, 1); for (int i : rep(N)) { G[i][i] = 0; } for (int i : rep(M)) { static_cast<void>(i); const auto [A, B] = in.tup<int, int>(1, 1); G[A][B] = G[B][A] = 0; } auto xss = ndVec<mint>({N, N}); constexpr u64 MAX = 0xFFFFFFFFFFFFFFFF_u64; for (int i : rep(N)) { if (G[X][i]) { xss[X][i] = rng.val<u64>(0, MAX); } if (G[i][X]) { xss[i][X] = rng.val<u64>(0, MAX); } } for (int i : rep(N)) { for (int j : irange(i + 1, N)) { if (i == X or j == X) { continue; } if (G[i][j] == 0) { continue; } xss[i][j] = xss[j][i] = rng.val<u64>(0, MAX); } } void(0); auto dp = ndVec<mint>({8, N, N}, 0); auto ndp = ndVec<mint>({8, N, N}, 0); auto sdp = ndVec<mint>({8, N}, 0); auto doDp = [&, X = X, Y = Y, Z = Z, N = N, M = M](int minV) -> int { assert(minV <= std::min({X, Y, Z})); fillAll(dp, 0); dp[0][X][X] = 1; for (int i : rep(N)) { fillAll(sdp, 0); for (int m : rep(8)) { for (int i : rep(N)) { for (int j : rep(N)) { sdp[m][i] += dp[m][i][j]; } } } fillAll(ndp, 0); for (int m : rep(8)) { for (int u : rep(N)) { if (u < minV) { continue; } for (int nu : rep(N)) { if (nu < minV) { continue; } int nm = m; if (nu == X) { if (btest(nm, 0)) { continue; } nm |= 1; } else if (nu == Y) { if (btest(nm, 1)) { continue; } nm |= 2; } else if (nu == Z) { if (btest(nm, 2)) { continue; } nm |= 4; } ndp[nm][nu][u] += xss[u][nu] * (sdp[m][u] - dp[m][u][nu]); } } } std::swap(dp, ndp); mint sum = 0; for (int pu : rep(N)) { sum += dp[7][X][pu]; } void(0); if (sum != 0) { return i + 1; } } return -1; }; const int L = doDp(0); void(0); if (L == -1) { out.ln(-1); } else { out.ln(L); } return 0; }