A - Divide it! CodeForces - 1176A

题目:

You are given an integer nn.

You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times:

  1. Replace nn with n2n2 if nn is divisible by 22;
  2. Replace nn with 2n32n3 if nn is divisible by 33;
  3. Replace nn with 4n54n5 if nn is divisible by 55.

For example, you can replace 3030 with 1515 using the first operation, with 2020 using the second operation or with 2424 using the third operation.

Your task is to find the minimum number of moves required to obtain 11 from nn or say that it is impossible to do it.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1q10001≤q≤1000) — the number of queries.

The next qq lines contain the queries. For each query you are given the integer number nn (1n10181≤n≤1018).

Output

Print the answer for each query on a new line. If it is impossible to obtain 11 from nn, 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

  虽然这是一道基础签到题,但是还真不怎么会,问了别人才知道还能这么玩。

  • 至于权重是这么来的:因为除以3后还要乘以2,所以还要再除一次2才能让n成为1,因为经过两次n才成为1,所以贡献值也就是权重是2;同理,n/5的权重也这么来,因为除以5后又要乘以4,4要被2除两次才为1,一共要除三次,所以权重为3,即算出来的cnt前面的系数是3.。

代码:

 1 #include<iostream>
 2 using namespace std;
 3  
 4 int main()
 5 {
 6     int t;
 7     scanf("%d", &t);
 8     while (t--)
 9     {
10         long long int n;
11         cin >> n;
12         int cnt1 = 0, cnt2 = 0, cnt3 = 0;
13         while (n % 2 == 0)
14         {
15             n = n / 2;
16             cnt1++;
17         }
18         while (n % 3 == 0)
19         {
20             n = n / 3;
21             cnt2++;
22         }
23         while (n % 5 == 0)
24         {
25             n = n / 5;
26             cnt3++;
27         }
28         if(n != 1)
29             cout << "-1" << endl;
30         else
31             {
32                 int sum = cnt1 + 2 * cnt2 + 3 * cnt3;  //系数为贡献值类似权重一样的东西
33                 cout << sum << endl;
34             }
35     }
36     return 0;
37 }

猜你喜欢

转载自www.cnblogs.com/Anber82/p/11146347.html