이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ll long long
#define fi first
#define se second
#define ii pair<int,int>
#define fo(i,a,b) for (int i = (a); i <= (b); ++i)
#define fd(i,a,b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(), x.end()
template<typename T> void _do(T x) { cerr << x << '\n'; }
template<typename T, typename ...U> void _do(T x, U ...y) { cerr << x << ", "; _do(y...); }
#define db(...) cerr << #__VA_ARGS__ << " = "; _do(__VA_ARGS__);
bool ST;
const int N = 1e5+5, M = 1e7+5, inf = 1e9, LOG = 19;
struct EDGE {
int u, v, c;
bool operator < (const EDGE &o) const {
return c < o.c;
}
};
vector<EDGE> e;
int n, a[N], nxt[M], p[M];
bitset<M> b;
int find(int u) {
return (u == p[u] ? u : p[u] = find(p[u]));
}
bool merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return 0;
return p[u] = v, 1;
}
void solve() {
cin >> n;
fo(i,1,n) cin >> a[i], b[a[i]] = 1;
fo(i,1,M-1) p[i] = i;
int lst = -1;
fd(i,M-1,1) {
if (b[i]) lst = i;
nxt[i] = lst;
}
fo(i,1,M-1) if (b[i]) for (int j = i; j < M; j += i) {
if (nxt[j] == -1) break;
if (nxt[j] <= j + i) e.pb({i, nxt[j], nxt[j]%i});
}
sort(all(e));
ll ans = 0;
for (EDGE it : e) if (merge(it.u, it.v)) ans += it.c;
cout << ans;
}
bool EN;
int main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("A.inp", "r")) {
freopen("A.inp", "r", stdin);
// freopen("A.out", "w", stdout);
}
int testcase = 1;
// cin >> testcase;
while (testcase--) solve();
}
컴파일 시 표준 에러 (stderr) 메시지
sirni.cpp: In function 'int main()':
sirni.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen("A.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |