CF 371B 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
分蛋糕,可以吃掉剩下1/2,1/3,1/5,求相同的最小次数
官方题解
It is easy to see that the fox can do three type of operations: divide by , divide by and divide by . Let’s write both given numbers in form a = x·2a2·3a3·5a5, b = y·2b2·3b3·5b5, where x and y are not dibisible by , and . If x ≠ y the fox can’t make numbers equal and program should print -. If x = y then soluion exists. The answer equals to |a2 - b2| + |a3 - b3| + |a5 - b5|, because |a2 - b2| is the minimal number of operations to have in the same power in both numbers, |a3 - b3| is the minimal number of operations to have in the same power in both numbers, and |a5 - b5| is the same for .
稍微不同的思路
如果有解,a b除了2,3,5之外的因子一定在最后的相同里,并且2,3,5因子相同也可以不分,那么最后分成的一定是最大公约数
//
// main.cpp
// cf371b
//
// Created by Candy on 9/15/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
using namespace std;
int a,b;
inline int fac(int &x,int p){
int cnt=;
while(x%p==) {x/=p;cnt++;}
return cnt;
}
int main(int argc, const char * argv[]) {
scanf("%d%d",&a,&b);
if(a==b){printf("");return ;} int a2=fac(a,),a3=fac(a,),a5=fac(a,);
int b2=fac(b,),b3=fac(b,),b5=fac(b,);
if(a!=b){printf("-1");return ;} printf("%d",abs(a2-b2)+abs(a3-b3)+abs(a5-b5));
return ;
}
CF 371B 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( ...
- cf B. Fox Dividing Cheese
http://codeforces.com/contest/371/problem/B #include <cstdio> #include <iostream> #inclu ...
- 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 ...
- Codeforces 371BB. Fox Dividing Cheese
Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, corre ...
- CF 980D Perfect Groups(数论)
CF 980D Perfect Groups(数论) 一个数组a的子序列划分仅当这样是合法的:每个划分中的任意两个数乘积是完全平方数.定义a的权值为a的最小子序列划分个数.现在给出一个数组b,问权值为 ...
- CF 510b Fox And Two Dots
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...
- [CF #290-C] Fox And Names (拓扑排序)
题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...
- cf E. Fox and Card Game
http://codeforces.com/contest/389/problem/E 题意:给你n个序列,然后两个人x,y,两个人玩游戏,x从序列的前面取,y从序列的后面取,两个人都想自己得到的数的 ...
随机推荐
- 【Bootstrap】2.作品展示站点
假设我们已经想好了要给自己的作品弄一个在线站点.一如既往,时间紧迫.我们需要快一点,但作品展示效果又必须专业.当然,站点还得是响应式的,能够在各种设备上正常浏览,因为这是我们向目标客户推销时的卖点.这 ...
- iOS上架90034问题解决
开发完成的APP,我们当然要上传到AppStore里面了,这可是我们心血的结晶. 可是,就当我们兴奋之余,却发现我们的App根本无法上传到AppStore.我们百度.谷歌了N种方法,就像我这种逗逼,整 ...
- Android Material design
1.Material Design:扁而不平 2.Android Support Design 库 之 Snackbar使用及源码分析 3.十大Material Design开源项目,直接拿来用!
- Android消息机制入门
接着处理<Android 网络图片查看器>中出现的问题 使用添加子线程,修改原程序: package com.wuyudong.imagesviewer; import java.io.I ...
- iOS 滑动隐藏导航栏-三种方式
/** 1隐藏导航栏-简单- */ self.navigationController.hidesBarsOnSwipe = YES; /** 2隐藏导航栏-不随tableView滑动消失效果 ...
- C语言中的函数与指针
1. 为什么需要函数? 函数就是功能的封装. 函数就是为了实现某个功能而编写的一段代码 scanf() , printf() 2.函数优点: 代码更简洁 代码复用 如果业务逻辑变化,只把相应的 ...
- GitHub 上有哪些完整的 iOS-App 源码值得参考?
1. Coding iOS 客户端 Coding官方客户端. 笔者强烈推荐的值得学习的完整APP.GitHub - Coding/Coding-iOS: Coding iOS 客户端源代码 2. OS ...
- php文件下载
public function down() { $lang = strtolower(cookie('think_language')); if ($lang == 'en-us') { $file ...
- js 模仿块级作用域(私有作用域)、私有变量
function outputNumbers(count){ var privateVariable = 10;//私有/局部变量,函数外部不能被访问 publicVariable = 20;//全局 ...
- PHP操作mysql数据库:[2]查询数据听语音
本文主要详细讲解如何使用php语言,对mysql数据库进行查询.添加.删除.更新等操作. 工具/原料 Macromedia Dreamweaver 8 mysql数据库,php语言 一.前言 ...