A - Game 23

Polycarp plays "Game 23". Initially he has a number n and his goal is to transform it to m. In one move, he can multiply n by 2 or multiply n by 3. He can perform any number of moves.

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

It is easy to prove that any way to transform n to m 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 n and m (1≤n≤m≤5⋅108).

Output

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

Examples

Input

120 51840

Output

7

Input

42 42

Output

0

Input

48 72

Output

-1

Note

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

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

In the third example, it is impossible to transform 48 to 72.

正确代码

#include<cstdio>
int res, num = 0,flag = 0, n, m;
void getRes(int n, int m)
{
if(n == m)
{
res = num;
flag = 1;
return;
}
if(n > m)return;
num++;
getRes(n * 2, m);
getRes(n * 3, m);
num--;
}
int main()
{
scanf("%d %d", &n, &m);
getRes(n, m);
if(flag)
{
printf("%d\n", res);
}
else
{
printf("-1\n");
}
return 0;
}

代码理解

解决这个问题首要想到的是使用递归函数进行运算,题目较为简单,但是实际解决时遇到问题导致不能AC,其一是因为受汉诺塔的影响使用递归时经常想要从后往前推,其实不然,递归问题由前往后推应该也是我们必须理解并使用的问题。这类题型就使用了递归问题由前向后推,由前向后推更容易理解,由后往前推也可以做出,使用变量num使之当做指示变量,进行几次递归则进行几次加一,递归使用后再进行相减得到原始变量防止运行程序出错。

#C++初学记录(算法2)的更多相关文章

  1. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  2. #C++初学记录(贪心算法#结构体#贪心算法)

    贪心算法#结构体 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋 ...

  3. #C++初学记录(算法效率与度量)

    时间性能 算法复杂性函数: \[ f(n)=n^2 +1000n+\log_{10}n+1000 \] 当n的数据规模逐渐增大时,f(n)的增长趋势: 当n增大到一定值以后,计算公式中影响最大的就是n ...

  4. #C++初学记录(贪心算法#二分查找)

    D - Aggressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 < ...

  5. #C++初学记录(算法考试1)

    B - Maximal Continuous Rest Each day in Berland consists of n hours. Polycarp likes time management. ...

  6. #C++初学记录(算法3)

    C - 不要62 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司 ...

  7. #C++初学记录(算法测试2019/5/5)(深度搜索)

    深度搜索:Oil Deposits GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每 ...

  8. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  9. #C++初学记录(动态规划(dynamic programming)例题1 钞票)

    浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...

随机推荐

  1. sencha touch 在视图中显示一个html页面

    Ext.define('app.view.about.About', { alternateClassName: 'about', extend: 'Ext.Container', xtype: 'a ...

  2. [转]redhat7(centos7) not registered to Red Hat Subscription Management

    [root@controller0 ~]# yum install ntp Loaded plugins: fastestmirror, product-id, search-disabled-rep ...

  3. 【CF662C】Binary Table 按位处理

    [CF662C]Binary Table 题意:给你一个$n\times m$的01网格,你可以进行任意次操作,每次操作是将一行或一列的数都取反,问你最多可以得到多少个1? $n\le 20,m\le ...

  4. 使用COSBench工具对ceph s3接口进行压力测试--续

    之前写的使用COSBench工具对ceph s3接口进行压力测试是入门,在实际使用是,配置内容各不一样,下面列出 压力脚本是xml格式的,套用UserGuide文档说明,如下 有很多模板的例子,在co ...

  5. Unity3D笔记 GUI 二 、实现选项卡一窗口

    实现目标: 1.个性化Box控件 2.新建TextAmount样式 3.新建TextItem样式 一.个性化Windows界面 设置GUI Skin 1.2 部分代码 Rect stateBox = ...

  6. Spring JPA配置讲解

    JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 Spring提供三种方法集成JPA: 1. ...

  7. python的for else组合用法

    如下代码,输入评论,如果评论中含有敏感词则更换成*号,否则正常输入. li = ["老师", "你好", "333", "4444 ...

  8. Java基础之理解封装,继承,多态三大特性

    目录 封装 继承 多态 封装 封装隐藏了类的内部实现机制,可以在不影响使用的情况下改变类的内部结构,同时也保护了数据.对外界而已它的内部细节是隐藏的,暴露给外界的只是它的访问方法. 代码理解 publ ...

  9. ELK之nginx日志使用json格式输出

    json Nginx默认日志输出格式为文本非json格式,修改配置文件即可输出json格式便于收集以及绘图 修改nginx配置文件添加配置,增加一个json输出格式的日志格式 log_format a ...

  10. 【技术开放日】msup携手HPE揭秘全球测试中心背后的12条技术实践

    保证软件产品质量是软件测试永恒的目标. 以控制为出发点的传统IT时代正在快速的向以激活生产力为目的的移动互联时代转变.这不仅是技术的升级,更是思想意识的巨大变革,也对软件技术的发展带来的更高的要求和更 ...