結果

問題 No.3270 No Coprime Cycles
ユーザー rabimea
提出日時 2025-09-23 20:54:43
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 1,227 bytes
コンパイル時間 3,930 ms
コンパイル使用メモリ 292,700 KB
実行使用メモリ 33,024 KB
最終ジャッジ日時 2025-09-23 20:54:57
合計ジャッジ時間 11,898 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC optimize ("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target ("avx,avx2,fma")
#include <bits/stdc++.h>

using std::cin, std::cout, std::cerr;
using ll = long long;

const int N = 1e6 + 1;
int p[N];

int main() {
    std::iota(p, p + N, 0);
    for(int i = 2; i < N; i ++) if(p[i] == i) {
        for(int j = i * 2; j < N; j += i) {
            p[j] = p[j] == j ? i : p[j];
        }
    }
    std::ios::sync_with_stdio(false);
    int n; cin >> n;
    std::vector<std::tuple<int, int, int>> e;
    for(int i = 1; i <= n; i ++) {
        int x; cin >> x;
        while(x != 1) {
            int y = p[x], c = 0;
            while(x % y == 0) {
                x /= y;
                c += y;
            }
            e.push_back({c, i, n + y});
        }
    }
    std::sort(e.rbegin(), e.rend());
    std::vector<int> fa(n + N + 1);
    std::iota(fa.begin(), fa.end(), 0);
    std::function<int(int)> root = [&](int x) {
        if(x == fa[x]) return x;
        return fa[x] = root(fa[x]);
    };
    ll ans = 0;
    for(auto [c, u, v] : e) {
        int x = root(u), y = root(v);
        if(x == y)
            ans += c;
        else
            fa[x] = y;
    }
    cout << ans << '\n';
}
0