给定一个整数N ,任务是将N减为1的最小数量的运算(包括乘以2和除以10) 。如果无法获得1 ,则打印“ -1” 。
例子:
Input: N = 5
Output: 2
Explanation:
Below are the operations performed:
1st operation: Multiply N by 2. Therefore, N = 5 * 2 = 10.
2nd operation: Divide N by 10. Therefore, N = 10/10 = 1.
Therefore, minimum number of operations required is 2.
Input: N = 4
Output: -1
方法:想法是检查给定数M的素数。如果给定数具有2和5以外的素数,则无法通过给定操作将给定数减少为1 。如果2的数量在其素数上超过5的数量,则不可能将N减小为1,因为无法将2的所有幂都减小。
请按照以下步骤解决问题:
- 计算存在于N的素因子中的2 s的数量,并将其存储在变量中,例如cnt2 ,并将N更新为N / 2 cnt2 。
- 计算存在于N的素因子中的5 s的数目,并将其存储在变量中,例如cnt5 ,并将N更新为N / 5 cnt5 。
- 完成上述步骤后,如果N为1且cnt2≤cnt5 ,则所需的最小步骤数为2 * cnt5 – cnt2 。
- 否则,打印“ -1”,因为使用给定的操作无法将N减小为1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// operations required to reduce N to 1
int minimumMoves(int n)
{
// Stores count of powers of 2 and 5
int cnt2 = 0, cnt5 = 0;
// Calculating the primefactors 2
while (n % 2 == 0) {
n /= 2;
cnt2++;
}
// Calculating the primefactors 5
while (n % 5 == 0) {
n /= 5;
cnt5++;
}
// If n is 1 and cnt2 <= cnt5
if (n == 1 && cnt2 <= cnt5) {
// Return the minimum operations
return 2 * cnt5 - cnt2;
}
// Otherwise, n can't be reduced
else
return -1;
}
// Driver Code
int main()
{
// Given Number N
int N = 25;
// Function Call
cout << minimumMoves(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum number
// operations required to reduce N to 1
static int minimumMoves(int n)
{
// Stores count of powers of 2 and 5
int cnt2 = 0, cnt5 = 0;
// Calculating the primefactors 2
while (n % 2 == 0)
{
n /= 2;
cnt2++;
}
// Calculating the primefactors 5
while (n % 5 == 0)
{
n /= 5;
cnt5++;
}
// If n is 1 and cnt2 <= cnt5
if (n == 1 && cnt2 <= cnt5)
{
// Return the minimum operations
return 2 * cnt5 - cnt2;
}
// Otherwise, n can't be reduced
else
return -1;
}
// Driver Code
public static void main(String[] args)
{
// Given Number N
int N = 25;
// Function Call
System.out.print(minimumMoves(N));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to find the minimum number
# operations required to reduce N to 1
def minimumMoves(n):
# Stores count of powers of 2 and 5
cnt2 = 0
cnt5 = 0
# Calculating the primefactors 2
while (n % 2 == 0):
n //= 2
cnt2 += 1
# Calculating the primefactors 5
while (n % 5 == 0):
n //= 5
cnt5 += 1
# If n is 1 and cnt2 <= cnt5
if (n == 1 and cnt2 <= cnt5):
# Return the minimum operations
return 2 * cnt5 - cnt2
# Otherwise, n can't be reduced
else:
return -1
# Driver Code
if __name__ == '__main__':
# Given Number N
N = 25
# Function Call
print(minimumMoves(N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum number
// operations required to reduce N to 1
static int minimumMoves(int n)
{
// Stores count of powers of 2 and 5
int cnt2 = 0, cnt5 = 0;
// Calculating the primefactors 2
while (n % 2 == 0)
{
n /= 2;
cnt2++;
}
// Calculating the primefactors 5
while (n % 5 == 0)
{
n /= 5;
cnt5++;
}
// If n is 1 and cnt2 <= cnt5
if (n == 1 && cnt2 <= cnt5)
{
// Return the minimum operations
return 2 * cnt5 - cnt2;
}
// Otherwise, n can't be reduced
else
return -1;
}
// Driver Code
public static void Main()
{
// Given Number N
int N = 25;
// Function Call
Console.WriteLine(minimumMoves(N));
}
}
// This code is contributed by SURENDRA_GANGWAR
输出:
4
时间复杂度: O(log N)
辅助空间: O(1)