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+10;

int main()
{

    int n;
    cin>>n;
    ll x;
    for(int i=0;i<n;i++)
    {

        cin>>x;
        if(x==1)
            puts("0");
        else
        {
            ll sum=0;
            bool flag=true;
            while(1) {
                if ((x / 2) * 2 == x) {
                    while ((x / 2) * 2 == x) {
                      //  cout<<"2fff";
                        x = x / 2;
                       // cout<<x;
                        sum++;
                    }
                }
                else if ((x / 3) * 3 == x) {
                    while ((x / 3) *3 == x) {
                        x = (x * 2) / 3;
                        sum++;
                    }
                }
               else if ((x / 5) * 5 == x) {
                    while ((x / 5) * 5 == x) {
                        x = (x * 4) / 5;
                        sum++;
                    }
                }
                else if(x==1)
                    break;
                else  {
                    flag=false;
                    break;
                }
            }

            if(!flag)
                cout<<"-1"<<endl;
            else
                cout<<sum<<endl;

        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Vampire6/p/11001256.html

时间: 2024-10-11 19:30:02

Codeforces Round #565 (Div. 3) A的相关文章

Codeforces Round #565 (Div. 3) C. Lose it!

链接: https://codeforces.com/contest/1176/problem/C 题意: You are given an array a consisting of n integers. Each ai is one of the six following numbers: 4,8,15,16,23,42. Your task is to remove the minimum number of elements to make this array good. An a

Codeforces Round #565 (Div. 3) B

B. Merge it! 题目链接:http://codeforces.com/contest/1176/problem/B 题目 You are given an array a consisting of n integers a1,a2,…,an In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not

Codeforces Round #565 (Div. 3) E. Cover it!

题目:https://codeforces.com/contest/1176/problem/E 思路:奇偶染色 #include<bits/stdc++.h> using namespace std; #define DEBUG cout<<"DEBUG"<<endl typedef long long ll; typedef unsigned long long ull; typedef pair<int,int>pii; typed

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/5代替n 如果可以经过上述操作使得 n 变为 1,输出最小操作次数,反之,输出-1: •思路 n/2 < 2n/3 < 4n/5 要想操作次数最少,优先操作 1 > 2 > 3: •代码 #include<bits/stdc++.h> using namespace std

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除