首页 技术 正文
技术 2022年11月16日
0 收藏 742 点赞 2,319 浏览 2386 个字

B. Fox Dividing Cheesetime 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.

Examplesinput

15 20

output

3

input

14 8

output

-1

input

6 6

output

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898