Advertisement
Niloy007

B. Multiply by 2, divide by 6

Jun 28th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int test;
  6.     long long n, count;
  7.     cin >> test;
  8.     while (test--) {
  9.         count = 0;
  10.         cin >> n;
  11.         if (n == 1) {
  12.             cout << 0 << endl;
  13.             continue;
  14.         }
  15.  
  16.         while (n % 3 == 0) {
  17.             if (n == 1) {
  18.                 break;
  19.             }
  20.             if (n % 6 == 0) {
  21.                 n /= 6;
  22.                 count++;
  23.             } else {
  24.                 n *= 2;
  25.                 count++;
  26.             }
  27.         }
  28.         if (n == 1) {
  29.             cout << count << endl;
  30.         } else {
  31.             cout << -1 << endl;
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement