1141A Game 23

A. Game 23

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp plays "Game 23". Initially he has a number nn and his goal is to transform it to mm. In one move, he can multiply nn by 22 or multiply nn by 33. He can perform any number of moves.

Print the number of moves needed to transform nn to mm. Print -1 if it is impossible to do so.

扫描二维码关注公众号,回复: 10625349 查看本文章

It is easy to prove that any way to transform nn to mm contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).

Input

The only line of the input contains two integers nn and mm (1≤n≤m≤5⋅1081≤n≤m≤5⋅108).

Output

Print the number of moves to transform nn to mm, or -1 if there is no solution.

Examples

input

Copy

120 51840

output

Copy

7

input

Copy

42 42

output

Copy

0

input

Copy

48 72

output

Copy

-1

Note

In the first example, the possible sequence of moves is: 120→240→720→1440→4320→12960→25920→51840.120→240→720→1440→4320→12960→25920→51840. The are 77 steps in total.

In the second example, no moves are needed. Thus, the answer is 00.

In the third example, it is impossible to transform 4848 to 7272.

题意:给了一个n,一个m,n每次可以乘2,或者乘3算出经过多少次乘2或乘3能够变成m。

题解:m%n是否除的尽,部分可以,然后算出m是n的多少倍,然后倍数来不停除3,除2,每出一次步数ans++,如果最后的结构不等于1,就不满足,例如 n=1,m=5,满足的话,除完一定等于1.

c++:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,ans=0;
    cin>>n>>m;
    if(m%n)
    {
        cout<<-1<<endl;
        return 0;
    }
    else
    {
        int x=m/n;
        while(x%3==0) ans++,x/=3;
        while(x%2==0) ans++,x/=2;
        if(x!=1)
        {
            cout<<-1<<endl;
            return 0;
        }
    }
    cout<<ans<<endl;
    return 0;
}

python:

n,m=map(int,input().split())
ans=0
if m%n:print(-1);exit()
else:
          x=m//n
          while x%3==0:ans+=1;x//=3
          while x%2==0:ans+=1;x//=2
          if x!=1:print(-1);exit()
print(ans)
          
发布了395 篇原创文章 · 获赞 126 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/88680375
23
今日推荐