Submission #357876

# Submission time Handle Problem Language Result Execution time Memory
357876 2021-01-24T21:21:29 Z MetB Sirni (COCI17_sirni) C++14
84 / 140
5000 ms 786436 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace __gnu_pbds;
 
using namespace std;
 
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
 
const ll N = 2000001;
const ll INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
const int maxa = (int)1e7 + 1;
int a[N], n;

int p[N], sz[N], e[maxa + 1];

vector<pair<int, int>> v[maxa + 1];

int f(int i, int j) {
	return a[j] % a[i];
}

int find(int x) {
	if (p[x] == x) return x;
	return p[x] = find(p[x]);
}

bool unite(int a, int b) {
	a = find(a), b = find(b);
	if (a == b) return false;

	if (sz[a] < sz[b]) swap(a, b);

	sz[a] += sz[b];
	p[b] = a;

	return true;
}

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;

	fill(e, e + maxa + 1, -1);

	for (int i = 0; i < n; i++) {
		sz[i] = 1, p[i] = i;
	}

	for (int i = 0; i < n; i++) {
		cin >> a[i];
		e[a[i]] = i;
	}

	for (int i = maxa - 1; i >= 0; i--) {
		if (e[i] == -1) e[i] = e[i+1];
	}

	for (int i = 0; i < n; i++) {
		if (e[a[i]] != i) v[0].push_back({i, e[a[i]]});
		if (e[a[i] + 1] != -1) {
			v[f(i, e[a[i] + 1])].push_back({i, e[a[i] + 1]});
		}

		for (int k = 2 * a[i]; k < maxa; k += a[i]) {
			if (e[k] != -1) {
				v[f(i, e[k])].push_back({i, e[k]});
			}
		}
	}

	ll ans = 0;

	for (int i = 0; i <= maxa; i++) {
		for (auto& x : v[i]) {
			if (unite(x.first, x.second)) {
				ans += f(x.first, x.second);
				//cout << a[x.first] << ' ' << a[x.second] << ' ' << f(x.first, x.second) << endl;
			}
		}	
	}

	cout << ans;

}
# Verdict Execution time Memory Grader output
1 Correct 232 ms 274540 KB Output is correct
2 Correct 401 ms 314860 KB Output is correct
3 Correct 246 ms 274668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 274540 KB Output is correct
2 Runtime error 1823 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 230 ms 274540 KB Output is correct
2 Correct 227 ms 274720 KB Output is correct
3 Correct 234 ms 274668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 492 ms 285608 KB Output is correct
2 Correct 1239 ms 329788 KB Output is correct
3 Correct 687 ms 302512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 269 ms 276460 KB Output is correct
2 Execution timed out 5132 ms 469680 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 849 ms 300048 KB Output is correct
2 Correct 1836 ms 369356 KB Output is correct
3 Correct 586 ms 294148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 501 ms 278736 KB Output is correct
2 Correct 1960 ms 367864 KB Output is correct
3 Correct 630 ms 300000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 406 ms 289132 KB Output is correct
2 Correct 3289 ms 758828 KB Output is correct
3 Correct 442 ms 292824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 417 ms 292784 KB Output is correct
2 Runtime error 4399 ms 786436 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 259 ms 276844 KB Output is correct
2 Execution timed out 5134 ms 709220 KB Time limit exceeded
3 Halted 0 ms 0 KB -