#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 |
18 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
5624 KB |
Output is correct |
2 |
Correct |
160 ms |
5652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
281 ms |
6136 KB |
Output is correct |
2 |
Correct |
227 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1569 ms |
64140 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
351 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
337 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1155 ms |
51600 KB |
Output is correct |
2 |
Runtime error |
424 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
393 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
418 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |