#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
//------------------------------DEFINE------------------------------
//******************************************************************
#define IOS ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0)
#define ll long long
#define pb push_back
#define F first
#define S second
#define INF 1e18
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define OK cout<<"Ok"<<endl;
#define MOD (ll)(1e9+7)
#define endl "\n"
//******************************************************************
//----------------------------FUNCTION------------------------------
//******************************************************************
ll gcd(ll a,ll b){
if(a>b) swap(a,b);
if(a==0) return a+b;
return gcd(b%a,a);
}
ll lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
bool is_prime(ll n){
ll k=sqrt(n);
if(n==2) return true;
if(n<2||n%2==0||k*k==n) return false;
for(int i=3;i<=k;i+=2){
if(n%i==0){
return false;
}
}
return true;
}
//*****************************************************************
//--------------------------MAIN-CODE------------------------------
const int mxn=2e5+5,N=1e7;
int t=1,n,arr[mxn],nxt[N+5],idx[N+5],sz[mxn],p[mxn];
vector<pair<int,pii>>vt;
int fnd(int u){
if(p[u]==u){
return u;
}
return p[u]=fnd(p[u]);
}
void Union(int u,int v){
u=fnd(u);
v=fnd(v);
if(u!=v){
if(sz[u]<sz[v]){
swap(u,v);
}
sz[u]+=sz[v];
p[v]=u;
}
}
void solve(){
map<int,int>mp;
cin>>n;
for(int i=1;i<=n;i++){
int k;
cin>>k;
mp[k]++;
}
int n=0;
for(auto [x,y]:mp){
if(y%2){
arr[++n]=x;
nxt[x]=x;
idx[x]=n;
}
}
for(int i=N;i>=1;i--){
if(nxt[i]==0){
nxt[i]=nxt[i+1];
}
}
for(int i=1;i<=n;i++){
int id=0,dis=1e9;
int k=nxt[arr[i]+1];
dis=(k-arr[i])%arr[i];
id=idx[k];
int id2=-1;
if(id!=0){
int x=arr[i],y=arr[id];
if(x<y) swap(x,y);
vt.pb({x%y,{i,id}});
id2=id;
}
for(int j=arr[i]*2;j<=N;j+=arr[i]){
if(!nxt[j]) break;
//cout<<i<<' '<<nxt[j]<<endl;
int k=nxt[j];
//if((k-j)%arr[i]<dis){
id=idx[k];
//dis=(k-j)%arr[i];
if(id!=id2){
id2=id;
int x=arr[i],y=arr[id];
if(x<y) swap(x,y);
vt.pb({x%y,{i,id}});
}
//}
}
}
for(int i=1;i<=n;i++){
sz[i]=1;
p[i]=i;
}
sort(all(vt));
ll ans=0;
for(int i=0;i<vt.size();i++){
int u=vt[i].S.F,v=vt[i].S.S,k=vt[i].F;
if(fnd(u)!=fnd(v)){
ans+=k;
Union(u,v);
}
}
cout<<ans<<endl;
}
int main(){
//IOS;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
sirni.cpp: In function 'void solve()':
sirni.cpp:86:18: warning: variable 'dis' set but not used [-Wunused-but-set-variable]
86 | int id=0,dis=1e9;
| ^~~
sirni.cpp:119:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | for(int i=0;i<vt.size();i++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
43280 KB |
Output is correct |
2 |
Incorrect |
98 ms |
44624 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
39636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
43576 KB |
Output is correct |
2 |
Correct |
45 ms |
41888 KB |
Output is correct |
3 |
Correct |
47 ms |
43572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
268 ms |
61228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
73 ms |
47384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
442 ms |
73544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
109 ms |
48444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
326 ms |
109144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
333 ms |
109076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
77928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |