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; ...
随机推荐
- Mybatis一二级缓存的理解
频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO操作速度相比内存操作速度慢了好几个量级),尤其是对于一些相同的查询语句,完全可以 ...
- TX2上yolov3精度和速度优化方向
速度优化的方向: 1.减少输入图片的尺寸, 但是相应的准确率可能会有所下降2.优化darknet工程源代码(去掉一些不必要的运算量或者优化运算过程)3.剪枝和量化yolov3网络(压缩模型---> ...
- Batch Normalization层
Batch Normalization的加速作用体现在两个方面:一是归一化了每层和每维度的scale,所以可以整体使用一个较高的学习率,而不必像以前那样迁就小scale的维度:二是归一化后使得更多的权 ...
- C++之pair与make_pair
std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型. 例如std::pair<int,float> 或者 std::pair<double,d ...
- GitHub的使用方法
版本控制系统 > Git 分布式 > Subversion 集中式 1. 安装git: # apt-get install git //root权限 $ sudo apt-get inst ...
- AtCoder Grand Contest 006 C:Rabbit Exercise
题目传送门:https://agc006.contest.atcoder.jp/tasks/agc006_c 题目翻译 数轴上有\(N\)只兔子,从\(1\)到\(N\)编号,每只兔子初始位置是\(x ...
- vue-element el-select value-key
如果select绑定的值为对象,请务必指定value-key为它的唯一性标示 demo: data(){ return{ test:'', arr:[{id:1,name:'张三'},{id:2,na ...
- 【旧文章搬运】关于NtUserBuildHwndList的一点记录~
原文发表于百度空间,2011-04-07========================================================================== 该函数与r ...
- 免证书发布ipa文件真机测试
首先设备得越狱 众所周知,在Xcode上开发的程序只能在模拟器中运行,如果要放到真机上则要花费99美金购买开发者证书iDP.这严重阻碍了我等草根开发者探索的脚步.写个小程序,同学间分享一下这个小小的愿 ...
- 《剑指offer》面试题16—反转链表
Node* p1 p2 p3 思路:开始时,p1为NULL,p2=phead,p3=p2—>next.使p2—>next = p1,然后使p1=p2,p2=p3.如果只有1个结点则此时 ...