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. Splashscreen

    Splashscreen Enables developers to show/hide the application's splash screen. Methods show hide Perm ...

  2. startActivityForResult用法详解

    一.如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int requestCode ...

  3. LintCode "Binary Representation"

    Not hard to think of a solution. But the key is all details. class Solution { public: /** *@param n: ...

  4. javascript url 相关函数(escape/encodeURL/encodeURIComponent区别)

    JS获取url参数及url编码.解码 完整的URL由这几个部分构成:scheme://host:port/path?query#fragment ,各部分的取法如下: window.location. ...

  5. MySQL使用Union创建视图报错

    mysql> select * from test_main; +----+-------+ | id | value | +----+-------+ |  1 | ONE   | |  2  ...

  6. c#启动进程

    //System.Diagnostics.Process p = new System.Diagnostics.Process(); //p.StartInfo.FileName = @"C ...

  7. css设置背景图片,ie显示不了

    本来是想给导航栏<div class="nav"></div>添加背景图片的,设置css样式如下: .nav{background:url("ht ...

  8. JAVA使用HBASE常用方法

    package HBaseTest; /** * Created by root on 11/11/22. */ import java.io.IOException; import org.apac ...

  9. IGS_学习笔记01_Integrated SOA Gateway介绍(案例)

    2014-01-01 Created By BaoXinjian

  10. IDC机房网络测试要求

    IDC机房网络质量要求: 我司项目目前覆盖目标机房是国内BGP机房.三线机房.电信机房.联通机房. 要求XXXX IDC机房到目标机房的网络质量,平均丢包≤0.5%,网络延迟≤50ms;高峰期平均丢包 ...