Codeforces Round #565 (Div. 3) A. Divide it!

链接:

https://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.

思路:

优先操作2, 再3,再5.
暴力即可。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
LL n, m, k, t;

int main()
{
//    freopen("test.in", "r", stdin);
    cin >> t;
    while (t--)
    {
        cin >> n;
        int res = 0;
        bool flag = true;
        while (n != 1)
        {
            if (n%2 == 0)
                n = n/2;
            else if (n%3 == 0)
                n = (n/3)*2;
            else if (n%5 == 0)
                n = (n/5)*4;
            else
            {
                flag = false;
                break;
            }
            res++;
        }
        if (flag)
            cout << res << endl;
        else
            cout << -1 << endl;
    }

    return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/10998680.html