Divide by 2 or 3

Problem Statement

You are given a sequence of positive integers: A=(a_1,a_2,\ldots,a_N)A=(a1​,a2​,…,aN​).
You can choose and perform one of the following operations any number of times, possibly zero.

  • Choose an integer ii such that 1 \leq i \leq N1≤i≤N and a_iai​ is a multiple of 22, and replace a_iai​ with \frac{a_i}{2}2ai​​.
  • Choose an integer ii such that 1 \leq i \leq N1≤i≤N and a_iai​ is a multiple of 33, and replace a_iai​ with \frac{a_i}{3}3ai​​.

Your objective is to make AA satisfy a_1=a_2=\ldots=a_Na1​=a2​=…=aN​.
Find the minimum total number of times you need to perform an operation to achieve the objective. If there is no way to achieve the objective, print -1 instead.

Constraints

  • 2 \leq N \leq 10002≤N≤1000
  • 1 \leq a_i \leq 10^91≤ai​≤109
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN
a_1a1​ a_2a2​ \ldots… a_NaN​

Output

Print the answer.


Sample Input 1 Copy

Copy

3
1 4 3

Sample Output 1 Copy

Copy

3

Here is a way to achieve the objective in three operations, which is the minimum needed.

  • Choose an integer i=2i=2 such that a_iai​ is a multiple of 22, and replace a_2a2​ with \frac{a_2}{2}2a2​​. AA becomes (1,2,3)(1,2,3).
  • Choose an integer i=2i=2 such that a_iai​ is a multiple of 22, and replace a_2a2​ with \frac{a_2}{2}2a2​​. AA becomes (1,1,3)(1,1,3).
  • Choose an integer i=3i=3 such that a_iai​ is a multiple of 33, and replace a_3a3​ with \frac{a_3}{3}3a3​​. AA becomes (1,1,1)(1,1,1).

Sample Input 2 Copy

Copy

3
2 7 6

Sample Output 2 Copy

Copy

-1

There is no way to achieve the objective.


Sample Input 3 Copy

Copy

6
1 1 1 1 1 1

Sample Output 3 Copy

Copy

0

题解:要想所有的数能变成同一个数,那它们的因素只可能有2 , 3 以及 它们的最大公因数 ,注意最大公因数也可能是2,3的倍数。

因此: 1.所有数的求最大公因数

            2.数组元素除最大公因数,此时不数组中不可能存在一个元素(不等于1),可以使得数组中每个数都等于该元素.

           3.因此最小的操作次数是除去最大公因数后到1 的操作次数之和。

#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;ll n, p, ans;
ll a[1100];long long gcd(long long a, long long b)
{while (b ^= a ^= b ^= a %= b);return a;
}int main() {cin >> n;for (ll i = 1; i <= n; i++) {cin >> a[i];}p = a[1];for (ll i = 2; i <= n; i++) {p = gcd(a[i], p);//找最大公因数}for (ll i = 1; i <= n; i++) {ll x = a[i] / p;while (x % 2 == 0) {x /= 2;ans++;}while (x % 3 == 0) {x /= 3;ans++;}if (x != 1) {ans = -1;break;}}printf("%lld\n", ans);return 0;
}

Divide by 2 or 3相关推荐

  1. BigDecimal divide方法结果为无限小数问题

    参考:http://mengxiaozhe.iteye.com/blog/763552 10/3=3.3333333333333333.............. Java代码   public st ...

  2. LeetCode Divide Two Integers(不使用乘、除,求模来计算商)

    题意:给出被除数,除数,求商(要求不能用乘,除,求模) 思路:用被除数减去除数,直到小于除数为止.在减的过程中能够做些优化.将除数连续左移直到大于被除数,在这个过程中可以计算是左移后的数是除数的多少倍 ...

  3. [leetcode]Divide Two Integers

    直接用除数去一个一个加,直到被除数被超过的话,会超时. 解决办法每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本操作. #include <io ...

  4. Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 思路:不能使用乘法除法以及取模运算来计算两个数 ...

  5. leetcode之Divide Two Integers

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

  6. numpy.divide详解

    numpy.divide 用例: numpy.divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dt ...

  7. 【OpenCV3】cv::divide()使用详解

    cv::divide()是一个简单的除法函数,有以下两种用法: 用法1: void cv::divide(cv::InputArray src1, // 输入数组1 (分子)cv::InputArra ...

  8. 成功解决RuntimeWarning: divide by zero encountered in double_scalars

    成功解决RuntimeWarning: divide by zero encountered in double_scalars 目录 解决问题 解决思路 解决方法 解决问题 RuntimeWarni ...

  9. PowerBI随笔(7)-lookupvalue、divide

    1.divide可以处理分母为0情况(返回空) 2.lookupvalue可以处理多条件lookup.

  10. Divide by three, multiply by two CodeForces - 977D (思维排序)

    Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, a ...

最新文章

  1. Upgrade after a crash is not supported. The redo log was created with Maria的解决办法
  2. 百度开源联邦学习框架 PaddleFL:简化大规模分布式集群部署
  3. php的辅助工具下载,PHPTools 工具包(让你的EditPlus可以调试PHP)
  4. python在列表末尾删除一个_从链接列表的尾部移除(Python)
  5. Delphi实现类似Android锁屏的密码锁控件
  6. 《构建之法》阅读笔记4
  7. Adb+.net 实现微信跳一跳自动化
  8. jquery div拖动效果示例代码
  9. Android权限之动态权限
  10. 如何通过PHP将excel的数据导入MySQL中
  11. 流行歌单片机c语言编程,单片机6首音乐播放的proteus仿真电路及C语言程序设计...
  12. 偏微分方程数值解法python_Python数值计算----------求解简单的偏微分方程
  13. 如何完全卸载oracle和删除oracle在注册表中的注册信息
  14. [译] 为什么给设计定义 UX、UI、CX、IA、IxD 和其他类型的头衔是愚蠢的行为
  15. python监听键盘事件pyhook用法_python 监听键盘事件pyHook
  16. java抽象类的属性_JAVA 抽象类
  17. disparity和parallax的区别
  18. Git分布式版本控制遇到的问题如何把本地的项目上传到码市上
  19. Bootstrap轮播插件
  20. Octotree在GitHub中出错(已解决)

热门文章

  1. mtk8735 mtk8163 mtk8127 mtk6735量产方案 核心板方案 mtk系列平台
  2. Oracle向mysql改造
  3. Adams/Car和Matlab联合仿真
  4. 千寻位置终端配置参数说明
  5. 【Android笔记65】Android小案例之简易版的房贷计算器(附源代码)
  6. Kaggle八门神器(一):竞赛神器之XGBoost介绍
  7. photoshop扣发丝——就这么简单
  8. Hibernate高级映射技术(一)自定义数据类型StringList (转)
  9. 对比两张图片的MD5值
  10. 德州扑克实践之二------判断牌型