結果
問題 |
No.1917 LCMST
|
ユーザー |
|
提出日時 | 2025-09-21 04:05:25 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 454 ms / 4,000 ms |
コード長 | 4,427 bytes |
コンパイル時間 | 3,990 ms |
コンパイル使用メモリ | 311,120 KB |
実行使用メモリ | 80,888 KB |
最終ジャッジ日時 | 2025-09-21 04:05:48 |
合計ジャッジ時間 | 22,094 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
#include<bits/stdc++.h> #if __has_include(<atcoder/all>) #endif using namespace std; #define eb emplace_back #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define RDVL(T,n,...) vec<T>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n);lin(__VA_ARGS__) #define VL(n,...) RDVL(ll,n,__VA_ARGS__) #define fo(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(0,__VA_ARGS__);i<i##stop;i+=i##step) #define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a) #define defpp template<ostream&o=cout>void pp(const auto&...a){[[maybe_unused]]const char*c="";((o<<c<<a,c=" "),...);o<<'\n';}void epp(const auto&...a){pp<cerr>(a...);} #define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{ namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);} using ll=long long; constexpr auto refs(auto&...a){return array{ref(a)...};} template<class T>constexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};} void lin(auto&...a){(cin>>...>>a);} template<class A,class B=A>struct pair{ A a;B b; pair()=default; pair(A aa,B bb):a(aa),b(bb){} auto operator<=>(const pair&)const=default; }; template<class F=less<>>auto&sort(auto&a,F f={}){ranges::sort(a,f);return a;} auto&unique(auto&a){sort(a).erase(ranges::unique(a).begin(),a.end());return a;} template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>; } namespace my{ template<class V>concept vectorial=is_base_of_v<vector<typename remove_cvref_t<V>::value_type>,remove_cvref_t<V>>; template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;} template<class V>constexpr int depth=0; template<class T>struct core_t_helper{using type=T;}; template<class T>using core_t=core_t_helper<T>::type; template<class V>struct vec; template<int D,class T>struct hvec_helper{using type=vec<typename hvec_helper<D-1,T>::type>;}; template<class T>struct hvec_helper<0,T>{using type=T;}; template<int D,class T>using hvec=hvec_helper<D,T>::type; template<class V>struct vec:vector<V>{ static constexpr int D=depth<V>+1; using C=core_t<V>; using vector<V>::vector; void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else{ }} static auto make(ll n,const auto&...a){ if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]); else { } } ll size()const{return vector<V>::size();} auto&emplace_back(auto&&...a){vector<V>::emplace_back(std::forward<decltype(a)>(a)...);return*this;} auto fold(const auto&f)const{ pair<C,bool>r{}; fe(*this,e){ if constexpr(!vectorial<V>){ if(r.b)f(r.a,e); else r={e,1}; }else { } } return r; } auto max()const{return fold([](auto&a,auto b){if(a<b)a=b;}).a;} template<class F=less<>>auto sort(F f={})const{vec v=*this;ranges::sort(v,f);return v;} auto rle()const{vec<pair<V,ll>>r;fe(*this,e)if(r.size()&&e==r.back().a)++r.back().b;else r.eb(e,1);return r;} auto rce()const{return sort().rle();} }; template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>; } namespace my{ struct dsu_plain{ ll n_; vec<int>par_or_size_; dsu_plain(ll n):n_(n),par_or_size_(n,-1){} auto merge(ll a,ll b){ ll x=leader(a),y=leader(b); if(x==y)return x; if(-par_or_size_[x]<-par_or_size_[y])swap(x,y),swap(a,b); par_or_size_[x]+=par_or_size_[y]; par_or_size_[y]=x; return x; } bool same(ll a,ll b){return leader(a)==leader(b);} ll leader(ll a){ if(par_or_size_[a]<0)return a; return par_or_size_[a]=leader(par_or_size_[a]); } }; } namespace my{ template<class WT>auto kruskal_weight_sum(ll n,vec<tuple<ll,ll,WT>>edges){ sort(edges,[&](auto&p,auto&q){return get<2>(p)<get<2>(q);}); dsu_plain uf(n); WT res=0; fe(edges,a,b,c){ if(uf.same(a,b))continue; uf.merge(a,b); res+=c; } return res; } } namespace my{entry void main(){ LL(N); VL(N,a); ll ans=0; fe(a.rce(),e,c)ans+=e*(c-1); // 値が同じもの同士は1個だけ残せば良い. unique(a); ll M=a.max()+1; vec<ll>exist(M); fe(a,e)exist[e]=1; vec<vec<ll>>g(M); // g[i]:iの倍数 vec<tuple<ll,ll,ll>>edges; fo(i,1,M){ fo(j,i,M,i)if(exist[j])g[i].eb(j); sort(g[i]); fo(j,1,g[i].size()){ ll a=g[i][0],b=g[i][j]; //pp(i,a,b); edges.eb(a,b,a/i*b); } } ans+=kruskal_weight_sum(M,edges); pp(ans); }}