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>
using namespace std;
const int maxn = 1e3+10;
int pai[maxn], sz[maxn];
int ans[maxn][maxn];
int Find(int x)
{
if (pai[x] == x) return x;
return pai[x] = Find(pai[x]);
}
void Join(int x, int y)
{
x = Find(x), y = Find(y);
if (x == y) return;
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
pai[y] = x;
}
void init(int n)
{
for (int i = 1; i <= n; i++)
pai[i] = i, sz[i] = 1;
}
int main(void)
{
memset(ans, -1, sizeof ans);
int n, m, q;
cin >> n >> m >> q;
init(n);
vector< pair<int, int> > Q;
for (int i = 1; i <= q; i++)
{
int a, b;
cin >> a >> b;
Q.push_back({a, b});
}
for (int i = m; i >= 1; i--)
{
for (int j = i; j <= n; j += i)
Join(j, i);
for (auto P: Q)
{
int a = P.first, b = P.second;
if (Find(a) == Find(b) && ans[a][b] == -1)
ans[a][b] = m-i+1;
}
}
for (auto P: Q)
cout << ans[P.first][P.second] << "\n";
}
# | 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... |