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:)
随机推荐
- OS + CentOS / windows / xrdp / vnc
s 通过windows远程访问linux桌面的方法(简单) https://www.cnblogs.com/lizhangshu/p/9709531.html https://dl.fedorapro ...
- 03-oracle中的高级查询
1.连接查询 1.1 使用连接谓词指定的连接 介绍: 在连接谓词表示形式中,连接条件由比较运算符在WHERE子句中给出,将这种表示形式称为连接谓词表示形式,连接谓词又称为连接条件. 语法: [< ...
- postgreSql 基本操作总结
0. 启动pgsl数据库 pg_ctl -D /xx/pgdata start 1. 命令行登录数据库 1 psql -U username -d dbname -h hostip -p po ...
- js 遍历集合删除元素
js 遍历集合删除元素 /** * 有效的方式 - 改变下标,控制遍历 */ for (var i = 0; i < arr.length; i++) { if (...) { arr.spli ...
- 1. Ansible 简介
目录 1. Ansible 是什么? 2. Ansible 特性 3. 控制主机需求 4. 被管理节点需求 1. Ansible 是什么? Ansible 是一个配置管理系统(configuratio ...
- day22 栈 , 队列 , 约束和反射
#!/usr/bin/env python# -*- coding:utf-8 -*- # 1.请使用面向对象实现栈(后进先出)"""class Account: def ...
- JavaScript数据类型 Math对象详解
前言 javascript使用算术运算符实现基本的算术运算,如果要实现更加复杂的算术运算,需要通过Math对象定义的常量和函数来实现.和其他对象不同,Math只是一个静态对象,并没有Math()构造函 ...
- 查找修补文件差异diff、patch
diff patch -p1 diff -Naur directory1 directory2
- 关于selenium的8种元素定位
selenium中有八种元素定位,分别是:id,name,class_name,tag_name,link_text.partial_link_text.xpath.css 简单的定位可以用 id.n ...
- Python 高级特性之:生成器(generator)和迭代器(Iterator)
前言: 之前学习Python自动化,接触了不少python的学习,对生成器印象尤其深,网上也看了很多介绍,下面主要是这些概念的个人学习整理(如侵删). 正文: 如要创建一个非常大的列表,受到内存限制, ...