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

转载于:https://www.cnblogs.com/yuanhang110/p/11267893.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值