B. Fox Dividing Cheese

两只小熊找到两块重量不同的奶酪,一只狐狸提出通过特定的吃法使两块奶酪重量相等。本文探讨如何用最少的操作步骤实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 
 

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

struct part
{
	int qq;
int q1,q2,q3;
};


part get_part(int a)
{
	part temp;
int tmp=0;
while(a%2==0) 
{
	tmp++;
	a=a/2;
}
temp.q1=tmp;
tmp=0;
while(a%3==0) 
{
	tmp++;
	a=a/3;
}
temp.q2=tmp;

 tmp=0;

while(a%5==0) 
{
	tmp++;
	a=a/5;
} 
temp.q3=tmp;
temp.qq=a;

	return temp;
}

int main()
{
	int a,b;
	 
scanf("%d%d",&a,&b);
part aa=get_part(a);
part bb=get_part(b);

if (aa.qq!=bb.qq) 
{
	printf("-1\n");
 
}
else
{
	printf("%d\n",abs(aa.q1-bb.q1)+abs(aa.q2-bb.q2)+abs(aa.q3-bb.q3));
}

/*	printf("%d\n",abs(aa.q1-bb.q2) );
		printf("%d\n",abs(aa.q2-bb.q2));
				printf("%d\n",abs(aa.q3-bb.q3));
	 */


	return 0;
	
} 

理解了就很好做了。。。可惜之前没理解。。解析   据说还可以“

第2题BFS,搜索规模取决于a和b含有的2,3,5的个数,不超过32个。


”一时想不出

371B - Fox Dividing Cheese

It is easy to see that the fox can do three type of operations: divide by 2, divide by 3 and divide by 5. Let’s write both given numbers in form a = x·2a2·3a3·5a5b = y·2b2·3b3·5b5, where x and y are not dibisible by 2, 3 and 5. If x ≠ y the fox can’t make numbers equal and program should print -1. 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 2 in the same power in both numbers, |a3 - b3| is the minimal number of operations to have 3 in the same power in both numbers, and|a5 - b5| is the same for 5.

### Dividing 在编程和技术领域中的含义和用法 在编程和技术领域,“dividing”通常指代一种操作或过程,具体意义取决于其使用的场景。以下是几个常见的解释及其应用: #### 1. 数学运算中的除法 (Division) “Dividing”最常见的是表示数学上的除法操作,在大多数编程语言中通过 `/` 或 `//` 符号实现。例如,在 Python 中: ```python result = 10 / 3 # 结果为浮点数 3.333... integer_result = 10 // 3 # 结果为整数 3 ``` 这种操作广泛用于数值计算、比例分配以及单位转换等场合[^1]。 #### 2. 数据分割 (Data Partitioning) 在数据处理和机器学习领域,“dividing”可以指将数据集划分为训练集、验证集和测试集的过程。这一步骤对于模型评估至关重要。例如: ```python from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) ``` 上述代码展示了如何将特征矩阵 `X` 和目标向量 `y` 划分到不同的子集中[^2]。 #### 3. 图像处理中的像素划分 在计算机视觉领域,“dividing”可能涉及对图像进行区域划分或逐像素访问的操作。例如,使用 OpenCV 可以遍历图像的每个像素并执行特定逻辑: ```python import cv2 image = cv2.imread('example.jpg') for i in range(image.shape[0]): for j in range(image.shape[1]): pixel = image[i, j] # 对像素进行某种变换 ``` 此方法适用于需要单独分析或修改每个像素的情况。 #### 4. 资源分配与负载均衡 在网络通信或分布式系统设计中,“dividing”也可能代表资源分配或任务分解的概念。比如,为了防止区块链交易中的溢出漏洞,开发者会仔细规划智能合约内的变量范围和边界条件[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值