2019.7.29
You are given an integer n.You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times:
Replace n with n/2 if n is divisible by 2;
Replace n with (2n)/3 if n is divisible by 3;
Replace n with (4n)/5 if n is divisible by 5.
For example, you can replace 30 with 15 using the first operation, with 20 using the second operation or with 24 using the third operation.
Your task is to find the minimum number of moves required to obtain 1 from n or say that it is impossible to do it.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1≤q≤1000) — the number of queries.
The next q lines contain the queries. For each query you are given the integer number n (1≤n≤1018).
Output
Print the answer for each query on a new line. If it is impossible to obtain 1 from n, print -1. Otherwise, print the minimum number of moves required to do it.
Example
Input
7
1
10
25
30
14
27
1000000000000000000
Output
0
4
6
6
-1
6
72
代码:
#include <iostream>
using namespace std;
int main()
{
int q, ans;
long long n;
cin >> q;
for(int i=0; i<q; i++)
{
ans = 0;
cin >> n;
while(n != 1)
{
if(n%30==0 || n%15==0 || n%10==0 || n%5==0)
{
n = (4*n)/5;
ans++;
}
else if(n%6 == 0 || n%3==0)
{
n = (2*n)/3;
ans++;
}
else if(n%2 == 0)
{
n = n/2;
ans++;
}
else if(n%2!=0 || n%3!=0 || n%5!=0)
{
ans = -1;
n = 1;
}
}
cout << ans << endl;
}
}