Advertisement
Josif_tepe

Untitled

Jan 25th, 2024
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. //#include <bits/stdc++.h>
  3. #include <vector>
  4. #include <map>
  5. using namespace std;
  6. typedef long long ll;
  7. map<ll, int> A;
  8. void simulate(ll x) {
  9.     int cnt = 0;
  10.     A[x] = cnt;
  11.     while(x != 1) {
  12.         cnt++;
  13.         if(x % 2 == 0) {
  14.             x /= 2;
  15.         }
  16.         else {
  17.             x *= 3;
  18.             x++;
  19.         }
  20.         if(!A.count(x)) {
  21.             A[x] = cnt;
  22.         }
  23.     }
  24. }
  25. int main() {
  26.     ll a, b;
  27.     while(cin >> a >> b) {
  28.         A.clear();
  29.        
  30.         if(a == 0 and b == 0) {
  31.             break;
  32.         }
  33.        
  34.         simulate(a);
  35.         ll tmp = b;
  36.         int cnt = 0;
  37.         while(true) {
  38.            
  39.             if(A.count(b)) {
  40.                 break;
  41.             }
  42.             if(b % 2 == 0) {
  43.                 b /= 2;
  44.             }
  45.             else {
  46.                 b *= 3;
  47.                 b++;
  48.             }
  49.             cnt++;
  50.         }
  51.         cout << a << " needs " << A[b] << " steps, " << tmp << " needs " << cnt << " steps, they meet at " << b << endl;
  52.    
  53.     }
  54.    
  55.     return 0;
  56. }
  57. /*
  58.  7 needs 13 steps, 8 needs 0 steps, they meet at 8
  59.  27 needs 95 steps, 30 needs 2 steps, they meet at 46
  60.  **/
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement