結果
問題 | No.2705 L to R Graph (Another ver.) |
ユーザー | zlxFTH |
提出日時 | 2024-05-25 13:23:34 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,240 ms / 3,000 ms |
コード長 | 4,549 bytes |
コンパイル時間 | 1,801 ms |
コンパイル使用メモリ | 173,816 KB |
実行使用メモリ | 20,944 KB |
最終ジャッジ日時 | 2024-05-25 13:24:59 |
合計ジャッジ時間 | 82,693 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 67 ms
19,284 KB |
testcase_01 | AC | 65 ms
19,864 KB |
testcase_02 | AC | 69 ms
19,440 KB |
testcase_03 | AC | 64 ms
20,224 KB |
testcase_04 | AC | 66 ms
19,448 KB |
testcase_05 | AC | 67 ms
20,200 KB |
testcase_06 | AC | 69 ms
19,948 KB |
testcase_07 | AC | 69 ms
19,772 KB |
testcase_08 | AC | 1,523 ms
19,992 KB |
testcase_09 | AC | 1,875 ms
20,188 KB |
testcase_10 | AC | 1,158 ms
19,856 KB |
testcase_11 | AC | 1,408 ms
20,140 KB |
testcase_12 | AC | 885 ms
20,756 KB |
testcase_13 | AC | 462 ms
19,948 KB |
testcase_14 | AC | 1,780 ms
20,112 KB |
testcase_15 | AC | 155 ms
19,572 KB |
testcase_16 | AC | 897 ms
19,896 KB |
testcase_17 | AC | 323 ms
19,696 KB |
testcase_18 | AC | 1,661 ms
20,128 KB |
testcase_19 | AC | 687 ms
20,120 KB |
testcase_20 | AC | 395 ms
19,784 KB |
testcase_21 | AC | 1,322 ms
20,280 KB |
testcase_22 | AC | 1,301 ms
19,900 KB |
testcase_23 | AC | 2,086 ms
20,212 KB |
testcase_24 | AC | 2,090 ms
20,268 KB |
testcase_25 | AC | 2,028 ms
20,384 KB |
testcase_26 | AC | 2,087 ms
20,272 KB |
testcase_27 | AC | 1,955 ms
20,496 KB |
testcase_28 | AC | 1,924 ms
20,140 KB |
testcase_29 | AC | 1,866 ms
20,316 KB |
testcase_30 | AC | 1,947 ms
20,204 KB |
testcase_31 | AC | 1,917 ms
20,208 KB |
testcase_32 | AC | 1,960 ms
20,264 KB |
testcase_33 | AC | 1,905 ms
20,412 KB |
testcase_34 | AC | 2,097 ms
20,540 KB |
testcase_35 | AC | 1,932 ms
20,476 KB |
testcase_36 | AC | 2,073 ms
20,500 KB |
testcase_37 | AC | 2,149 ms
20,312 KB |
testcase_38 | AC | 1,913 ms
20,320 KB |
testcase_39 | AC | 2,097 ms
20,340 KB |
testcase_40 | AC | 2,011 ms
20,692 KB |
testcase_41 | AC | 2,190 ms
20,324 KB |
testcase_42 | AC | 2,066 ms
20,308 KB |
testcase_43 | AC | 2,188 ms
20,288 KB |
testcase_44 | AC | 2,188 ms
20,944 KB |
testcase_45 | AC | 2,189 ms
20,304 KB |
testcase_46 | AC | 2,215 ms
20,808 KB |
testcase_47 | AC | 2,193 ms
20,460 KB |
testcase_48 | AC | 2,240 ms
20,352 KB |
testcase_49 | AC | 2,213 ms
20,412 KB |
testcase_50 | AC | 2,197 ms
20,260 KB |
testcase_51 | AC | 2,196 ms
20,180 KB |
testcase_52 | AC | 2,205 ms
20,644 KB |
ソースコード
#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 R = j / (j / l); auto F = [&](int x) { return (j - 1) / (j / x) + 1; }; auto r = F(R); 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; }