Codeforces Round #547 (Div. 3) A.Game 23

链接:https://codeforces.com/contest/1141/problem/A

题意:

给n和m,有两种操作:将n×2 或 n×3,求最少的乘法次数由n得到m。

不能得到时为-1。

思路:

先判断是否为整数倍。

再将倍数不断除以2和3。

最后剩下1则可以达到否则-1。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

int main()
{
    int n, m;
    cin >> n >> m;
    if (m % n != 0)
        cout << -1 << endl;
    else
    {
        int cnt = 0;
        int t = m / n;
        while (t % 2 == 0)
            t /= 2, cnt++;
        while (t % 3 == 0)
            t /= 3, cnt++;
        if (t != 1)
            cout << -1 << endl;
        else
            cout << cnt << endl;
    }


    return 0;
}