B. Fox Dividing Cheese
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That's where the fox comes in and starts the dialog: "Little bears, wait a little, I want to make your pieces equal" "Come off it fox, how are you going to do that?", the curious bears asked. "It's easy", said the fox. "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I'll eat a little here and there and make the pieces equal".

The little bears realize that the fox's proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal.

Input

The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109).

Output

If the fox is lying to the little bears and it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cheese are initially equal, the required number is 0.

Sample test(s)
input
15 20
output
3
input
14 8
output
-1
input
6 6
output
0

挺好的一个题目,思想简单。

#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int inf = ;
map<int ,int> meA ,meB ;
map<int,int>::iterator it ;
set<int>st ;
void dfs(int x,int step,map<int,int> &me){
if(me.find(x) != me.end()){
if(me[x] > step)
me[x] = Min(me[x],step) ;
else
return ;
}
else
me[x] = step ;
for(int i = ; i <= ; i++){
if(i == )
continue ;
if(x%i == )
dfs(x/i,step+,me) ;
}
} int main(){
int ans ,a ,b ;
while(cin>>a>>b){
meA.clear() ;
meB.clear() ;
st.clear() ;
dfs(a,,meA) ;
dfs(b,,meB) ;
ans = inf ;
for(it = meA.begin() ; it != meA.end() ; it++)
st.insert(it->first) ;
for(it = meB.begin() ; it != meB.end() ; it++){
int x = it->first ;
if(st.find(x) != st.end()){
ans = Min(ans , meA[x] + meB[x]) ;
}
}
if(ans == inf)
cout<<"-1"<<endl ;
else
cout<<ans<<endl ;
}
return ;
}

Codeforces Round #218 (Div. 2) B. Fox Dividing Cheese的更多相关文章

  1. 二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers

    题目传送门 /* 题意:一个汉堡制作由字符串得出,自己有一些原材料,还有钱可以去商店购买原材料,问最多能做几个汉堡 二分:二分汉堡个数,判断此时所花费的钱是否在规定以内 */ #include < ...

  2. Codeforces Round #290 (Div. 2) D. Fox And Jumping dp

    D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...

  3. Codeforces Round #290 (Div. 2) C. Fox And Names dfs

    C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...

  4. Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...

  5. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  6. Codeforces Round #218 (Div. 2)

    500pt, 题目链接:http://codeforces.com/problemset/problem/371/A 分析:k-periodic说明每一段长度为k,整个数组被分成这样长度为k的片段,要 ...

  7. Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模

    E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈

    C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...

  9. Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造

    B. Fox and Minimal path 题目连接: http://codeforces.com/contest/388/problem/B Description Fox Ciel wants ...

随机推荐

  1. js/jquery 操作document对象

    一.获取对象 //js获取的是dom对象,jquery获取的是jquery对象 //jquery对象可以输出dom对象,索引方式输出dom对象,eq()[]方式输出dom对象;eq()输出jquery ...

  2. 程序员书单_UML篇

    UML基础与Rose建模教程 http://download.csdn.net/detail/shenzhq1980/9076199 UML和模式应用1 Applying UML and Patter ...

  3. DIV的垂直居中

    一直以来都没有找到一种完美解决DIV垂直居中的方法,今天终于找到了,特记录如下,从此不再用table来居中了.^^ <html> <style> #image{ width:5 ...

  4. 【LVM】LVM自动扩容脚本

    概要说明: /dev/mapper/vg0-data挂载在目录/data下: 当新增物理磁盘时,需要自动对/dev/mapper/vg0-data磁盘时进行扩容(自动化脚本): 当新增物理磁盘时,自动 ...

  5. Nginx/LVS/HAProxy负载均衡软件的优缺点详解(转)

    PS:Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,参考了一些资料,结合自己的一些使用经验,总结一下. 一般对负载均衡的使用是随着网站规模的提升根据不 ...

  6. android学习笔记19——对话框(DatePickerDialog、TimePickerDialog)

    DatePickerDialog.TimePickerDialog ==> DatePickerDialog.TimePickerDialog功能.用法都比较简单,操作步骤: 1.通过new关键 ...

  7. framework 安装出错 1603

    安装frame work 3.5的时候老是出现 1603错误. 百度了一圈,各种方法都试了,仍不行. 像: 1.打开临时目录看安装日志,然后修改注册表Main的权限. 2.停止服务Cryptograp ...

  8. [dts]Device Tree格式解析

    转自:http://blog.csdn.net/airk000/article/details/21345159 目录: 1. 作用 2. 基本数据格式 3. 一些基本概念 4. 工作方式 a. 地址 ...

  9. jquery ajax 总是还未等到success回调就刷掉了,就进入了onError函数的错误案例分析

    jquery ajax 总是还未等到success回调就刷掉了,就进入了onError函数的错误案例分析: 同样的请求同时请求了2次,然后第二次的请求把第一次的给刷掉了! (比如:<div on ...

  10. Java的最大优势还是跨平台么?

    之前码农界有码农说Java的最大优势是跨平台,真的是这样么?其实当时网络没这么火爆的时候,确实是这样的,但现在这已然不是了. 跨平台还是Java的最大优势么? 有跨平台需求的仅仅是客户端应用,而不是服 ...