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 ...
随机推荐
- nginx ssi 配置小细节(一)
最近工作需要使用nginx的ssi (server side include)技术,在这里,将使用中的一点心得分享一下,也是一种备忘! 首先,nginx的ssi启用很简单,就只有三个最基本的指令: s ...
- Debian安装记录
Fedora着实让我伤心透了.前天和昨天搞了整整两天Fedora 20的安装,午睡也没有,晚上就睡了四个小时不到,几乎尝试了所有Fedora 20的桌面版本,全部出问题了!就因为我的笔记本显卡是ATI ...
- UIPickerView自定义背景
#import <UIKit/UIKit.h> @interface MyPicker : UIPickerView { } @end -------------------------- ...
- 读书笔记:Sheldon Ross:概率论基础教程:随机变量
例1b 一个坛子里装有编号1-20的球,无放回抽取3个,取出球中至少一个号码大于等于17的概率是多少? 除了书上的解法外,还有一种解法: 考虑相反的情况:三个球的号码都小于17. 第一次从编号1-16 ...
- bzoj4109: [Wf2015]Cutting Cheese
Description 给定一个100*100*100(单位:毫米)的奶酪方块,这个奶酪含有n个球形小孔.现在要求将这个奶酪切成s片使得每片质量相等. Input 第一行包含两个整数n,s,表示奶酪有 ...
- 给windows的VM更换网卡到VMNET3从E1000
1. Login to vCenter via vSphere client. go to the vm 2. create a 1G new disk(SCSI 1:2) for the VM te ...
- android学习笔记37——Menu资源
Menu菜单资源 android应用推荐使用XML来定义菜单,其可提供更好的解耦方式. 菜单资源通常位于res/menu文件夹下,其菜单根元素为<menu.../>,menu元素下可包含子 ...
- 25 个超棒的 HTML5 & JavaScript 游戏引擎开发库
就像在汽车中,引擎完成主要的工作,使汽车看起来不可思议.游戏引擎同理,游戏开发者完成细节的工作,使游戏看起来真实.吸引人眼球.游戏引擎负责其余的事情.早期,游戏开发者通常从草图做起,花费高昂,且不容易 ...
- U3d中实现A*寻路,附源文件
图片看不清楚,请点击看大图 http://pan.baidu.com/s/1pKwmOYn 写了好多,没保存,哎哎哎 空格键开始移动
- [物理学与PDEs]书中一些对数学研究有用的引理
P 35--38 1. 若 ${\bf B}$ 为横场 ($\Div{\bf B}=0\ra {\bf k}\cdot {\bf B}=0\ra $ 波的振动方向与传播方向平行), 则 $$\bex ...