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

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

题意是输入a,b;
每次可以把a或b变为原来的二分之一、三分之一或者五分之一;
求改变的最小次数使二者相等。
自己做的时候完全没有思路,看过大神的代码终于明白了是什么意思
思路是这样的:
例如a=15,b=20;
则可以写作:a=3*5;b=2*2*5;
这样就可以很直观的看出,二者有最大公约数(公约数必须是2、3、5乘积)5,除去5还有三个数,因此需要做三次变动。
又如a=36,b=30;
泽可以写作:a=2*2*3*3;b=2*3*5;
可以看出二者有最大公约数(公约数必须是2、3、5乘积)2*3,除去2*3还有三个数,因此需要做三次变动。
由此可以归纳方法:
a=x*pow(2,n1)*pow(3,m1)*pow(5,o1);
b=y*pow(2,n2)*pow(3,m2)*pow(5,o2);
若x!=y则必有a,b不能化为相等
证明:因为x!=y,若要使a,b能够化简相等,必有两个数c,d使得x*c==y*d---------(1);
且c、d均为2、3、5的倍数,则可分析知若要使(1)式成立,必须x/y==d/c;
即x/d==y/c;则必有x必为d的倍数,y必为c的倍数,而这与上述x、y是a、b除去2、3、5的因数所矛盾
故上述得证。
了解思路后自己写的代码如下:
#include <stdio.h>
#include <stdlib.h>
int main(void){
	int a, b;
	while(scanf("%d%d",&a,&b)!=EOF){
		int count=0;
		if (a == b){ 
		 printf("0\n");
	     continue;
		}
		int c[4]={0},d[4]={0};
		while (!(a % 2)) a /= 2, c[0]++;
		while (!(a % 3)) a /= 3, c[1]++;
		while (!(a % 5)) a /= 5, c[2]++;
		while (!(b % 2)) b /= 2, d[0]++;
		while (!(b % 3)) b /= 3, d[1]++;
		while (!(b % 5)) b /= 5, d[2]++;
		if (a != b){
		 printf("-1\n");
	     continue;
		}
		for (int i=0; i<3; i++)
		 count += abs(c[i] - d[i]);
		printf("%d\n",count);
	}
} 

再贴上大神的代码,学习可学习之处:
#include <stdio.h>
#include <stdlib.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Rep(i, n) for (int i = 1; i <= (n); ++i)
#define clr(x, a) memset(x, (a), sizeof x)//这种使用宏的方法值得学习!!! 

int max(int a,int b){
	return a>b?a:b;
}

int min(int a,int b){
	return a>b?b:a;
}

typedef long long ll;//简洁!!! 

int c[10], d[10];
int main(void) {
    int a, b; 
	scanf("%d%d",&a,&b);
    int t = a, y = b;
    while (t % 2 == 0) t /= 2, ++c[0];
    while (t % 3 == 0) t /= 3, ++c[1];
    while (t % 5 == 0) t /= 5, ++c[2];
    while (y % 2 == 0) y /= 2, ++d[0];
    while (y % 3 == 0) y /= 3, ++d[1];
    while (y % 5 == 0) y /= 5, ++d[2];
    if (t != y) puts("-1");//为什么? 
    else {
        int ret = 0;
        rep(i, 3) ret += max(c[i], d[i]) - min(c[i], d[i]);
        printf("%d\n",ret);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值