P8207 题解
考虑下怎么减少边数。去掉无效边。
首先连两条边
不妨令
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn =4e6+100;
int tot;
struct node{
int u,v,w;
}edge[maxn];
bool cmp(node a,node b){
return a.w<b.w;
}
int fa[maxn];
int found(int u){
if(fa[u]==u){
return u;
}
else{
return fa[u]=found(fa[u]);
}
}
int res=0; hg
signed main(){
int l,r;
cin>>l>>r;
for(int i=l;i<=r;i++) fa[i]=i;
for(int i=1;i<=r;i++){
int v=ceil(l*1.0/i)*i;
for(int j=ceil(l*1.0/i)*i;j<=r;j+=i){
if(v!=j){
edge[++tot].u=v;
edge[tot].v=j;
edge[tot].w=v*j/__gcd(v,j);
}
}
}
sort(edge+1,edge+tot+1,cmp);
for(int i=1;i<=tot;i++){
int u=edge[i].u;
int v=edge[i].v;
int w=edge[i].w;
if(found(u)!=found(v)){
//cout<<u<<' '<<v<<' '<<w<<'\n';
fa[found(u)]=found(v);
res+=w;
}
}
cout<<res;
}