算法
模拟
题目描述
判断一个数是否可以由另一个数仅乘2、3得到
解题思路
先判断是否能被整除,然后对商不断除以2、3,直到不能整除。
有余数则不可,反之可以。
代码
#include<iostream>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if(b%a!=0){
cout<<-1<<endl;
return 0;
}
b/=a;
int ans=0;
while(b>1&&b%2==0){
b/=2;
ans++;
}
while(b>1&&b%3==0){
b/=3;
ans++;
}
if(b==1){
cout<<ans<<endl;
}
else{
cout<<-1<<endl;
}
return 0;
}
题目总结
题目原文
题面
东东在玩游戏“Game23”。
在一开始他有一个数字n,他的目标是把它转换成m,在每一步操作中,他可以将n乘以2或乘以3,他可以进行任意次操作。输出将n转换成m的操作次数,如果转换不了输出-1。
Input
输入的唯一一行包括两个整数n和m(1<=n<=m<=5*10^8).
Output
输出从n转换到m的操作次数,否则输出-1.
Simple Input 1
120 51840
Simple Output 1
7
Simple Input 2
42 42
Simple Output 2
0
Simple Input 3
48 72
Simple Output 3
-1