CF2114F Small Operations 题解
lucasincyber · · 题解
题目传送门。
思路
令
直接用 DP 解决。设
所以答案就为
令
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, inf = 1e9;
int t, x, y, k;
vector<int> fact[N];
int dp[N];
void init()
{
for (int i = 1; i < N; i++)
for (int j = i; j < N; j += i)
fact[j].push_back(i);
for (int i = 1; i < N; i++) dp[i] = inf;
}
int calc(int n)
{
dp[1] = 0;
for (int i : fact[n])
for (int j : fact[i])
if (j <= k) dp[i] = min(dp[i], dp[i / j] + 1);
int now = dp[n];
for (int i : fact[n]) dp[i] = inf;
return now;
}
void solve()
{
scanf("%d%d%d", &x, &y, &k);
int d = __gcd(x, y);
int a = x / d, b = y / d;
int res = calc(a) + calc(b);
if (res >= inf) printf("-1\n");
else printf("%d\n", res);
}
int main()
{
init();
scanf("%d", &t);
while (t--) solve();
return 0;
}