結果

問題 No.2756 GCD Teleporter
ユーザー FplusFplusFFplusFplusF
提出日時 2024-05-10 23:12:41
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,221 bytes
コンパイル時間 3,767 ms
コンパイル使用メモリ 266,492 KB
実行使用メモリ 20,200 KB
最終ジャッジ日時 2024-05-10 23:13:20
合計ジャッジ時間 37,381 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
7,936 KB
testcase_01 AC 9 ms
8,188 KB
testcase_02 AC 9 ms
8,060 KB
testcase_03 AC 10 ms
8,060 KB
testcase_04 AC 237 ms
9,724 KB
testcase_05 AC 669 ms
11,260 KB
testcase_06 AC 1,474 ms
17,224 KB
testcase_07 AC 1,200 ms
14,424 KB
testcase_08 AC 1,107 ms
13,988 KB
testcase_09 AC 958 ms
13,388 KB
testcase_10 AC 1,369 ms
16,484 KB
testcase_11 AC 1,728 ms
18,112 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 TLE -
testcase_20 AC 1,356 ms
19,784 KB
testcase_21 AC 733 ms
13,600 KB
testcase_22 AC 1,188 ms
20,100 KB
testcase_23 AC 269 ms
9,940 KB
testcase_24 AC 1,133 ms
17,540 KB
testcase_25 AC 1,180 ms
18,032 KB
testcase_26 AC 1,220 ms
19,544 KB
testcase_27 AC 1,170 ms
20,200 KB
testcase_28 AC 613 ms
14,248 KB
testcase_29 AC 793 ms
18,396 KB
testcase_30 AC 965 ms
19,928 KB
testcase_31 AC 542 ms
13,228 KB
testcase_32 AC 943 ms
17,156 KB
testcase_33 AC 361 ms
11,448 KB
testcase_34 AC 59 ms
9,212 KB
testcase_35 AC 371 ms
17,284 KB
testcase_36 AC 254 ms
19,260 KB
testcase_37 WA -
testcase_38 AC 90 ms
17,344 KB
testcase_39 AC 90 ms
17,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
const ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> inline bool chmin(T &a,T b){
    if(a>b){
        a=b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T &a,T b){
    if(a<b){
        a=b;
        return true;
    }
    return false;
}
struct union_find{
    int n,now_comp_cnt;
    vector<int> par,sz,min_v;
    union_find(int n_):n(n_),now_comp_cnt(n_),par(n),sz(n,1),min_v(n){
        for(int i=0;i<n;i++){
            par[i]=i;
            min_v[i]=i;
        }
    }
    int root(int x){
        if(par[x]==x) return x;
        return par[x]=root(par[x]);
    }
    bool unite(int x,int y){
        x=root(x);
        y=root(y);
        if(x==y) return false;
        if(sz[x]<sz[y]) swap(x,y);
        par[y]=x;
        sz[x]+=sz[y];
        min_v[x]=min_v[y]=min(min_v[x],min_v[y]);
        now_comp_cnt--;
        return true;
    }
    bool same(int x,int y){
        int rx=root(x);
        int ry=root(y);
        return rx==ry;
    }
    int leader(int x){
        return min_v[x];
    }
    int size(int x){
        return sz[root(x)];
    }
    int comp_cnt(){
        return now_comp_cnt;
    }
    vector<int> all_size(){
        vector<int> cnt(n,0);
        for(int i=0;i<n;i++){
            cnt[root(i)]++;
        }
        vector<int> ret;
        for(int i=0;i<n;i++){
            if(0<cnt[i]) ret.push_back(cnt[i]);
        }
        return ret;
    }
    vector<vector<int>> all_comp(){
        vector<vector<int>> comp(n);
        for(int i=0;i<n;i++){
            comp[root(i)].push_back(i);
        }
        int comp_idx=0;
        vector<vector<int>> ret(now_comp_cnt);
        for(int i=0;i<n;i++){
            if(!comp[i].empty()){
                swap(ret[comp_idx],comp[i]);
                comp_idx++;
            }
        }
        return ret;
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll mx=2e5+1;
    vector<ll> p(mx,1);
    for(ll i=2;i<mx;i++){
        if(!p[i]) continue;
        for(ll j=i+i;j<mx;j+=i) p[j]=0;
    }
    vector<ll> v;
    for(ll i=2;i<mx;i++){
        if(p[i]) v.push_back(i);
    }
    ll sz=(ll)v.size();
    vector<ll> idx(mx,-1);
    rep(i,sz) idx[v[i]]=i;
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    union_find uf(n+sz);
    rep(i,n){
        ll x=a[i];
        for(ll j=2;j*j<=x;j++){
            if(x%j!=0) continue;
            while(x%j==0){
                x/=j;
            }
            uf.unite(i,n+idx[j]);
        }
        if(x!=1) uf.unite(i,n+idx[x]);
    }
    ll ans=0;
    vector<vector<int>> ac=uf.all_comp();
    sort(all(ac));
    ll cnt=0;
    rep(j,sz){
        if(uf.size(n+j)==1) continue;
        cnt++;
        if(cnt==30){
            ac=uf.all_comp();
            sort(all(ac));
            cnt=0;
        }
        for(auto c:ac){
            if(n<=c[0]) break;
            if(!uf.same(c[0],n+j)){
                ans+=v[j];
                uf.unite(c[0],n+j);
            }
        }
    }
    cout << ans << '\n';
}
0