結果
| 問題 |
No.3187 Mingle
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-06-21 15:31:56 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 23,955 bytes |
| コンパイル時間 | 5,465 ms |
| コンパイル使用メモリ | 193,104 KB |
| 実行使用メモリ | 220,232 KB |
| 最終ジャッジ日時 | 2025-06-22 19:57:31 |
| 合計ジャッジ時間 | 59,143 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 TLE * 1 |
ソースコード
#pragma GCC optimize("O3") // たまにバグる
#pragma GCC optimize("unroll-loops")
constexpr int Q = 180;
#ifndef HIDDEN_IN_VS // 折りたたみ用
// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS
// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;
// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9)
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
// 定数の定義
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定
// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod
// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif // 折りたたみ用
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint998244353;
//using mint = static_modint<(int)1e9 + 7>;
using mint = modint; // mint::set_mod(m);
//namespace atcoder {
// inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
// inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
//}
string mint_to_frac(mint x, int v_max = 31595) {
repi(dnm, 1, v_max) {
int num = (x * dnm).val();
if (num == 0) {
return "0";
}
if (num <= v_max) {
if (dnm == 1) return to_string(num);
return to_string(num) + "/" + to_string(dnm);
}
if (mint::mod() - num <= v_max) {
if (dnm == 1) return "-" + to_string(mint::mod() - num);
return "-" + to_string(mint::mod() - num) + "/" + to_string(dnm);
}
}
return to_string(x.val());
}
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
#ifdef _MSC_VER
inline ostream& operator<<(ostream& os, const mint& x) { os << mint_to_frac(x); return os; }
#else
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
#endif
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
int mute_dump = 0;
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_math(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す
#endif
//【階乗など(法が大きな素数)】
/*
* Factorial_mint(int N) : O(n)
* N まで計算可能として初期化する.
*
* mint fact(int n) : O(1)
* n! を返す.
*
* mint fact_inv(int n) : O(1)
* 1/n! を返す(n が負なら 0 を返す)
*
* mint inv(int n) : O(1)
* 1/n を返す.
*
* mint perm(int n, int r) : O(1)
* 順列の数 nPr を返す.
*
* mint perm_inv(int n, int r) : O(1)
* 順列の数の逆数 1/nPr を返す.
*
* mint bin(int n, int r) : O(1)
* 二項係数 nCr を返す.
*
* mint bin_inv(int n, int r) : O(1)
* 二項係数の逆数 1/nCr を返す.
*
* mint mul(vi rs) : O(|rs|)
* 多項係数 nC[rs] を返す.(n = Σrs)
*
* mint hom(int n, int r) : O(1)
* 重複組合せの数 nHr = n+r-1Cr を返す(0H0 = 1 とする)
*
* mint neg_bin(int n, int r) : O(1)
* 負の二項係数 nCr = (-1)^r -n+r-1Cr を返す(n ≦ 0, r ≧ 0)
*
* mint pochhammer(int x, int n) : O(1)
* ポッホハマー記号 x^(n) を返す(n ≧ 0)
*
* mint pochhammer_inv(int x, int n) : O(1)
* ポッホハマー記号の逆数 1/x^(n) を返す(n ≧ 0)
*/
class Factorial_mint {
int n_max;
// 階乗と階乗の逆数の値を保持するテーブル
vm fac, fac_inv;
public:
// n! までの階乗とその逆数を前計算しておく.O(n)
Factorial_mint(int n) : n_max(n), fac(n + 1), fac_inv(n + 1) {
// verify : https://atcoder.jp/contests/dwacon6th-prelims/tasks/dwacon6th_prelims_b
fac[0] = 1;
repi(i, 1, n) fac[i] = fac[i - 1] * i;
fac_inv[n] = fac[n].inv();
repir(i, n - 1, 0) fac_inv[i] = fac_inv[i + 1] * (i + 1);
}
Factorial_mint() : n_max(0) {} // ダミー
// n! を返す.
mint fact(int n) const {
// verify : https://atcoder.jp/contests/dwacon6th-prelims/tasks/dwacon6th_prelims_b
Assert(0 <= n && n <= n_max);
return fac[n];
}
// 1/n! を返す(n が負なら 0 を返す)
mint fact_inv(int n) const {
// verify : https://atcoder.jp/contests/abc289/tasks/abc289_h
Assert(n <= n_max);
if (n < 0) return 0;
return fac_inv[n];
}
// 1/n を返す.
mint inv(int n) const {
// verify : https://atcoder.jp/contests/exawizards2019/tasks/exawizards2019_d
Assert(n > 0);
Assert(n <= n_max);
return fac[n - 1] * fac_inv[n];
}
// 順列の数 nPr を返す.
mint perm(int n, int r) const {
// verify : https://atcoder.jp/contests/abc172/tasks/abc172_e
Assert(n <= n_max);
if (r < 0 || n - r < 0) return 0;
return fac[n] * fac_inv[n - r];
}
// 順列の数 nPr の逆数を返す.
mint perm_inv(int n, int r) const {
// verify : https://yukicoder.me/problems/no/3139
Assert(n <= n_max);
Assert(0 <= r); Assert(r <= n);
return fac_inv[n] * fac[n - r];
}
// 二項係数 nCr を返す.
mint bin(int n, int r) const {
// verify : https://judge.yosupo.jp/problem/binomial_coefficient_prime_mod
Assert(n <= n_max);
if (r < 0 || n - r < 0) return 0;
return fac[n] * fac_inv[r] * fac_inv[n - r];
}
// 二項係数の逆数 1/nCr を返す.
mint bin_inv(int n, int r) const {
// verify : https://www.codechef.com/problems/RANDCOLORING
Assert(n <= n_max);
Assert(r >= 0);
Assert(n - r >= 0);
return fac_inv[n] * fac[r] * fac[n - r];
}
// 多項係数 nC[rs] を返す.
mint mul(const vi& rs) const {
// verify : https://yukicoder.me/problems/no/2141
if (*min_element(all(rs)) < 0) return 0;
int n = accumulate(all(rs), 0);
Assert(n <= n_max);
mint res = fac[n];
repe(r, rs) res *= fac_inv[r];
return res;
}
// 重複組合せの数 nHr = n+r-1Cr を返す(0H0 = 1 とする)
mint hom(int n, int r) {
// verify : https://mojacoder.app/users/riantkb/problems/toj_ex_2
if (n == 0) return (int)(r == 0);
if (r < 0 || n - 1 < 0) return 0;
Assert(n + r - 1 <= n_max);
return fac[n + r - 1] * fac_inv[r] * fac_inv[n - 1];
}
// 負の二項係数 nCr を返す(n ≦ 0, r ≧ 0)
mint neg_bin(int n, int r) {
// verify : https://atcoder.jp/contests/abc345/tasks/abc345_g
if (n == 0) return (int)(r == 0);
if (r < 0 || -n - 1 < 0) return 0;
Assert(-n + r - 1 <= n_max);
return (r & 1 ? -1 : 1) * fac[-n + r - 1] * fac_inv[r] * fac_inv[-n - 1];
}
// ポッホハマー記号 x^(n) を返す(n ≧ 0)
mint pochhammer(int x, int n) {
// verify : https://atcoder.jp/contests/agc070/tasks/agc070_c
int x2 = x + n - 1;
if (x <= 0 && 0 <= x2) return 0;
if (x > 0) {
Assert(x2 <= n_max);
return fac[x2] * fac_inv[x - 1];
}
else {
Assert(-x <= n_max);
return (n & 1 ? -1 : 1) * fac[-x] * fac_inv[-x2 - 1];
}
}
// ポッホハマー記号の逆数 1/x^(n) を返す(n ≧ 0)
mint pochhammer_inv(int x, int n) {
// verify : https://atcoder.jp/contests/agc070/tasks/agc070_c
int x2 = x + n - 1;
Assert(!(x <= 0 && 0 <= x2));
if (x > 0) {
Assert(x2 <= n_max);
return fac_inv[x2] * fac[x - 1];
}
else {
Assert(-x <= n_max);
return (n & 1 ? -1 : 1) * fac_inv[-x] * fac[-x2 - 1];
}
}
};
mint naive(int n) {
Factorial_mint fm(n);
vm dp(n + 1);
mint res = 0;
dp[n] = 1;
repir(i, n, 3) {
int stop = 0;
repi(j, 1, i) {
int ni = i - i % j;
if (ni != i);
else stop++;
}
repi(j, 1, i) {
int ni = i - i % j;
if (ni != i) dp[ni] += dp[i] * fm.inv(i - stop);
else;
}
res += dp[i] * i * fm.inv(i - stop);
//dump(dp);
}
return res;
}
void zikken() {
repi(n, 3, 20) {
dump(n, naive(n));
}
exit(0);
}
/*
3 3
4 7
5 22/3
6 61/6
7 311/30
8 1381/120
9 8567/720
10 5569/432
11 5617/432
12 10147/720
13 1823/26787
14 -28639/1484
15 -8069/16059
16 -18149/8753
17 -10028/27729
18 -28278/18587
19 10335/8054
20 28645/6327
*/
//【約数倍数変換】
/*
* Div_mul_transform(int n) : O(n log(log n))
* n 以下の素数を持って初期化する.
*
* divisor_zeta(vT& a) : O(n log(log n))
* A[i] = Σ_(j | i) a[j] なる A に上書きする(約数からの寄与を足し込む)
*
* divisor_mobius(vT& A) : O(n log(log n))
* A[i] = Σ_(j | i) a[j] なる a に上書きする(約数からの寄与を取り除く)
*
* vT lcm_convolution(vT a, vT b) : O(n log(log n))
* c[k] = Σ_(LCM(i, j) = k) a[i] b[j] なる c を返す.ただし c(n..∞) は切り捨てる.
*
* multiple_zeta(vT& a) : O(n log(log n))
* A[i] = Σ_(i | j) a[j] なる A に上書きする(倍数からの寄与を足し込む)
*
* multiple_mobius(vT& A) : O(n log(log n))
* A[i] = Σ_(i | j) a[j] なる a に上書きする(倍数からの寄与を取り除く)
*
* vT gcd_convolution(vT a, vT b) : O(n log(log n))
* c[k] = Σ_(GCD(i, j) = k) a[i] b[j] なる c を返す.
*
* 制約:1-indexed とし,a[0], b[0] は使用しない.
*/
class Div_mul_transform {
// 参考 : https://qiita.com/convexineq/items/afc84dfb9ee4ec4a67d5
vi ps; // 素数のリスト
public:
// n 以下の素数を持って初期化する.
Div_mul_transform(int n) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
// is_prime[i] : i が素数か
vb is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
int i = 2;
// √n 以下の i の処理
for (; i <= n / i; i++) if (is_prime[i]) {
ps.push_back(i);
for (int j = i * i; j <= n; j += i) is_prime[j] = false;
}
// √n より大きい i の処理
for (; i <= n; i++) if (is_prime[i]) ps.push_back(i);
}
Div_mul_transform() {}
// A[i] = Σ_(j | i) a[j] なる A に上書きする(約数からの寄与を足し込む)
template <typename T>
void divisor_zeta(vector<T>& a) {
// verify : https://judge.yosupo.jp/problem/lcm_convolution
//【例(n = 8 のとき)】
// A[1] = a[1]
// A[2] = a[1] + a[2]
// A[3] = a[1] + a[3]
// A[4] = a[1] + a[2] + a[4]
// A[5] = a[1] + a[5]
// A[6] = a[1] + a[2] + a[3] + a[6]
// A[7] = a[1] + a[7]
// A[8] = a[1] + a[2] + a[4] + a[8]
//【備考】
// a[1..n] のディリクレ母関数を α(s) = Σ_i a[i] i^(-s) とすると,
// α(s) にゼータ関数 ζ(s) = Σ_i i^(-s) を掛けることに対応する.
int n = sz(a) - 1;
// 各素因数ごとに下からの累積和をとる
repe(p, ps) repi(i, 1, n / p) a[p * i] += a[i];
}
// A[i] = Σ_(j | i) a[j] なる a に上書きする(約数からの寄与を取り除く)
template <typename T>
void divisor_mobius(vector<T>& A) {
// verify : https://judge.yosupo.jp/problem/lcm_convolution
//【例(n = 8 のとき)】
// a[1] = A[1]
// a[2] = -A[1] + A[2]
// a[3] = -A[1] + A[3]
// a[4] = - A[2] + A[4]
// a[5] = -A[1] + A[5]
// a[6] = A[1] - A[2] - A[3] + A[6]
// a[7] = -A[1] + A[7]
// a[8] = - A[4] + A[8]
//【備考】
// A[1..n] のディリクレ母関数を α(s) = Σ_i A[i] i^(-s) とすると,
// α(s) にゼータ関数 ζ(s) = Σ_i i^(-s) の逆数を掛けることに対応する.
int n = sz(A) - 1;
// 各素因数ごとに上からの差分をとる
repe(p, ps) repir(i, n / p, 1) A[p * i] -= A[i];
}
// c[k] = Σ_(LCM(i, j) = k) a[i] b[j] なる c を返す.
template <typename T>
vector<T> lcm_convolution(vector<T> a, vector<T> b) {
// verify : https://judge.yosupo.jp/problem/lcm_convolution
int n = sz(a) - 1;
// 各素因数の max をとったものが LCM なので max 畳込みを行う.
divisor_zeta(a);
divisor_zeta(b);
repi(i, 1, n) a[i] *= b[i];
divisor_mobius(a);
return a;
}
// A[i] = Σ_(i | j) a[j] なる A に上書きする(倍数からの寄与を足し込む)
template <typename T>
void multiple_zeta(vector<T>& a) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
//【例(n = 8 のとき)】
// A[1] = a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8]
// A[2] = a[2] + a[4] + a[6] + a[8]
// A[3] = a[3] + a[6]
// A[4] = a[4] + a[8]
// A[5] = a[5]
// A[6] = a[6]
// A[7] = a[7]
// A[8] = a[8]
//【備考】
// a[1..n] のディリクレ母関数を α(s) = Σ_i a[i] i^(-s) とすると,
// α(s) にゼータ関数の変種 ζ(-s) = Σ_i i^s を掛けることに対応する.
int n = sz(a) - 1;
// 各素因数ごとに上からの累積和をとる
repe(p, ps) repir(i, n / p, 1) a[i] += a[p * i];
}
// A[i] = Σ_(i | j) a[j] なる a に上書きする(倍数からの寄与を取り除く)
template <typename T>
void multiple_mobius(vector<T>& A) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
//【例(n = 8 のとき)】
// a[1] = A[1] - A[2] - A[3] - A[5] + A[6] - a[7]
// a[2] = A[2] - A[4] - A[6]
// a[3] = A[3] - A[6]
// a[4] = A[4] - A[8]
// a[5] = A[5]
// a[6] = A[6]
// a[7] = A[7]
// a[8] = A[8]
//【備考】
// A[1..n] のディリクレ母関数を α(s) = Σ_i a[i] i^(-s) とすると,
// α(s) にゼータ関数の変種 ζ(-s) = Σ_i i^s の逆数を掛けることに対応する.
int n = sz(A) - 1;
// 各素因数ごとに下からの差分をとる
repe(p, ps) repi(i, 1, n / p) A[i] -= A[p * i];
}
// c[k] = Σ_(GCD(i, j) = k) a[i] b[j] なる c を返す.
template <typename T>
vector<T> gcd_convolution(vector<T> a, vector<T> b) {
// verify : https://judge.yosupo.jp/problem/gcd_convolution
int n = sz(a) - 1;
// 各素因数の min をとったものが GCD なので min 畳込みを行う.
multiple_zeta(a);
multiple_zeta(b);
repi(i, 1, n) a[i] *= b[i];
multiple_mobius(a);
return a;
}
};
//【約数和関数(一括)】O(n log(log n))
/*
* 各 i∈[1..n] について約数和関数 σ_k(i) = (i の約数の k 乗和) を格納したリストを返す.
* 特に k = 0 なら約数の個数,k = 1 なら約数の総和と等価である.
*
* 利用:【約数倍数変換】
*/
template <class T>
vector<T> divisor_sigma(int k, int n) {
// 参考 : https://maspypy.com/%E6%95%B0%E5%AD%A6-%E7%95%B3%E3%81%BF%E8%BE%BC%E3%81%BF%E5%85%A5%E9%96%80%EF%BC%9Adirichlet%E7%A9%8D%E3%81%A8%E3%82%BC%E3%83%BC%E3%82%BF%E5%A4%89%E6%8F%9B%E3%83%BB%E3%83%A1%E3%83%93%E3%82%A6
// verify : https://atcoder.jp/contests/abc172/tasks/abc172_d
//【方法】
// 約数和関数の定義より,等式
// σ_k(i) = Σ_(d|i) d^k
// を得る.これは σ_k が a[i] = i^k を約数ゼータ変換したものであることを意味する.
vector<T> a(n + 1);
a[0] = 0;
repi(i, 1, n) a[i] = T(powi(i, k));
Div_mul_transform dt(n);
dt.divisor_zeta(a);
return a;
}
//【商列挙】O(√N)
/*
* 区間 [1..N] を N/i = q(切り捨て)となる半開区間 i∈(il..ir] に分割し,
* i について昇順にそれぞれに対して f(il, ir, q) を呼び出す.
* なお各範囲においては N mod i は公差 -q の等差数列を成す.
*/
template <class T, class FUNC>
void quotient_range(T N, const FUNC& f) {
// 参考 : https://ei1333.github.io/luzhiled/snippets/math/quotient-range.html
// verify : https://judge.yosupo.jp/problem/enumerate_quotients
//【方法】
// N/i の商が q となるような i の範囲を考える.条件を i について整理すると
// q = floor(N/i)
// ⇔ q ≦ N/i < q+1
// ⇔ i q ≦ N < i(q+1)
// ⇔ N/(q+1) < i ≦ N/q (⇔ floor(N/(q+1)) < i ≦ floor(N/q))
// となる.
//
// この幅が 1 以下であれば,q に対応する i は高々 1 個である.その条件は
// N/q - N/(q+1) ≦ 1
// ⇔ (q+1)N - q N ≦ q(q+1)
// ⇔ N ≦ q(q+1)
// である.条件をやや弱めて
// N ≦ q^2 ⇔ √N ≦ q
// としてもオーダーに影響はない.
//(例)
// 例えば N = 15 のときは (0..15] を以下のように分割できる:
// i の範囲 q=N/i N mod i
// (0..1] 15 [0]
// (1..2] 7 [1]
// (2..3] 5 [0]
// (3..5] 3 [3, 0]
// (5..7] 2 [3, 1]
// (7..15] 1 [7, 6, 5, 4, 3, 2, 1, 0]
T sqrt_n = (T)(sqrt(N) - 1e-9);
// q に対応する i が高々 1 個の部分は i ごとに愚直に考える.
T i_max = N / (sqrt_n + 1);
for (T i = 1; i <= i_max; ++i) f(i - 1, i, N / i);
// そうでない部分は q ごとにまとめて考える.
T il, ir = i_max;
for (T q = sqrt_n; q >= 1; --q) {
il = ir;
ir = N / q;
f(il, ir, q);
}
/* f の定義の雛形
using T = ll;
auto f = [&](T il, T ir, T q) {
};
quotient_range(N, f);
*/
}
mint TLE(int n) {
Factorial_mint fm(n);
auto s0 = divisor_sigma<ll>(0, n);
vm dp(n + 1);
dp[n] = 1;
mint res = 0;
constexpr int Q = 500;
// 50 : 3255 ms
// 100 : 2946 ms
// 200 : 3058 ms
// 500 : 4783 ms
// 1000 : 10000+ ms
vvm imos(Q, vm(n + 1));
repir(i, n, 3) {
rep(q, Q) {
dp[i] += imos[q][i];
}
int stop = (int)s0[i];
mint add = dp[i] * fm.inv(i - stop);
using T = int;
auto f = [&](T il, T ir, T q) {
int j0 = i - i % (il + 1);
int j1 = i - i % ir;
if (j1 == i) j1 -= q;
if (q < Q) {
imos[q][j1] += add;
if (j0 - q >= 3) imos[q][j0 - q] -= add;
}
else {
for (auto j = j1; j >= j0; j -= q) {
dp[j] += add;
}
}
};
quotient_range(i, f);
//repi(j, 1, i) {
// int ni = i - i % j;
// if (ni != i) dp[ni] += dp[i] * fm.inv(i - stop);
//}
res += dp[i] * i * fm.inv(i - stop);
rep(q, Q) {
if (i - q >= 3) imos[q][i - q] += imos[q][i];
}
//dump(dp);
}
return res;
}
mint imos[Q][300001];
int MOD;
mint solve(int n) {
Factorial_mint fm(n);
auto s0 = divisor_sigma<ll>(0, n);
vm dp(n + 1);
dp[n] = 1;
mint res = 0;
//constexpr int Q = 500;
// 100 : 2772 ms
//vvm imos(Q, vm(n + 1));
rep(q, Q) repi(i, 0, n) imos[q][i] = 0;
repir(N, n, 3) {
rep(q, Q) {
dp[N] += imos[q][N];
}
int stop = (int)s0[N];
mint add = dp[N] * fm.inv(N - stop);
{
int sqrt_n = (int)(sqrt(N) - 1e-9);
// q に対応する i が高々 1 個の部分は i ごとに愚直に考える.
int i_max = N / (sqrt_n + 1);
for (int i = 1; i <= i_max; ++i) {
//f(i - 1, i, N / i);
dp[N - N % i] += add;
}
// そうでない部分は q ごとにまとめて考える.
int il, ir = i_max;
for (int q = sqrt_n; q >= 1; --q) {
il = ir;
ir = N / q;
//f(il, ir, q);
int j0 = N - N % (il + 1);
int j1 = N - N % ir;
if (j1 == N) j1 -= q;
if (q < Q) {
imos[q][j1] += add;
//if (imos[q][j1] >= MOD) imos[q][j1] -= MOD;
if (j0 - q >= 3) {
imos[q][j0 - q] -= add;
//if (imos[q][j0 - q] < 0) imos[q][j0 - q] += MOD;
}
}
else {
for (auto j = j1; j >= j0; j -= q) {
dp[j] += add;
}
}
}
}
//repi(j, 1, i) {
// int ni = i - i % j;
// if (ni != i) dp[ni] += dp[i] * fm.inv(i - stop);
//}
res += N * add;
rep(q, Q) {
if (N - q >= 3) {
imos[q][N - q] += imos[q][N];
//if (imos[q][N - q] >= MOD) imos[q][N - q] -= MOD;
}
}
//dump(dp);
}
return res;
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
// zikken();
int n, mod;
cin >> n >> mod;
mint::set_mod(mod);
MOD = mod;
dump(naive(n).val()); dump("=====");
EXIT(solve(n).val());
}