A. Divide it!

题目链接:http://codeforces.com/contest/1176/problem/A

题目

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 n2 if n is divisible by 2;
Replace n with 2n3 if n is divisible by 3;
Replace n with 4n5 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
Copy

7
1
10
25
30
14
27
1000000000000000000

Output

0
4
6
6
-1
6
72

题意

给你一个数n,如果能整除2,则n变成n/2;如果能整除3,则n变成(2/3)*n;如果能整除5,则变成(4/5)*n;

如果n能经过上述任意操作使n达到1,输出操作步数最小值,如果得不到1,输出-1.

思路

经过推算,

一个数如果能被2整除,那么可以先一直乘以1/2,

能够整除3的可以再一直乘以2/3,

能够整除5的可以再一直乘4/5,

和既能先乘以1/2的再乘2/3的再乘1/2的步数相同,所以代码就好实现了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+; int main()
{ int n;
cin>>n;
ll x;
for(int i=;i<n;i++)
{ cin>>x;
if(x==)
puts("");
else
{
ll sum=;
bool flag=true;
while() {
if ((x / ) * == x) {
while ((x / ) * == x) {
// cout<<"2fff";
x = x / ;
// cout<<x;
sum++;
}
}
else if ((x / ) * == x) {
while ((x / ) * == x) {
x = (x * ) / ;
sum++;
}
}
else if ((x / ) * == x) {
while ((x / ) * == x) {
x = (x * ) / ;
sum++;
}
}
else if(x==)
break;
else {
flag=false;
break;
}
} if(!flag)
cout<<"-1"<<endl;
else
cout<<sum<<endl; }
}
return ;
}

最新文章

  1. 去 IOE,MySQL 完胜 PostgreSQL
  2. 【Android】Android Studio 快速打开Github上的Demo
  3. HDU #5733 tetrahedron
  4. VC中GBK与UTF8转化
  5. openstack kilo compute更新后报错IncompatibleObjectVersion: Version 1.2 of PciDeviceList is not supported
  6. 【Hibernate】Hibernate系列2之Session详解
  7. html的标签中 unselectable=on 属性的作用
  8. DateTimePicker时间控件:
  9. linux lvm的操作手册_pvcreate_vgcreate_lvcreate_相关
  10. makefile的编写规则
  11. Hybris license过期的解决办法
  12. 完整教程--idea使用git进行项目管理
  13. IDEA 的maven项目打jar 编写UDF 在hive端运行 (全过程,有录制的操作视频)
  14. NIO原理及案例使用
  15. skynet框架之日程表设计
  16. Open-Source Cybersecurity Infrastructure
  17. day10(闭包、import模块、函数命名空间)
  18. [转]jvm调优-命令大全(jps jstat jmap jhat jstack jinfo)
  19. ubuntu 18.04下安装Hadoop
  20. Oracle 表空间的概念

热门文章

  1. Lync 2013和Exchange 2013集成
  2. Android Intent传递对象摘要
  3. swift4.0 Http 请求
  4. C#基础:ref和out的区别 [转]
  5. 人像美妆---妆容迁移算法研究(Makeup transfer)
  6. SQL Server 数据库所有表增加同一列
  7. js中的scrollTop、offsetTop、clientTop
  8. C# string.Format()格式
  9. 不同格式图片相互转换的开源库分享(使用CxImage,并有VC6的配置过程)
  10. 桌面程序阻止Windows关机(使用Message.Result取得DefWindowProc API函数的返回值,非常重要)