Codeforces Round #565 (Div. 3) A
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 ;
}
Codeforces Round #565 (Div. 3) A的更多相关文章
- Codeforces Round #565 (Div. 3) B. Merge it!
链接: https://codeforces.com/contest/1176/problem/B 题意: You are given an array a consisting of n integ ...
- Codeforces Round #565 (Div. 3) A. Divide it!
链接: https://codeforces.com/contest/1176/problem/A 题意: You are given an integer n. You can perform an ...
- Codeforces Round #565 (Div. 3) C. Lose it!
链接: https://codeforces.com/contest/1176/problem/C 题意: You are given an array a consisting of n integ ...
- Codeforces Round #565 (Div. 3) B
B. Merge it! 题目链接:http://codeforces.com/contest/1176/problem/B 题目 You are given an array a consistin ...
- Codeforces Round #565 (Div. 3) F.Destroy it!
题目地址:http://codeforces.com/contest/1176/problem/F 思路:其实就是一个01背包问题,只是添加了回合和每回合的01限制,和每当已用牌数到了10的倍数,那张 ...
- Codeforces Round #565 (Div. 3)
传送门 A. Divide it! •题意 给定一个数n, 每次可以进行下列一种操作 1.如果n可以被2整除,用n/2代替n 2.如果n可以被3整除,用2n/3代替n 3.如果n可以被5整除,用4n/ ...
- Codeforces Round #565 (Div. 3)--D. Recover it!--思维+欧拉筛
D. Recover it! Authors guessed an array aa consisting of nn integers; each integer is not less than ...
- Codeforces Round #565 (Div. 3) C. Lose it! (思维)
题意:给你一串只含\(4,8,15,16,23,42\)的序列,如果它满足长度是\(6\)的倍数并且有\(\frac {k}{6}\)个子序列是\([4,8,15,16,23,42]\),则定义它是好 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- Jconsole使用实例解说
Jconsole是jdk自带的一套java虚拟机执行状况监视器,它能够用来监控虚拟机的内存,线程,cpu使用情况以及相关的java进程相关的MBean,主界面例如以下: 图1 怎样连接被监控的Java ...
- c#调用ffmpeg嵌入srt/ass字幕提示Unable to open xxx.srt......
最近接触到c#调用ffmpeg嵌入srt/ass字幕,碰到一个错误困扰了很久 Unable to open xxx.srt Error initializing filter 'subtitles' ...
- EBS OAF 发展 URL商标、加密和编码
EBS OAF 发展 URL商标.加密和编码 (版权声明.我原来的或翻译的文章,如需转载,转载的个人学习,转载请注明出处:否则,请与我联系.版权所有) 马克 当您指定页面定义声明URL参数,文本也能够 ...
- WPF中用于嵌入其他进程窗口的自定义控件(AppContainer)
原文:WPF中用于嵌入其他进程窗口的自定义控件(AppContainer) 版权声明:本文为博主原创文章,转载请注明作者和出处 https://blog.csdn.net/ZZZWWWPPP11199 ...
- wpf实现两头渐窄的线条效果
原文:wpf实现两头渐窄的线条效果 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/article/details/800 ...
- [WPF]获取鼠标指针下的元素
原文:[WPF]获取鼠标指针下的元素 [WPF]获取鼠标指针下的元素 周银辉 以前写过一些GetElementUnderMouse之类的函数,要用到坐标换算而显得有些麻烦(特别是当元素有XXXTr ...
- Hermite曲线插值
原文 Hermite Curve Interpolation Hermite Curve Interpolation Hamburg (Germany), the 30th March 1998. W ...
- WPF扩展标记X:STATIC
原文:WPF扩展标记X:STATIC public class XStaic { public static string Content = "确定"; ...
- 【shell】递归函数----调用自身的函数
什么是递归函数? 一句话,调用自己的函数称为递归函数! #!/bin/bash declare -i count checkoutCount(){ read -p "Enter an cou ...
- Delphi中close与Terminate方法的区别
在有多个Form窗体时可以体现出来.用close是只关闭本窗体,而用Application.terminate是关闭整个程序,包括所有窗体.(1)当Close是一个主窗体时,程序会退出.Close会发 ...