// the library can be found here: https://maspypy.github.io/library/ // note, the version used here could be different from the official version // BEGIN: b.cpp #line 1 "b.cpp" // BEGIN: ../library/my_template.hpp #line 1 "../library/my_template.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // C++ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(MASPY_LOCAL) #include #else 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; // f128 is not supported so we will use using f128 = long double; template constexpr T infty = 0; template <> constexpr int infty = 1'010'000'000; template <> constexpr ll infty = 2'020'000'000'000'000'000; template <> constexpr u32 infty = infty; template <> constexpr u64 infty = infty; template <> constexpr i128 infty = i128(infty) * 2'000'000'000'000'000'000; template <> constexpr double infty = numeric_limits::infinity(); template <> constexpr long double infty = numeric_limits::infinity(); using pi = pair; using vi = vector; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template using vvvvc = vector>; template using pq_max = priority_queue; template using pq_min = priority_queue, greater>; #define vv(type, name, h, ...) \ vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector>> name( \ h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name( \ a, vector>>( \ b, vector>(c, vector(__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 = (a) - 1; i >= ll(0); --i) #define FOR2_R(i, a) for (ll i = (a) - 1; i >= ll(0); --i) #define FOR3_R(i, a, b) for (ll i = (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 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 T kth_bit(int k) { return T(1) << k; } template bool has_kth_bit(T x, int k) { return x >> k & 1; } int __lg(unsigned long long x) { return x ? (63 - __builtin_clzll(x)) : -1; } template struct all_bit { struct iter { UINT s; iter(UINT s) : s(s) {} int operator*() const { return lowbit(s); } iter &operator++() { s &= s - 1; return *this; } bool operator!=(const iter) const { return s != 0; } }; UINT s; all_bit(UINT s) : s(s) {} iter begin() const { return iter(s); } iter end() const { return iter(0); } }; template struct all_subset { static_assert(is_unsigned::value); struct iter { UINT s, t; bool ed; iter(UINT s) : s(s), t(s), ed(0) {} UINT operator*() const { return s ^ t; } iter &operator++() { (t == 0 ? ed = 1 : t = (t - 1) & s); return *this; } bool operator!=(const iter) const { return !ed; } }; UINT s; all_subset(UINT s) : s(s) {} iter begin() const { return iter(s); } iter end() const { return iter(0); } }; template T floor(T a, T b) { return a / b - (a % b && (a ^ b) < 0); } template T ceil(T x, T y) { return floor(x + y - 1, y); } template T bmod(T x, T y) { return x - y * floor(x, y); } template pair divmod(T x, T y) { T q = floor(x, y); return {q, x - q * y}; } constexpr ll TEN[] = { 1LL, 10LL, 100LL, 1000LL, 10000LL, 100000LL, 1000000LL, 10000000LL, 100000000LL, 1000000000LL, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL, 1000000000000000000LL, }; template 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 inline long long LB(const C &c, const T &x) { return lower_bound(c.begin(), c.end(), x) - c.begin(); } template 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()), x.shrink_to_fit() template T POP(deque &que) { T a = que.front(); que.pop_front(); return a; } template T POP(priority_queue &que) { T a = que.top(); que.pop(); return a; } template T POP(vc &que) { T a = que.back(); que.pop_back(); return a; } template ll binary_search(F check, ll ok, ll ng, bool check_ok = true) { if (check_ok) assert(check(ok)); while (llabs(ok - ng) > 1) { auto x = (ng + ok) / 2; (check(x) ? ok : ng) = x; } return ok; } template 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 inline bool chmax(T &a, const S &b) { T c = max(a, b); bool changed = (c != a); a = c; return changed; } template inline bool chmin(T &a, const S &b) { T c = min(a, b); bool changed = (c != a); a = c; return changed; } // ? は -1 vc s_to_vi(const string &S, char first_char) { vc A(S.size()); FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); } return A; } template vc cumsum(const vc &A, int off = 1) { int N = A.size(); vc 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 vc argsort(const vc &A) { vc 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 vc rearrange(const vc &A, const vc &I) { vc B(len(I)); FOR(i, len(I)) B[i] = A[I[i]]; return B; } template void concat(vc &first, const Vectors &...others) { vc &res = first; (res.insert(res.end(), others.begin(), others.end()), ...); } #endif // END: ../library/my_template.hpp #line 2 "b.cpp" // BEGIN: ../library/nt/extgcd.hpp #line 1 "../library/nt/extgcd.hpp" // {g,x,y}, ax+by=g template tuple extgcd(T a, T b) { static_assert(is_same_v || is_same_v); T x = 1, y = 0, x1 = 0, y1 = 1; while (b != 0) { T q = a / b; swap(a %= b, b); T X = x - q * x1, Y = y - q * y1; x = x1, y = y1, x1 = X, y1 = Y; } return {a, x, y}; } tuple Farey_lr(ll a, ll b) { assert(a > 0 && b > 0); /* Farey 数列で、a/b が最初に現れるときの、左右を求める。 a/b = 19/12 → (x1/y1, x2/y2) = (11/7, 8/5) → (11,7,8,5) を返す。 */ if (a == b) return {0, 1, 1, 0}; ll q = (a - 1) / b; auto [x1, y1, x2, y2] = Farey_lr(b, a - q * b); return {q * x2 + y2, x2, q * x1 + y1, x1}; } tuple extgcd2(ll a, ll b) { // ax + by = d の最小解 (x, y, d) を返す。 // (|x|+|y|, x) に関する辞書順最小とする。 auto [x1, y1, x2, y2] = Farey_lr(a, b); tie(x1, y1) = mp(y1, -x1); tie(x2, y2) = mp(-y2, x2); ll g = a * x1 + b * y1; pi key1 = mp(abs(x1) + abs(y1), x1); pi key2 = mp(abs(x2) + abs(y2), x2); return (key1 < key2 ? mt(x1, y1, g) : mt(x2, y2, g)); } // END: ../library/nt/extgcd.hpp #line 3 "b.cpp" // BEGIN: ../library/nt/divisors.hpp #line 1 "../library/nt/divisors.hpp" // BEGIN: ../library/nt/factor.hpp #line 1 "../library/nt/factor.hpp" // BEGIN: ../library/random/base.hpp #line 1 "../library/random/base.hpp" u64 RNG_64() { static u64 x_ = u64(chrono::duration_cast(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: ../library/random/base.hpp #line 4 "../library/nt/factor.hpp" // BEGIN: ../library/nt/primetest.hpp #line 1 "../library/nt/primetest.hpp" // BEGIN: ../library/mod/mongomery_modint.hpp #line 1 "../library/mod/mongomery_modint.hpp" // odd mod. // x の代わりに rx を持つ template struct Mongomery_modint { using mint = Mongomery_modint; inline static U1 m, r, n2; static constexpr int W = numeric_limits::digits; static void set_mod(U1 mod) { assert(mod & 1 && mod <= U1(1) << (W - 2)); m = mod, n2 = -U2(m) % m, r = m; FOR(5) 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; Mongomery_modint() : x(0) {} Mongomery_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 using Mongomery_modint_32 = Mongomery_modint; template using Mongomery_modint_64 = Mongomery_modint; // END: ../library/mod/mongomery_modint.hpp #line 3 "../library/nt/primetest.hpp" bool primetest(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 = Mongomery_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: ../library/nt/primetest.hpp #line 5 "../library/nt/factor.hpp" template 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 (primetest(n)) return n; FOR(100) { ll m = 0; if (n < (1 << 30)) { using mint = Mongomery_modint_32<20231025>; mint::set_mod(n); m = rho(n, RNG(0, n)); } else { using mint = Mongomery_modint_64<20231025>; mint::set_mod(n); m = rho(n, RNG(0, n)); } if (primetest(m)) return m; n = m; } assert(0); return -1; } // ソートしてくれる vc> factor(ll n) { assert(n >= 1); vc> 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> factor_by_lpf(ll n, vc& lpf) { vc> 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: ../library/nt/factor.hpp #line 2 "../library/nt/divisors.hpp" // sort はしない vc divisors_by_pf(const vc>& pf) { vi div = {1}; for (auto&& [p, e]: pf) { ll n = len(div); ll pp = 1; FOR3(i, 1, e + 1) { pp *= p; FOR(j, n) div.eb(div[j] * pp); } } return div; } // sort はしない vc divisors(ll N) { auto pf = factor(N); return divisors_by_pf(pf); } // sort はしない vc divisors_by_lpf(ll N, vc& lpf) { auto pf = factor_by_lpf(N, lpf); return divisors_by_pf(pf); }// END: ../library/nt/divisors.hpp #line 4 "b.cpp" #include std::vector> g_divisor_table; void solve() { int n; std::cin >> n; std::vector v(n); for (auto &it : v) { std::cin >> it; it--; } std::vector used(n, 0); std::vector> cycles; for (int i = 0; i < n; i++) { if (used[i]) continue; int curr = i; used[curr] = 1; cycles.push_back({}); auto &cycle = cycles.back(); cycle.push_back(i); while (!used[v[curr]]) { curr = v[curr]; used[curr] = 1; cycle.push_back(curr); } } // for (auto cycle: cycles) { // for (auto it: cycle) { // std::cout << it << " "; // } // std::cout << std::endl; // } // maps divisor to number of cycles which won't get the discount std::map no_discount; std::map edge_count; ll live_cycles = 0; // for each cycle index we will add it everywhere it participates in std::map> participants; // std::cout << "cycles.size() = " << cycles.size() << std::endl; for (int i = 0; i < cycles.size(); i++) { // std::cout << "iter: " << i << " cycle size: " << cycles[i].size() << std::endl; if (cycles[i].size() <= 1) continue; live_cycles += 1; ll com = std::abs(cycles[i][0] - cycles[i].back()); // std::cout << "com = " << com << std::endl; edge_count[com] += 1; if (cycles[i].size() >= 3) { for (auto d : g_divisor_table[com]) { participants[d].insert(i); } } for (int j = 1; j < cycles[i].size(); j++) { ll expr = std::abs(cycles[i][j] - cycles[i][j - 1]); if (cycles[i].size() >= 3) { edge_count[expr] += 1; for (auto d : g_divisor_table[expr]) { participants[d].insert(i); } } com = std::get<0>(extgcd(com, expr)); } auto com_d = divisors(com); for (auto d: com_d) { no_discount[d] += 1; } } // for (auto [e, cnt] : edge_count) { // std::cout << cnt << " edges with len " << e << std::endl; // } // std::cout << "live cycles: " << live_cycles << std::endl; // for (auto [d, cnd] : no_discount) { // std::cout << "for d = " << d << ", " << cnd << " cycles will not get discount" << std::endl; // } for (ll d = 1; d <= n - 1; d++) { // first we need to count overall number of edges with d | k then apply discounts ll ans = 0; for (ll k = d; k <= n; k += d) { ans += edge_count[k]; } // std::cout << "overall payment: " << ans << std::endl; // edge does not pay if it can be plased at the last position // which is same as the number of participants // so now the problem is to find the number of cycles containing d, 2*d, 3*d and so on // or we can ans -= participants[d].size(); // std::cout << "after discount: " << ans << std::endl; // ans -= participants[d].size(); // std::cout << "after discount: " << ans << std::endl; // ans -= (participants[d].size() - no_discount[d]); // then retract the discount for those who marked as no discount // std::cout << "after discount more: " << ans << std::endl; std::cout << ans << std::endl; } } int main() { g_divisor_table.resize(2 * 100'000 + 10); for (int i = 1; i < g_divisor_table.size(); i++) { g_divisor_table[i] = divisors(i); } int T; // std::cin >> T; T = 1; for (int i = 0; i < T; i++) { solve(); // std::cout << "==================\n"; } }// END: b.cpp