// Calm down.
// Think three times, code twice.
#include "bits/stdc++.h"
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <int,int>
#define pll pair <ll,ll>
#define piii pair <int,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7; // 998244353
const ll oo = 1e18;
int n, par[N], sz[N];
int find(int v) {
if(v == par[v]) return v;
return par[v] = find(par[v]);
}
bool join(int x, int y) {
x = find(x); y = find(y);
if(x != y) {
if(sz[x] < sz[y]) swap(x, y);
par[y] = x;
sz[x] += sz[y];
return true;
}
return false;
}
void to_nho_cau() {
cin >> n;
vector<int> a;
forr(i, 1, n) {
int x; cin >> x;
a.pb(x);
}
if(a.size()) {
sort(all(a)); a.resize(unique(all(a)) - a.begin());
}
int mx = a.back();
vector<int> nxt(mx + 500, -1);
forf(i, 0, a.size()) {
nxt[a[i]] = i;
}
ford(i, mx-1, 0) {
if(nxt[i] == -1) nxt[i] = nxt[i+1];
}
vector<vector<pair<int, int>>> f(mx + 1);
for (int i = 0; i < a.size() - 1; i++) {
f[a[i + 1] % a[i]].push_back({i, i + 1});
for (int at = 2 * a[i]; at <= mx; at += a[i]) {
int good_mod = nxt[at];
f[a[good_mod] % a[i]].push_back({i, good_mod});
}
}
forf(i, 0, a.size()) {
par[i] = i;
sz[i] = 1;
}
ll ans = 0;
forr(i, 0, mx) {
for(pii e: f[i]) {
int u = e.st, v = e.nd;
if(join(u, v)) {
ans = ans + i;
}
}
}
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
freopen(file".inp","r",stdin);
freopen(file".out","w",stdout);
#endif
int t = 1;
//cin >> t;
while(t--) to_nho_cau();
}
/*
1.self check:
2.long long
3.size of array
4.code for testing
5.initializing
6.modulo number
*/
/**
∧__∧
(`•ω• )づ__∧
(つ /( •ω•。)
しーJ (nnノ) pat pat
**/
/** /\_/\
* (= ._.)
* / >☕ \>💻
**/
Compilation message
sirni.cpp:112:9: warning: "/*" within comment [-Wcomment]
112 | /** /\_/\
|
sirni.cpp: In function 'void to_nho_cau()':
sirni.cpp:6:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | #define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
| ^
sirni.cpp:58:5: note: in expansion of macro 'forf'
58 | forf(i, 0, a.size()) {
| ^~~~
sirni.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = 0; i < a.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~
sirni.cpp:6:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | #define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
| ^
sirni.cpp:72:5: note: in expansion of macro 'forf'
72 | forf(i, 0, a.size()) {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
274248 KB |
Output is correct |
2 |
Correct |
187 ms |
302604 KB |
Output is correct |
3 |
Correct |
125 ms |
273492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
848 KB |
Output is correct |
2 |
Correct |
1024 ms |
668236 KB |
Output is correct |
3 |
Correct |
127 ms |
275064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
274248 KB |
Output is correct |
2 |
Correct |
116 ms |
273736 KB |
Output is correct |
3 |
Correct |
119 ms |
274440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
38584 KB |
Output is correct |
2 |
Correct |
89 ms |
74056 KB |
Output is correct |
3 |
Correct |
57 ms |
51392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
30032 KB |
Output is correct |
2 |
Correct |
63 ms |
50888 KB |
Output is correct |
3 |
Correct |
38 ms |
25520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
54168 KB |
Output is correct |
2 |
Correct |
112 ms |
88828 KB |
Output is correct |
3 |
Correct |
52 ms |
51644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
9700 KB |
Output is correct |
2 |
Correct |
127 ms |
88732 KB |
Output is correct |
3 |
Correct |
59 ms |
52084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
288200 KB |
Output is correct |
2 |
Correct |
914 ms |
636172 KB |
Output is correct |
3 |
Correct |
207 ms |
292040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
292800 KB |
Output is correct |
2 |
Correct |
1326 ms |
749488 KB |
Output is correct |
3 |
Correct |
330 ms |
348420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
276808 KB |
Output is correct |
2 |
Correct |
1102 ms |
635012 KB |
Output is correct |
3 |
Correct |
65 ms |
54712 KB |
Output is correct |