結果

問題 No.3270 No Coprime Cycles
ユーザー apricity
提出日時 2025-07-26 23:48:13
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 1,304 bytes
コンパイル時間 3,196 ms
コンパイル使用メモリ 117,264 KB
実行使用メモリ 46,812 KB
最終ジャッジ日時 2025-09-12 07:51:49
合計ジャッジ時間 11,971 ms
ジャッジサーバーID
(参考情報)
judge1 / judge
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <atcoder/dsu.hpp>
using namespace std;

constexpr int M = 1010101;

vector<bool> isprime(M);
vector<int> minfactor(M);

void init() {
    for (int i = 0; i < M; i++) isprime[i] = true, minfactor[i] = -1;
    isprime[0] = isprime[1] = false;
    for (int p = 2; p < M; p++) if (isprime[p]) {
        minfactor[p] = p;
        for (int q = p*2; q < M; q += p) {
            isprime[q] = false;
            if (minfactor[q] == -1) minfactor[q] = p;
        }
    }
}

vector<pair<int, int>> factorize(int n) {
    vector<pair<int, int>> res;
    while (n > 1) {
        int p = minfactor[n];
        int e = 0;
        while (n%p == 0) n /= p, e++;
        res.emplace_back(p, e);
    }
    return res;
}

int main() {
    init();
    int n; cin >> n;
    vector<vector<pair<int, int>>> edges(M);
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        int a; cin >> a;
        for (auto [p, e] : factorize(a)) {
            edges[p*e].emplace_back(i, n+p);
            ans += p*e;
        }
    }
    atcoder::dsu d(n + M);
    for (int w = M-1; w >= 0; w--) {
        for (auto [u, v] : edges[w]) {
            if (not d.same(u, v)) {
                d.merge(u, v);
                ans -= w;
            }
        }
    }
    cout << ans << "\n";
}
0