通过减 1 或将 A 除以 B 和 B 除以 A,最小化操作以将 A 和 B 减少到 1
给定两个正整数A和B 。任务是最小化将A和B减少到1所需的操作。有两种类型的操作:
- 将A或B减1 。
- 仅当除法的余数为0时,才将A除以B或B除以A或同时执行两个除法。
例子:
Input: A = 13, B = 5
Output: 6
Explanation: Below are the operations performed to reduce A and B to 1.
Initially A = 13, B = 5
Step 1: Decrement A by 1: A = 12, B = 5
Step 2: Decrement B by 1: A = 12, B = 4
Step 3: Divide A by B and assign it to A : A = 3, B = 4
Step 4: Decrement A by 1: A = 2, B = 4
Step 5: Divide B by A and assign it to B : A = 2, B = 2
Step 6: Divide both A by B and B by A, assign it to A and B respectively: A = 1, B = 1
Therefore, total 6 steps required to reduce A and B to 1. Which is minimum possible.
Input: A = 3, B = 27
Output: 3
方法:这个问题可以通过递归来解决。创建一个递归并取一个变量说cntOperations = 0以跟踪执行的操作数。对于基本条件检查A=1和B=1 ,否则返回cntOperations ,检查可以执行的每个可能的操作。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Recursive function to find minimum operation
// required to reduce A and B to 1
int solve(int A, int B, int ans = 0)
{
// Base Condition: When A and B reduced to 1
if (A == 1 && B == 1) {
return ans;
}
// If A and B are equal
if (A % B == 0 && B % A == 0) {
return solve(A / B, B / A, ans + 1);
}
// If A is divisible by B
else if (A % B == 0 && B % A != 0) {
return solve(A / B, B, ans + 1);
}
// If B is divisible by A
else if (A % B != 0 && B % A == 0) {
return solve(A, B / A, ans + 1);
}
// If A-1 is even
else if ((A - 1) % 2 == 0) {
return solve(A - 1, B, ans + 1);
}
// If B-1 is even
else if ((B - 1) % 2 == 0) {
return solve(A, B - 1, ans + 1);
}
else {
// If A is less than B
if (A < B) {
return solve(A - 1, B, ans + 1);
}
// If B is less than A
else {
return solve(A, B - 1, ans + 1);
}
}
}
// Driver Code
int main()
{
int A = 13;
int B = 5;
cout << solve(A, B);
}
Java
// Java program for above approach
class GFG
{
// Recursive function to find minimum operation
// required to reduce A and B to 1
public static int solve(int A, int B, int ans)
{
// Base Condition: When A and B reduced to 1
if (A == 1 && B == 1) {
return ans;
}
// If A and B are equal
if (A % B == 0 && B % A == 0) {
return solve(A / B, B / A, ans + 1);
}
// If A is divisible by B
else if (A % B == 0 && B % A != 0) {
return solve(A / B, B, ans + 1);
}
// If B is divisible by A
else if (A % B != 0 && B % A == 0) {
return solve(A, B / A, ans + 1);
}
// If A-1 is even
else if ((A - 1) % 2 == 0) {
return solve(A - 1, B, ans + 1);
}
// If B-1 is even
else if ((B - 1) % 2 == 0) {
return solve(A, B - 1, ans + 1);
}
else {
// If A is less than B
if (A < B) {
return solve(A - 1, B, ans + 1);
}
// If B is less than A
else {
return solve(A, B - 1, ans + 1);
}
}
}
// Driver Code
public static void main(String args[]) {
int A = 13;
int B = 5;
System.out.println(solve(A, B, 0));
}
}
// This code is contributed by gfgking,
Python3
# python program for above approach
# Recursive function to find minimum operation
# required to reduce A and B to 1
def solve(A, B, ans=0):
# Base Condition: When A and B reduced to 1
if (A == 1 and B == 1):
return ans
# If A and B are equal
if (A % B == 0 and B % A == 0):
return solve(A // B, B // A, ans + 1)
# If A is divisible by B
elif (A % B == 0 and B % A != 0):
return solve(A // B, B, ans + 1)
# If B is divisible by A
elif (A % B != 0 and B % A == 0):
return solve(A, B // A, ans + 1)
# If A-1 is even
elif ((A - 1) % 2 == 0):
return solve(A - 1, B, ans + 1)
# If B-1 is even
elif ((B - 1) % 2 == 0):
return solve(A, B - 1, ans + 1)
else:
# If A is less than B
if (A < B):
return solve(A - 1, B, ans + 1)
# If B is less than A
else:
return solve(A, B - 1, ans + 1)
# Driver Code
if __name__ == "__main__":
A = 13
B = 5
print(solve(A, B))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG
{
// Recursive function to find minimum operation
// required to reduce A and B to 1
static int solve(int A, int B, int ans)
{
// Base Condition: When A and B reduced to 1
if (A == 1 && B == 1) {
return ans;
}
// If A and B are equal
if (A % B == 0 && B % A == 0) {
return solve(A / B, B / A, ans + 1);
}
// If A is divisible by B
else if (A % B == 0 && B % A != 0) {
return solve(A / B, B, ans + 1);
}
// If B is divisible by A
else if (A % B != 0 && B % A == 0) {
return solve(A, B / A, ans + 1);
}
// If A-1 is even
else if ((A - 1) % 2 == 0) {
return solve(A - 1, B, ans + 1);
}
// If B-1 is even
else if ((B - 1) % 2 == 0) {
return solve(A, B - 1, ans + 1);
}
else {
// If A is less than B
if (A < B) {
return solve(A - 1, B, ans + 1);
}
// If B is less than A
else {
return solve(A, B - 1, ans + 1);
}
}
}
// Driver Code
public static void Main(String[] args)
{
int A = 13;
int B = 5;
Console.WriteLine(solve(A, B, 0));
}
}
// This code is contributed by dwivediyash
Javascript
6
时间复杂度: O(max(A, B))
辅助空间: O(1)