#include<bits/stdc++.h>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define task "test"
#define pii pair<pll, pll>
using namespace std;
using ll = int;
const int N = 1e5+5;
const ll mod = 1e8+7;
const ll base = 113;
const ll inf = 1e9;
ll m, n, t, k, a[N], tong, d[N], b[N], h[N], ans, near[N*100];
vector<pll> adj[N*100];
vector<ll> kq, ask[N];
vector<pll> val;
ll pw(ll k, ll n, ll md)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n&1)total = total * k % md;
k = k * k % md;
}
return total;
}
struct DSU
{
ll n;
vector<ll> lab;
DSU(ll _n)
{
n = _n;
lab.resize(n+1, -1);
}
ll findp(ll u)
{
return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
bool hop(ll u, ll v)
{
u = findp(u);
v = findp(v);
if(u == v)return false;
if(lab[u] > lab[v])swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
};
struct BIT
{
ll n;
vector<ll> fe;
BIT(ll _n)
{
n = _n;
fe.resize(n+1, 0);
}
void reset()
{
fill(fe.begin(), fe.end(), 0);
}
void add(ll id, ll x)
{
for(; id <= n; id += id & -id)fe[id] += x;
}
ll get(ll id)
{
ll total = 0;
for(; id ; id -= id & -id)total += fe[id];
return total;
}
};
string s;
void sol()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
kq.pb(a[i]);
}
sort(kq.begin(), kq.end());
kq.erase(unique(kq.begin(), kq.end()), kq.end());
m = kq.back();
for(int i = 0; i < kq.size(); i ++)
{
for(int j = t+1; j <= kq[i]; j ++)near[j] = i;
t = kq[i];
}
for(int i = 0; i < kq.size()-1; i ++)
{
for(int j = kq[i]; j <= m; j += kq[i])
{
if(near[j] != i)
{
//cout << kq[i] <<" "<< kq[near[j]] << '\n';
adj[kq[near[j]] % kq[i]].pb({near[j], i});
}
}
}
DSU dsu(kq.size());
long long ans = 0;
for(int i = 0; i <= m; i ++)
{
for(pll x: adj[i])
{
if(dsu.hop(x.fi, x.se))ans += i;
}
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
Compilation message
sirni.cpp: In function 'void sol()':
sirni.cpp:88:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int i = 0; i < kq.size(); i ++)
| ~~^~~~~~~~~~~
sirni.cpp:93:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int i = 0; i < kq.size()-1; i ++)
| ~~^~~~~~~~~~~~~
sirni.cpp: In function 'int main()':
sirni.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
151 ms |
276644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
112 ms |
237640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
139 ms |
276784 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
171 ms |
251404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
162 ms |
243200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
222 ms |
262500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
126 ms |
241884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
288 ms |
289864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
294 ms |
293820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
167 ms |
278908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |