#C++初学记录(算法2)
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)的更多相关文章
- #C++初学记录(算法4)
A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...
- #C++初学记录(贪心算法#结构体#贪心算法)
贪心算法#结构体 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋 ...
- #C++初学记录(算法效率与度量)
时间性能 算法复杂性函数: \[ f(n)=n^2 +1000n+\log_{10}n+1000 \] 当n的数据规模逐渐增大时,f(n)的增长趋势: 当n增大到一定值以后,计算公式中影响最大的就是n ...
- #C++初学记录(贪心算法#二分查找)
D - Aggressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 < ...
- #C++初学记录(算法考试1)
B - Maximal Continuous Rest Each day in Berland consists of n hours. Polycarp likes time management. ...
- #C++初学记录(算法3)
C - 不要62 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司 ...
- #C++初学记录(算法测试2019/5/5)(深度搜索)
深度搜索:Oil Deposits GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每 ...
- #C++初学记录(sort函数)
sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...
- #C++初学记录(动态规划(dynamic programming)例题1 钞票)
浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...
随机推荐
- ubuntu 加扩展网卡遇到网卡无法识别
原创文章,如转载请注明出处 ============================ 今天在安装扩展网卡的时候出现了问题,ubuntu和Centos的网络配置是不一样的. ubuntu的配置: ...
- IIS "rewrite.dll failed to load. The data is the error." 错误解决方法
在Windows 10 build 17133.73上部署一个较老版本的ASP.NET 2.0程序,访问时候出现: Service Unavailable HTTP Error 503. The se ...
- [工具] Dienstag
Dienstag 是一款专门用来进行团队排班的应用,无论是正常班.倒班,只需要几分钟就能生成一目了然的排班表,能够显示休假与缺勤.职务以及详尽的统计功能,并且能将排班情况导入系统日历.@Appinn ...
- 爬取博主的所有文章并保存为PDF文件
继续改进上一个项目,上次我们爬取了所有文章,但是保存为TXT文件,查看不方便,而且还无法保存文章中的代码和图片. 所以这次保存为PDF文件,方便查看. 需要的工具: 1.wkhtmltopdf安装包, ...
- Java Mail 发送邮件(SSL加密方式,TSL加密方式)
一.一般配置 发送邮件需要用到 mail包 maven 依赖如下: <!-- https://mvnrepository.com/artifact/javax.mail/mail --> ...
- 9.12DjangoORM回顾和路由.
2018-9-12 13:44:41 周末继续整理一下博客!不知不觉记了好多! 越努力越幸运! 永远不要高估自己! 关于反射的复习 # /usr/bin/env python # -*- coding ...
- Numpy基础学习与总结
Numpy类型学习 1.数组的表示 import numpy as np In [2]: #numpy核心是高维数组,库中的ndarray支持多维数组,同时提供了数值运算,可对向量矩阵进行运算 In ...
- Oracle SQL之 序列使用限制
Restrictions on Sequence Values You cannot use CURRVAL and NEXTVAL in thefollowing constructs:■ A su ...
- ionic 相关问题解决办法记录
1.npm无法安装模块 报code E400错误 解决办法: 删除npmrc文件. 注意:不是nodejs安装目录npm模块下的那个npmrc文件,而是在C:\Users\{账户}\下的.npmrc文 ...
- idea启动tomcat后访问项目报java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
一.报错“java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet” 1.File --- ...