Codeforces 371BB. Fox Dividing Cheese
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
其实这就是一道数学题,题意可以看成“给两个数a,b,让它们除以2,3,5,最后相等,最少除几次”。
那么我们先求出他们的最大公约数c(因为要出的次数尽可能小),再看从原数到C分别用几步,或者能否除到C。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<math.h>
using namespace std;
int a,b,ans;
int maxyin;
void chu(int a,int b)
{
int aa=a%b;
if(aa==) maxyin=b;
else chu(b,aa);
}
int main()
{
cin>>a>>b;
int c;
if(b>a)
{
c=a;
a=b;
b=c;
} if(a==b)//特判
{
cout<<;
return ;
}
if(a%b==)//特潘
{
c=a/b;
while(c%==)
c/=,ans++;
while(c%==)
c/=,ans++;
while(c%==)
c/=,ans++;
if(c==)
cout<<ans;
else cout<<-;
return ;
}
chu(a,b);
c=a/maxyin;int d =b/maxyin;
if(((c%)&&(c%)&&(c%))||((d%)&&(d%)&&(d%)))
{
cout<<-;
return ;
}
ans=;
while(c%==)
c/=,ans++;
while(c%==)
c/=,ans++;
while(c%==)
c/=,ans++;
while(d%==)
d/=,ans++;
while(d%==)
d/=,ans++;
while(d%==)
d/=,ans++;
cout<<ans;
return ;
}
来个好看的代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<math.h>
using namespace std;
int a,b;
int a2,a3,a5,b2,b3,b5;
int x;
void gcd(int a,int b)
{
int aa=a%b;
if(aa==) {x=b;return ;}
else gcd(b,aa);
}
int main()
{
cin>>a>>b;
if(a==b)
{
cout<<;
return ;
}
gcd(a,b);
int m=a/x,n=b/x;
while(m%==) m/=,a3++;
while(m%==) m/=,a2++;
while(m%==) m/=,a5++;
while(n%==) n/=,b3++;
while(n%==) n/=,b2++;
while(n%==) n/=,b5++;
if(m*n==)
{
cout<<(a3+a2+a5+b2+b3+b5);
return ;
}
cout<<-;
return ;
}
下面是个搜索的
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
int a,b;
struct ab{
int a;int b;
int ans;
}k;
queue<ab>q;
int f,ans;
void bfs()
{
ab g,n;
g=q.front();q.pop();
while(g.a!=g.b)
{
f=;
if(g.a>g.b)
{
n=g;
if(g.a%==)
{ n=g;
n.ans++;
n.a=g.a/;
q.push(n);
f=;
}
if(g.a % ==)
{ n=g;
n.ans++;
n.a=g.a/;
q.push(n);
f=;
}
if(g.a %==)
{ n=g;
n.ans++;
n.a=g.a/;
q.push(n);
f=;
}
}else
{ if(g.b%==)
{ n=g;
n.ans++;
n.b=g.b/;
q.push(n);
f=;
}
if(g.b % ==)
{ n=g;
n.ans++;
n.b=g.b/;
q.push(n);
f=;
}
if(g.b %==)
{ n=g;
n.ans++;
n.b=g.b/;
q.push(n);
f=;
}
}
g=q.front();ans=g.ans;
q.pop();
if(!f)
return ;
} }
int main( )
{
cin>>k.a>>k.b;
if(k.a==k.b)
{
cout<<;
return ;
}
k.ans=;
q.push(k);
bfs();
if(!f){
cout<<-;
return ;
}else
cout<<ans;
return ;
}
Codeforces 371BB. Fox Dividing Cheese的更多相关文章
- Codeforces 371B Fox Dividing Cheese(简单数论)
题目链接 Fox Dividing Cheese 思路:求出两个数a和b的最大公约数g,然后求出a/g,b/g,分别记为c和d. 然后考虑c和d,若c或d中存在不为2,3,5的质因子,则直接输出-1( ...
- codeforces 371B - Fox Dividing Cheese
#include<stdio.h> int count; int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); ...
- Codeforces Round #218 (Div. 2) B. Fox Dividing Cheese
B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CF 371B Fox Dividing Cheese[数论]
B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...
- cf B. Fox Dividing Cheese
http://codeforces.com/contest/371/problem/B #include <cstdio> #include <iostream> #inclu ...
- CodeForces 388A Fox and Box Accumulation (模拟)
A. Fox and Box Accumulation time limit per test:1 second memory limit per test:256 megabytes Fox Cie ...
- Codeforces 388C Fox and Card Game (贪心博弈)
Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her fr ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
- Codeforces 388A - Fox and Box Accumulation
388A - Fox and Box Accumulation 思路: 从小到大贪心模拟. 代码: #include<bits/stdc++.h> using namespace std; ...
随机推荐
- nyoj 86 --位标记
nyoj 86 --位标记 点击打开题目链接 : 找球号(一) 这道题目很多解法,其他解法请参考 http://www.cnblogs.com/play ...
- python 基础之第九天
###############错误和异常######################## 说明:e 是错误的具体原因,else 表示没有异常才会执行else的语句,finally 是无乱有没异常都要执 ...
- cuda 版本查阅
查看cuda版本 cat /usr/local/cuda/version.txt nvcc -V
- Win32环境下代码注入与API钩子的实现
本文详细的介绍了在Visual Studio(以下简称VS)下实现API钩子的编程方法,阅读本文需要基础:有操作系统的基本知识(进程管理,内存管理),会在VS下编写和调试Win32应用程序和动态链接库 ...
- 使用svnsync实时备份SVN版本库
前段时间把SVN版本库从win迁移到了Linux上,没隔几天那台win的磁盘就严重坏道了....这TMD什么运气! 花费了点时间研究了下svn自己的同步工具.写个日志记录下. 注意:svnsync要求 ...
- android 收集的一些颜色值
<?xml version="1.0" encoding="utf-8"?> <resources> <color name=&q ...
- Firebug的安装与使用
第一步,点击 Firefox 浏览器上的“工具”选项,然后点击“附加软件”,在弹出的小窗口中,点击右下角的“获取扩展”选项,如图 4 所示. 图 4. 获取扩展 第二步,在点击“获取扩展”选项后,打开 ...
- 1-2关键字 & 2-1什么是变量
2-1什么是变量 驼峰第一个字母首字母小写后面字母首字母大写 Pascal命名规范:
- 668. Kth Smallest Number in Multiplication Table
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
- Withdraw From OI
Withdraw From OI 已经退役一周了,但还是迟迟没有去写退役记,在这个2017年的最后一天,写下这一篇沉重的“withdraw from OI”. 距离联赛成绩出来已经一个多月了.这一个月 ...