1141A Game 23

电玩女神 2022-03-02 08:57 305阅读 0赞

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.

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

  1. 120 51840

output

Copy

  1. 7

input

Copy

  1. 42 42

output

Copy

  1. 0

input

Copy

  1. 48 72

output

Copy

  1. -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++:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,m,ans=0;
  6. cin>>n>>m;
  7. if(m%n)
  8. {
  9. cout<<-1<<endl;
  10. return 0;
  11. }
  12. else
  13. {
  14. int x=m/n;
  15. while(x%3==0) ans++,x/=3;
  16. while(x%2==0) ans++,x/=2;
  17. if(x!=1)
  18. {
  19. cout<<-1<<endl;
  20. return 0;
  21. }
  22. }
  23. cout<<ans<<endl;
  24. return 0;
  25. }

python:

  1. n,m=map(int,input().split())
  2. ans=0
  3. if m%n:print(-1);exit()
  4. else:
  5. x=m//n
  6. while x%3==0:ans+=1;x//=3
  7. while x%2==0:ans+=1;x//=2
  8. if x!=1:print(-1);exit()
  9. print(ans)

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU_2580 A simple stone game

    刚开始看这一题时,就知道这根本不是一道简单题(对当时没学K倍动态减法的我来说),因为前几天刚做完一道斐波那契额数列的博弈而且它仅仅是这道题k=2的一个特例而已-\_-|||。

    相关 HDU(1851) A Simple Game (博弈)

    任给N堆石子,两人轮流从任一堆中任取(每次只能取自一堆),规定每方每次最多取K颗,取最后一颗石子的一方获胜.问先取的人如何获胜? 巴什博奕和尼姆博弈的综合。 令Bi=Mi