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.

Examples
input
15 20
output
3
input
14 8
output
-1
input
6 6
output
0

其实这就是一道数学题,题意可以看成“给两个数a,b,让它们除以2,3,5,最后相等,最少除几次”。

  那么我们先求出他们的最大公约数c(因为要出的次数尽可能小),再看从原数到C分别用几步,或者能否除到C。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<math.h>
using namespace std;
int a,b,ans;
int maxyin;
void chu(int a,int b)
{
int aa=a%b;
if(aa==) maxyin=b;
else chu(b,aa);
}
int main()
{
cin>>a>>b;
int c;
if(b>a)
{
c=a;
a=b;
b=c;
} if(a==b)//特判
{
cout<<;
return ;
}
if(a%b==)//特潘
{
c=a/b;
while(c%==)
c/=,ans++;
while(c%==)
c/=,ans++;
while(c%==)
c/=,ans++;
if(c==)
cout<<ans;
else cout<<-;
return ;
}
chu(a,b);
c=a/maxyin;int d =b/maxyin;
if(((c%)&&(c%)&&(c%))||((d%)&&(d%)&&(d%)))
{
cout<<-;
return ;
}
ans=;
while(c%==)
c/=,ans++;
while(c%==)
c/=,ans++;
while(c%==)
c/=,ans++;
while(d%==)
d/=,ans++;
while(d%==)
d/=,ans++;
while(d%==)
d/=,ans++;
cout<<ans;
return ;
}

来个好看的代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<math.h>
using namespace std;
int a,b;
int a2,a3,a5,b2,b3,b5;
int x;
void gcd(int a,int b)
{
int aa=a%b;
if(aa==) {x=b;return ;}
else gcd(b,aa);
}
int main()
{
cin>>a>>b;
if(a==b)
{
cout<<;
return ;
}
gcd(a,b);
int m=a/x,n=b/x;
while(m%==) m/=,a3++;
while(m%==) m/=,a2++;
while(m%==) m/=,a5++;
while(n%==) n/=,b3++;
while(n%==) n/=,b2++;
while(n%==) n/=,b5++;
if(m*n==)
{
cout<<(a3+a2+a5+b2+b3+b5);
return ;
}
cout<<-;
return ;
}

下面是个搜索的

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
int a,b;
struct ab{
int a;int b;
int ans;
}k;
queue<ab>q;
int f,ans;
void bfs()
{
ab g,n;
g=q.front();q.pop();
while(g.a!=g.b)
{
f=;
if(g.a>g.b)
{
n=g;
if(g.a%==)
{ n=g;
n.ans++;
n.a=g.a/;
q.push(n);
f=;
}
if(g.a % ==)
{ n=g;
n.ans++;
n.a=g.a/;
q.push(n);
f=;
}
if(g.a %==)
{ n=g;
n.ans++;
n.a=g.a/;
q.push(n);
f=;
}
}else
{ if(g.b%==)
{ n=g;
n.ans++;
n.b=g.b/;
q.push(n);
f=;
}
if(g.b % ==)
{ n=g;
n.ans++;
n.b=g.b/;
q.push(n);
f=;
}
if(g.b %==)
{ n=g;
n.ans++;
n.b=g.b/;
q.push(n);
f=;
}
}
g=q.front();ans=g.ans;
q.pop();
if(!f)
return ;
} }
int main( )
{
cin>>k.a>>k.b;
if(k.a==k.b)
{
cout<<;
return ;
}
k.ans=;
q.push(k);
bfs();
if(!f){
cout<<-;
return ;
}else
cout<<ans;
return ;
}

Codeforces 371BB. Fox Dividing Cheese的更多相关文章

  1. Codeforces 371B Fox Dividing Cheese(简单数论)

    题目链接 Fox Dividing Cheese 思路:求出两个数a和b的最大公约数g,然后求出a/g,b/g,分别记为c和d. 然后考虑c和d,若c或d中存在不为2,3,5的质因子,则直接输出-1( ...

  2. codeforces 371B - Fox Dividing Cheese

    #include<stdio.h> int count; int gcd(int a,int b) { if(b==0) return a;     return gcd(b,a%b); ...

  3. Codeforces Round #218 (Div. 2) B. Fox Dividing Cheese

    B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. CF 371B Fox Dividing Cheese[数论]

    B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. cf B. Fox Dividing Cheese

    http://codeforces.com/contest/371/problem/B #include <cstdio> #include <iostream> #inclu ...

  6. CodeForces 388A Fox and Box Accumulation (模拟)

    A. Fox and Box Accumulation time limit per test:1 second memory limit per test:256 megabytes Fox Cie ...

  7. Codeforces 388C Fox and Card Game (贪心博弈)

    Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her fr ...

  8. codeforces 510B. Fox And Two Dots 解题报告

    题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...

  9. Codeforces 388A - Fox and Box Accumulation

    388A - Fox and Box Accumulation 思路: 从小到大贪心模拟. 代码: #include<bits/stdc++.h> using namespace std; ...

随机推荐

  1. 自己实现c++中string 类

    class String { public: String(const char *str = NULL);// 普通构造函数 String(const String &other);// 拷 ...

  2. Azure SQL Database (27) 创建Table Partition

    <Windows Azure Platform 系列文章目录> 昨天客户正好提到这个问题,现在记录一下. 我们在使用传统的SQL Server,会使用Table Partition,这个功 ...

  3. 级联MobileNet-V2实现CelebA人脸关键点检测(转)

    https://blog.csdn.net/u011995719/article/details/79435615

  4. 三、mysql登录详解及版本号查询

    1.用window+r,输入cmd,用mysql -uuser -ppassword登录时出现‘mysql’不是有效的内部命令? 答:这是因为没有配置MySQL的环境变量path所致. MySQL的环 ...

  5. 爬虫库之BeautifulSoup学习(一)

    Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. 官方解释如下: Beautiful Soup提供一些简单的.pytho ...

  6. JAVA实现DIJKSTRA算法

    import java.util.Queue; import java.util.LinkedList; public class dijkstra{ public static void main( ...

  7. E20190215-mt

    parenthesis n. 圆括号; 插入语; 插入成分; 间歇; (parentheses) individual  adj. 个人的; 个别的; 独特的;    n. 个人; 个体; priva ...

  8. 洛谷 - P1829 - Crash的数字表格 - 莫比乌斯反演

    求: \(S(n,m)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}lcm(i,j)\) 显然: \(S(n,m)=\sum\limits_{i=1}^{n}\ ...

  9. CodeForces 665B 【水-暴力】

    题意(来自网络): 现在有k件商品,每个商品的位置已经告诉你了 现在有n个人,每个人有m个需求,每个需求就是要把第a[i][j]个物品拿到第一个位置来 他的代价是pos[a[i][j]] 问你所有代价 ...

  10. java日期时间处理集合

    本文主要介绍java中日期时间的处理,包括获取时间,时间相加减,格式化等操作. 持续更新中... 时间格式化 //时间格式化 SimpleDateFormat dateFormat = new Sim ...