题解:P13016 [GESP202506 六级] 最大因数
XiaoHongChong · · 题解
首先,一个节点(下文用
要这道它们的距离,就是
怎么求它们的
那怎么求出快速一个节点的父节点呢?如果暴力枚举,时间是
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;
int n, x, y, len, p[N];
bool flag[N];
void init(int n)
{
for (int i = 2; i <= n; i++)
{
if (flag[i] == false)
p[++len] = i;
for (int j = 1; i * p[j] <= n&& j <= len; j++)
{
flag[i * p[j]] = true;
if (i % p[j] == 0)
break;
}
}
}
int main()
{
init(1e5);
cin >> n;
while (n--)
{
cin >> x >> y;
int pos1 = 1, pos2 = 1, ans = 0;
while (x != y)
{
if (x < y)
{
swap(x, y);
swap(pos1, pos2);
}
while (pos1 <= len && x % p[pos1] != 0)
pos1++;
ans++;
if (pos1 == len + 1)
x = 1;
else
x = x / p[pos1];
}
cout << ans << endl;
}
return 0;
}