結果

問題 No.1917 LCMST
ユーザー Trí Tâm NguyễnTrí Tâm Nguyễn
提出日時 2024-03-13 00:27:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,820 bytes
コンパイル時間 2,068 ms
コンパイル使用メモリ 180,192 KB
実行使用メモリ 51,488 KB
最終ジャッジ日時 2024-03-13 00:27:55
合計ジャッジ時間 13,894 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,644 KB
testcase_01 WA -
testcase_02 AC 5 ms
7,644 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 4 ms
7,644 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=1e6+5;
const ll offset=2e5;
const ll blk=317;
const ll inf=1e9;
const int base=350;
const ll mod=998244353;
int n;
int a[maxn],b[maxn];
int par[maxn];
int cnt[maxn];
int Find(int u)
{
    return par[u]<0?u:par[u]=Find(par[u]);
}
bool Union(int u,int v)
{
    if ((u=Find(u))==(v=Find(v))) return false;
    if (par[u]> par[v]) swap(u,v);
    par[u]+=par[v];
    par[v]=u;
    return true;
}
vector<int> L;
vector<pair<int,pii>> Edge;
int lcm(int a,int b)
{
    return a * b / __gcd(a,b);
}
void sol() {
    cin >> n;
    for1(i,1,n) par[i]=-1;
    for1(i,1,n)
    {
        cin >> a[i];
        cnt[a[i]]++;
    }
    sort(a+1,a+1+n);
    for1(i,1,n)
    {
        if (b[a[i]]) Union(b[a[i]],i);
        else b[a[i]]=i;
    }
    for1(i,1,1e5)
    {
        if(cnt[i] > 1) continue;
        L.clear();
        for3(j,i,1e5,i)
        {
            if (b[j]) L.pb(j);
        }
        if (L.size()<2) continue;
        for1(j,1,L.size()-1) Edge.pb({lcm(L[0],L[j]),{L[0],L[j]}});
    }
    sort(all(Edge));
    int res=0;
    for(auto x:Edge)
    {
        if (Union(b[x.se.fi],b[x.se.se])) res+=x.fi;
    }
    cout << res<<'\n';

}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("paleta.inp","r",stdin);
//    freopen("paleta.out","w",stdout);

    int t=1;//cin >> t;
    while (t--) {
        sol();
    }
}

/*
1
5 3
4 1 2 3 1
*/
0