Beingamanforever

GCD Algo, CF, Pair of Numbers

Dec 27th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. /**
  2.  *    author:  heWhoCooks
  3.  *    created: 2024-12-27 19:11:27
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define pb push_back
  21. #define pf push_front
  22. #define yes cout << "YES" << endl
  23. #define no cout << "NO" << endl
  24. #define endl "\n"
  25. #define M_PI 3.14159265358979323846L
  26. const int mod = 1e9 + 7;
  27. const int INF = 2e18;
  28. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  29. int steps(int a, int b)
  30. {
  31.     if (a == 1)
  32.     {
  33.         return b - 1;
  34.     }
  35.     if (a > b)
  36.     {
  37.         return steps(b, a);
  38.     }
  39.     if (b % a)
  40.     {
  41.         return b / a + steps(b % a, a);
  42.     }
  43.     else
  44.     {
  45.         // not reachable as b%a is 0, means one would be zero
  46.         return 1e9;
  47.     }
  48. }
  49. void solve()
  50. {
  51.     int n;
  52.     cin >> n;
  53.     if (n == 1)
  54.     {
  55.         cout << 0 << endl;
  56.         return;
  57.     }
  58.     int ans = INF;
  59.     // calculating the num steps to reach (a, b) where one number is n
  60.     for (int i = 1; i < n; i++)
  61.     {
  62.         ans = min(ans, steps(i, n));
  63.     }
  64.     cout << ans << endl;
  65.     return;
  66. }
  67.  
  68. signed main()
  69. {
  70.     NeedForSpeed;
  71.     int t = 1;
  72.     // cin >> t;
  73.     while (t--)
  74.     {
  75.         solve();
  76.     }
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment