• Divide by 2 or 3


    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 的操作次数之和。

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. typedef long long ll;
    8. ll n, p, ans;
    9. ll a[1100];
    10. long long gcd(long long a, long long b)
    11. {
    12. while (b ^= a ^= b ^= a %= b);
    13. return a;
    14. }
    15. int main() {
    16. cin >> n;
    17. for (ll i = 1; i <= n; i++) {
    18. cin >> a[i];
    19. }
    20. p = a[1];
    21. for (ll i = 2; i <= n; i++) {
    22. p = gcd(a[i], p);//找最大公因数
    23. }
    24. for (ll i = 1; i <= n; i++) {
    25. ll x = a[i] / p;
    26. while (x % 2 == 0) {
    27. x /= 2;
    28. ans++;
    29. }
    30. while (x % 3 == 0) {
    31. x /= 3;
    32. ans++;
    33. }
    34. if (x != 1) {
    35. ans = -1;
    36. break;
    37. }
    38. }
    39. printf("%lld\n", ans);
    40. return 0;
    41. }

  • 相关阅读:
    LightGBM 实现基于内容的个性化推荐
    互联网医院系统:数字化时代中医疗服务的未来
    img图片丢失后默认图
    Mac使用鼠标滚轮方向与Win相反的解决办法
    Spring Boot和XXL-Job:高效定时任务管理
    1.1 - Android启动概览
    Linux系统中标准输入设备的控制实现
    基于SSM实现前后端分离在线考试管理系统
    《WEB安全渗透测试》(29)记一次HOST头投毒漏洞
    groupadd
  • 原文地址:https://blog.csdn.net/weixin_62599885/article/details/127734550