zoukankan      html  css  js  c++  java
  • Codeforces Round #565 (Div. 3) A. Divide it!

    Codeforces Round #565

    A. Divide it!

    You are given an integer n.

    You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times:

    1. Replace n with n/2 if n is divisible by 2;
    2. Replace n with 2*n/3 if n is divisible by 3;
    3. Replace n with 4*n/5 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≤10^18).

    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
    7
    1
    10
    25
    30
    14
    27
    1000000000000000000
    
    output
    0
    4
    6
    6
    -1
    6
    72



    题意:这题意思是给你一个数,让你进行三种操作,1:能被2整除,变成n/2 。2:能被3整除,变成2*n/3 。3:能被5整除,变成4*n/5;
    问你变到1的最少操作次数,如果不能变成1,则输出-1。

    思路:直接暴力就可以了......


     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<map>
     7 #include<set>
     8 #include<vector>
     9 using namespace std;
    10 #define ll long long
    11 //const int maxn=;
    12 int main()
    13 {
    14     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    15     int T;
    16     cin>>T;
    17     ll int num;
    18     while(T--)
    19     {
    20         cin>>num;
    21         if(num==1)
    22         {
    23             cout<<0<<endl;
    24             continue;
    25         }
    26         ll int ans=0;
    27         while(num!=1)
    28         {
    29             if(num%2==0)
    30                 num>>=1;
    31             else if(num%3==0)
    32             {
    33                 num/=3;
    34                 num<<=1;
    35             }
    36             else if(num%5==0)
    37             {
    38                 num/=5;
    39                 num<<=2;
    40             }
    41             else
    42             {
    43                 ans=-1;
    44                 break;
    45             }
    46             ans++;
    47         }
    48         cout<<ans<<endl;
    49     } 
    50     return 0;
    51 }
     
    大佬见笑,,
  • 相关阅读:
    免费试用Windows Azure云平台(无须提供信用卡)
    如何下载Ubuntu命令对应的源码
    Unix编程艺术——优化、工具、重用、可移植性、文档
    Choice of Xen Toolstacks
    [转]数据驱动编程之表驱动法
    获取Centos命令对应的源码
    Unix编程艺术——配置
    [转]vim ctags使用方法
    format and indent xml
    python得到本地网卡的IP
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/11000435.html
Copyright © 2011-2022 走看看