結果
| 問題 | No.3833 Encounter |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-28 00:11:12 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
WA
不安定
|
| 実行時間 | - |
| コード長 | 29,323 bytes |
| 記録 | |
| コンパイル時間 | 4,502 ms |
| コンパイル使用メモリ | 399,024 KB |
| 実行使用メモリ | 43,564 KB |
| 最終ジャッジ日時 | 2026-11-28 00:11:24 |
| 合計ジャッジ時間 | 11,462 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 WA * 17 |
ソースコード
#line 2 "template\\template.hpp"
// Library URL:
// https://github.com/prime3372/library
_Pragma("GCC optimize(\"O3\")")
_Pragma("GCC optimize(\"unroll-loops\")")
#include <bits/stdc++.h>
#include <cassert>
#line 11 "template\\template.hpp"
_Pragma("GCC target(\"avx2\")")
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
template <class T> using vec = std::vector<T>;
using str = std::string;
template <class T>
using minque = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T> using maxque = std::priority_queue<T>;
using std::views::keys;
using std::views::values;
using std::views::zip;
using namespace std;
#ifdef LOCAL
#define local 1
#else
#define local 0
#endif
#define __CHOOSE4(a, b, c, d, ...) d
#define __CHOOSE5(a, b, c, d, e, ...) e
#define __CHOOSE6(a, b, c, d, e, f, ...) f
#define rep1(n) for (ll _ = 0; _ < ll(n); _++)
#define rep2(i, n) for (ll i = 0; i < ll(n); i++)
#define rep3(i, a, b) for (ll i = (a); i < ll(b); i++)
#define rep4(i, a, b, c) for (ll i = (a); i < ll(b); i += (c))
#define rep(...) __CHOOSE5(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep2(i, n) for (ll i = ll(n) - 1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); i--)
#define rrep(...) __CHOOSE4(__VA_ARGS__, rrep3, rrep2)(__VA_ARGS__)
#define REP2(i, n) for (ll i = 0; i <= ll(n); i++)
#define REP3(i, a, b) for (ll i = (a); i <= ll(b); i++)
#define REP4(i, a, b, c) for (ll i = (a); i <= ll(b); i += (c))
#define REP(...) __CHOOSE5(__VA_ARGS__, REP4, REP3, REP2)(__VA_ARGS__)
#define RREP2(i, n) for (ll i = (n); i >= 0; i--)
#define RREP3(i, a, b) for (ll i = (b); i >= ll(a); i--)
#define RREP(...) __CHOOSE4(__VA_ARGS__, RREP3, RREP2)(__VA_ARGS__)
#define fore2(x, a) for (auto&& x : a)
#define fore3(x, y, a) for (auto&& [x, y] : a)
#define fore4(x, y, z, a) for (auto&& [x, y, z] : a)
#define fore5(x, y, z, w, a) for (auto&& [x, y, z, w] : a)
#define fore(...) \
__CHOOSE6(__VA_ARGS__, fore5, fore4, fore3, fore2)(__VA_ARGS__)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define inrange(i, a, b) ((a) <= (i) && (i) < (b))
#ifdef LOCAL
#define dump(...) \
[](const auto&... _args) { \
str _args_str = #__VA_ARGS__; \
_args_str.append(": "); \
cerr << _args_str; \
str _indent(_args_str.size(), ' '); \
ostringstream _oss; \
_oss << make_tuple(_args...); \
for (char _c : _oss.str()) { \
cerr << _c; \
if (_c == '\n') cerr << _indent; \
} \
cerr << "\n"; \
}(__VA_ARGS__)
#define msg(...) \
[](const auto&... _args) -> void { (std::cerr << ... << _args); }(__VA_ARGS__)
#else
#define dump(...)
#define msg(...)
#endif
constexpr int INF = int(1e9);
constexpr long long LINF = (long long)(2e18);
template <class T> inline bool chmin(T& a, const T& b) {
return b < a ? (a = b, true) : false;
}
template <class T> inline bool chmax(T& a, const T& b) {
return b > a ? (a = b, true) : false;
}
template <class Range>
requires(ranges::range<Range>)
inline int len(const Range& r) {
return int(ranges::size(r));
}
template <class Range>
requires(ranges::range<Range>)
inline auto min(const Range& r) {
return ranges::min(r);
}
template <class Range>
requires(ranges::range<Range>)
inline auto max(const Range& r) {
return ranges::max(r);
}
template <class Range, class T>
requires(ranges::range<Range>)
inline T accum(const Range& r, T x) {
return accumulate(r.begin(), r.end(), x);
}
template <class Range,
class Compare = std::less<std::ranges::range_value_t<Range>>>
requires(ranges::range<Range>)
void uniq(Range& a, Compare compare = Compare{}) {
sort(a.begin(), a.end(), compare);
a.erase(unique(a.begin(), a.end(),
[&](const auto& x, const auto& y) {
return !compare(x, y) && !compare(y, x);
}),
a.end());
}
template <class Range, class Compare = less<ranges::range_value_t<Range>>>
requires(ranges::range<Range>)
inline auto lowb(const Range& a,
ranges::range_value_t<Range> val,
Compare compare = Compare{}) {
return ranges::lower_bound(a, val, compare);
}
template <class Range, class Compare = less<ranges::range_value_t<Range>>>
requires(ranges::range<Range>)
inline auto upb(const Range& a,
ranges::range_value_t<Range> val,
Compare compare = Compare{}) {
return ranges::upper_bound(a, val, compare);
}
namespace cp {}
using namespace cp;
#line 2 "util\\io-float128.hpp"
#line 7 "util\\io-float128.hpp"
namespace cp {
namespace io {
// fixed-point format only
std::istream& operator>>(std::istream& is, __float128& val) {
std::string s;
if (!(is >> s)) return is;
val = 0;
__float128 p10 = 1;
bool has_dec = false;
for (int i = (s[0] == '+' || s[0] == '-'); i < int(s.size()); i++) {
if (s[i] == '.') {
if (has_dec) break;
has_dec = true;
} else {
if (!std::isdigit(s[i])) break;
val = val * 10 + (s[i] - '0');
if (has_dec) p10 *= 10;
}
}
val /= p10;
if (s[0] == '-') val = -val;
return is;
}
// output in fixed-point format
std::ostream& operator<<(std::ostream& os, __float128 x) {
assert(!__builtin_isnan(x) && !__builtin_isinf(x));
int prec = int(os.precision());
if (x < 0) {
os << '-';
x = -x;
}
__float128 p10 = 1;
for (int i = 0; i < prec; i++) p10 *= 10;
x += __float128(0.5) / p10;
int int_len = 0;
while (x >= 1) {
x /= 10;
int_len++;
}
for (int i = 0; i < int_len; i++) {
x *= 10;
int d = int(x);
if (d > 9) d = 9;
os << d;
x -= d;
}
if (int_len == 0) os << 0;
if (prec > 0) {
os << '.';
for (int i = 0; i < prec; i++) {
x *= 10;
int d = int(x);
if (d > 9) d = 9;
os << d;
x -= d;
}
}
return os;
}
} // namespace io
} // namespace cp
#line 2 "util\\io-int128.hpp"
#line 7 "util\\io-int128.hpp"
namespace cp {
namespace io {
std::istream& operator>>(std::istream& is, __int128& val) {
std::string s;
if (!(is >> s)) return is;
val = 0;
for (int i = (s[0] == '+' || s[0] == '-'); i < int(s.size()); i++) {
if (!std::isdigit(s[i])) break;
val = val * 10 + (s[i] - '0');
}
if (s[0] == '-') val = -val;
return is;
}
std::istream& operator>>(std::istream& is, unsigned __int128& val) {
std::string s;
if (!(is >> s)) return is;
val = 0;
for (int i = (s[0] == '+' || s[0] == '-'); i < int(s.size()); i++) {
if (!std::isdigit(s[i])) break;
val = val * 10 + (s[i] - '0');
}
if (s[0] == '-') val = -val;
return is;
}
std::ostream& operator<<(std::ostream& os, __int128 val) {
if (val == (long long)(val)) return os << (long long)(val);
unsigned __int128 uval = val;
if (val < 0) {
os << '-';
uval = -val;
}
std::string s;
while (uval) {
s.push_back(char('0' + (uval % 10)));
uval /= 10;
}
std::reverse(s.begin(), s.end());
return os << s;
}
std::ostream& operator<<(std::ostream& os, unsigned __int128 val) {
if (val == (unsigned long long)(val)) return os << (unsigned long long)(val);
std::string s;
while (val) {
s.push_back(char('0' + (val % 10)));
val /= 10;
}
std::reverse(s.begin(), s.end());
return os << s;
}
} // namespace io
} // namespace cp
#line 2 "util\\io-utility.hpp"
#line 6 "util\\io-utility.hpp"
#include <charconv>
#line 10 "util\\io-utility.hpp"
#include <ranges>
#line 14 "util\\io-utility.hpp"
#include <type_traits>
#line 17 "util\\io-utility.hpp"
#line 20 "util\\io-utility.hpp"
namespace cp {
namespace io {
namespace internal {
std::string combine_outputs(const std::vector<std::string>& outs) {
char delimiter = ' ';
for (const auto& s : outs) {
double dummy;
auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), dummy);
if (ec != std::errc{} || ptr != s.data() + s.size()) {
delimiter = '\n';
break;
}
}
std::string res;
bool first = true;
for (const auto& s : outs) {
if (!first) res.push_back(delimiter);
res += s;
first = false;
}
return res;
}
} // namespace internal
template <class T, std::size_t Size>
std::istream& operator>>(std::istream& is, std::array<T, Size>& a);
template <class T, class U>
std::istream& operator>>(std::istream& is, std::pair<T, U>& p);
template <class... Args>
std::istream& operator>>(std::istream& is, std::tuple<Args...>& t);
template <class T, class Alloc>
std::istream& operator>>(std::istream& is, std::vector<T, Alloc>& v);
template <class T, std::size_t Size>
std::istream& operator>>(std::istream& is, std::array<T, Size>& a) {
for (auto& x : a) is >> x;
return is;
}
template <class T, class U>
std::istream& operator>>(std::istream& is, std::pair<T, U>& p) {
return is >> p.first >> p.second;
}
template <class... Args>
std::istream& operator>>(std::istream& is, std::tuple<Args...>& t) {
std::apply([&](auto&... args) { (is >> ... >> args); }, t);
return is;
}
template <class T, class Alloc>
std::istream& operator>>(std::istream& is, std::vector<T, Alloc>& v) {
for (auto& x : v) is >> x;
return is;
}
template <class Range>
requires(std::ranges::range<Range> &&
!std::is_convertible_v<Range, std::string>)
std::ostream& operator<<(std::ostream& os, const Range& r);
template <class T, class Container>
std::ostream& operator<<(std::ostream& os, std::queue<T, Container> q);
template <class T, class Container, class Compare>
std::ostream& operator<<(std::ostream& os,
std::priority_queue<T, Container, Compare> pq);
template <class T, class Container>
std::ostream& operator<<(std::ostream& os, std::stack<T, Container> st);
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p);
template <class... Args>
std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& t);
template <class Range>
requires(std::ranges::range<Range> &&
!std::is_convertible_v<Range, std::string>)
std::ostream& operator<<(std::ostream& os, const Range& r) {
std::vector<std::string> outs;
std::ostringstream oss;
for (const auto& x : r) {
oss << x;
outs.push_back(oss.str());
oss.str("");
}
return os << internal::combine_outputs(outs);
}
template <class T, class Container>
std::ostream& operator<<(std::ostream& os, std::queue<T, Container> q) {
std::vector<T> v;
v.reserve(q.size());
while (!q.empty()) {
v.push_back(q.front());
q.pop();
}
return os << v;
}
template <class T, class Container, class Compare>
std::ostream& operator<<(std::ostream& os,
std::priority_queue<T, Container, Compare> pq) {
std::vector<T> v;
v.reserve(pq.size());
while (!pq.empty()) {
v.push_back(pq.top());
pq.pop();
}
return os << v;
}
template <class T, class Container>
std::ostream& operator<<(std::ostream& os, std::stack<T, Container> st) {
std::vector<T> v;
v.reserve(st.size());
while (!st.empty()) {
v.push_back(st.top());
st.pop();
}
return os << v;
}
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
return os << std::tie(p.first, p.second);
}
template <class... Args>
std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& t) {
std::vector<std::string> outs;
outs.reserve(sizeof...(Args));
std::ostringstream oss;
std::apply(
[&](const auto&... args) {
((oss << args, outs.push_back(oss.str()), oss.str("")), ...);
},
t);
return os << internal::combine_outputs(outs);
}
} // namespace io
} // namespace cp
#line 165 "template\\template.hpp"
using namespace io;
#line 2 "util\\static-modint.hpp"
#line 5 "util\\static-modint.hpp"
#line 2 "number\\ext-gcd.hpp"
#line 5 "number\\ext-gcd.hpp"
namespace cp {
// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> ext_gcd(long long a, long long b) {
assert(1 <= b);
a %= b;
if (a < 0) a += b;
long long g0 = b, g1 = a;
long long x0 = 0, x1 = 1;
while (g1) {
long long q = g0 / g1;
g0 -= g1 * q;
x0 -= x1 * q;
std::swap(g0, g1);
std::swap(x0, x1);
}
if (x0 < 0) x0 += b / g0;
return {g0, x0};
}
constexpr long long inv_mod(long long x, long long m) {
assert(1 <= m);
auto z = ext_gcd(x, m);
assert(z.first == 1);
return z.second;
}
} // namespace cp
#line 2 "number\\is-prime.hpp"
#line 4 "number\\is-prime.hpp"
#line 2 "number\\barrett.hpp"
namespace cp {
// Barrett reduction
class Barrett {
public:
constexpr Barrett() : m(0) {}
explicit constexpr Barrett(unsigned int _m) : m(_m), im(-1ULL / _m + 1) {}
constexpr unsigned int umod() const { return m; }
// @param a `0 <= a < m`
// @param b `0 <= b < m`
// @return `a * b % m`
constexpr unsigned int mul(unsigned int a, unsigned int b) const {
// im = ceil(2^64 / m)
// -> im * m = 2^64 + r (0 <= r < m)
// let z = a*b = c*m + d (0 <= c, d < m)
// a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
// c*r + d*im < m*m + m*im < m*m + 2^64 + m = 2^64 + m*(m + 1) < 2^64 * 2
// (a*b * im) >> 64 = c or c + 1
unsigned long long z = a;
z *= b;
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
unsigned long long y = x * m; // y = c*m or c*m + m
return (unsigned int)(z - y + (z < y ? m : 0)); // d = a*b - c*m
}
private:
unsigned int m;
unsigned long long im;
};
} // namespace cp
#line 2 "number\\pow-mod.hpp"
#line 4 "number\\pow-mod.hpp"
#line 6 "number\\pow-mod.hpp"
namespace cp {
constexpr long long pow_mod(long long x, long long n, long long m) {
assert(0 <= n && 1 <= m);
if (m == 1) return 0;
if (m < (1LL << 32)) {
Barrett bt((unsigned int)(m));
x %= m;
if (x < 0) x += m;
unsigned int r = 1, y = (unsigned int)(x);
while (n) {
if (n & 1) r = bt.mul(r, y);
y = bt.mul(y, y);
n >>= 1;
}
return r;
} else {
__int128 r = 1, y = x % m;
if (y < 0) y += m;
while (n) {
if (n & 1) r = (r * y) % m;
y = (y * y) % m;
n >>= 1;
}
return (long long)(r);
}
}
} // namespace cp
#line 7 "number\\is-prime.hpp"
namespace cp {
// Miller窶迭abin primality test
// https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
// https://docslib.org/doc/5395180/fast-primality-testing-for-integers-that-fit-into-a-machine-word
constexpr bool is_prime(long long n) {
if (n <= 2) return n == 2;
if (n % 2 == 0) return false;
int base_num;
std::array<long long, 7> bases;
if (n < 4759123141) {
base_num = 3;
bases = {2, 7, 61};
} else {
base_num = 7;
bases = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
}
long long d = n - 1;
while (d % 2 == 0) d /= 2;
for (int i = 0; i < base_num; i++) {
long long a = bases[i];
if (a % n == 0) continue;
__int128 x = pow_mod(a, d, n);
long long t = d;
while (t != n - 1 && x != 1 && x != n - 1) {
x = x * x % n;
t *= 2;
}
if (x != n - 1 && t % 2 == 0) return false;
}
return true;
}
} // namespace cp
#line 2 "util\\type-traits.hpp"
#line 4 "util\\type-traits.hpp"
namespace cp {
namespace internal {
// is_int128
template <class T> using is_signed_int128 = std::is_same<T, __int128>;
template <class T>
inline constexpr bool is_signed_int128_v = is_signed_int128<T>::value;
template <class T>
using is_unsigned_int128 = std::is_same<T, unsigned __int128>;
template <class T>
inline constexpr bool is_unsigned_int128_v = is_unsigned_int128<T>::value;
template <class T>
using is_128bit_int =
std::conditional_t<is_signed_int128_v<T> || is_unsigned_int128_v<T>,
std::true_type, std::false_type>;
template <class T>
inline constexpr bool is_128bit_int_v = is_128bit_int<T>::value;
// is_integral
template <class T>
using is_signed_int =
std::conditional_t<(std::is_integral_v<T> && std::is_signed_v<T>) ||
is_signed_int128_v<std::remove_cv_t<T>>,
std::true_type, std::false_type>;
template <class T>
inline constexpr bool is_signed_int_v = is_signed_int<T>::value;
template <class T>
using is_unsigned_int =
std::conditional_t<(std::is_integral_v<T> && std::is_unsigned_v<T>) ||
is_unsigned_int128_v<std::remove_cv_t<T>>,
std::true_type, std::false_type>;
template <class T>
inline constexpr bool is_unsigned_int_v = is_unsigned_int<T>::value;
template <class T>
using is_integral = std::conditional_t<std::is_integral_v<T> ||
is_128bit_int_v<std::remove_cv_t<T>>,
std::true_type, std::false_type>;
template <class T> inline constexpr bool is_integral_v = is_integral<T>::value;
// is_float128
template <class T> using is_float128 = std::is_same<T, __float128>;
template <class T> inline constexpr bool is_float128_v = is_float128<T>::value;
// is_floating_point
template <class T>
using is_floating_point =
std::conditional_t<std::is_floating_point_v<T> ||
is_float128_v<std::remove_cv_t<T>>,
std::true_type, std::false_type>;
template <class T>
inline constexpr bool is_floating_point_v = is_floating_point<T>::value;
// is_modint
template <class> struct is_modint : public std::false_type {};
template <class T> inline constexpr bool is_modint_v = is_modint<T>::value;
template <class> struct is_static_modint : public std::false_type {};
template <class T>
inline constexpr bool is_static_modint_v = is_static_modint<T>::value;
template <class> struct is_dynamic_modint : public std::false_type {};
template <class T>
inline constexpr bool is_dynamic_modint_v = is_dynamic_modint<T>::value;
} // namespace internal
} // namespace cp
#line 9 "util\\static-modint.hpp"
namespace cp {
template <int m> struct static_modint {
static_assert(1 <= m, "modulo must be positive.");
public:
using mint = static_modint;
static constexpr int mod() { return m; }
static_modint() : v(0) {}
template <class T>
requires(internal::is_signed_int_v<T>)
static_modint(T _v) {
long long x = (long long)(_v % (long long)(umod()));
if (x < 0) x += umod();
v = (unsigned int)(x);
}
template <class T>
requires(internal::is_unsigned_int_v<T>)
static_modint(T _v) {
v = (unsigned int)(_v % umod());
}
int val() const { return v; }
mint& operator++() {
v++;
if (v == umod()) v = 0;
return *this;
}
mint& operator--() {
if (v == 0) v = umod();
v--;
return *this;
}
mint operator++(int) {
mint res = *this;
++*this;
return res;
}
mint operator--(int) {
mint res = *this;
--*this;
return res;
}
mint& operator+=(const mint& rhs) {
v += rhs.v;
if (v >= umod()) v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
v -= rhs.v;
if (v >= umod()) v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = v;
z *= rhs.v;
v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
if (prime) {
assert(v);
return pow(umod() - 2);
} else {
auto eg = ext_gcd(v, m);
assert(eg.first == 1);
return eg.second;
}
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs.v == rhs.v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs.v != rhs.v;
}
friend bool operator<(const mint& lhs, const mint& rhs) {
return lhs.v < rhs.v;
}
friend bool operator>(const mint& lhs, const mint& rhs) {
return lhs.v > rhs.v;
}
friend bool operator<=(const mint& lhs, const mint& rhs) {
return lhs.v <= rhs.v;
}
friend bool operator>=(const mint& lhs, const mint& rhs) {
return lhs.v >= rhs.v;
}
friend std::istream& operator>>(std::istream& is, mint& x) {
long long t;
is >> t;
x = t;
return is;
}
friend std::ostream& operator<<(std::ostream& os, const mint& x) {
return os << x.v;
}
private:
static constexpr bool prime = is_prime(m);
static constexpr unsigned int umod() { return m; }
unsigned int v;
};
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
namespace internal {
template <int m> struct is_modint<static_modint<m>> : public std::true_type {};
template <int m>
struct is_static_modint<static_modint<m>> : public std::true_type {};
} // namespace internal
} // namespace cp
#line 168 "template\\template.hpp"
using mint = cp::modint998244353;
#line 2 "random\\engine.hpp"
#line 8 "random\\engine.hpp"
namespace cp {
struct xorshift64 {
public:
using result_type = unsigned long long;
static constexpr unsigned long long min() { return 0; }
static constexpr unsigned long long max() { return -1ULL; }
xorshift64() : x(88172645463325252ULL) {}
explicit xorshift64(unsigned long long seed) : x(seed) {}
unsigned long long operator()() {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
return x;
}
private:
unsigned long long x;
};
inline xorshift64 rng(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count());
template <class T> T uniform(T l, T r) {
assert(l <= r);
return std::uniform_int_distribution<T>(l, r)(rng);
}
bool uniform_bool() { return rng() % 2; }
double uniform01() {
static std::uniform_real_distribution dist(0.0, 1.0);
return dist(rng);
}
template <class Container> void shuffle(Container& a) {
std::shuffle(a.begin(), a.end(), rng);
}
} // namespace cp
#line 2 "util\\algo-utility.hpp"
#line 9 "util\\algo-utility.hpp"
namespace cp {
// replace `a[i]` with `a[p[i]]` by the minimum number of `std::swap`
template <class Range, class Indices>
requires(std::ranges::range<Range>)
const Indices& rearrange(Range& a, const Indices& p) {
assert(a.size() == p.size());
std::vector<bool> processed(a.size());
for (int i = 0; i < int(a.size()); i++) {
if (processed[i]) continue;
processed[i] = true;
for (int j = p[i]; j != i; j = p[j]) {
assert(!processed[j]);
processed[j] = true;
std::swap(a[j], a[p[j]]);
}
}
return p;
}
template <class Range,
class Compare = std::less<std::ranges::range_value_t<Range>>>
requires(std::ranges::range<Range>)
std::vector<int> sorted_indices(const Range& a, Compare compare = Compare{}) {
std::vector<int> p(a.size());
std::iota(p.begin(), p.end(), 0);
std::sort(p.begin(), p.end(), [&](int i, int j) {
return compare(a[i], a[j]) || (!compare(a[j], a[i]) && i < j);
});
return p;
}
template <class Range,
class Compare = std::less<std::ranges::range_value_t<Range>>>
requires(std::ranges::range<Range>)
std::vector<int> sort(Range&& a, Compare compare = Compare{}) {
return rearrange(a, sorted_indices(a, compare));
}
template <class Range,
class Compare = std::less<std::ranges::range_value_t<Range>>>
requires(std::ranges::range<Range>)
std::vector<int> compress(Range a, Compare compare = Compare{}) {
int n = int(a.size());
std::vector<int> res(n);
auto p = sort(a, compare);
for (int i = 0, j = 0; i < n; i++) {
if (i > 0 && compare(a[i - 1], a[i])) j++;
res[p[i]] = j;
}
return res;
}
template <class Range>
requires(std::ranges::range<Range>)
Range inverse(const Range& p) {
Range q(p.size());
std::fill(q.begin(), q.end(), -1);
for (int i = 0; i < int(p.size()); i++) {
assert(0 <= p[i] && p[i] < int(p.size()));
assert(q[p[i]] == -1);
q[p[i]] = i;
}
return q;
}
} // namespace cp
#line 2 "util\\math-utility.hpp"
#line 6 "util\\math-utility.hpp"
#line 8 "util\\math-utility.hpp"
namespace cp {
template <class T = long long>
requires(internal::is_integral_v<T>)
T ipow(long long x, long long n) {
assert(0 <= n);
T r = 1, t = x;
while (n) {
if (n & 1) r *= t;
t *= t;
n >>= 1;
}
return r;
}
template <class T>
requires(internal::is_integral_v<T>)
T isqrt(T x) {
assert(0 <= x);
T y = T(std::sqrt(double(x)));
if (sizeof(T) > 8) y = (y + x / y) / 2; // Newton's method
while (y != 0 && y > x / y) y--;
while ((y + 1) <= x / (y + 1)) y++;
return y;
}
template <class T>
requires(internal::is_integral_v<T>)
T icbrt(T x) {
T y = T(std::cbrt(double(x)));
while (y != 0 && y * y > x / y) y--;
while ((y + 1) * (y + 1) <= x / (y + 1)) y++;
return y;
}
template <class T> int sign(T x) { return x < 0 ? -1 : x == 0 ? 0 : 1; }
template <class T, class U>
requires(internal::is_integral_v<T> && internal::is_integral_v<U>)
std::common_type_t<T, U> floor(T n, U d) {
assert(d != 0);
if (d < 0) {
n = -n;
d = -d;
}
if (n < 0) return (n + 1) / d - 1;
return n / d;
}
template <class T, class U>
requires(internal::is_integral_v<T> && internal::is_integral_v<U>)
std::common_type_t<T, U> ceil(T n, U d) {
return floor(n - sign(d), d) + 1;
}
template <class T, class U>
requires(internal::is_integral_v<T> && internal::is_integral_v<U>)
std::common_type_t<T, U> mod(T n, U m) {
return n - floor(n, m) * m;
}
} // namespace cp
#line 2 "template\\template.cpp"
#line 2 "graph\\dijkstra.hpp"
#line 8 "graph\\dijkstra.hpp"
namespace cp {
template <class T> struct Dijkstra {
public:
Dijkstra() : Dijkstra(0) {}
explicit Dijkstra(int _n) : n(_n), g(_n), d(_n), prev_v(_n), prev_e(_n) {}
int add_edge(int from, int to, T cost) {
assert(0 <= from && from < n);
assert(0 <= to && to < n);
assert(0 <= cost);
g[from].push_back(Edge{to, m, cost});
return m++;
}
void init(int from) {
assert(0 <= from && from < n);
std::fill(d.begin(), d.end(), T(-1));
std::priority_queue<std::tuple<T, int, int, int>> pq;
pq.emplace(0, from, -1, -1);
while (!pq.empty()) {
auto [c, v, pv, pe] = pq.top();
pq.pop();
if (d[v] != T(-1)) continue;
d[v] = -c;
prev_v[v] = pv;
prev_e[v] = pe;
for (auto e : g[v]) pq.emplace(c - e.cost, e.to, v, e.id);
}
initialized = true;
}
T operator()(int to) const {
assert(initialized);
assert(0 <= to && to < n);
return d[to];
}
std::pair<std::vector<int>, std::vector<int>> route(int to) const {
assert(initialized);
assert(0 <= to && to < n);
if (d[to] == T(-1)) return {};
std::vector<int> vs, es;
while (prev_v[to] != -1) {
vs.push_back(to);
es.push_back(prev_e[to]);
to = prev_v[to];
}
vs.push_back(to);
std::reverse(vs.begin(), vs.end());
std::reverse(es.begin(), es.end());
return {vs, es};
}
private:
struct Edge {
int to, id;
T cost;
};
int n, m = 0;
std::vector<std::vector<Edge>> g;
std::vector<T> d;
std::vector<int> prev_v, prev_e;
bool initialized = false;
};
} // namespace cp
#line 4 "template\\template.cpp"
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (local) cout << unitbuf;
ll n, m;
cin >> n >> m;
vec<vec<pair<ll, ll>>> adj(n);
Dijkstra<ll> dist(n);
rep(i, m) {
ll a, b, c;
cin >> a >> b >> c;
a--, b--;
adj[a].emplace_back(c, b);
adj[b].emplace_back(c, a);
dist.add_edge(a, b, c);
dist.add_edge(b, a, c);
}
dist.init(0);
rep(i, n) sort(adj[i]);
ll now = n - 1;
ll t = 0;
rep(m) {
auto [c, nxt] = adj[now][0];
if (t == dist(now)) {
cout << "Yes\n";
cerr << 1 << "\n";
return 0;
}
if (dist(now) - dist(nxt) == c && t <= dist(now) && dist(now) <= t + c) {
cout << "Yes\n";
cerr << 2 << "\n";
return 0;
}
now = nxt;
t += c;
}
auto [c, nxt] = adj[now][0];
if (t <= dist(now) && (dist(now) - t) % c == 0) {
cout << "Yes\n";
cerr << 3 << "\n";
return 0;
}
if (t + c <= dist(nxt) && (dist(now) - t) % c == 0) {
cout << "Yes\n";
cerr << 4 << "\n";
return 0;
}
if (dist(nxt) - dist(now) == c && t <= min(dist(now), dist(nxt))) {
cout << "Yes\n";
cerr << 5 << "\n";
return 0;
}
cout << "No\n";
}