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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值