結果
| 問題 | No.3651 K-th Sum of Divisors |
| コンテスト | |
| ユーザー |
maspy
|
| 提出日時 | 2026-08-28 21:30:49 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 100 ms / 2,000 ms |
| + 294µs | |
| コード長 | 50,281 bytes |
| 記録 | |
| コンパイル時間 | 5,838 ms |
| コンパイル使用メモリ | 405,524 KB |
| 実行使用メモリ | 36,556 KB |
| 最終ジャッジ日時 | 2026-08-28 21:31:08 |
| 合計ジャッジ時間 | 13,767 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 55 |
ソースコード
// BEGIN: main.cpp
#line 1 "main.cpp"
// BEGIN: my_template.hpp
#line 1 "my_template.hpp"
#if defined(USE_PCH)
#include <my_template_compiled.hpp>
#else
#if defined(__GNUC__)
#include <bits/allocator.h>
#pragma GCC optimize("Ofast,unroll-loops")
// 環境によってはコンパイル成功かつ実行時エラー
#pragma GCC target("avx2,popcnt")
#endif
#include <bits/stdc++.h>
#include <cassert>
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> = 2'020'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>) * 2'000'000'000'000'000'000;
template <>
constexpr double infty<double> = numeric_limits<double>::infinity();
template <>
constexpr long double infty<long double> =
numeric_limits<long double>::infinity();
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 FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#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 overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, 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>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (1) {
ll 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;
}
// ? は -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) {
first.reserve(first.size() + (others.size() + ... + 0));
(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
// END: my_template.hpp
#line 2 "main.cpp"
// BEGIN: other/io.hpp
#line 1 "other/io.hpp"
#define FASTIO
// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;
bool input_eof = false;
template <class T>
constexpr bool is_signed_integer_v = is_signed_v<T> || is_same_v<T, i128>;
template <class T>
struct unsigned_integer {
using type = make_unsigned_t<T>;
};
template <>
struct unsigned_integer<i128> {
using type = u128;
};
template <>
struct unsigned_integer<u128> {
using type = u128;
};
template <class T>
using unsigned_integer_t = typename unsigned_integer<T>::type;
[[noreturn]] inline void input_error(const char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
struct Pre {
char num[10000][4];
constexpr Pre() : num() {
for (int i = 0; i < 10000; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i][j] = n % 10 | '0';
n /= 10;
}
}
}
} constexpr pre;
inline void load() {
uint32_t n = pir - pil;
memmove(ibuf, ibuf + pil, n);
pil = 0;
pir = n;
if (input_eof) return;
pir += fread(ibuf + pir, 1, SZ - pir, stdin);
if (ferror(stdin)) input_error("fastio: input error");
if (feof(stdin)) {
input_eof = true;
// Allows the last token to end exactly at EOF without a trailing
// whitespace.
if (pir < SZ) ibuf[pir++] = '\n';
}
}
inline char get_char() {
if (pil == pir) {
load();
if (pil == pir) input_error("fastio: unexpected EOF");
}
return ibuf[pil++];
}
inline void flush() {
fwrite(obuf, 1, por, stdout);
por = 0;
}
void rd(char &c) {
do c = get_char();
while (isspace(static_cast<unsigned char>(c)));
}
void rd(string &x) {
x.clear();
char c;
do c = get_char();
while (isspace(static_cast<unsigned char>(c)));
do {
x += c;
c = get_char();
} while (!isspace(static_cast<unsigned char>(c)));
}
template <typename T>
void rd_real(T &x) {
string s;
rd(s);
x = stod(s);
}
template <typename T>
void rd_integer_slow(T &x) {
char c;
do c = get_char();
while (c < '-');
bool minus = 0;
if constexpr (is_signed_integer_v<T>) {
if (c == '-') {
minus = 1, c = get_char();
}
}
x = 0;
assert('0' <= c && c <= '9');
while ('0' <= c && c <= '9') {
x = x * 10 + (c & 15), c = get_char();
}
assert(isspace(static_cast<unsigned char>(c)));
if constexpr (is_signed_integer_v<T>) {
if (minus) x = -x;
}
}
template <typename T>
void rd_integer(T &x) {
if (pil + 100 > pir) {
load();
if (pil + 100 > pir) {
rd_integer_slow(x);
return;
}
}
char c;
do c = ibuf[pil++];
while (c < '-');
bool minus = 0;
if constexpr (is_signed_integer_v<T>) {
if (c == '-') {
minus = 1, c = ibuf[pil++];
}
}
x = 0;
assert('0' <= c && c <= '9');
while ('0' <= c && c <= '9') {
x = x * 10 + (c & 15), c = ibuf[pil++];
}
assert(isspace(static_cast<unsigned char>(c)));
if constexpr (is_signed_integer_v<T>) {
if (minus) x = -x;
}
}
template <class T>
enable_if_t<is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>> rd(
T &x) {
rd_integer(x);
}
template <class T>
enable_if_t<is_floating_point_v<T> || is_same_v<T, f128>> rd(T &x) {
rd_real(x);
}
template <class T, class U>
void rd(pair<T, U> &p) {
rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
if constexpr (N < tuple_size<T>::value) {
auto &x = get<N>(t);
rd(x);
rd_tuple<N + 1>(t);
}
}
template <class... T>
void rd(tuple<T...> &tpl) {
rd_tuple(tpl);
}
template <class T, size_t N>
void rd(array<T, N> &x) {
for (auto &d : x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
for (auto &d : x) rd(d);
}
template <class... T>
void read(T &...x) {
(rd(x), ...);
}
inline void wt_range(const char *s, size_t n) {
size_t i = 0;
while (i < n) {
if (por == SZ) flush();
size_t chunk = min(n - i, (size_t)(SZ - por));
memcpy(obuf + por, s + i, chunk);
por += chunk;
i += chunk;
}
}
void wt(const char c) {
if (por == SZ) flush();
obuf[por++] = c;
}
void wt(const char *s) { wt_range(s, strlen(s)); }
void wt(const string &s) { wt_range(s.data(), s.size()); }
template <typename T>
void wt_integer(T x) {
if (por > SZ - 100) flush();
using U = unsigned_integer_t<T>;
U y = static_cast<U>(x);
if constexpr (is_signed_integer_v<T>) {
if (x < 0) {
obuf[por++] = '-';
y = U(0) - y;
}
}
int outi;
for (outi = 96; y >= 10000; outi -= 4) {
memcpy(out + outi, pre.num[y % 10000], 4);
y /= 10000;
}
if (y >= 1000) {
memcpy(obuf + por, pre.num[y], 4);
por += 4;
} else if (y >= 100) {
memcpy(obuf + por, pre.num[y] + 1, 3);
por += 3;
} else if (y >= 10) {
int q = (y * 103) >> 10;
obuf[por] = q | '0';
obuf[por + 1] = (y - q * 10) | '0';
por += 2;
} else
obuf[por++] = y | '0';
memcpy(obuf + por, out + outi + 4, 96 - outi);
por += 96 - outi;
}
template <typename T>
inline void wt_real(T x) {
static char buf[1000];
int n = std::snprintf(buf, sizeof(buf), "%.15f", (double)x);
wt_range(buf, (size_t)n);
}
template <class T>
enable_if_t<is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>> wt(
T x) {
wt_integer(x);
}
template <class T>
enable_if_t<is_floating_point_v<T> || is_same_v<T, f128>> wt(T x) {
wt_real(x);
}
inline void wt(bool b) { wt(static_cast<char>('0' + (b ? 1 : 0))); }
template <class T, class U>
void wt(const pair<T, U> &val) {
wt(val.first);
wt(' ');
wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T &t) {
if constexpr (N < tuple_size<T>::value) {
if constexpr (N > 0) wt(' ');
wt(get<N>(t));
wt_tuple<N + 1>(t);
}
}
template <class... T>
void wt(const tuple<T...> &tpl) {
wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> &val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
template <class T>
void wt(const vector<T> &val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
wt(forward<Head>(head));
((wt(' '), wt(forward<Tail>(tail))), ...);
wt('\n');
}
// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::flush;
using fastio::print;
using fastio::read;
#if defined(LOCAL)
#define HDR "[DEBUG:", __func__, __LINE__, "]"
#define SHOW(...) \
SHOW_IMPL(__VA_ARGS__, SHOW8, SHOW7, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, \
SHOW1) \
(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
#define SHOW1(x) print(HDR, #x, "=", (x)), flush()
#define SHOW2(x, y) print(HDR, #x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v)), \
flush()
#define SHOW6(x, y, z, w, v, u) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v), #u, "=", (u)), \
flush()
#define SHOW7(x, y, z, w, v, u, t) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v), #u, "=", (u), #t, "=", (t)), \
flush()
#define SHOW8(x, y, z, w, v, u, t, s) \
print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \
(v), #u, "=", (u), #t, "=", (t), #s, "=", (s)), \
flush()
#else
#define SHOW(...)
#endif
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define U32(...) \
u32 __VA_ARGS__; \
read(__VA_ARGS__)
#define U64(...) \
u64 __VA_ARGS__; \
read(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
read(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
read(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
read(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
read(name)
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
read(name)
void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void YA(bool t = 1) { print(t ? "YA" : "TIDAK"); }
void TIDAK(bool t = 1) { YA(!t); }
void Alice(bool t = 1) { print(t ? "Alice" : "Bob"); }
void Bob(bool t = 1) { Alice(!t); }// END: other/io.hpp
#line 3 "main.cpp"
// BEGIN: nt/factor.hpp
#line 1 "nt/factor.hpp"
// BEGIN: random/base.hpp
#line 1 "random/base.hpp"
u64 RNG_64() {
static u64 x_ = u64(chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count()) * 10150724397891781847ULL;
x_ ^= x_ << 7;
return x_ ^= x_ >> 9;
}
u64 RNG(u64 lim) { return RNG_64() % lim; }
ll RNG(ll l, ll r) { return l + RNG_64() % (r - l); }
// END: random/base.hpp
#line 3 "nt/factor.hpp"
// BEGIN: nt/is_prime.hpp
#line 1 "nt/is_prime.hpp"
// BEGIN: other/bit.hpp
#line 1 "other/bit.hpp"
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) {
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
return x >> k & 1;
}
template <typename UINT>
struct all_bit {
UINT s;
all_bit(UINT s) : s(s) {}
struct iter {
UINT 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 UINT>
struct all_subset {
UINT s;
all_subset(UINT s) : s(s) {}
struct iter {
UINT s, t;
bool done = false;
UINT 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) { 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;
}// END: other/bit.hpp
#line 2 "nt/is_prime.hpp"
// BEGIN: mod/montgomery_modint.hpp
#line 1 "mod/montgomery_modint.hpp"
// odd mod.
// x の代わりに rx を持つ
template <int id, typename U1, typename U2>
struct Montgomery_modint {
using mint = Montgomery_modint;
inline static U1 m, r, n2;
static constexpr int W = numeric_limits<U1>::digits;
static void set_mod(U1 mod) {
assert(mod & 1 && mod <= U1(1) << (W - 2));
m = mod, n2 = -U2(m) % m, r = m;
FOR(6) r *= 2 - m * r;
r = -r;
assert(r * m == U1(-1));
}
static U1 reduce(U2 b) { return (b + U2(U1(b) * r) * m) >> W; }
U1 x;
Montgomery_modint() : x(0) {}
Montgomery_modint(U1 x) : x(reduce(U2(x) * n2)){};
U1 val() const {
U1 y = reduce(x);
return y >= m ? y - m : y;
}
mint &operator+=(mint y) {
x = ((x += y.x) >= m ? x - m : x);
return *this;
}
mint &operator-=(mint y) {
x -= (x >= y.x ? y.x : y.x - m);
return *this;
}
mint &operator*=(mint y) {
x = reduce(U2(x) * y.x);
return *this;
}
mint operator+(mint y) const { return mint(*this) += y; }
mint operator-(mint y) const { return mint(*this) -= y; }
mint operator*(mint y) const { return mint(*this) *= y; }
bool operator==(mint y) const {
return (x >= m ? x - m : x) == (y.x >= m ? y.x - m : y.x);
}
bool operator!=(mint y) const { return not operator==(y); }
mint pow(ll n) const {
assert(n >= 0);
mint y = 1, z = *this;
for (; n; n >>= 1, z *= z)
if (n & 1) y *= z;
return y;
}
};
template <int id>
using Montgomery_modint_32 = Montgomery_modint<id, u32, u64>;
template <int id>
using Montgomery_modint_64 = Montgomery_modint<id, u64, u128>;
// END: mod/montgomery_modint.hpp
#line 3 "nt/is_prime.hpp"
bool is_prime(const u64 x) {
assert(x < u64(1) << 62);
if (x == 2 or x == 3 or x == 5 or x == 7) return true;
if (x % 2 == 0 or x % 3 == 0 or x % 5 == 0 or x % 7 == 0) return false;
if (x < 121) return x > 1;
const u64 d = (x - 1) >> lowbit(x - 1);
using mint = Montgomery_modint_64<202311020>;
mint::set_mod(x);
const mint one(u64(1)), minus_one(x - 1);
auto ok = [&](u64 a) -> bool {
auto y = mint(a).pow(d);
u64 t = d;
while (y != one && y != minus_one && t != x - 1) y *= y, t <<= 1;
if (y != minus_one && t % 2 == 0) return false;
return true;
};
if (x < (u64(1) << 32)) {
for (u64 a : {2, 7, 61})
if (!ok(a)) return false;
} else {
for (u64 a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
if (!ok(a)) return false;
}
}
return true;
}// END: nt/is_prime.hpp
#line 4 "nt/factor.hpp"
template <typename mint>
ll rho(ll n, ll c) {
assert(n > 1);
const mint cc(c);
auto f = [&](mint x) { return x * x + cc; };
mint x = 1, y = 2, z = 1, q = 1;
ll g = 1;
const ll m = 1LL << (__lg(n) / 5);
for (ll r = 1; g == 1; r <<= 1) {
x = y;
FOR(r) y = f(y);
for (ll k = 0; k < r && g == 1; k += m) {
z = y;
FOR(min(m, r - k)) y = f(y), q *= x - y;
g = gcd(q.val(), n);
}
}
if (g == n) do {
z = f(z);
g = gcd((x - z).val(), n);
} while (g == 1);
return g;
}
ll find_prime_factor(ll n) {
assert(n > 1);
if (is_prime(n)) return n;
FOR(100) {
ll m = 0;
if (n < (1 << 30)) {
using mint = Montgomery_modint_32<20231025>;
mint::set_mod(n);
m = rho<mint>(n, RNG(0, n));
} else {
using mint = Montgomery_modint_64<20231025>;
mint::set_mod(n);
m = rho<mint>(n, RNG(0, n));
}
if (is_prime(m)) return m;
n = m;
}
assert(0);
return -1;
}
// ソートしてくれる
vc<pair<ll, int>> factor(ll n) {
assert(n >= 1);
vc<pair<ll, int>> pf;
FOR(p, 2, 100) {
if (p * p > n) break;
if (n % p == 0) {
ll e = 0;
do {
n /= p, e += 1;
} while (n % p == 0);
pf.eb(p, e);
}
}
while (n > 1) {
ll p = find_prime_factor(n);
ll e = 0;
do {
n /= p, e += 1;
} while (n % p == 0);
pf.eb(p, e);
}
sort(all(pf));
return pf;
}
vc<pair<ll, int>> factor_by_lpf(ll n, vc<int>& lpf) {
vc<pair<ll, int>> res;
while (n > 1) {
int p = lpf[n];
int e = 0;
while (n % p == 0) {
n /= p;
++e;
}
res.eb(p, e);
}
return res;
}
// END: nt/factor.hpp
#line 5 "main.cpp"
// BEGIN: nt/lpf_table.hpp
#line 1 "nt/lpf_table.hpp"
// BEGIN: nt/prime_table.hpp
#line 1 "nt/prime_table.hpp"
template <typename T = int>
vc<T> prime_table(int LIM) {
++LIM;
const int S = 32768;
static int done = 2;
static vc<T> primes = {2}, sieve(S + 1);
if (done < LIM) {
done = LIM;
primes = {2}, sieve.assign(S + 1, 0);
const int R = LIM / 2;
primes.reserve(int(LIM / log(LIM) * 1.1));
vc<pair<int, int>> cp;
for (int i = 3; i <= S; i += 2) {
if (!sieve[i]) {
cp.eb(i, i * i / 2);
for (int j = i * i; j <= S; j += 2 * i) sieve[j] = 1;
}
}
for (int L = 1; L <= R; L += S) {
array<bool, S> block{};
for (auto& [p, idx] : cp)
for (int i = idx; i < S + L; idx = (i += p)) block[i - L] = 1;
FOR(i, min(S, R - L)) if (!block[i]) primes.eb((L + i) * 2 + 1);
}
}
int k = LB(primes, LIM + 1);
return {primes.begin(), primes.begin() + k};
}
// END: nt/prime_table.hpp
#line 2 "nt/lpf_table.hpp"
// [0, LIM], 0, 1 には -1 が入る。
vc<int> lpf_table(ll LIM) {
auto primes = prime_table(LIM);
vc<int> res(LIM + 1, -1);
FOR_R(i, len(primes)) {
auto p = primes[i];
FOR3(j, 1, LIM / p + 1) res[p * j] = p;
}
return res;
}
// END: nt/lpf_table.hpp
#line 6 "main.cpp"
// BEGIN: graph/functional_graph.hpp
#line 1 "graph/functional_graph.hpp"
// BEGIN: alg/monoid/add.hpp
#line 1 "alg/monoid/add.hpp"
template <typename E>
struct Monoid_Add {
using X = E;
using value_type = X;
static constexpr X op(const X &x, const X &y) noexcept { return x + y; }
static constexpr X inverse(const X &x) noexcept { return -x; }
static constexpr X power(const X &x, ll n) noexcept { return X(n) * x; }
static constexpr X unit() { return X(0); }
static constexpr bool commute = true;
};
// END: alg/monoid/add.hpp
#line 2 "graph/functional_graph.hpp"
// BEGIN: alg/monoid_pow.hpp
#line 1 "alg/monoid_pow.hpp"
// chat gpt
template <typename U, typename Arg1, typename Arg2>
struct has_power_method {
private:
// ヘルパー関数の実装
template <typename V, typename A1, typename A2>
static auto check(int)
-> decltype(std::declval<V>().power(std::declval<A1>(),
std::declval<A2>()),
std::true_type{});
template <typename, typename, typename>
static auto check(...) -> std::false_type;
public:
// メソッドの有無を表す型
static constexpr bool value = decltype(check<U, Arg1, Arg2>(0))::value;
};
template <typename Monoid>
typename Monoid::X monoid_pow(typename Monoid::X x, ll exp) {
using X = typename Monoid::X;
if constexpr (has_power_method<Monoid, X, ll>::value) {
return Monoid::power(x, exp);
} else {
assert(exp >= 0);
if (exp == 0) return Monoid::unit();
if (exp == 1) return x;
X res = Monoid::unit();
while (exp) {
if (exp & 1) res = Monoid::op(res, x);
x = Monoid::op(x, x);
exp >>= 1;
}
return res;
}
}// END: alg/monoid_pow.hpp
#line 3 "graph/functional_graph.hpp"
// BEGIN: graph/tree.hpp
#line 1 "graph/tree.hpp"
// BEGIN: graph/base.hpp
#line 1 "graph/base.hpp"
// BEGIN: ds/hashmap.hpp
#line 1 "ds/hashmap.hpp"
// u64 -> Val
template <typename Val>
struct HashMap {
// n は入れたいものの個数で ok
HashMap(u32 n = 0) { build(n); }
void build(u32 n) {
u32 k = 8;
while (k < n * 2) k *= 2;
cap = k / 2, mask = k - 1;
key.resize(k), val.resize(k), used.assign(k, 0);
}
// size を保ったまま. size=0 にするときは build すること.
void clear() {
used.assign(len(used), 0);
cap = (mask + 1) / 2;
}
int size() { return len(used) / 2 - cap; }
int index(const u64& k) {
int i = 0;
for (i = hash(k); used[i] && key[i] != k; i = (i + 1) & mask) {}
return i;
}
Val& operator[](const u64& k) {
if (cap == 0) extend();
int i = index(k);
if (!used[i]) { used[i] = 1, key[i] = k, val[i] = Val{}, --cap; }
return val[i];
}
Val get(const u64& k, Val default_value) {
int i = index(k);
return (used[i] ? val[i] : default_value);
}
bool count(const u64& k) {
int i = index(k);
return used[i] && key[i] == k;
}
// f(key, val)
template <typename F>
void enumerate_all(F f) {
FOR(i, len(used)) if (used[i]) f(key[i], val[i]);
}
private:
u32 cap, mask;
vc<u64> key;
vc<Val> val;
vc<bool> used;
u64 hash(u64 x) {
static const u64 FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();
x += FIXED_RANDOM;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return (x ^ (x >> 31)) & mask;
}
void extend() {
vc<pair<u64, Val>> dat;
dat.reserve(len(used) / 2 - cap);
FOR(i, len(used)) {
if (used[i]) dat.eb(key[i], val[i]);
}
build(2 * len(dat));
for (auto& [a, b]: dat) (*this)[a] = b;
}
};// END: ds/hashmap.hpp
#line 2 "graph/base.hpp"
template <typename T>
struct Edge {
int frm, to;
T cost;
int id;
};
template <typename T = int, bool directed = false>
struct Graph {
static constexpr bool is_directed = directed;
int N, M;
using cost_type = T;
using edge_type = Edge<T>;
vector<edge_type> edges;
vector<int> indptr;
vector<edge_type> csr_edges;
vc<int> vc_deg, vc_indeg, vc_outdeg;
HashMap<int> MP_FOR_EID;
bool prepared;
class OutgoingEdges {
public:
OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {}
const edge_type* begin() const {
if (l == r) {
return 0;
}
return &G->csr_edges[l];
}
const edge_type* end() const {
if (l == r) {
return 0;
}
return &G->csr_edges[r];
}
private:
const Graph* G;
int l, r;
};
bool is_prepared() { return prepared; }
Graph() : N(0), M(0), prepared(0) {}
Graph(int N) : N(N), M(0), prepared(0) {}
void build(int n) {
N = n, M = 0;
prepared = 0;
edges.clear();
indptr.clear();
csr_edges.clear();
vc_deg.clear();
vc_indeg.clear();
vc_outdeg.clear();
MP_FOR_EID.clear();
}
void add(int frm, int to, T cost = 1, int i = -1) {
assert(!prepared);
assert(0 <= frm && frm < N && 0 <= to && to < N);
if (i == -1) i = M;
auto e = edge_type({frm, to, cost, i});
edges.eb(e);
++M;
}
#ifdef FASTIO
// wt, off
void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); }
void read_graph(int M, bool wt = false, int off = 1) {
for (int m = 0; m < M; ++m) {
INT(a, b);
a -= off, b -= off;
if (!wt) {
add(a, b);
} else {
T c;
read(c);
add(a, b, c);
}
}
build();
}
#endif
void build() {
assert(!prepared);
prepared = true;
indptr.assign(N + 1, 0);
for (auto&& e : edges) {
indptr[e.frm + 1]++;
if (!directed) indptr[e.to + 1]++;
}
for (int v = 0; v < N; ++v) {
indptr[v + 1] += indptr[v];
}
auto counter = indptr;
csr_edges.resize(indptr.back() + 1);
for (auto&& e : edges) {
csr_edges[counter[e.frm]++] = e;
if (!directed)
csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id});
}
}
OutgoingEdges operator[](int v) const {
assert(prepared);
return {this, indptr[v], indptr[v + 1]};
}
vc<int> deg_array() {
if (vc_deg.empty()) calc_deg();
return vc_deg;
}
pair<vc<int>, vc<int>> deg_array_inout() {
if (vc_indeg.empty()) calc_deg_inout();
return {vc_indeg, vc_outdeg};
}
int deg(int v) {
if (vc_deg.empty()) calc_deg();
return vc_deg[v];
}
int in_deg(int v) {
if (vc_indeg.empty()) calc_deg_inout();
return vc_indeg[v];
}
int out_deg(int v) {
if (vc_outdeg.empty()) calc_deg_inout();
return vc_outdeg[v];
}
#ifdef FASTIO
void debug() {
#ifdef LOCAL
print("Graph");
if (!prepared) {
print("frm to cost id");
for (auto&& e : edges) print(e.frm, e.to, e.cost, e.id);
} else {
print("indptr", indptr);
print("frm to cost id");
FOR(v, N) for (auto&& e : (*this)[v]) print(e.frm, e.to, e.cost, e.id);
}
flush();
#endif
}
#endif
vc<int> new_idx;
vc<bool> used_e;
// G における頂点 V[i] が、新しいグラフで i になるようにする
// {G, es}
// sum(deg(v)) の計算量になっていて、
// 新しいグラフの n+m より大きい可能性があるので注意
Graph<T, directed> rearrange(vc<int> V, bool keep_eid = 0) {
if (len(new_idx) != N) new_idx.assign(N, -1);
int n = len(V);
FOR(i, n) new_idx[V[i]] = i;
Graph<T, directed> G(n);
vc<int> history;
FOR(i, n) {
for (auto&& e : (*this)[V[i]]) {
if (len(used_e) <= e.id) used_e.resize(e.id + 1);
if (used_e[e.id]) continue;
int a = e.frm, b = e.to;
if (new_idx[a] != -1 && new_idx[b] != -1) {
history.eb(e.id);
used_e[e.id] = 1;
int eid = (keep_eid ? e.id : -1);
G.add(new_idx[a], new_idx[b], e.cost, eid);
}
}
}
FOR(i, n) new_idx[V[i]] = -1;
for (auto&& eid : history) used_e[eid] = 0;
G.build();
return G;
}
Graph<T, true> to_directed_tree(int root = -1) {
if (root == -1) root = 0;
assert(!is_directed && prepared && M == N - 1);
Graph<T, true> G1(N);
vc<int> par(N, -1);
auto dfs = [&](auto& dfs, int v) -> void {
for (auto& e : (*this)[v]) {
if (e.to == par[v]) continue;
par[e.to] = v, dfs(dfs, e.to);
}
};
dfs(dfs, root);
for (auto& e : edges) {
int a = e.frm, b = e.to;
if (par[a] == b) swap(a, b);
assert(par[b] == a);
G1.add(a, b, e.cost);
}
G1.build();
return G1;
}
int get_eid(u64 a, u64 b) {
if (len(MP_FOR_EID) == 0) {
MP_FOR_EID.build(N - 1);
for (auto& e : edges) {
u64 a = e.frm, b = e.to;
u64 k = to_eid_key(a, b);
MP_FOR_EID[k] = e.id;
}
}
return MP_FOR_EID.get(to_eid_key(a, b), -1);
}
u64 to_eid_key(u64 a, u64 b) {
if (!directed && a > b) swap(a, b);
return N * a + b;
}
private:
void calc_deg() {
assert(vc_deg.empty());
vc_deg.resize(N);
for (auto&& e : edges) vc_deg[e.frm]++, vc_deg[e.to]++;
}
void calc_deg_inout() {
assert(vc_indeg.empty());
vc_indeg.resize(N);
vc_outdeg.resize(N);
for (auto&& e : edges) {
vc_indeg[e.to]++, vc_outdeg[e.frm]++;
}
}
};
// END: graph/base.hpp
#line 3 "graph/tree.hpp"
// HLD euler tour をとっていろいろ
// HLD=false: 入力辺順で preorder
template <typename GT, bool HLD = true>
struct Tree {
using Graph_type = GT;
GT &G;
using WT = typename GT::cost_type;
int N;
vector<int> LID, RID, head, V, parent, VtoE;
vc<int> depth;
vc<WT> depth_weighted;
vc<int> memo_tail;
Tree(GT &G, int r = 0) : G(G) { build(r); }
void build(int r = 0) {
if (r == -1) return; // build を遅延したいとき
if constexpr (!HLD)
build_simple(r);
else
build_HLD(r);
}
vc<int> heavy_path_at(int v) const {
static_assert(HLD);
assert(head[v] == v);
int k = LID[v];
vc<int> P;
while (k < N && head[V[k]] == v) P.eb(V[k++]);
return P;
}
int heavy_child(int v) const {
static_assert(HLD);
if (RID[v] == LID[v] + 1) return -1;
return V[LID[v] + 1];
}
int tail(int v) {
static_assert(HLD);
if (memo_tail.empty()) {
memo_tail.assign(N, -1);
FOR_R(i, N) {
int v = V[i];
int w = heavy_child(v);
memo_tail[v] = (w == -1 ? v : memo_tail[w]);
}
}
return memo_tail[v];
}
int e_to_v(int eid) const {
auto e = G.edges[eid];
return (parent[e.frm] == e.to ? e.frm : e.to);
}
int v_to_e(int v) const { return VtoE[v]; }
int get_eid(int u, int v) const {
if (parent[u] != v) swap(u, v);
assert(parent[u] == v);
return VtoE[u];
}
int ELID(int v) const { return 2 * LID[v] - depth[v]; }
int ERID(int v) const { return 2 * RID[v] - depth[v] - 1; }
// 目標地点へ進む個数が k
int LA(int v, int k) const {
static_assert(HLD);
assert(k <= depth[v]);
while (1) {
int u = head[v];
if (LID[v] - k >= LID[u]) return V[LID[v] - k];
k -= LID[v] - LID[u] + 1;
v = parent[u];
}
}
int LCA(int u, int v) const {
static_assert(HLD);
for (;; v = parent[head[v]]) {
if (LID[u] > LID[v]) swap(u, v);
if (head[u] == head[v]) return u;
}
}
int meet(int a, int b, int c) const {
static_assert(HLD);
return LCA(a, b) ^ LCA(a, c) ^ LCA(b, c);
}
int subtree_size(int v) const { return RID[v] - LID[v]; }
int subtree_size(int v, int root) const {
static_assert(HLD);
if (v == root) return N;
int x = jump(v, root, 1);
if (in_subtree(v, x)) return RID[v] - LID[v];
return N - RID[x] + LID[x];
}
int dist(int a, int b) const {
static_assert(HLD);
int c = LCA(a, b);
return depth[a] + depth[b] - 2 * depth[c];
}
WT dist_weighted(int a, int b) const {
static_assert(HLD);
int c = LCA(a, b);
return depth_weighted[a] + depth_weighted[b] - WT(2) * depth_weighted[c];
}
// a is in b
bool in_subtree(int a, int b) const {
return LID[b] <= LID[a] && LID[a] < RID[b];
}
int jump(int a, int b, ll k) const {
static_assert(HLD);
if (k == 1) {
if (a == b) return -1;
return (in_subtree(b, a) ? LA(b, depth[b] - depth[a] - 1) : parent[a]);
}
int c = LCA(a, b);
int d_ac = depth[a] - depth[c];
int d_bc = depth[b] - depth[c];
if (k > d_ac + d_bc) return -1;
if (k <= d_ac) return LA(a, k);
return LA(b, d_ac + d_bc - k);
}
vc<int> collect_child(int v) const {
vc<int> res;
for (auto &&e : G[v])
if (e.to != parent[v]) res.eb(e.to);
return res;
}
vc<int> collect_subtree(int v) const {
return {V.begin() + LID[v], V.begin() + RID[v]};
}
vc<int> collect_light(int v) const {
static_assert(HLD);
vc<int> res;
for (auto &&e : G[v]) {
if (e.to != parent[v] && head[e.to] == e.to) res.eb(e.to);
}
return res;
}
vc<pair<int, int>> get_path_decomposition(int u, int v, bool edge) const {
static_assert(HLD);
// [始点, 終点] の"閉"区間列。
vc<pair<int, int>> up, down;
while (1) {
if (head[u] == head[v]) break;
if (LID[u] < LID[v]) {
down.eb(LID[head[v]], LID[v]);
v = parent[head[v]];
} else {
up.eb(LID[u], LID[head[u]]);
u = parent[head[u]];
}
}
if (LID[u] < LID[v]) down.eb(LID[u] + edge, LID[v]);
elif (LID[v] + edge <= LID[u]) up.eb(LID[u], LID[v] + edge);
reverse(all(down));
up.insert(up.end(), all(down));
return up;
}
// 辺の列の情報 (frm,to,str)
// str = "heavy_up", "heavy_down", "light_up", "light_down"
vc<tuple<int, int, string>> get_path_decomposition_detail(
int u, int v) const {
static_assert(HLD);
vc<tuple<int, int, string>> up, down;
while (1) {
if (head[u] == head[v]) break;
if (LID[u] < LID[v]) {
if (v != head[v]) down.eb(head[v], v, "heavy_down"), v = head[v];
down.eb(parent[v], v, "light_down"), v = parent[v];
} else {
if (u != head[u]) up.eb(u, head[u], "heavy_up"), u = head[u];
up.eb(u, parent[u], "light_up"), u = parent[u];
}
}
if (LID[u] < LID[v]) down.eb(u, v, "heavy_down");
elif (LID[v] < LID[u]) up.eb(u, v, "heavy_up");
reverse(all(down));
concat(up, down);
return up;
}
vc<int> restore_path(int u, int v) const {
vc<int> L, R;
while (depth[u] > depth[v]) L.eb(u), u = parent[u];
while (depth[u] < depth[v]) R.eb(v), v = parent[v];
while (u != v) L.eb(u), R.eb(v), u = parent[u], v = parent[v];
L.eb(u);
while (len(R)) L.eb(POP(R));
return L;
}
// path [a,b] と [c,d] の交わり. 空ならば {-1,-1}.
// https://codeforces.com/problemset/problem/500/G
pair<int, int> path_intersection(int a, int b, int c, int d) const {
static_assert(HLD);
int ab = LCA(a, b), ac = LCA(a, c), ad = LCA(a, d);
int bc = LCA(b, c), bd = LCA(b, d), cd = LCA(c, d);
int x = ab ^ ac ^ bc, y = ab ^ ad ^ bd; // meet(a,b,c), meet(a,b,d)
if (x != y) return {x, y};
int z = ac ^ ad ^ cd;
if (x != z) x = -1;
return {x, x};
}
// uv path 上で check(v) を満たす最後の v
// なければ (つまり check(v) が ng )-1
template <class F>
int max_path(F check, int u, int v) const {
static_assert(HLD);
if (!check(u)) return -1;
auto pd = get_path_decomposition(u, v, false);
for (auto [a, b] : pd) {
if (!check(V[a])) return u;
if (check(V[b])) {
u = V[b];
continue;
}
int c =
binary_search([&](int c) -> bool { return check(V[c]); }, a, b, 0);
return V[c];
}
return u;
}
private:
void build_simple(int r = 0) {
N = G.N;
LID.assign(N, 0), RID.assign(N, 0);
V.assign(N, -1), parent.assign(N, -1), VtoE.assign(N, -1);
depth.assign(N, 0), depth_weighted.assign(N, 0);
assert(G.is_prepared());
// 1st dfs.
int k = 0;
vc<int> st;
st.reserve(N);
st.eb(r);
while (len(st)) {
int v = POP(st);
LID[v] = k, V[k] = v;
++k;
for (int i = G.indptr[v + 1] - 1; i >= G.indptr[v]; --i) {
auto &e = G.csr_edges[i];
if (e.to == parent[v]) continue;
parent[e.to] = v;
depth[e.to] = depth[v] + 1;
depth_weighted[e.to] = depth_weighted[v] + e.cost;
VtoE[e.to] = e.id;
st.eb(e.to);
}
}
FOR_R(i, N) {
int v = V[i];
chmax(RID[v], LID[v] + 1);
if (parent[v] != -1) chmax(RID[parent[v]], RID[v]);
}
}
void build_HLD(int r = 0) {
N = G.N;
LID.assign(N, 0), RID.assign(N, 0), head.assign(N, r);
V.assign(N, -1), parent.assign(N, -1), VtoE.assign(N, -1);
depth.assign(N, 0), depth_weighted.assign(N, 0);
memo_tail.clear();
assert(G.is_prepared());
// 1st dfs.
{
int k = 0;
vc<int> st;
st.reserve(N);
st.eb(r);
while (len(st)) {
int v = POP(st);
V[k++] = v;
for (auto &e : G[v]) {
if (e.to == parent[v]) continue;
parent[e.to] = v, st.eb(e.to), depth[e.to] = depth[v] + 1;
depth_weighted[e.to] = depth_weighted[v] + e.cost;
VtoE[e.to] = e.id;
}
}
// 一時的に RID[v] := sz[v]
FOR_R(i, N) {
int v = V[i];
RID[v] += 1;
if (parent[v] != -1) RID[parent[v]] += RID[v];
}
}
// 2nd dfs.
{
int k = 0;
vc<int> st;
st.reserve(N);
st.eb(r);
while (len(st)) {
int v = POP(st);
V[k] = v, LID[v] = k;
RID[v] = k + RID[v];
++k;
int max_sz = 0, max_ch = -1;
for (auto &e : G[v]) {
if (e.to == parent[v]) continue;
if (chmax(max_sz, RID[e.to])) max_ch = e.to;
}
for (int i = G.indptr[v + 1] - 1; i >= G.indptr[v]; --i) {
auto &e = G.csr_edges[i];
if (e.to == parent[v] || e.to == max_ch) continue;
st.eb(e.to), head[e.to] = e.to;
}
if (max_ch != -1) st.eb(max_ch), head[max_ch] = head[v];
}
}
}
};
// END: graph/tree.hpp
#line 4 "graph/functional_graph.hpp"
// BEGIN: ds/unionfind/unionfind.hpp
#line 1 "ds/unionfind/unionfind.hpp"
struct UnionFind {
int n, n_comp;
vc<int> dat; // par or (-size)
UnionFind(int n = 0) { build(n); }
void build(int m) {
n = m, n_comp = m;
dat.assign(n, -1);
}
void reset() { build(n); }
int operator[](int x) {
while (dat[x] >= 0) {
int pp = dat[dat[x]];
if (pp < 0) { return dat[x]; }
x = dat[x] = pp;
}
return x;
}
ll size(int x) {
x = (*this)[x];
return -dat[x];
}
bool merge(int x, int y) {
x = (*this)[x], y = (*this)[y];
if (x == y) return false;
if (-dat[x] < -dat[y]) swap(x, y);
dat[x] += dat[y], dat[y] = x, n_comp--;
return true;
}
vc<int> get_all() {
vc<int> A(n);
FOR(i, n) A[i] = (*this)[i];
return A;
}
};
// END: ds/unionfind/unionfind.hpp
#line 5 "graph/functional_graph.hpp"
// 内部実装は N が根となる木を新たに作る
// functional graph の辺に static な群の要素があるとする
// (モノイドにもできるがそれは doubling してもらうということでさぼり. )
template <typename Monoid>
struct Functional_Graph {
using MX = Monoid;
using X = typename MX::value_type;
int N, M;
vc<int> TO;
vc<X> wt, dp;
vc<int> root;
Graph<int, 1> G;
Functional_Graph() {}
Functional_Graph(int N)
: N(N), M(0), TO(N, -1), wt(N, MX::unit()), root(N, -1) {}
void add(int a, int b, X c = MX::unit()) {
assert(0 <= a && a < N);
assert(TO[a] == -1);
++M;
TO[a] = b;
wt[a] = c;
}
pair<Graph<int, 1>, Tree<Graph<int, 1>>> build() {
assert(N == M);
UnionFind uf(N);
FOR(v, N) if (!uf.merge(v, TO[v])) { root[v] = v; }
FOR(v, N) if (root[v] == v) root[uf[v]] = v;
FOR(v, N) root[v] = root[uf[v]];
G.build(N + 1);
FOR(v, N) {
if (root[v] == v)
G.add(N, v);
else
G.add(TO[v], v);
}
G.build();
Tree<Graph<int, 1>> tree(G, N);
dp.assign(N, MX::unit());
FOR(i, 1, N + 1) {
int v = tree.V[i];
int p = tree.parent[v];
if (p == N) {
continue;
}
dp[v] = MX::op(wt[v], dp[p]);
}
return {G, tree};
}
// a -> b にかかる回数. 不可能なら infty<int>. O(1).
template <typename TREE>
int dist(TREE& tree, int a, int b) {
if (tree.in_subtree(a, b)) return tree.depth[a] - tree.depth[b];
int r = root[a];
int btm = TO[r];
// a -> r -> btm -> b
if (tree.in_subtree(btm, b)) {
int x = tree.depth[a] - tree.depth[r];
x += 1;
x += tree.depth[btm] - tree.depth[b];
return x;
}
return infty<int>;
}
// functional graph に向かって進む
// return: 終点, 群の積
template <typename TREE>
pair<int, X> jump(TREE& tree, int v, ll step) {
int d = tree.depth[v];
if (step <= d - 1) {
int w = tree.jump(v, N, step);
return {w, MX::op(dp[v], MX::inverse(dp[w]))};
}
X x = dp[v];
v = root[v];
step -= d - 1;
int bottom = TO[v];
int c = tree.depth[bottom];
x = MX::op(x, monoid_pow<MX>(MX::op(wt[v], dp[bottom]), step / c));
step %= c;
if (step == 0) return {v, x};
int w = tree.jump(bottom, N, step - 1);
x = MX::op(x, wt[v]);
x = MX::op(x, dp[bottom]);
x = MX::op(x, MX::inverse(dp[w]));
return {w, x};
}
// check(to, prod). infty<ll> 以下. step をかえす
template <typename TREE, typename F>
ll max_jump(TREE& tree, F check, int v) {
X prod = MX::unit();
assert(check(v, prod));
ll ans = 0;
if (check(root[v], dp[v])) {
ans += tree.depth[v] - 1, prod = dp[v], v = root[v];
int bottom = TO[v];
ll c = tree.depth[bottom];
vc<X> pw;
pw.eb(MX::op(wt[v], dp[bottom]));
FOR(k, 63) {
if (!check(root[v], MX::op(prod, pw[k]))) {
break;
}
if (ans + (c << k) >= infty<ll>) return infty<ll>;
pw.eb(MX::op(pw.back(), pw.back()));
}
FOR_R(k, len(pw)) {
if (check(root[v], MX::op(prod, pw[k]))) {
ans = min(ans + (c << k), infty<ll>);
prod = MX::op(prod, pw[k]);
}
}
if (!check(bottom, MX::op(prod, wt[v]))) return ans;
v = bottom, prod = MX::op(prod, wt[v]);
}
auto pd = tree.get_path_decomposition(v, root[v], false);
auto mycheck = [&](int w) -> bool {
X x = MX::op(prod, MX::op(dp[v], MX::inverse(dp[w])));
return check(w, x);
};
int last = v;
for (auto [a, b] : pd) {
swap(a, b);
assert(a <= b);
if (mycheck(tree.V[a])) {
last = tree.V[a];
continue;
}
if (!mycheck(tree.V[b])) {
break;
}
int k = binary_search([&](int i) -> bool { return mycheck(tree.V[i]); },
b, a, 0);
last = tree.V[k];
break;
}
ans += tree.depth[v] - tree.depth[last];
return min(ans, infty<ll>);
}
// functional graph に step 回進む
template <typename TREE>
vc<int> jump_all(TREE& tree, ll step) {
vc<int> res(N, -1);
// v の k 個先を res[w] に入れる
vvc<pair<int, int>> query(N);
FOR(v, N) {
int d = tree.depth[v];
int r = root[v];
if (d - 1 > step) {
query[v].eb(v, step);
}
if (d - 1 <= step) {
ll k = step - (d - 1);
int bottom = TO[r];
int c = tree.depth[bottom];
k %= c;
if (k == 0) {
res[v] = r;
continue;
}
query[bottom].eb(v, k - 1);
}
}
vc<int> path;
auto dfs = [&](auto& dfs, int v) -> void {
path.eb(v);
for (auto&& [w, k] : query[v]) {
res[w] = path[len(path) - 1 - k];
}
for (auto&& e : G[v]) dfs(dfs, e.to);
path.pop_back();
};
for (auto&& e : G[N]) {
dfs(dfs, e.to);
}
return res;
}
template <typename TREE>
bool in_cycle(TREE& tree, int v) {
int r = root[v];
int bottom = TO[r];
return tree.in_subtree(bottom, v);
}
// 葉側から順にならんだものを出力
vc<int> collect_cycle(int r) {
assert(r == root[r]);
vc<int> cyc = {TO[r]};
while (cyc.back() != r) cyc.eb(TO[cyc.back()]);
return cyc;
}
// F^k(i)==F^k(j) となる最小の k OR -1
template <typename TREE>
int meet_time(TREE& tree, int i, int j) {
if (i == j) return 0;
if (root[i] != root[j]) return -1;
int r = root[i];
int b = TO[r];
int n = tree.depth[b] - tree.depth[r] + 1; // cyc len
if ((tree.depth[i] - tree.depth[j]) % n != 0) return -1;
if (tree.depth[i] == tree.depth[j]) {
int lca = tree.LCA(i, j);
return tree.depth[i] - tree.depth[lca];
}
int ti = tree.depth[i] - tree.depth[tree.LCA(b, i)];
int tj = tree.depth[j] - tree.depth[tree.LCA(b, j)];
return max(ti, tj);
}
};
// END: graph/functional_graph.hpp
#line 7 "main.cpp"
void solve() {
ll N = 100003;
auto lpf = lpf_table(1 << 22);
Functional_Graph<Monoid_Add<int>> FG(N);
auto nxt = [&](ll x) -> ll {
auto pfs = factor_by_lpf(x, lpf);
ll ans = 1;
for (auto& [p, e] : pfs) {
ll v = 1;
FOR(e) v = v * p + 1;
ans *= v;
ans %= N;
}
if (x == 6) SHOW(x, ans);
return ans;
};
FG.add(0, 0);
FOR(x, 1, N) { FG.add(x, nxt(x)); }
auto [G, tree] = FG.build();
LL(s, K);
if (K == 1) return print(s);
--K;
s = nxt(s);
--K;
SHOW(s, K);
ll ANS = FG.jump(tree, s, K).fi;
print(ANS);
}
signed main() { solve(); }
// END: main.cpp
maspy