Codeforces1176A(A题)Divide it!

Divide it!

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.

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int main() {
 5     int q;
 6     long long a[1005];
 7     cin>>q;
 8     for(int i=0; i<q; i++) {
 9         cin>>a[i];
10     }
11     for(int q1=0;q1<q;q1++){
12         int cnt=0;
13         while(a[q1]!=1) {
14             if(a[q1]%2!=0&&a[q1]%3!=0&&a[q1]%5!=0){
15                 cnt=-1;
16                 break;
17             }
18             if(a[q1]%2==0) {
19                 a[q1]/=2;
20                 cnt++;
21             }
22             if(a[q1]%3==0) {
23                 a[q1]=a[q1]*2/3;
24                 cnt++;
25             }
26             if(a[q1]%5==0) {
27                 a[q1]=a[q1]*4/5;
28                 cnt++;
29             }
30             if(a[q1]==0){
31                 break;
32             }
33         }
34         cout<<cnt<<endl;
35     }
36 }

思路分析:数要是不能被3,5,2整除,就设置cnt为-1并输出。先除2/n再2n/3z再4n/5统计次数,最后化为1,输出cnt。

链接:https://codeforces.com/problemset/problem/1176/A

posted @ 2019-07-30 09:01  远航。  阅读(261)  评论(0编辑  收藏  举报