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; ...
随机推荐
- 004-画图神器-graphviz
1 安装及基本使用 1) 下载安装 下载地址 可以下载安装版进行安装或者解压版直接使用 2) 添加系统path 为了能够在dos中使用命令, 需要添加环境变量 默认安装路径为 C:\Program F ...
- 002-Fatal error in launcher: Unable to create process using '""
这个问题出在先安装Python3之后再安装python2, 使用pip安装的时候出现的故障 原因是python3的环境变量写入在了用户的环境变量上 但是一旦安装python2之后, Python会把信 ...
- Python的GIL是什么鬼,多线程性能究竟如何
前言:博主在刚接触Python的时候时常听到GIL这个词,并且发现这个词经常和Python无法高效的实现多线程划上等号.本着不光要知其然,还要知其所以然的研究态度,博主搜集了各方面的资料,花了一周内几 ...
- CQOI2017 部分题解
部分题解是指没写那道算几. BZOJ上目前没有day2的题面D2T2的图. BZOJ4813 小Q的棋盘 显然可以$O(n^2)$DP,然而可以$O(n)$贪心:只有一条从根出发的一条链上的边可以只经 ...
- Mutual information and Normalized Mutual information 互信息和标准化互信息
实验室最近用到nmi( Normalized Mutual information )评价聚类效果,在网上找了一下这个算法的实现,发现满意的不多. 浙江大学蔡登教授有一个,http://www.zju ...
- Linux设备驱动之Kobject、Kset
作者:lizuobin(也是我们兼职的论坛答疑助手) 原文: https://blog.csdn.net/lizuobin2/article/details/51523693 纠结又纠结,虽然看了一些 ...
- 解除win10禁ping方法
在局域网中,有一台电脑安装了win10操作系统,win10电脑连接网络没有问题,就是在其他电脑ping这台windows10电脑时就是ping不通,显示“请求超时.”.虽然这台windows10电脑联 ...
- PHP 时间戳
<?php php 获取今日.昨日.上周.本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime.下面首先还是直奔主题以示例说明如何使用 mktime 获取今日.昨日 ...
- matlab 2017版本修改帮助文档为离线
安装matlab2017a后发现,帮助文档为在线版本,必须关联账号. 经过查询资料,帮助文档默认是使用web, on mathworks.com 可以将帮助文档改为离线版本. 具体修改方法: Pref ...
- sql语句之连表操作
内连接 select * from employee inner join department on employee.dep_id = department.id 左连接 在内连接的基础上保留左表 ...