• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #218 Div2 B

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

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
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <vector>
 4 #include <stack>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 #define maxn 101000
 9 #define INF 0x7fffffff
10 #define ll long long
11 ll n, m, k, x, y, z, d, t;
12 ll gcd(ll n, ll m){
13     if (m == 0)return n;
14     return gcd(m, n%m);
15 }
16 int main(){
17     scanf("%I64d%I64d", &n, &m);
18     x = gcd(n, m);
19     n /= x; m /= x;
20     int a = 0, b = 0;
21     x = 1; y = 1; z = 1;
22     for (int i = 0; i < 20; i++){
23         if(i)x *= 5;
24         y = 1;
25         for (int j = 0; j < 30; j++){
26             if(j)y *= 3;
27             z = 1;
28             for (int k = 0; k < 50; k++){
29                 if(k)z *= 2;
30                 if (x*y*z == n)a = i + j + k;
31                 if (x*y*z == m)b = i + j + k;
32                 if (a&&b || n == 1 && b || m == 1 && a || n == m)i = 100, j = 100, k = 100;
33             }
34         }
35     }
36     if (a&&b||n==1&&b||m==1&&a||n==m)printf("%d\n", a + b);
37     else printf("-1\n");
38     return 0;
39 }
View Code
posted @ 2013-12-09 12:52  HaibaraAi  阅读(137)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3