結果

問題 No.2756 GCD Teleporter
ユーザー FplusFplusFFplusFplusF
提出日時 2024-05-10 22:55:59
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,985 bytes
コンパイル時間 3,436 ms
コンパイル使用メモリ 256,288 KB
実行使用メモリ 10,748 KB
最終ジャッジ日時 2024-05-10 22:56:29
合計ジャッジ時間 28,641 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
6,816 KB
testcase_01 AC 7 ms
6,940 KB
testcase_02 AC 7 ms
6,940 KB
testcase_03 AC 6 ms
6,940 KB
testcase_04 AC 61 ms
6,940 KB
testcase_05 AC 270 ms
7,932 KB
testcase_06 AC 792 ms
9,852 KB
testcase_07 AC 602 ms
9,212 KB
testcase_08 AC 555 ms
8,828 KB
testcase_09 AC 475 ms
8,700 KB
testcase_10 AC 690 ms
9,468 KB
testcase_11 AC 879 ms
10,236 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1,249 ms
10,492 KB
testcase_21 AC 551 ms
8,444 KB
testcase_22 AC 1,065 ms
10,496 KB
testcase_23 AC 149 ms
7,292 KB
testcase_24 AC 999 ms
9,724 KB
testcase_25 AC 1,062 ms
9,852 KB
testcase_26 AC 1,104 ms
10,240 KB
testcase_27 AC 1,043 ms
10,620 KB
testcase_28 AC 531 ms
8,700 KB
testcase_29 AC 767 ms
9,724 KB
testcase_30 AC 1,026 ms
10,624 KB
testcase_31 AC 448 ms
8,444 KB
testcase_32 AC 1,469 ms
10,620 KB
testcase_33 AC 554 ms
8,316 KB
testcase_34 AC 127 ms
7,420 KB
testcase_35 AC 913 ms
10,620 KB
testcase_36 AC 763 ms
10,624 KB
testcase_37 WA -
testcase_38 AC 614 ms
10,748 KB
testcase_39 AC 637 ms
10,748 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;
    rep(j,sz){
        if(1000<j) break;
        rep(i,n){
            if(!uf.same(i,n+j)&&1<uf.size(n+j)){
                ans+=v[j];
                uf.unite(i,n+j);
            }
        }
    }
    cout << ans << '\n';
}
0