結果
問題 |
No.2756 GCD Teleporter
|
ユーザー |
|
提出日時 | 2024-05-11 19:06:28 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,940 bytes |
コンパイル時間 | 2,096 ms |
コンパイル使用メモリ | 201,316 KB |
最終ジャッジ日時 | 2025-02-21 13:43:13 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | WA * 1 TLE * 1 -- * 34 |
ソースコード
#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; } } for(auto p:P){ vector<int>v; for(int i=0;i<N;i++){ if(A[i]%p==0){ v.push_back(i); } } int l=v.size(); for(int i=0;i<l-1;i++){ uf.unite(v[i],v[i+1]); } } ll ans=1LL<<60; for(auto p:P){ int at=-1; for(int i=0;i<N;i++){ if(A[i]%p==0){ at=i; break; } } if(at!=-1){ chmin(ans,(ll)(N-uf.size(at))*p); }else{ chmin(ans,(ll)N*p); } } cout<<ans<<"\n"; }