//Copyright © 2022 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define REP(a) F0R(_, a)
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7; //998244353;
const ld PI = acos((ld)-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
os << '{';
string sep;
for (const T &x : v)
os << sep << x, sep = ", ";
return os << '}';
}
void dbg_out()
{
cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif
inline namespace RecursiveLambda{
template <typename Fun>
struct y_combinator_result{
Fun fun_;
template <typename T>
explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)){}
template <typename...Args>
decltype(auto) operator()(Args &&...args){
return fun_(ref(*this), forward<Args>(args)...);
}
};
template <typename Fun>
decltype(auto) y_combinator(Fun &&fun){
return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
}
};
void setIO(string s) // USACO
{
#ifndef LOCAL
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
#endif
}
const int N = 1e7 + 10, MN = 1e5;
int nums[N + 1];
int s[MN], par[MN];
int co{};
int idx[N];
vpi edge[N];
int get(int u) {
return par[u] == u ? u : par[u] = get(par[u]);
}
void unite(int u, int v) {
u = get(u), v = get(v);
if (u == v) return;
if (s[u] > s[v]) swap(u, v);
s[v] += s[u];
par[u] = v;
}
void solve()
{
int n;
cin >> n;
F0R(i, n) s[i] = 1, par[i] = i;
set<int> st;
F0R(i, n) {
int x;
cin >> x;
st.insert(x);
}
for (auto &num : st) {
idx[num] = co++;
}
auto it = st.begin();
for (int i = 1; i <= 1e7; i++) {
while (it != st.end() && *it < i) it++;
if (it == st.end()) nums[i] = -1;
else nums[i] = *it;
}
for (auto &num : st) {
int pr = -1;
for (int j = num; j <= 1e7; j += num) {
int it = (j == num ? nums[j + 1] : nums[j]);
if (it == -1) break;
if (pr == it) continue;
pr = it;
edge[it % num].pb({idx[it], idx[num]});
dbg(idx[it], idx[num], it, num);
}
}
ll ans{};
FOR(c, 0, 1e7 + 1) {
for (auto &[u, v] : edge[c]) {
if (get(u) != get(v)) ans += c, unite(u, v);
}
}
cout << ans;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int testcase=1;
// cin >> testcase;
while (testcase--)
{
solve();
}
}
Compilation message
sirni.cpp: In function 'void solve()':
sirni.cpp:71:18: warning: statement has no effect [-Wunused-value]
71 | #define dbg(...) 42
| ^~
sirni.cpp:146:4: note: in expansion of macro 'dbg'
146 | dbg(idx[it], idx[num], it, num);
| ^~~
sirni.cpp: In function 'void setIO(std::string)':
sirni.cpp:94:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:95:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
278084 KB |
Output is correct |
2 |
Correct |
196 ms |
278724 KB |
Output is correct |
3 |
Correct |
144 ms |
278516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
274432 KB |
Output is correct |
2 |
Correct |
392 ms |
275552 KB |
Output is correct |
3 |
Correct |
145 ms |
278468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
278348 KB |
Output is correct |
2 |
Correct |
143 ms |
276812 KB |
Output is correct |
3 |
Correct |
165 ms |
278364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
293244 KB |
Output is correct |
2 |
Correct |
348 ms |
320416 KB |
Output is correct |
3 |
Correct |
251 ms |
297780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
280692 KB |
Output is correct |
2 |
Correct |
311 ms |
300836 KB |
Output is correct |
3 |
Correct |
218 ms |
291024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
326 ms |
305184 KB |
Output is correct |
2 |
Correct |
425 ms |
335240 KB |
Output is correct |
3 |
Correct |
257 ms |
296920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
280428 KB |
Output is correct |
2 |
Correct |
408 ms |
329128 KB |
Output is correct |
3 |
Correct |
268 ms |
296176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
331088 KB |
Output is correct |
2 |
Correct |
1577 ms |
527744 KB |
Output is correct |
3 |
Correct |
341 ms |
334008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
331188 KB |
Output is correct |
2 |
Correct |
3216 ms |
615944 KB |
Output is correct |
3 |
Correct |
498 ms |
337096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
311756 KB |
Output is correct |
2 |
Correct |
3534 ms |
604344 KB |
Output is correct |
3 |
Correct |
261 ms |
298116 KB |
Output is correct |