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 ...
随机推荐
- modern-cpp-features
C++17/14/11 Overview Many of these descriptions and examples come from various resources (see Acknow ...
- 文章之间的基本总结Activity生命周期
子曰:溫故而知新,能够為師矣.<論語> 学习技术也一样,对于技术文档或者经典的技术书籍来说,指望看一遍就全然掌握,那基本不大可能,所以我们须要常常回过头再细致研读几遍,以领悟到作者的思想精 ...
- BIOS 选项设置的含义
SATA Mode Section: ADHI: Advanced Host Controller Interface - this is a hardware mechanism that allo ...
- 各种Message中文解释(一部分)
函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.该函数是应用程序和应用程序之间进行消息传递的主要手段之一. 函数原型:LRESUL ...
- DataGrid DataGridTemplateColumn
设置单元格的样式 <DataGrid.Columns> <DataGridTextColumn Header="检测项目" Binding="{Bind ...
- 二维码彩色广告招牌的切割制作问题(C#.net下对彩色二维码圆角样式及改进)
原文:二维码彩色广告招牌的切割制作问题(C#.net下对彩色二维码圆角样式及改进) 我们知道,目前二维码还很少用于广告招牌的制作.但随着智能手机越来越普及,互联网等网络的应用也越来越广泛,作为连接物理 ...
- WPF x:Array的使用
<Window x:Class="XamlTest.Window1" xmlns="http://schemas.microsoft.com/winf ...
- .net与.net core学习目录
.net C#调用python 模拟请求(模拟header/gzip解压/泛型) C#控制台关闭之前做一些操作 C# 元组.匿名对象.ref&out DataTable转换为Entity(反射 ...
- Winform入门见解
winform算是C#比较快速的入门的一个了,简单的控件拖拽然后写上每个控件对应的事件.然后就可以了.需要美观的点 可以用Skin皮肤就完成了.我们先不说复杂的,就来个普通的三层架构来增删改查 分页和 ...
- 解决C++项目使用sqlite中文乱码问题
我参考的是这篇文章:https://www.2cto.com/database/201411/354891.html 理论是:sqlite使用的是UTF-8,C++中用的字符串是ascii或unico ...