pb_jiang

CF ROUND 1027 F AC

May 27th, 2025
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. // Problem: F. Small Operations
  2. // Contest: Codeforces - Codeforces Round  1027 (Div. 3)
  3. // URL: https://codeforces.com/contest/2114/problem/F
  4. // Memory Limit: 256 MB
  5. // Time Limit: 3000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. namespace rngs = std::ranges;
  18. using ll = long long;
  19. using a2l = array<ll, 2>;
  20. using pll = pair<ll, ll>;
  21. using vl = vector<ll>;
  22.  
  23. ll calc_step(ll x, ll k)
  24. {
  25.     vl dist(x + 1, LLONG_MAX / 2);
  26.     dist[1] = 0;
  27.     queue<ll> q;
  28.     q.push(1);
  29.     while (!q.empty()) {
  30.         ll u = q.front(), step = dist[u];
  31.         q.pop();
  32.         for (ll i = 2; i <= k && u * i <= x; ++i) {
  33.             ll v = u * i;
  34.             if (x % v)
  35.                 continue;
  36.             if (dist[v] != LLONG_MAX / 2)
  37.                 continue;
  38.             dist[v] = step + 1;
  39.             q.push(v);
  40.         }
  41.     }
  42.     dbg(x, k, dist[x]);
  43.     return dist[x];
  44. }
  45.  
  46. bool check_div(ll x, ll k)
  47. {
  48.     for (ll i = 2; i * i <= x && i <= k; ++i)
  49.         while (x % i == 0)
  50.             x /= i;
  51.     return x <= k;
  52. }
  53.  
  54. vl get_factor(ll x, ll k)
  55. {
  56.     vl ret{1, x};
  57.     for (ll i = 2; i * i <= x; ++i) {
  58.         if (x % i == 0) {
  59.             ret.push_back(i);
  60.             if (i != x / i)
  61.                 ret.push_back(x / i);
  62.         }
  63.     }
  64.     rngs::sort(ret);
  65.     return ret;
  66. }
  67.  
  68. ll calc_step2(ll x, ll k)
  69. {
  70.     if (x == 1)
  71.         return 0;
  72.     vl fs = get_factor(x, k);
  73.     dbg(x, k, fs);
  74.     vl ans(fs.size(), LLONG_MAX / 2);
  75.     ans[0] = 0;
  76.     queue<ll> q;
  77.     q.push(0);
  78.     while (!q.empty()) {
  79.         ll u = q.front(), step = ans[u];
  80.         q.pop();
  81.         for (ll v = u + 1; v < ans.size() && fs[v] <= fs[u] * k; ++v) {
  82.             if (fs[v] % fs[u] == 0) {
  83.                 if (ans[v] > step + 1) {
  84.                     ans[v] = step + 1;
  85.                     q.push(v);
  86.                 }
  87.             }
  88.         }
  89.     }
  90.     dbg(x, k, ans);
  91.     return ans.back();
  92. }
  93.  
  94. void solve2()
  95. {
  96.     ll x, y, k;
  97.     cin >> x >> y >> k;
  98.  
  99.     ll g = gcd(x, y);
  100.     x /= g, y /= g;
  101.  
  102.     if (!check_div(x, k) || !check_div(y, k)) {
  103.         cout << -1 << '\n';
  104.         return;
  105.     }
  106.  
  107.     // ll ans = calc_step(x, k) + calc_step(y, k);
  108.     ll ans = calc_step2(x, k) + calc_step2(y, k);
  109.     if (ans < LLONG_MAX / 2) {
  110.         cout << ans << '\n';
  111.     } else {
  112.         cout << -1 << '\n';
  113.     }
  114. }
  115.  
  116. int main(int argc, char **argv)
  117. {
  118.     std::ios::sync_with_stdio(false);
  119.     std::cin.tie(nullptr);
  120.  
  121.     ll t;
  122.     cin >> t;
  123.     while (t--)
  124.         solve2();
  125.  
  126.     return 0;
  127. };
  128.  
Advertisement
Add Comment
Please, Sign In to add comment