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. linux下的chmod,chown和chgrp

    对于linux的权限掌握以下几个命令就可以非常熟练的操作系统中的各种权限了. 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode f ...

  2. 带Cookie的 WebClient

    /// <summary> /// WebClient的扩展 /// </summary> public class webClient : WebClient { /// & ...

  3. 替换、恢复Html中的特殊字符

    public static string HtmlEncode(string theString){theString = theString.Replace(">", &q ...

  4. bzojj1764: [Baltic2009]monument

    Description 给一个p*q*r的立方体,它由p*q*r个1*1*1的小立方体构成.每个立方体要么被虫蛀,要么不被.现在郑爽要选出一个a*a*b的立方体(方向任意),使得它没有被虫蛀过,并且4 ...

  5. C#全角和半角转换

    在计算机屏幕上,一个汉字要占两个英文字符的位置,人们把一个英文字符所占的位置称为"半角",相对地把一个汉字所占的位置称为"全角".在汉字输入时,系统提供&quo ...

  6. div+css之清除浮动

    当元素有浮动属性时,会对其父元素或后面的元素产生影响,会出现一个布局错乱的现象,可以通过清除浮动的方法来解决浮动的影响. 浮动的清理(clear): 值:none:默认值.允许两边都可以有浮动对象:l ...

  7. (XAML)"XXXX" does not exist in the namespace "clr-

    Error 139 Assembly 'System.Activities.Core.Presentation' was not found. Verify that you are not miss ...

  8. windows server 2012将计算机、回收站、文档等图标添加到桌面

    rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0

  9. Hbase与hive整合

    //hive与hbase整合create table lectrure.hbase_lecture10(sname string, score int) stored by 'org.apache.h ...

  10. sublime代码片段功能

    tools - > developer - > new snippet有了这个,你就可以通过iu这两个键,直接弄出自己的模板了,这就很方便了,不用在复制粘贴了.上是代码模板,下面是快捷键, ...