結果
問題 |
No.2756 GCD Teleporter
|
ユーザー |
|
提出日時 | 2024-05-11 19:29:18 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 329 ms / 2,000 ms |
コード長 | 2,187 bytes |
コンパイル時間 | 2,083 ms |
コンパイル使用メモリ | 203,400 KB |
最終ジャッジ日時 | 2025-02-21 13:44:02 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 36 |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll=long long; #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} struct union_find { public: explicit union_find() {} explicit union_find(int N) : n(N), par(N, -1), siz(N, 1) {} int find(int x) { assert(0 <= x && x < n); if (par[x] == -1) return x; par[x] = find(par[x]); return par[x]; } int same(int x, int y) { assert(0 <= x && x < n); assert(0 <= y && y < n); return find(x) == find(y); } int size(int x) { assert(0 <= x && x < n); return siz[find(x)]; } bool unite(int x, int y) { assert(0 <= x && x < n); assert(0 <= y && y < n); x = find(x); y = find(y); if (x == y) return false; if (siz[x] < siz[y]) std::swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } private: int n; std::vector<int> par, siz; }; const int M=200'000; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin>>N; vector<int>A(N); for(int i=0;i<N;i++)cin>>A[i]; union_find uf(N); vector<int>P; vector<bool>is_prime(M+1,true); is_prime[1]=false; for(int p=2;p<=M;p++){ if(!is_prime[p])continue; P.push_back(p); for(int i=2*p;i<=M;i+=p){ is_prime[i]=false; } } vector<vector<int>>V(M+1); vector<int>at(M+1,-1); for(int i=0;i<N;i++){ int a=A[i]; for(int x=2;x*x<=a;x++){ bool ok=false; while(a%x==0){ a/=x; ok=true; } if(ok){ at[x]=i; V[x].push_back(i); } } if(a>1){ at[a]=i; V[a].push_back(i); } } for(auto p:P){ int l=V[p].size(); for(int i=0;i<l-1;i++){ uf.unite(V[p][i],V[p][i+1]); } } ll ans=1LL<<60; vector<bool>flag(N); int cnt=0; for(int i=0;i<N;i++){ if(!flag[uf.find(i)]){ flag[uf.find(i)]=true; cnt++; } } for(auto p:P){ if(at[p]!=-1){ chmin(ans,(ll)(cnt-1)*p); }else{ chmin(ans,(ll)cnt*p); } } cout<<ans<<"\n"; }