結果

問題 No.2756 GCD Teleporter
ユーザー aradarad
提出日時 2024-05-10 22:29:31
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 983 ms / 2,000 ms
コード長 2,616 bytes
コンパイル時間 6,542 ms
コンパイル使用メモリ 332,856 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2024-05-10 22:29:50
合計ジャッジ時間 18,422 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,812 KB
testcase_04 AC 23 ms
6,816 KB
testcase_05 AC 98 ms
6,940 KB
testcase_06 AC 270 ms
6,940 KB
testcase_07 AC 213 ms
6,944 KB
testcase_08 AC 198 ms
6,940 KB
testcase_09 AC 172 ms
6,940 KB
testcase_10 AC 244 ms
6,940 KB
testcase_11 AC 339 ms
6,940 KB
testcase_12 AC 74 ms
6,940 KB
testcase_13 AC 363 ms
7,424 KB
testcase_14 AC 333 ms
7,040 KB
testcase_15 AC 76 ms
6,940 KB
testcase_16 AC 186 ms
6,944 KB
testcase_17 AC 230 ms
6,940 KB
testcase_18 AC 70 ms
6,944 KB
testcase_19 AC 370 ms
7,424 KB
testcase_20 AC 627 ms
6,940 KB
testcase_21 AC 269 ms
6,944 KB
testcase_22 AC 382 ms
6,940 KB
testcase_23 AC 71 ms
6,944 KB
testcase_24 AC 500 ms
6,944 KB
testcase_25 AC 496 ms
6,944 KB
testcase_26 AC 458 ms
6,944 KB
testcase_27 AC 386 ms
6,940 KB
testcase_28 AC 182 ms
6,944 KB
testcase_29 AC 276 ms
6,940 KB
testcase_30 AC 360 ms
6,940 KB
testcase_31 AC 153 ms
6,944 KB
testcase_32 AC 983 ms
6,940 KB
testcase_33 AC 376 ms
6,944 KB
testcase_34 AC 59 ms
6,944 KB
testcase_35 AC 411 ms
6,940 KB
testcase_36 AC 267 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 136 ms
6,940 KB
testcase_39 AC 152 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using VS = vector<string>;
using P = pair<ll,ll>;
using VP = vector<P>;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define out(x) cout << x << endl
#define dout(x) cout << fixed << setprecision(10) << x << endl
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)(x.size())
#define re0 return 0
#define pcnt __builtin_popcountll
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int inf = 1e9;
constexpr ll INF = 1e18;
//using mint = modint1000000007;
using mint = modint998244353;
int di[4] = {1,0,-1,0};
int dj[4] = {0,1,0,-1};

vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0; // 指数

        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        res.push_back({a, ex});
    }

    if (N != 1) res.push_back({N, 1});
    return res;
}

int main(){
    ll n;
    cin >> n;
    VL a(n);
    rep(i,n) cin >> a[i];
    sort(all(a));
    if(a.back()==1){
        out(n*2);
        re0;
    }
    const ll mx = 1e5;
    dsu uf(n+mx);
    map<ll,ll> id;
    ll now = n;
    rep(i,n){
        VP ps = prime_factorize(a[i]);
        for(auto [c,d]:ps){
            if(id.count(c)){
                uf.merge(id[c],i);
            } else {
                id[c] = now++;
                uf.merge(id[c],i);
            }
        }
    }
    ll ans = INF;
    {
        set<ll> seen;
        bool flag = false;
        if(!id.count(2)){
            id[2] = now++;
            flag = true;
        }
        ll cnt = 0;
        rep(i,n){
            if(uf.same(id[2],i)) continue;
            if(seen.count(uf.leader(i))) continue;
            cnt++;
            seen.insert(uf.leader(i));
        }
        chmin(ans,cnt*2);
        if(flag) id.erase(2);
    }
    {
        set<ll> seen;
        ll mn = id.begin()->first;
        ll cnt = 0;
        rep(i,n){
            if(uf.same(id[mn],i)) continue;
            if(seen.count(uf.leader(i))) continue;
            cnt++;
            seen.insert(uf.leader(i));
        }
        chmin(ans,cnt*mn);
    }
    out(ans);
}
0