Submission #799435

# Submission time Handle Problem Language Result Execution time Memory
799435 2023-07-31T14:21:47 Z Baytoro Sirni (COCI17_sirni) C++17
140 / 140
4350 ms 758104 KB
#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define endl "\n"
#define ll long long
//#define int long long
void fopn(string name){
	freopen((name+".in").c_str(),"r",stdin);
	freopen((name+".out").c_str(),"w",stdout);
}
const ll INF=1e18,mod=1e9,N=1e7+5;
int n;
int ans=0,par[N];
int f(int x){
	if(par[x]==x) return x;
	return par[x]=f(par[x]);
}
vector<pair<int,int>> g[N];
void add(int a, int b, int c){
	a=f(a);b=f(b);
	if(a==b) return;
	ans+=c;
	if(rand()%2) swap(a,b);
	par[a]=b;
}
void solve(){
	int n;cin>>n;
	vector<int> p(n);
	for(int i=0;i<n;i++){
		cin>>p[i];
	}
	for(int i=1;i<=1e7;i++)
		par[i]=i;
	sort(all(p));
	for(int i=0;i<n;i++){
		int j=i+1;
		for(int cur=p[i];cur<=1e7;cur+=p[i]){
			while(j<n && p[j]<cur) j++;
			if(j<n) g[p[j]%p[i]].pb({i,j});
			j++;
		}
	}
	for(int i=0;i<=1e7;i++){
		for(auto it: g[i])
			add(it.fr,it.sc,i);
	}
	cout<<ans<<endl;
}
main(){
	ios;
	int T=1;
	//cin>>T;
	for(int i=1;i<=T;i++){
		//cout<<"Case #"<<i<<": ";
		solve();
	}
}

Compilation message

sirni.cpp:54:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   54 | main(){
      | ^~~~
sirni.cpp: In function 'void fopn(std::string)':
sirni.cpp:13:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:14:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 150 ms 274456 KB Output is correct
2 Correct 149 ms 277396 KB Output is correct
3 Correct 147 ms 274604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 274516 KB Output is correct
2 Correct 186 ms 275316 KB Output is correct
3 Correct 141 ms 274476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 143 ms 274332 KB Output is correct
2 Correct 164 ms 274388 KB Output is correct
3 Correct 169 ms 274380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3654 ms 285092 KB Output is correct
2 Correct 3109 ms 324696 KB Output is correct
3 Correct 3333 ms 297016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 219 ms 276108 KB Output is correct
2 Correct 4322 ms 565928 KB Output is correct
3 Correct 3010 ms 287808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3358 ms 299372 KB Output is correct
2 Correct 3674 ms 355768 KB Output is correct
3 Correct 3486 ms 290220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 627 ms 278588 KB Output is correct
2 Correct 3550 ms 345872 KB Output is correct
3 Correct 3471 ms 294312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2206 ms 288376 KB Output is correct
2 Correct 2827 ms 598724 KB Output is correct
3 Correct 2254 ms 291300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2208 ms 287952 KB Output is correct
2 Correct 3990 ms 711520 KB Output is correct
3 Correct 2295 ms 299476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 276720 KB Output is correct
2 Correct 4350 ms 758104 KB Output is correct
3 Correct 3348 ms 299044 KB Output is correct