题意:给定\(n,m(1\le n\le m\le 5\times 10^8),通过将\)n\times 2\(或\)\times 3\(使得\)n=m$,求最小操作次数。
题解
很简单的一道题。
首先,\(n\)一定要是\(m\)的倍数。否则就无法转换,输出-1;
然后令\(k=\dfrac{m}{n}\),即\(kn=m\)。
将\(k\)分解质因数,若分解结果是\(k=2^x+3^y\),那答案就是\(x+y\)。
如果无法分解成\(2^x+3^y\),那么输出-1。
\(\mathfrak{Code here}\)
#include<cstdio>
using namespace std;
int n,m;
signed main()
{
scanf("%d%d",&n,&m);
if(m==n){puts("0");return 0;}
if(m%n!=0){puts("-1");return 0;}
int tmp=m/n;
int cnt=0;
while(tmp%2==0)cnt++,tmp/=2;
while(tmp%3==0)cnt++,tmp/=3;
if(tmp!=1){puts("-1");return 0;}
printf("%d",cnt);
return 0;
}