답안 #412779

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412779 2021-05-27T13:34:02 Z kshitij_sodani Sirni (COCI17_sirni) C++14
112 / 140
2441 ms 786436 KB
//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
#define endl '\n'

int n;
bool vis[10000001];
int ind[10000001];
int re[10000001+1];
int par[100001];
int find(int no){
	if(par[no]==no){
		return no;
	}
	par[no]=find(par[no]);
	return par[no];
}
vector<pair<int,int>> pre[10000001];
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	int ind2=0;
	vector<int> it;
	for(int i=0;i<n;i++){
		int x;
		cin>>x;
		if(vis[x]==0){
			vis[x]=1;
			ind[x]=ind2;
			it.pb(x);
			//cout<<x<<":"<<ind2<<endl;
			ind2++;
		}
	}	
	for(int i=0;i<ind2;i++){
		par[i]=i;
	}
	/*for(int i=0;i<ind2;i++){
		for(int j=0;j<ind2;j++){
			if(it[i]>it[j]){
				pre[it[i]%it[j]].pb({i,j});
			}
		}
	}*/
	re[10000001]=-1;
	for(int i=10000000;i>=1;i--){
		re[i]=re[i+1];
		if(vis[i]==1){
			re[i]=i;
		}
	}
	for(int i=1;i<=10000000-1;i++){
		if(vis[i]==1){
			for(int j=i;j<=10000000;j+=i){
				if(re[j]>=0){
					if(re[j]==i){
						if(re[j+1]>=0){
							//cout<<re[j+1]-j<<":"<<re[j+1]<<":"<<i<<endl;
							pre[re[j+1]-j].pb({ind[re[j+1]],ind[i]});
							continue;
						}

					}
					pre[re[j]-j].pb({ind[re[j]],ind[i]});
				}
			}
		}
	}
	llo ans=0;
	for(int i=0;i<=10000000;i++){
		for(auto j:pre[i]){
			int x=find(j.a);
			int y=find(j.b);
			/*if(i==0){
				cout<<it[j.a]<<","<<it[j.b]<<endl;
			}*/
			//cout<<j.a<<","<<j.b<<":"<<i<<endl;
			if(x==y){

			}
			else{
				ans+=i;
				par[x]=y;
			}
		}
	}
	cout<<ans<<endl;






	return 0;
}
 
# 결과 실행 시간 메모리 Grader output
1 Correct 199 ms 281156 KB Output is correct
2 Correct 455 ms 310652 KB Output is correct
3 Correct 200 ms 281924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 274428 KB Output is correct
2 Runtime error 2441 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 197 ms 281632 KB Output is correct
2 Correct 215 ms 277948 KB Output is correct
3 Correct 197 ms 281680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 350 ms 289924 KB Output is correct
2 Correct 699 ms 319408 KB Output is correct
3 Correct 507 ms 301336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 229 ms 281320 KB Output is correct
2 Correct 525 ms 302084 KB Output is correct
3 Correct 423 ms 287484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 549 ms 302360 KB Output is correct
2 Correct 828 ms 337828 KB Output is correct
3 Correct 393 ms 298396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 370 ms 278884 KB Output is correct
2 Correct 889 ms 340660 KB Output is correct
3 Correct 385 ms 301208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 342 ms 337448 KB Output is correct
2 Correct 1805 ms 683800 KB Output is correct
3 Correct 350 ms 341224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 345 ms 342320 KB Output is correct
2 Runtime error 1851 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 242 ms 320588 KB Output is correct
2 Correct 2099 ms 698672 KB Output is correct
3 Correct 411 ms 300796 KB Output is correct