結果
| 問題 |
No.1917 LCMST
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-05-13 10:41:31 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 402 ms / 4,000 ms |
| コード長 | 1,549 bytes |
| コンパイル時間 | 2,097 ms |
| コンパイル使用メモリ | 209,484 KB |
| 実行使用メモリ | 85,824 KB |
| 最終ジャッジ日時 | 2025-05-13 10:41:48 |
| 合計ジャッジ時間 | 15,074 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
#include <bits/stdc++.h>
#define ll int64_t
#define int long long
#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;
}
signed 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(a[muls[i][0]],a[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;
}