#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(i=a;i<b;i++)
int nex[10000002];
int sz[10000002],vis[10000002];
int p[10000000];
ll ans=0;
int par(int x)
{
if(p[x]==x)
return x;
p[x]=par(p[x]);
return p[x];
}
void un(ll x,ll y,ll cost)
{
//cout<<x<<' '<<y<<' '<<cost<<endl;
x=par(x);
y=par(y);
if(x==y)
{
return ;
}
ans+=cost;
if(sz[x]<sz[y])
{
swap(x,y);
}
sz[x]+=sz[y];
sz[y]=0;
p[y]=x;
}
int main()
{
ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++)
{
cin>>a[i];
p[a[i]]=a[i];
sz[a[i]]=1;
nex[a[i]]=a[i];
}
for(ll i=10000002;i>=0;i--)
{
if(nex[i]==i)
{
continue;
}
nex[i]=nex[i+1];
}
vector<pair<int,int> >v[10000002];
for(int i=1;i<=1e7;i++)
{
if(nex[i]==i&&vis[i]==0)
{
vis[i]=1;
for(int y=i;y<=1e7;y+=i)
{
vis[y]=1;
// if(nex[y]!=0)
if(nex[y]==y)
{
v[0].push_back({i,y});
}
if(nex[y+1]!=0)
v[nex[y+1]%i].push_back({i,nex[y+1]});
}
}
}
for(int i=0;i<1e7;i++)
{
for(auto y:v[i])
{
un(y.first,y.second,i);
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
298040 KB |
Output is correct |
2 |
Correct |
304 ms |
346840 KB |
Output is correct |
3 |
Correct |
205 ms |
318840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
257 ms |
313552 KB |
Output is correct |
2 |
Correct |
620 ms |
463516 KB |
Output is correct |
3 |
Correct |
206 ms |
321972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
299576 KB |
Output is correct |
2 |
Correct |
200 ms |
286912 KB |
Output is correct |
3 |
Correct |
219 ms |
308676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
354 ms |
330004 KB |
Output is correct |
2 |
Correct |
466 ms |
338332 KB |
Output is correct |
3 |
Correct |
437 ms |
341036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
323260 KB |
Output is correct |
2 |
Correct |
472 ms |
333848 KB |
Output is correct |
3 |
Correct |
433 ms |
325140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
470 ms |
335360 KB |
Output is correct |
2 |
Correct |
415 ms |
343616 KB |
Output is correct |
3 |
Correct |
345 ms |
333940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
341 ms |
317816 KB |
Output is correct |
2 |
Correct |
448 ms |
340516 KB |
Output is correct |
3 |
Correct |
393 ms |
339808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
405648 KB |
Output is correct |
2 |
Correct |
878 ms |
528648 KB |
Output is correct |
3 |
Correct |
364 ms |
409248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
364 ms |
409636 KB |
Output is correct |
2 |
Correct |
919 ms |
559580 KB |
Output is correct |
3 |
Correct |
544 ms |
463280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
246 ms |
384076 KB |
Output is correct |
2 |
Correct |
843 ms |
515808 KB |
Output is correct |
3 |
Correct |
425 ms |
345304 KB |
Output is correct |