当前位置:网站首页>OJ 1451 digital games
OJ 1451 digital games
2022-07-28 06:38:00 【JETECHO】
describe
Given an integer n.
You can use this number to do any of the following ( It could be zero ) frequency :
If n Can be 2 to be divisible by , Then use n/2 Instead of n;
If n Can be 3 to be divisible by , Then use 2n/3 Instead of n;
If n Can be 5 to be divisible by , Just use 4n/5 Instead of n.
for example , You can use the first operation to 30 Replace with 15, Using the second operation will 30 Replace with 20, Or use the third operation to 30 Replace with 24.
Your task is to find out from n Get in 1 Minimum number of steps required , Or this is impossible .
You must answer independently of q Query for .
Print the answer to each query in a new row . If you can't get from n Get in 1, Then print -1. otherwise , The minimum number of steps required for printing .
Input
The first line of input contains an integer q(1≤q≤1000)—— Number of queries .
Next q The row contains the query . For each query , There is an integer n(1≤n≤1018).
Output
Print the answer of each query in each row . If you can't get from n Get in 1, Then print -1. otherwise , The minimum number of steps required for printing .
Digital games
describe
Given an integer n.
You can use this number to do any of the following ( It could be zero ) frequency :
If n Can be 2 to be divisible by , Then use n/2 Instead of n;
If n Can be 3 to be divisible by , Then use 2n/3 Instead of n;
If n Can be 5 to be divisible by , Just use 4n/5 Instead of n.
for example , You can use the first operation to 30 Replace with 15, Using the second operation will 30 Replace with 20, Or use the third operation to 30 Replace with 24.
Your task is to find out from n Get in 1 Minimum number of steps required , Or this is impossible .
You must answer independently of q Query for .
Print the answer to each query in a new row . If you can't get from n Get in 1, Then print -1. otherwise , The minimum number of steps required for printing .
Input
The first line of input contains an integer q(1≤q≤1000)—— Number of queries .
Next q The row contains the query . For each query , There is an integer n(1≤n≤1018).
Output
Print the answer of each query in each row . If you can't get from n Get in 1, Then print -1. otherwise , The minimum number of steps required for printing .
sample input 1
7
1
10
25
30
14
27
1000000000000000000
sample output 1
0
4
6
6
-1
6
72
The problem requires that the data be changed into 1, If a number changes many times and becomes a non Can be 2,3,5 The number divided by an integer, then this data cannot become 1, Otherwise, the number of steps may be less , Then the reduction should also be as large as possible , Cut from small to large 1/2,2/3,4/5, Then use this rule to analogy .
#include <iostream>
using namespace std;
long long MIN;
void doit (long long a,int cont)
{
if(a%2==0)
{
if(a/2==1)
{
if(MIN>cont)
MIN=cont;
}
else
doit(a/2,cont+1);
}
else if(a%3==0)
{
if(2*a/3==1)
{
if(MIN>cont)
MIN=cont;
}
else
doit(2*a/3,cont+1);
}
else if(a%5==0)
{
if(4*a/5==1)
{
if(MIN>cont)
MIN=cont;
}
else
doit(4*a/5,cont+1);
}
else
MIN=-1;
}
int main()
{
long long n,a;
while(cin>>n)
{
while(n--)
{
MIN=0x3f3f3f;
cin>>a;
if(a==1)
cout<<"0"<<endl;
else
{
doit(a,1);
cout<<MIN<<endl;
}
}
}
return 0;
}
边栏推荐
猜你喜欢

气传导耳机哪个品牌比较好、这四款不要错过

【动态规划--买卖股票的最佳时期系列2】

2022-05-24 use of spiel

What's a good gift for your girlfriend on the Chinese Valentine's day in 2022? Practical and beautiful gift recommendation

Development of clip arbitrage / brick carrying arbitrage system

What's a gift for girls on Chinese Valentine's day? Selfie online and thoughtful gift recommendation

QT implementation outputs relevant information to log file

图形管线基础(番外篇)

What are the open earphones? Four types of air conduction earphones with excellent sound quality are recommended

用c语言实现三子棋小游戏
随机推荐
小程序创建组件
OJ 1129 分数矩阵
scrapy 定时执行
2021-11-10
2022-06-07 六.日志实现
Bug experience related to IAP jump of stm32
Combine multiple ICs calendars into a single ICs calendar
浮点型数据在内存中如何存储
execjs 调用
QT batch operation control and set signal slot
气传导耳机哪个好、性价比最高的气传导耳机推荐
[c语言]简易通讯录的实现
QT solves the problem of rebuilding UI files every time they are modified
【自我救赎的开始】
[PTA----树的遍历]
NFT数藏盲盒+模式系统开发
The startup fails when switching Chinese when using wampserver3.2.6
[untitled]
OJ 1253 ordering problem
2022-07-17 Damon database installation