• Codeforces1176A(A题)Divide it!


    Divide it!

    You are given an integer nn.

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

    1. Replace nn with n2n2 if nn is divisible by 22;
    2. Replace nn with 2n32n3 if nn is divisible by 33;
    3. Replace nn with 4n54n5 if nn is divisible by 55.

    For example, you can replace 3030 with 1515 using the first operation, with 2020 using the second operation or with 2424 using the third operation.

    Your task is to find the minimum number of moves required to obtain 11 from nn or say that it is impossible to do it.

    You have to answer qq independent queries.

    Input

    The first line of the input contains one integer qq (1q10001≤q≤1000) — the number of queries.

    The next qq lines contain the queries. For each query you are given the integer number nn (1n10181≤n≤1018).

    Output

    Print the answer for each query on a new line. If it is impossible to obtain 11 from nn, print -1. Otherwise, print the minimum number of moves required to do it.

    代码:

     1 #include<iostream>
     2 #include<algorithm>
     3 using namespace std;
     4 int main() {
     5     int q;
     6     long long a[1005];
     7     cin>>q;
     8     for(int i=0; i<q; i++) {
     9         cin>>a[i];
    10     }
    11     for(int q1=0;q1<q;q1++){
    12         int cnt=0;
    13         while(a[q1]!=1) {
    14             if(a[q1]%2!=0&&a[q1]%3!=0&&a[q1]%5!=0){
    15                 cnt=-1;
    16                 break;
    17             }
    18             if(a[q1]%2==0) {
    19                 a[q1]/=2;
    20                 cnt++;
    21             }
    22             if(a[q1]%3==0) {
    23                 a[q1]=a[q1]*2/3;
    24                 cnt++;
    25             }
    26             if(a[q1]%5==0) {
    27                 a[q1]=a[q1]*4/5;
    28                 cnt++;
    29             }
    30             if(a[q1]==0){
    31                 break;
    32             }
    33         }
    34         cout<<cnt<<endl;
    35     }
    36 }

    思路分析:数要是不能被3,5,2整除,就设置cnt为-1并输出。先除2/n再2n/3z再4n/5统计次数,最后化为1,输出cnt。

    链接:https://codeforces.com/problemset/problem/1176/A

  • 相关阅读:
    汇编四(习题)
    汇编子程序模块化(near&far)
    win10关闭防火墙
    python中numpy中的shape()的使用
    文件的拷贝linux命令
    python中的os.path.dirname(__file__)
    ubuntu系统下安装及查看opencv版本
    用git命令行克隆项目及出现failed解决方案
    ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '
    记录CenterNet代码编译成功运行
  • 原文地址:https://www.cnblogs.com/yuanhang110/p/11267893.html
Copyright © 2020-2023  润新知