给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;
}
原文:https://www.cnblogs.com/YDDDD/p/10570937.html