結果

問題 No.1917 LCMST
ユーザー Bảo Nguyên Nguyễn
提出日時 2025-05-13 10:36:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,514 bytes
コンパイル時間 2,443 ms
コンパイル使用メモリ 207,788 KB
実行使用メモリ 55,436 KB
最終ジャッジ日時 2025-05-13 10:36:38
合計ジャッジ時間 19,020 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other AC * 3 WA * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll int64_t
#define ld long double
using namespace std;
// 
const int maxn =1e6+5;
const int maxm=1e5;
const int mod = 1e9+7; // 998244353,1610612741
const ll inf = 1e18;
const ld pi = atan(1.0L)*4;
struct nai{
	int u,v;
	ll w;
	bool operator<(const nai& other) const {
		return w<other.w;
	}
};
int n,par[maxn],m[maxm+5],pos[maxm+5];
vector<int> muls[maxm+5];
int find (int u) {
	return (u==par[u]?u:par[u]=find(par[u]));
}
bool join(int u,int v) {
	if ((u=find(u))==(v=find(v))) return false;
	par[v]=u;
	return true;
}
ll lcm(ll a,ll b) {
	return a/__gcd(a,b)*b;
}
int main() {
    // freopen("../input.inp","r",stdin);
    // freopen("output.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    clock_t start,end;
	start=clock();
	cin >>n;
	vector<int> a={0};
	for (int i=1;i<=n;i++) {
		int x;
		par[i]=i;
		cin >>x;
		m[x]++;
		a.push_back(x);
	}
	sort(a.begin(),a.end());
	a.erase(unique(a.begin(),a.end()),a.end());
	ll res=0;
	vector<nai> e;
	for (int i=1;i<=maxm;i++) {
		if (m[i]) res+=(ll)i*(m[i]-1);
	}
	for (int i=1;i<=n;i++) pos[a[i]]=i;
	for (int i=1;i<=maxm;i++) {
		for (int j=i;j<=maxm;j+=i) {
			if (pos[j]) muls[i].push_back(pos[j]);
		}
		for (int j=1;j<muls[i].size();j++) e.push_back({muls[i][0],muls[i][j],lcm(muls[i][0],muls[i][j])});
	}
	sort(e.begin(),e.end());
	for (auto i:e) {
		if (join(i.u,i.v)) res+=i.w;
	}
	cout <<res;
	end=clock();
	ld dur=(ld)(end-start)/(ld)(CLOCKS_PER_SEC)*(1000.0L);
	cerr<<dur<<'\n';
    return 0;
}
0