1 second
256 megabytes
standard input
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.
The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109).
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.
15 20
3
14 8
-1
6 6
0
分蛋糕,可以吃掉剩下1/2,1/3,1/5,求相同的最小次数
官方题解
It is easy to see that the fox can do three type of operations: divide by , divide by and divide by . Let’s write both given numbers in form a = x·2a2·3a3·5a5, b = y·2b2·3b3·5b5, where x and y are not dibisible by , and . If x ≠ y the fox can’t make numbers equal and program should print -. If x = y then soluion exists. The answer equals to |a2 - b2| + |a3 - b3| + |a5 - b5|, because |a2 - b2| is the minimal number of operations to have in the same power in both numbers, |a3 - b3| is the minimal number of operations to have in the same power in both numbers, and |a5 - b5| is the same for .
稍微不同的思路
如果有解,a b除了2,3,5之外的因子一定在最后的相同里,并且2,3,5因子相同也可以不分,那么最后分成的一定是最大公约数
//
// main.cpp
// cf371b
//
// Created by Candy on 9/15/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
using namespace std;
int a,b;
inline int fac(int &x,int p){
int cnt=;
while(x%p==) {x/=p;cnt++;}
return cnt;
}
int main(int argc, const char * argv[]) {
scanf("%d%d",&a,&b);
if(a==b){printf("");return ;} int a2=fac(a,),a3=fac(a,),a5=fac(a,);
int b2=fac(b,),b3=fac(b,),b5=fac(b,);
if(a!=b){printf("-1");return ;} printf("%d",abs(a2-b2)+abs(a3-b3)+abs(a5-b5));
return ;
}