#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define FF first
#define SS second
#define pb push_back
#define sz(x) (int)x.size()
#define oo 1e15
#define eps 1e-9
#define PI acos(-1.0)
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
const int N = 1e5+7, M=1e7+1;
const int MOD = 1e9+7;
int n, m, k, t;
vector<int> a;
vector<ii> e[M];
int dsu[N];
int root(int u) {return (dsu[u]<0?u:dsu[u]=root(dsu[u]));}
bool join(int u, int v) {
if((u=root(u))==(v=root(v))) return 0;
dsu[u]+=dsu[v], dsu[v]=u;return 1;
}
signed main() {
//freopen("test.inp", "r", stdin);
//freopen("test.out", "w", stdout);
//fastIO;
scanf("%d", &n);a.assign(n, 0);int ans=0;
for(int i=0;i<n;i++) scanf("%d", &a[i]);
sort(a.begin(), a.end()), a.resize(unique(a.begin(), a.end())-a.begin());
memset(dsu, -1, sizeof(int)*(sz(a)+1));
for(int i=0;i<sz(a);i++)
for(int j=a[i];j<M;j+=a[i]) {
k=lb(a.begin(), a.end(), j+(j==a[i]))-a.begin();
if(k==sz(a)) break;
e[a[k]%a[i]].pb(ii(i+1, k+1));
}
for(int i=0;i<M;i++)
for(int j=0;j<sz(e[i]);j++)
if(join(e[i][j].FF, e[i][j].SS))
ans+=i;
printf("%d", ans);
return 0;
}
/* stuff you should look for
- int overflow, array bounds
- special cases (n=1?)
- do smth instead of do nothing and stay organized
- WRITE STUFF DOWN
- DONT JUST STICK ON ONE APPROACH
*/
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d", &n);a.assign(n, 0);int ans=0;
| ~~~~~^~~~~~~~~~
sirni.cpp:36:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | for(int i=0;i<n;i++) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
235340 KB |
Output is correct |
2 |
Correct |
264 ms |
264548 KB |
Output is correct |
3 |
Correct |
158 ms |
235548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
235136 KB |
Output is correct |
2 |
Correct |
1896 ms |
629912 KB |
Output is correct |
3 |
Correct |
170 ms |
235844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
235312 KB |
Output is correct |
2 |
Correct |
175 ms |
235144 KB |
Output is correct |
3 |
Correct |
171 ms |
235308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
315 ms |
246000 KB |
Output is correct |
2 |
Correct |
661 ms |
274348 KB |
Output is correct |
3 |
Correct |
390 ms |
257036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
237216 KB |
Output is correct |
2 |
Correct |
353 ms |
258700 KB |
Output is correct |
3 |
Correct |
309 ms |
246128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
471 ms |
258252 KB |
Output is correct |
2 |
Correct |
770 ms |
291780 KB |
Output is correct |
3 |
Correct |
381 ms |
255168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
238888 KB |
Output is correct |
2 |
Correct |
734 ms |
292564 KB |
Output is correct |
3 |
Correct |
366 ms |
257056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
249568 KB |
Output is correct |
2 |
Correct |
3176 ms |
594384 KB |
Output is correct |
3 |
Correct |
370 ms |
252180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
360 ms |
253916 KB |
Output is correct |
2 |
Correct |
4510 ms |
707980 KB |
Output is correct |
3 |
Correct |
667 ms |
309608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
237680 KB |
Output is correct |
2 |
Correct |
3829 ms |
595752 KB |
Output is correct |
3 |
Correct |
386 ms |
258860 KB |
Output is correct |