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; ...
随机推荐
- 使用XMLHttpRequest
请求种类 通过XMLHttpRequest的请求可以通过同步和异步的方式获取数据,请求的种类在XMLHttpRequest的open()方法的第三三个可选参数async设置.如果这个参数是true或者 ...
- Vue表格数据增删改查及搜索
<div id="app"> <div class="item"> <span class="name"> ...
- python print 字体颜色
例子: print '\033[35;43m(1)ip转换成数字\033[0m' \033[35;43m ===>35列属于字颜色,43列属于背景颜色 字背景颜色范围: 40--49 4 ...
- BZOJ_2002_[Hnoi2010]Bounce 弹飞绵羊_LCT
BZOJ_2002_[Hnoi2010]Bounce 弹飞绵羊_LCT Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏. ...
- Watir 简化日常工作实例
公司的官方主页要优化搜索任务,一共有110个独立页面,开发工程师做了以下工作:1. 为所有这些页面的每个图片添加了alt:2. 为页面上的每个标题添加了面包屑:3. 为网站最重要的标题设置H1标签,而 ...
- bzoj4589
fwt 原理并不知道 nim游戏石子异或和=0后手赢 那么也就是求a[1]^a[2]^...^a[n]=0的方案数 这个和bzoj3992一样可以dp dp[i][j]表示前i个数异或和为j的方案数 ...
- nginx proxy https
server {listen 443;server_name mail.jb51.net; ssl on;ssl_certificate server.crt;ssl_certificate_key ...
- 《剑指offer》面试题5—从尾到头打印链表
重要思路: 这个问题肯定要遍历链表,遍历链表的顺序是从头到尾,而要输出的顺序却是从尾到头,典型的“后进先出”,可以用栈实现. 注意stl栈的使用,遍历stack的方法. #include <io ...
- 安装APK时SO库的选择策略
此文已由作者尹彬彬授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 0X0 前言 在Android系统中,当我们安装apk文件的时候,lib目录下的so文件会被解压到app的原 ...
- js引用类型的赋值
在开发中,有时候需要将数组或者对象的值赋予其他另一个变量,但是两个变量之间会相互影响,因为在将引用类型的值赋给其他变量时,赋予的其实是内存中的存储地址 var arr = [1,2,3,4,5] va ...