結果

問題 No.2756 GCD Teleporter
ユーザー MZKiMZKi
提出日時 2023-03-13 16:10:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 2,227 bytes
コンパイル時間 4,822 ms
コンパイル使用メモリ 270,304 KB
実行使用メモリ 7,556 KB
最終ジャッジ日時 2023-10-18 11:13:28
合計ジャッジ時間 10,312 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,916 KB
testcase_01 AC 4 ms
4,916 KB
testcase_02 AC 4 ms
4,916 KB
testcase_03 AC 4 ms
4,916 KB
testcase_04 AC 13 ms
5,180 KB
testcase_05 AC 46 ms
5,972 KB
testcase_06 AC 123 ms
7,028 KB
testcase_07 AC 99 ms
6,764 KB
testcase_08 AC 90 ms
6,500 KB
testcase_09 AC 78 ms
6,500 KB
testcase_10 AC 113 ms
6,764 KB
testcase_11 AC 145 ms
7,292 KB
testcase_12 AC 31 ms
5,764 KB
testcase_13 AC 141 ms
7,556 KB
testcase_14 AC 131 ms
7,556 KB
testcase_15 AC 31 ms
5,776 KB
testcase_16 AC 73 ms
6,764 KB
testcase_17 AC 89 ms
7,028 KB
testcase_18 AC 29 ms
5,732 KB
testcase_19 AC 147 ms
7,556 KB
testcase_20 AC 115 ms
6,764 KB
testcase_21 AC 55 ms
5,708 KB
testcase_22 AC 116 ms
6,764 KB
testcase_23 AC 18 ms
5,180 KB
testcase_24 AC 92 ms
6,236 KB
testcase_25 AC 100 ms
6,500 KB
testcase_26 AC 111 ms
6,764 KB
testcase_27 AC 119 ms
6,764 KB
testcase_28 AC 64 ms
5,972 KB
testcase_29 AC 95 ms
6,236 KB
testcase_30 AC 122 ms
6,764 KB
testcase_31 AC 53 ms
5,708 KB
testcase_32 AC 81 ms
6,500 KB
testcase_33 AC 34 ms
5,444 KB
testcase_34 AC 15 ms
5,180 KB
testcase_35 AC 85 ms
6,500 KB
testcase_36 AC 82 ms
6,500 KB
testcase_37 AC 4 ms
4,916 KB
testcase_38 AC 140 ms
6,500 KB
testcase_39 AC 154 ms
6,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<atcoder/all>
using namespace atcoder;

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

struct smart_sieve{
	int n;
	vector<int> f;
	// 初期構築 : (nloglog(n))
	smart_sieve(int x = 1) :n(x), f(x+1, -1){
        for(ll i = 2; i <= n; i++){
            if(f[i] != -1)continue;
            for(ll j = i*i; j <= n; j += i){
                f[j] = (f[j] == -1 ? i : f[j]);
            }
            f[i] = i;
        }
	}
	// 素数判定 : O(1)
	bool isPrime(int x){return f[x] == x;}
	// 素因数分解 : O(log(n))
	vector<pair<ll,ll>> prime_factor(int x){
        if(x == 1)return {};
        vector<pair<ll, ll>> ret;
        ret.emplace_back(f[x], 1); x /= f[x];
		while(x != 1){
            if(f[x] == ret.back().first){
                ret.back().second++;
            }else{
                ret.emplace_back(f[x], 1);
            }
            x /= f[x];
        }
        return ret;
	}
};


int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n)cin >> a[i];

    set<int> pf;
    smart_sieve ss(200000);
    rep(i, n){
        vector<pair<ll, ll>> p;
        p = ss.prime_factor(a[i]);
        for(auto x : p)pf.insert(x.first);
    }
    
    int cnt = 0, mn = mod;
    vector<int> pos(200000, -1);
    for(auto it = pf.begin(); it != pf.end(); it++){
        mn = (mn == mod ? (*it) : mn); pos[*it] = cnt++;
    }

    dsu uf(n+cnt);
    rep(i, n){
        vector<pair<ll, ll>> p;
        p = ss.prime_factor(a[i]);
        for(auto x : p)uf.merge(i, n+pos[x.first]);
    }

    set<int> st;
    rep(i, n)st.insert(uf.leader(i));
    int ans = mod, S = st.size();
    if(pos[2] == -1){
        ans = min(2*S, mn * (S-1));
    }else{
        ans = 2 * (S-1);
    }
	
    cout << ans << endl;
}
0