Submission #547284

# Submission time Handle Problem Language Result Execution time Memory
547284 2022-04-10T08:58:42 Z AA_Surely Sirni (COCI17_sirni) C++14
0 / 140
387 ms 67452 KB
#include <bits/stdc++.h>

#define FOR(i,x,n) 	for(int i=x; i<n; i++)
#define F0R(i,n) 	FOR(i,0,n)
#define ROF(i,x,n) 	for(int i=n-1; i>=x; i--)
#define R0F(i,n) 	ROF(i,0,n)

#define WTF 		cout << "WTF" << endl

#define IOS 		ios::sync_with_stdio(false); cin.tie(0)
#define F 			first
#define S	 		second
#define pb 			push_back

#define ALL(x) 		x.begin(), x.end()
#define RALL(x) 	x.rbegin(), x.rend()

using namespace std;
typedef long long 		LL;

typedef pair<int, int> 	PII;
typedef pair<LL, LL> 	PLL;

typedef vector<int> 	VI;
typedef vector<LL> 		VLL;
typedef vector<PII> 	VPII;
typedef vector<PLL> 	VPLL;

const int N = 1e5 + 7;
const int A = 1e7 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;

struct Edge {
    int u, v, w;

    inline bool operator < (const Edge other) {
        return w < other.w;
    }
} eset[A];

int n, era[A];
int par[N];

int grand(int x) {
    if (par[x] < 0) return x;
    return par[x] = grand(par[x]);
}

inline bool merge(int a, int b) {
    a = grand(a); b = grand(b);
    if (a == b) return 0;

    if (par[a] > par[b]) swap(a, b);
    par[a] += par[b];
    par[b] = a;

    return 1;
}

int main() {
    IOS;

    fill(era, era + A, -1);
    
    cin >> n;
    VI ns(n);
    F0R(i, n) cin >> ns[i];

    sort(ALL(ns));
    ns.resize(unique(ALL(ns)) - ns.begin());
    n = ns.size();

    F0R(i, n) era[ ns[i] ] = i;
    R0F(i, A - 1) if (era[i] == -1)
        era[i] = era[i + 1];
    
    int fre = 0;
    F0R(i, n) for(int j = ns[i]; j < A; j += ns[i]) if (era[j] != -1) {
        eset[fre].u = i; eset[fre].v = era[j];
        eset[fre].w = ns[ era[j] ] % ns[i];
        fre++;
    }

    sort(eset, eset + fre);
    fill(par, par + n, -1);
    
    LL ans = 0;
    F0R(i, fre) if (merge(eset[i].u, eset[i].v)) ans += eset[i].w;
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 39500 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 96 ms 39456 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 41 ms 39476 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 188 ms 52528 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 80 ms 41636 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 387 ms 67452 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 195 ms 43992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 142 ms 55948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 168 ms 61864 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 64 ms 41796 KB Output isn't correct
2 Halted 0 ms 0 KB -