📌  相关文章
📜  通过乘以 2 或除以 6,在最小移动中将 N 减少到 1

📅  最后修改于: 2022-05-13 01:56:06.596000             🧑  作者: Mango

通过乘以 2 或除以 6,在最小移动中将 N 减少到 1

给定一个整数N ,通过使用以下两个操作找到将N 减少到 1的最小操作数:

  • 将 N 乘以 2
  • 如果 N 能被 6 整除,则将 N 除以 6

如果 N 不能减少到 1,则打印-1

例子:

方法:可以使用以下观察来解决该任务。

  • 如果该数字由23以外的素数组成,则答案为-1 ,因为通过上述操作无法将N减少到 1。
  • 否则,让count2是 n 的因式分解二的个数, count3n因式分解三的个数。
    • 如果count2 > count3那么答案是-1 ,因为我们无法摆脱所有的二。
    • 否则,答案是(count3−count2) + count3

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum number
// of moves to reduce N to 1
void minOperations(int N)
{
    int count2 = 0, count3 = 0;
 
    // Number of 2's in the
    // factorization of N
    while (N % 2 == 0) {
        N = N / 2;
        count2++;
    }
 
    // Number of 3's in the
    // factorization of n
    while (N % 3 == 0) {
        N = N / 3;
        count3++;
    }
 
    if (N == 1 && (count2 <= count3)) {
        cout << (2 * count3) - count2;
    }
 
    // If number of 2's are greater
    // than number of 3's or
    // prime factorization of N contains
    // primes other than 2 or 3
    else {
        cout << -1;
    }
}
 
// Driver Code
int main()
{
    int N = 54;
    minOperations(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the minimum number
// of moves to reduce N to 1
static void minOperations(int N)
{
    int count2 = 0, count3 = 0;
 
    // Number of 2's in the
    // factorization of N
    while (N % 2 == 0) {
        N = N / 2;
        count2++;
    }
 
    // Number of 3's in the
    // factorization of n
    while (N % 3 == 0) {
        N = N / 3;
        count3++;
    }
 
    if (N == 1 && (count2 <= count3)) {
        System.out.print((2 * count3) - count2);
    }
 
    // If number of 2's are greater
    // than number of 3's or
    // prime factorization of N contains
    // primes other than 2 or 3
    else {
        System.out.print(-1);
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 54;
    minOperations(N);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# python program for the above approach
 
# Function to find the minimum number
# of moves to reduce N to 1
def minOperations(N):
 
    count2 = 0
    count3 = 0
 
    # Number of 2's in the
    # factorization of N
    while (N % 2 == 0):
        N = N // 2
        count2 += 1
 
        # Number of 3's in the
        # factorization of n
    while (N % 3 == 0):
        N = N // 3
        count3 += 1
 
    if (N == 1 and (count2 <= count3)):
        print((2 * count3) - count2)
 
        # If number of 2's are greater
        # than number of 3's or
        # prime factorization of N contains
        # primes other than 2 or 3
    else:
        print(-1)
 
# Driver Code
if __name__ == "__main__":
 
    N = 54
    minOperations(N)
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to find the minimum number
    // of moves to reduce N to 1
    static void minOperations(int N)
    {
        int count2 = 0, count3 = 0;
 
        // Number of 2's in the
        // factorization of N
        while (N % 2 == 0) {
            N = N / 2;
            count2++;
        }
 
        // Number of 3's in the
        // factorization of n
        while (N % 3 == 0) {
            N = N / 3;
            count3++;
        }
 
        if (N == 1 && (count2 <= count3)) {
            Console.WriteLine((2 * count3) - count2);
        }
 
        // If number of 2's are greater
        // than number of 3's or
        // prime factorization of N contains
        // primes other than 2 or 3
        else {
            Console.WriteLine(-1);
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 54;
        minOperations(N);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出
5

时间复杂度: O(logN)
辅助空间: O(1)