結果
問題 | No.2705 L to R Graph (Another ver.) |
ユーザー | zlxFTH |
提出日時 | 2024-05-25 13:20:27 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 4,734 bytes |
コンパイル時間 | 1,938 ms |
コンパイル使用メモリ | 173,772 KB |
実行使用メモリ | 7,988 KB |
最終ジャッジ日時 | 2024-05-25 13:20:38 |
合計ジャッジ時間 | 10,826 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | RE | - |
testcase_37 | RE | - |
testcase_38 | RE | - |
testcase_39 | RE | - |
testcase_40 | RE | - |
testcase_41 | RE | - |
testcase_42 | RE | - |
testcase_43 | RE | - |
testcase_44 | RE | - |
testcase_45 | RE | - |
testcase_46 | RE | - |
testcase_47 | RE | - |
testcase_48 | RE | - |
testcase_49 | RE | - |
testcase_50 | RE | - |
testcase_51 | RE | - |
testcase_52 | RE | - |
ソースコード
#include <bits/stdc++.h> using namespace std; #define debug(...) fprintf(stderr, __VA_ARGS__) using LL = long long; const int N = 1e5 + 5; struct Mod { LL m, p; void init(LL pp) { p = pp; m = (__int128(1) << 64) / p; } LL operator()(LL x) { return x - (__int128(x) * m >> 64) * p; } } mod; int P; inline LL qp(LL a, LL b = P - 2) { LL c = 1; while (b) { if (b & 1) c = mod(c * a); a = mod(a * a); b /= 2; } return c; } LL fac[N], ifac[N], pn[N]; void initC(int n) { fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = mod(fac[i - 1] * i); ifac[n] = qp(fac[n]); for (int i = n; i > 0; --i) ifac[i - 1] = mod(ifac[i] * i); } inline LL binom(int n, int m) { if (n < m || m < 0) return 0; return mod(mod(fac[n] * ifac[m]) * ifac[n - m]); } LL mu[N]; void initP(int n) { int m = 0; static LL v[N], p[N]; mu[1] = 1; for (int i = 2; i <= n; ++i) { if (!v[i]) v[i] = i, p[++m] = i, mu[i] = -1; for (int j = 1; j <= m && p[j] * i <= n; ++j) { v[p[j] * i] = p[j]; if (i % p[j] == 0) { mu[p[j] * i] = 0; } else { mu[p[j] * i] = -mu[i]; } } } } int n; LL f[N], g[N]; vector<int> d[N]; void initD(int n) { for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; j += i) { d[j].push_back(i); } } } void calcF() { static LL s[N]; for (int j = 1; j < n; ++j) { for (int l = 1; l <= j; ++l) { int L = l, R = j; auto F = [&](int x) { return (j - 1) / (j / x) + 1; }; while (L < R) { int mid = (L + R + 1) / 2; if (F(mid) != F(L)) R = mid - 1; else L = mid; } auto r = F(R); // cerr << j << " " << l << " " << R << " " << r << "\n"; if (r <= n) { s[j] = mod(s[j] + LL(R - l + 1) * (n - r + 1)); if (R == r) s[j] = mod(s[j] + P - 1); } l = R; } } for (int i = 1; i <= n; ++i) { s[i] = mod(s[i] + s[i - 1]); } for (int i = 0; i <= n; ++i) { if (!i) { f[i] = P - mod(LL(n) * (n - 1) / 2); } else { // for (int j = 1; j < i; ++j) { // for (int l = 1; l <= n; ++l) { // int x = j / l; // if (!x) continue; // int r = (j + x - 1) / x; // if (r == l) ++r; // if (r <= n && l < r) { // f[i] = mod(f[i] + n - r + 1); // } // } // } for (int r = n; r >= 1; --r) { int x = (i + r - 1) / r; int l = (i + x - 1) / x; f[i] = mod(f[i] + LL(r - l + 1) * x); r = l; } // for (int j = 1; j <= n; ++j) { // f[i] = mod(f[i] + (i + j - 1) / j); // } } } for (int i = 1; i <= n; ++i) { f[i] = mod(f[i] + s[i - 1]); } } void calcG() { vector<int> vis(n + 1); for (int c = 1; c <= n; ++c) { g[c] = mod(mod(LL(n) * (n - 1) / 2) * c); // BF1 // for (int j = 1; j <= n; ++j) { // if (j % c == 0) continue; // int x = j % c; // g[c] = mod(g[c] + c / __gcd(x, c) - 1); // } // BF2 // vector<int> d1, d2; // for (int i = 1; i * i <= c; ++i) { // if (c % i) continue; // d1.push_back(i); // if (c / i != i) d2.push_back(c / i); // } // reverse(d1.begin(), d1.end()); // for (auto v : d2) { // for (int i = v; i < c; i += v) if (vis[i] != c) { // vis[i] = c; // g[c] = mod(g[c] + LL(c / v - 1) * ((n - i) / c + 1)); // } // } // for (auto v : d1) { // for (int i = v; i < c; i += v) if (vis[i] != c) { // vis[i] = c; // g[c] = mod(g[c] + LL(c / v - 1) * ((n - i) / c + 1)); // } // } auto calcg = [&](int v) { LL res = 0; for (auto i : d[c / v]) { if (!mu[i]) continue; LL var = n / v / i; if (mu[i] == -1) var = P - var; res = mod(res + var); } g[c] = mod(g[c] + (c / v - 1) * res); }; for (auto i : d[c]) calcg(i); } } int main() { freopen("butterfly.in", "r", stdin); freopen("butterfly.out", "w", stdout); cin.tie(0)->sync_with_stdio(0); cin >> n >> P; mod.init(P); initC(N - 5); initP(N - 5); initD(N - 5); pn[0] = 1; for (int i = 1; i <= n; ++i) pn[i] = mod(pn[i - 1] * n); calcF(); calcG(); // calc ans for (int i = 1; i <= n; ++i) f[i] = mod(f[i] + f[i - 1]); for (int i = 1; i <= n; ++i) g[i] = mod(g[i] + g[i - 1]); LL ans = 0; for (int j = 0; j <= n; ++j) { LL res = mod(binom(n, j) * pn[n - j]); res = mod(res * fac[j]); // for (int i = 0; i < j; ++i) { // ans = mod(ans + (f[i] + g[j - i]) * res); // } res = mod(res * ((j == 0 ? 0 : f[j - 1]) + g[j])); ans = mod(ans + res); } cout << ans % P << "\n"; return 0; }