#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 ...
随机推荐
- 查看运行中的Java其配置的堆大小
一.背景 有题目中的需求,也不是空穴来风:前一阵给公司搭建了一个持续集成服务器,Jenkins.最近发现,运行一段时间后,就变慢了. 随便一个操作,cpu就飙高了.然后就思考会不会是内存不够用,频繁G ...
- hihoCoder挑战赛28 题目1 : 异或排序
题目1 : 异或排序 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个长度为 n 的非负整数序列 a[1..n] 你需要求有多少个非负整数 S 满足以下两个条件: ...
- Junit4单元测试报错
转载博客:http://www.cnblogs.com/sxdcgaq8080/p/5649819.html 今天是用JUnit测试一段代码,报错method initializationerror ...
- calloc(), malloc(), realloc(), free(),alloca()
内存区域可以分为栈.堆.静态存储区和常量存储区,局部变量,函数形参,临时变量都是在栈上获得内存的,它们获取的方式都是由编译器自动执行的. 利用指针,我们可以像汇编语言一样处理内存地址,C 标准函数库提 ...
- Cocoa Touch框架
iOS – Cocoa Touch简介: iOS 应用程序的基础 Cocoa Touch 框架重用了许多 Mac 系统的成熟模式,但是它更加专注于触摸的接口和优化.UIKit 为开发者提供了在 iOS ...
- 有哪些sql优化工具
https://www.oschina.net/p/soar-xiaomi https://www.oschina.net/news/101034/xiaomi-opensource-soar SOA ...
- mui---子页面调用父页面的自定义方法
目前在开发APP的时候,有这样的一个需求:需要在登录页面返回后能够刷新父页面. 功能是这样的:在 A.html页面有头像和用户昵称,这些信息需要用户进行登录才能够拿到,登录页面是在B.html,点击A ...
- Spark2 文件处理和jar包执行
上传数据文件 mkdir -p data/ml/ hadoop fs -mkdir -p /datafile/wangxiao/ hadoop fs -ls / hadoop fs -put /hom ...
- Python面向对象之字段
类成员: 字段,方法,属性 属性是在一个函数方法前面加一个装饰器,伪造成为一个字段,调用的时候不需要加括号() 目的是把一个方法伪造成为一个字段,属性也是方法的一种,对这个方法进行改造就可以以 字段的 ...
- HOJ-1005 Fast Food(动态规划)
Fast Food My Tags (Edit) Source : Unknown Time limit : 3 sec Memory limit : 32 M Submitted : 3777, A ...