This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |