Game 23
Polycarp plays "Game 23". Initially he has a number nn and his goal is to transform it to m. In one move, he can multiply n by 22 or multiply n by 33. 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
120 51840
7
42 42
0
48 72
-1
Note
In the first example, the possible sequence of moves is: 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 0.
In the third example, it is impossible to transform 48 to 72.
思路:比较尴尬的一道题,偷看题解不下心被抓到(关键是代码啥也没看到,倒霉的一天),比赛完后才发现网上题解代码千篇一律,思路就是先将m%n如果不等于零直接输出 -1,否则继续往下进行让m/n此时m/n的值应该都是由2 或者是 3乘起来组成(其实不然),那么就继续对其%2或者是%3求进行的次数,但是有一个例外情况就是当n=1时,例如n=1,m=5,此时会进入一个死循环,所以应该加一个判断条件就是当n%2而且n%3都不等于0时候直接令x = -1然后break终止循环。(中间n%6是因为2x3 = 6,实际上可加可不加,所以注释掉)
#include<cstdio>
using namespace std;
int main()
{
long long a,b; a代表题中n,b代表题中m。
while(~scanf("%lld%lld",&a,&b)){
long long x = 0; //x记录执行的次数
if(b%a != 0){
printf("-1\n");
}
else{
b /= a;
while(b != 1){
/*if(b%6 == 0){ // 2x3 = 6,可以直接留下光剩下2或者3的情况,也可以不加,直接忽略
b /= 6;
x += 2;
}*/
if(b%3 == 0){
b /= 3;
x++;
}
else if(b%2 == 0){
b /= 2;
x++;
}
else{ //例如 1 5这种数据在前面b/a的时候无法筛掉,所以在这直接break然后跳出
x = -1;
break;
}
}
printf("%lld\n",x);
}
}
return 0;
}
听说这题可以用搜索,不过我不知道怎么做
Game 23的更多相关文章
- Java开发中的23种设计模式详解
[放弃了原文访问者模式的Demo,自己写了一个新使用场景的Demo,加上了自己的理解] [源码地址:https://github.com/leon66666/DesignPattern] 一.设计模式 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator
CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator 我还没有用过Compute Shader,所以现在把红宝书里的例子拿来了,加入CSharpGL中. ...
- ABP(现代ASP.NET样板开发框架)系列之23、ABP展现层——异常处理
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之23.ABP展现层——异常处理 ABP是“ASP.NET Boilerplate Project (ASP.NET ...
- Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- C#得到某月最后一天晚上23:59:59和某月第一天00:00:00
项目需求: 某学校订单截止操作时间的上一个月最后一天晚上23:59:59 为止所有支付的订单统计: 代码: /// <summary> /// 通过学校和截止时间得到订单 /// < ...
- C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试
在上篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及对应的对象模型,本篇继续微信小店的主题,介绍其中API接口的封装和测试使用.微信小店的相 ...
- [转载]IIS7报500.23错误的解决方法
原文出处: 原文作者:pizibaidu 原文链接:http://pizibaidu.blog.51cto.com/1361909/1794446 背景:今天公司终端上有一个功能打开异常,报500错误 ...
- [MySQL Reference Manual] 23 Performance Schema结构
23 MySQL Performance Schema 23 MySQL Performance Schema 23.1 性能框架快速启动 23.2 性能框架配置 23.2.1 性能框架编译时配置 2 ...
- Error:failed to find Build Tools revision 23.0.0 rc3
解决,选择AS里有的版本就可以了,已有的我这就一个23.0.3,导入的项目是23.0.2 Donate:)
随机推荐
- 001 Lua相关链接
Lua官网:http://www.lua.org/ Lua for windows地址:http://www.lua.org/download.html Lua教程:http://www.runoob ...
- APICloud学习第二天——操作云数据库
//连接apicloud云数据库 var model=api.require('model'); model.config({ appId: 'A6008558346855', appKey: '60 ...
- jquery 中 this 的范围
jquery 中的 this button 通常会存在 onclick;// input[type='file'] 会存在 onchange 事件 eg: onclick = " del ...
- ArcGis使用字段别名Alias Name导出Excel
在ArcMap10.3+(根据官网描述应该是,作者测试使用10.5,可行)以后的版本,可以使用ArcToolbox工具导出Excel. 工具位置ConversionTools——Excel——Tabl ...
- [Reinforcement Learning] 马尔可夫决策过程
在介绍马尔可夫决策过程之前,我们先介绍下情节性任务和连续性任务以及马尔可夫性. 情节性任务 vs. 连续任务 情节性任务(Episodic Tasks),所有的任务可以被可以分解成一系列情节,可以看作 ...
- 【nginx】中server配置说明
server { listen 80; //监听的端口号 server_name localhost; //用域名方式访问的地址 #charset koi8-r; //编码 #access_log / ...
- 我们来说说self.setinterval
学了js的你,肯定知道setInterval方法是按照指定的周期(以毫秒计)来调用函数或计算表达,setInterval方法会不停地调用函数,直到clearInterval被调用或窗口被关闭,这个se ...
- java学习笔记02-Eclipse IDE配置
Eclipse是java的ide工具,ide是集成开发工具.实际开发中都需要使用ide工具.eclipse下载 java代码在运行时,是需要先进行编译,然后才能运行.可以直接在IDE工具里运行,它会帮 ...
- 【easy-】437. Path Sum III 二叉树任意起始区间和
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- MySQL数据优化
很多企业,可能每天应对的数据量达百万,千万,甚至上亿的访问量,这样的量已经超过普通配置的mysql所承受的量,所以为了应对日益增长的访问量,我们需要对mysql做出相应的对策,进一步优化mysql以达 ...