📌  相关文章
📜  找到给定范围内某对可能的最大 GCD [L, R]

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

找到给定范围内某对可能的最大 GCD [L, R]

给定范围LR ,任务是找到GCD (X, Y)的最大可能值,使得 X 和 Y 属于给定范围,即L ≤ X < Y ≤ R。

例子:

朴素方法:可以从LR形成的每一对都可以使用两个嵌套循环进行迭代,并且可以找到最大GCD

时间复杂度: O((RL) 2 Log(R))
辅助空间: O(1)

有效方法:按照以下步骤解决问题:

  • 令最大GCDZ,因此, XY都是Z的倍数。相反,如果在片段[L, R]中有两个或多个Z的倍数,则可以通过在[L, R]中选择Z的连续倍数来选择(X, Y)使得GCD(x, y)最大.
  • R迭代到1并找出它们中的任何一个是否在[L, R]范围内至少有两个倍数
  • LR之间Z的倍数可以使用以下公式计算:
    • [L, R] 中 Z 的倍数 = [1, R] 中 Z 的倍数 – [1, L-1] 中 Z 的倍数
    • 这可以写成:
      • [L, R] 中 Z 的倍数 = floor(R/Z) – floor((L-1)/Z)
  • 我们可以通过将迭代从R/2限制为1来进一步优化这一点,因为最大可能的 GCD 是 R/2(具有倍数 R/2 和 R)

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate GCD
int GCD(int a, int b)
{
    if (b == 0)
        return a;
    return GCD(b, a % b);
}
 
// Function to calculate
// maximum GCD in a range
int maxGCDInRange(int L, int R)
{
    // Variable to store the answer
    int ans = 1;
 
    for (int Z = R/2; Z >= 1; Z--) {
 
        // If Z has two multiples in [L, R]
        if ((R / Z) - ((L - 1) / Z) > 1) {
 
            // Update ans
            ans = Z;
            break;
        }
    }
 
    // Return the value
    return ans;
}
// Driver code
int main()
{
    // Input
    int L = 102;
    int R = 139;
 
    // Function Call
    cout << maxGCDInRange(L, R);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
    // Function to calculate GCD
    public static int GCD(int a, int b)
    {
        if (b == 0)
            return a;
        return GCD(b, a % b);
    }
 
    // Function to calculate
    // maximum GCD in a range
    public static int maxGCDInRange(int L, int R)
    {
        // Variable to store the answer
        int ans = 1;
 
        for (int Z = R/2; Z >= 1; Z--) {
 
            // If Z has two multiples in [L, R]
            if ((R / Z) - ((L - 1) / Z) > 1) {
 
                // Update ans
                ans = Z;
                break;
            }
        }
 
        // Return the value
        return ans;
    }
   
  // Driver code
    public static void main(String[] args)
    {
        // Input
        int L = 102;
        int R = 139;
 
        // Function Call
        System.out.println(maxGCDInRange(L, R));
    }
 
   
   // This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
 
# Function to calculate GCD
def GCD(a, b):
     
    if (b == 0):
        return a
         
    return GCD(b, a % b)
 
# Function to calculate
# maximum GCD in a range
def maxGCDInRange(L, R):
     
    # Variable to store the answer
    ans = 1
 
    for Z in range(R//2, 1, -1):
         
        # If Z has two multiples in [L, R]
        if (((R // Z) - ((L - 1) // Z )) > 1):
             
            # Update ans
            ans = Z
            break
         
    # Return the value
    return ans
 
# Driver code
 
# Input
L = 102
R = 139
 
# Function Call
print(maxGCDInRange(L, R))
 
# This code is contributed by SoumikMondal


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to calculate GCD
public static int GCD(int a, int b)
{
    if (b == 0)
        return a;
         
    return GCD(b, a % b);
}
 
// Function to calculate
// maximum GCD in a range
public static int maxGCDInRange(int L, int R)
{
     
    // Variable to store the answer
    int ans = 1;
 
    for(int Z = R/2; Z >= 1; Z--)
    {
         
        // If Z has two multiples in [L, R]
        if ((R / Z) - ((L - 1) / Z) > 1)
        {
             
            // Update ans
            ans = Z;
            break;
        }
    }
 
    // Return the value
    return ans;
}
 
// Driver code
public static void Main()
{
     
    // Input
    int L = 102;
    int R = 139;
 
    // Function Call
    Console.Write(maxGCDInRange(L, R));
}
}
 
// This code is contributed by rishavmahato348


Javascript


输出
34

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