Advertisement
pb_jiang

CF ROUND 1027 F

May 27th, 2025
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 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. constexpr ll ub = 1e6 + 3;
  24. vector<vl> divs(ub);
  25. void init()
  26. {
  27.     for (ll i = 1; i < ub; ++i)
  28.         for (ll j = i; j < ub; j += i)
  29.             divs[j].push_back(i);
  30. }
  31.  
  32. void solve(ll t)
  33. {
  34.     ll x, y, k, ans = 0;
  35.     cin >> x >> y >> k;
  36.  
  37.     ll g = gcd(x, y);
  38.     x /= g, y /= g;
  39.  
  40.     auto remove_req = [&ans, k](ll val) -> ll {
  41.         dbg(val);
  42.         assert(val < ub);
  43.         const auto &ds = divs[val];
  44.         ll len = divs[val].size();
  45.         vl cost(len, LLONG_MAX / 2);
  46.         cost[0] = 0;
  47.  
  48.         for (ll i = 1; i < len; ++i)
  49.             for (ll j = 0; j < i; ++j)
  50.                 if (ds[i] % ds[j] == 0 && ds[i] <= ds[j] * k)
  51.                     cost[i] = min(cost[i], cost[j] + 1);
  52.         return cost.back();
  53.     };
  54.     ans = remove_req(x) + remove_req(y);
  55.     if (ans < LLONG_MAX / 2)
  56.         cout << ans << '\n';
  57.     else
  58.         cout << -1 << '\n';
  59. }
  60.  
  61. int main(int argc, char **argv)
  62. {
  63.     std::ios::sync_with_stdio(false);
  64.     std::cin.tie(nullptr);
  65.  
  66.     init();
  67.  
  68.     ll t;
  69.     cin >> t;
  70.     ll ot = t;
  71.     while (t--)
  72.         solve(ot - t);
  73.  
  74.     return 0;
  75. };
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement