pb_jiang

CF ROUND 1027 F TLE

May 27th, 2025 (edited)
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. bool check_div(ll x, ll k)
  6. {
  7.     for (ll i = 2; i * i <= x; ++i)
  8.         while (x % i == 0)
  9.             x /= i;
  10.     return x <= k;
  11. }
  12.  
  13. ll calc_step(ll x, ll k)
  14. {
  15.     vl dist(x + 1, LLONG_MAX / 2);
  16.     dist[1] = 0;
  17.     queue<ll> q;
  18.     q.push(1);
  19.     while (!q.empty()) {
  20.         ll u = q.front(), step = dist[u];
  21.         q.pop();
  22.         for (ll i = 2; i <= k && u * i <= x; ++i) {
  23.             ll v = u * i;
  24.             if (x % v)
  25.                 continue;
  26.             if (dist[v] != LLONG_MAX / 2)
  27.                 continue;
  28.             dist[v] = step + 1;
  29.             q.push(v);
  30.         }
  31.     }
  32.     dbg(x, k, dist[x]);
  33.     return dist[x];
  34. }
  35.  
  36. void solve2()
  37. {
  38.     ll x, y, k;
  39.     cin >> x >> y >> k;
  40.  
  41.     ll g = gcd(x, y);
  42.     x /= g, y /= g;
  43.  
  44.     if (!check_div(x, k) || !check_div(y, k)) {
  45.         cout << -1 << '\n';
  46.         return;
  47.     }
  48.  
  49.     ll ans = calc_step(x, k) + calc_step(y, k);
  50.     if (ans < LLONG_MAX / 2) {
  51.         cout << ans << '\n';
  52.     } else {
  53.         cout << -1 << '\n';
  54.     }
  55. }
  56.  
  57. int main(int argc, char **argv)
  58. {
  59.     std::ios::sync_with_stdio(false);
  60.     std::cin.tie(nullptr);
  61.  
  62.     // init();
  63.  
  64.     ll t;
  65.     cin >> t;
  66.     while (t--)
  67.         solve2();
  68.  
  69.     return 0;
  70. };
  71.  
Advertisement
Add Comment
Please, Sign In to add comment