CodeForces - 1176A Divide it! (模拟+分类处理)

2020-10-28 10:21:41 浏览数 (1)

You are given an integer nn.

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

Replace nn with n2n2 if nn is divisible by 22; Replace nn with 2n32n3 if nn is divisible by 33; 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 (1≤q≤10001≤q≤1000) — the number of queries.

The next qq lines contain the queries. For each query you are given the integer number nn (1≤n≤10181≤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.

Example Input 7 1 10 25 30 14 27 1000000000000000000 Output 0 4 6 6 -1 6 72

代码语言:javascript复制
#include <bits/stdc  .h>
using namespace std;
template <typename t>
void read(t &x)
{
    char ch = getchar();
    x = 0;
    t f = 1;
    while (ch < '0' || ch > '9')
        f = (ch == '-' ? -1 : f), ch = getchar();
    while (ch >= '0' && ch <= '9')
        x = x * 10   ch - '0', ch = getchar();
    x *= f;
}

#define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define rep(m, n, i) for (int i = m; i < n;   i)
#define rrep(m, n, i) for (int i = m; i > n; --i)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
#define N 10005
#define fil(a, n) rep(0, n, i) read(a[i])
//---------------https://lunatic.blog.csdn.net/-------------------//

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        long long n;
        scanf("%lld", &n);
        int l = 0, m = 0, p = 0;
        for (; n % 2 == 0; n /= 2, l  )
            ;
        for (; n % 3 == 0; n /= 3, m  )
            ;
        for (; n % 5 == 0; n /= 5, p  )
            ;
        printf("%dn", n == 1 ? (l   2 * m   3 * p) : -1);
    }
    return 0;
}

0 人点赞