結果
| 問題 |
No.2756 GCD Teleporter
|
| コンテスト | |
| ユーザー |
FplusFplusF
|
| 提出日時 | 2024-05-10 23:09:50 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,175 bytes |
| コンパイル時間 | 3,708 ms |
| コンパイル使用メモリ | 258,840 KB |
| 実行使用メモリ | 20,068 KB |
| 最終ジャッジ日時 | 2024-12-20 07:24:36 |
| 合計ジャッジ時間 | 33,209 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 WA * 8 TLE * 1 |
ソースコード
#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;
vector<vector<int>> ac=uf.all_comp();
ll cnt=0;
rep(j,sz){
if(uf.size(n+j)==1) continue;
cnt++;
if(cnt==30){
ac=uf.all_comp();
cnt=0;
}
for(auto c:ac){
if(n<=c[0]) break;
if(!uf.same(c[0],n+j)){
ans+=v[j];
uf.unite(c[0],n+j);
}
}
}
cout << ans << '\n';
}
FplusFplusF