# Codeforces Round #653 (Div. 3) B. Multiply by 2, divide by 6（暴力、思维）

## Multiply by 2, divide by 6

#include <iostream>
using namespace std;
typedef long long ll;

int main() {
int t;
cin >> t;
while(t--) {
ll n, s = 0;
cin >> n;
while(1) {
if(n % 3 && n % 6) //如果出现不能整除的情况，之后就一定不能得到1了（因为操作时*2或者/6，如果不满足不论怎么操作都不会满足）
break;
if(n == 1)
break;
if(n % 6 == 0){
n /= 6;
s++;
}else if(n % 3 == 0) {	//n *= 2， n /= 6；
n /= 3;
s += 2;
}
}

if(n == 1)
cout << s << endl;
else
cout << "-1" << endl;
}

return 0;
}

### 原题：

You are given an integer n. In one move, you can either multiply n by two or divide nn by 6 (if it is divisible by 6 without the remainder).

Your task is to find the minimum number of moves needed to obtain 1 from nn or determine if it's impossible to do that.

You have to answer tt independent test cases.

The first line of the input contains one integer tt (1≤t≤2⋅10^4) — the number of test cases. Then tt test cases follow.

The only line of the test case contains one integer nn (1≤n≤10^9).

For each test case, print the answer — the minimum number of moves needed to obtain 1 from n if it's possible to do that or -1 if it's impossible to obtain 1 from n.

Input：                 Output:

7
1 ------------------------- 0
2 ------------------------- -1
3 ------------------------- 2
12 ------------------------ -1
12345 ------------------- -1
15116544 -------------- 12
387420489 ------------ 36

Note:

Consider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer 1511654415116544 :

1. Divide by 66 and get 25194242519424 ;
2. divide by 66 and get 419904419904 ;
3. divide by 66 and get 6998469984 ;
4. divide by 66 and get 1166411664 ;
5. multiply by 22 and get 2332823328 ;
6. divide by 66 and get 38883888 ;
7. divide by 66 and get 648648 ;
8. divide by 66 and get 108108 ;
9. multiply by 22 and get 216216 ;
10. divide by 66 and get 3636 ;
11. divide by 66 and get 66 ;
12. divide by 66 and get 11 .

06-29 74

06-29 24

06-29 114

05-07 274

06-29 173

06-29 243

05-20 121

05-07 1111

06-29 209

06-29 102

06-29 1486

06-29 32

06-05 110

06-29 36

05-07 65

06-05 285

06-30 26

03-20 402

#### Codeforces Round #547 (Div. 3)

©️2020 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。