結果

問題 No.2756 GCD Teleporter
ユーザー FplusFplusF
提出日時 2024-05-10 23:26:18
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 932 ms / 2,000 ms
コード長 2,951 bytes
コンパイル時間 3,234 ms
コンパイル使用メモリ 260,560 KB
実行使用メモリ 10,876 KB
最終ジャッジ日時 2024-12-20 07:45:37
合計ジャッジ時間 13,155 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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);
    ll mn=INF;
    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]);
            chmin(mn,j);
        }
        if(x!=1){
            uf.unite(i,n+idx[x]);
            chmin(mn,x);
        }
    }
    set<ll> st;
    rep(i,n) st.insert(uf.root(i));
    ll s=(ll)st.size();
    cout << min(2*s,mn*(s-1)) << '\n';
}
0