Codeforce Round #218 Div2 B
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 #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 }
浙公网安备 33010602011771号