#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
typedef pair<int,int> pii;
int n,m,q;
vector <int> comps[MAXN];
int par[MAXN];
vector <pii> events[MAXN];
void init()
{
for(int i = 1; i <= n; i++)comps[i].push_back(i),par[i]=i,events[i].push_back({0,i});
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> n >> m >> q;
init();
for(int i = m; i >= 1; i--)
{
int mx = par[i];
for(int j = i; j <= n; j+=i)
{
if(comps[mx].size()<comps[par[j]].size())mx=par[j];
}
for(int j = i; j <= n; j+=i)
{
if(par[j]==mx)continue;
int compid = par[j];
for(auto x:comps[compid])
{
comps[mx].push_back(x);
par[x]=mx;
events[x].push_back({m-i+1,mx});
}
//comps[compid].clear();
}
}
while(q--)
{
int a,b;
cin >> a >> b;
int ans = m;
for(auto x:events[a])
{
for(auto y:events[b])
{
if(x.second==y.second)ans=min(ans,max(x.first,y.first));
}
}
cout << ans << " ";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
5436 KB |
Output is correct |
2 |
Correct |
130 ms |
5340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
5556 KB |
Output is correct |
2 |
Correct |
151 ms |
5656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
7852 KB |
Output is correct |
2 |
Correct |
99 ms |
7800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
8796 KB |
Output is correct |
2 |
Correct |
163 ms |
8692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
10612 KB |
Output is correct |
2 |
Correct |
155 ms |
10868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
9976 KB |
Output is correct |
2 |
Correct |
240 ms |
13404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
286 ms |
14596 KB |
Output is correct |
2 |
Correct |
265 ms |
15908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
17108 KB |
Output is correct |
2 |
Correct |
307 ms |
17156 KB |
Output is correct |