結果

問題 No.1917 LCMST
ユーザー suzuken_wsuzuken_w
提出日時 2022-04-29 23:02:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 332 ms / 4,000 ms
コード長 2,212 bytes
コンパイル時間 3,017 ms
コンパイル使用メモリ 226,500 KB
実行使用メモリ 49,004 KB
最終ジャッジ日時 2023-09-11 14:28:20
合計ジャッジ時間 15,086 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 318 ms
32,364 KB
testcase_10 AC 312 ms
32,040 KB
testcase_11 AC 313 ms
32,816 KB
testcase_12 AC 294 ms
31,792 KB
testcase_13 AC 212 ms
24,104 KB
testcase_14 AC 301 ms
33,604 KB
testcase_15 AC 190 ms
20,276 KB
testcase_16 AC 211 ms
23,556 KB
testcase_17 AC 235 ms
32,208 KB
testcase_18 AC 259 ms
32,848 KB
testcase_19 AC 192 ms
20,948 KB
testcase_20 AC 178 ms
17,196 KB
testcase_21 AC 194 ms
20,864 KB
testcase_22 AC 179 ms
17,316 KB
testcase_23 AC 178 ms
17,332 KB
testcase_24 AC 190 ms
19,636 KB
testcase_25 AC 173 ms
17,320 KB
testcase_26 AC 181 ms
17,332 KB
testcase_27 AC 186 ms
19,560 KB
testcase_28 AC 186 ms
19,908 KB
testcase_29 AC 330 ms
48,140 KB
testcase_30 AC 329 ms
48,896 KB
testcase_31 AC 325 ms
48,108 KB
testcase_32 AC 328 ms
48,236 KB
testcase_33 AC 332 ms
48,412 KB
testcase_34 AC 109 ms
14,984 KB
testcase_35 AC 107 ms
14,920 KB
testcase_36 AC 109 ms
15,056 KB
testcase_37 AC 330 ms
49,004 KB
testcase_38 AC 330 ms
48,780 KB
testcase_39 AC 328 ms
48,076 KB
testcase_40 AC 329 ms
48,812 KB
testcase_41 AC 329 ms
48,556 KB
testcase_42 AC 328 ms
48,532 KB
testcase_43 AC 108 ms
15,500 KB
testcase_44 AC 109 ms
15,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
// const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
class UF{
public:
	vector<int> par,num;
	int find(int v){
		return (par[v]==v)? v: (par[v]=find(par[v]));
	}
  explicit UF(){}
	explicit UF(int N):par(N),num(N,1){
		iota(all(par),0);
	}
  void init(int N){
     par.assign(N,0);
     num.assign(N,1);
     iota(all(par),0);
  }
	void unite(int u,int v){
		u=find(u),v=find(v);
		if(u==v)return;
		if(num[u]<num[v])swap(u,v);
		num[u]+=num[v];
		par[v]=u;
	}
	bool same(int u,int v){
		return find(u)==find(v);
	}
	bool ispar(int v){
		return v==find(v);
	}
	int size(int v){
		return num[find(v)];
	}
};
int main(){
    int n;
    cin>>n;
    V<int> a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    sort(all(a));
    ll ans=0;
    UF uf(n);
    for(int i=1;i<n;i++){
        if(a[i-1]==a[i]){
            uf.unite(i-1,i);
            ans+=a[i];
        }
    }
    ll sz=a.back();
    V<int> num(sz+1,-1);
    for(int i=0;i<n;i++){
        if(num[a[i]]!=-1)continue;
        num[a[i]]=i;
    }
    V<tuple<ll,int,int>> e;
    for(ll i=1;i<=sz;i++){
        ll v=-1;
        for(ll j=i;j<=sz;j+=i){
            if(num[j]!=-1){
                if(v==-1){
                    v=j;
                }else{
                    e.emplace_back(v*j/i,num[v],num[j]);
                }
            }
        }
    }
    sort(all(e));
    for(auto &[v,l,r]:e){
        if(uf.same(l,r))continue;
        ans+=v;
        uf.unite(l,r);
    }
    cout<<ans<<"\n";
}
0