Codeforces Round #218 (Div. 2) B. Fox Dividing Cheese
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
挺好的一个题目,思想简单。
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int inf = ;
map<int ,int> meA ,meB ;
map<int,int>::iterator it ;
set<int>st ;
void dfs(int x,int step,map<int,int> &me){
if(me.find(x) != me.end()){
if(me[x] > step)
me[x] = Min(me[x],step) ;
else
return ;
}
else
me[x] = step ;
for(int i = ; i <= ; i++){
if(i == )
continue ;
if(x%i == )
dfs(x/i,step+,me) ;
}
} int main(){
int ans ,a ,b ;
while(cin>>a>>b){
meA.clear() ;
meB.clear() ;
st.clear() ;
dfs(a,,meA) ;
dfs(b,,meB) ;
ans = inf ;
for(it = meA.begin() ; it != meA.end() ; it++)
st.insert(it->first) ;
for(it = meB.begin() ; it != meB.end() ; it++){
int x = it->first ;
if(st.find(x) != st.end()){
ans = Min(ans , meA[x] + meB[x]) ;
}
}
if(ans == inf)
cout<<"-1"<<endl ;
else
cout<<ans<<endl ;
}
return ;
}
Codeforces Round #218 (Div. 2) B. Fox Dividing Cheese的更多相关文章
- 二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers
题目传送门 /* 题意:一个汉堡制作由字符串得出,自己有一些原材料,还有钱可以去商店购买原材料,问最多能做几个汉堡 二分:二分汉堡个数,判断此时所花费的钱是否在规定以内 */ #include < ...
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs
B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...
- Codeforces Round #290 (Div. 2) A. Fox And Snake 水题
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
- Codeforces Round #218 (Div. 2)
500pt, 题目链接:http://codeforces.com/problemset/problem/371/A 分析:k-periodic说明每一段长度为k,整个数组被分成这样长度为k的片段,要 ...
- Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模
E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈
C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...
- Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造
B. Fox and Minimal path 题目连接: http://codeforces.com/contest/388/problem/B Description Fox Ciel wants ...
随机推荐
- 微信公众号开发笔记(C#)
这篇文章还不错,使用 .net , 对微信用户的想公众号发送的文字进行回复.比较简单,自己可以修改更复杂的回复. 微信公众号开发笔记(C#) 原文地址 需求分析 根据用户在微信上发送至价值中国公众号 ...
- android 组合控件接收不到点击事件的问题
android点击事件的传播是有子控件传给父控件,如果子控件处理过了,父控件不再处理,所以要想让组合控件接收点击事件,必须屏蔽子控件的点击事件. 设置组合控件的clickable和focusable属 ...
- .NET导入openssl生成的公钥之BEGIN RSA PUBLIC KEY
.NET导入openssl生成的公钥之BEGIN RSA PUBLIC KEY 我得到了一个公钥,形式如下 -----BEGIN RSA PUBLIC KEY----- MIGJAoGBAMroxz3 ...
- 清理java环境
system32中存在3个java*.exe文件,分别是: c:/windows/system32/java.exe c:/windows/system32/javaw.exe c:/windows/ ...
- 图像特征提取之(一)HOG特征
1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的 ...
- SSH_框架整合4--添加员工信息
SSH_框架整合4--添加员工信息 一. 1 index.jsp:添加:<a href="emp-input">添加员工向信息:Add Employees' Infor ...
- Centos7安装Zabbix3.0
1.安装服务器端包 #rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch. ...
- android学习笔记39——使用原始资源
原始资源 android中没有专门提供管理支持的类型文件,都被称为原始资源.例如:声音资源... android原始资源存放位置: 1.res/raw,android SDK会处理该目录下的原始资源, ...
- 使用 HTML5 Shiv 让 IE 支持 HTML5
HTML5 Shiv 使用 html5.js 必须在页面head元素内调用(因为 IE 必须在元素解析前知道这个元素,所以这个 JS 文件不能在页面底部调用.) 作者已经把js文件放在Google c ...
- C# Winform中WndProc 函数作用
http://blog.csdn.net/xochenlin/article/details/4328954 C# Winform中WndProc 函数作用: 主要用在拦截并处理系统消息和自定义消息 ...