📌  相关文章
📜  找到满足给定条件的给定输入整数 N 的最大和 (a+b)

📅  最后修改于: 2021-10-26 05:11:17             🧑  作者: Mango

给定一个整数N ,任务是找到最大的和 ( a + b ) {1 ≤ a ≤ N, 1 ≤ b ≤ N} 使得a * b/(a + b)是一个整数(即 a + b将 a * b) 和 a != b 相除。

例子:

朴素的方法:朴素的方法解决这个问题的想法是使用嵌套循环的概念。可以按照以下步骤计算结果:

  1. 运行从 1 到 N 的嵌套循环。
  2. 对于 [1, N] 范围内的每个数字a ,找到另一个整数b使得a != b和 ( a + b ) 除以a * b
  3. 如果满足条件,则 ( a + b ) 的值存储在变量中以跟踪获得的最大值。
  4. 最后,返回 ( a + b ) 的最大值。

下面是上述方法的实现:

C++
// C++ implementation to find the largest value
// of a + b satisfying the given condition
#include 
using namespace std;
 
// Function to return the maximum sum of
// a + b satisfying the given condition
int getLargestSum(int N)
{
    // Initialize max_sum
    int max_sum = 0;
 
    // Consider all the possible pairs
    for (int i = 1; i <= N; i++) {
        for (int j = i + 1; j <= N; j++) {
 
            // Check if the product is
            // divisible by the sum
            if (i * j % (i + j) == 0)
 
                // Storing the maximum sum
                // in the max_sum variable
                max_sum = max(max_sum, i + j);
        }
    }
 
    // Return the max_sum value
    return max_sum;
}
 
// Driver code
int main()
{
    int N = 25;
 
    int max_sum = getLargestSum(N);
 
    cout << max_sum << endl;
    return 0;
}


Java
// Java implementation to find the largest value
// of a + b satisfying the given condition
import java.util.*;
  
class GFG{
   
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
    // Initialize max_sum
    int max_sum = 0;
 
    // Consider all the possible pairs
    for (int i = 1; i <= N; i++) {
        for (int j = i + 1; j <= N; j++) {
 
            // Check if the product is
            // divisible by the sum
            if (i * j % (i + j) == 0)
 
                // Storing the maximum sum
                // in the max_sum variable
                max_sum = Math.max(max_sum, i + j);
        }
    }
 
    // Return the max_sum value
    return max_sum;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 25;
 
    int max_sum = getLargestSum(N);
 
    System.out.print(max_sum );
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 implementation to find the largest value
# of a + b satisfying the given condition
 
# Function to return the maximum sum of
# a + b satisfying the given condition
def getLargestSum(N):
    # Initialize max_sum
    max_sum = 0
 
    # Consider all the possible pairs
    for i in range(1,N+1):
        for j in range(i + 1, N + 1, 1):
 
            # Check if the product is
            # divisible by the sum
            if (i * j % (i + j) == 0):
 
                # Storing the maximum sum
                # in the max_sum variable
                max_sum = max(max_sum, i + j)
 
    # Return the max_sum value
    return max_sum
 
# Driver code
if __name__ == '__main__':
    N = 25
 
    max_sum = getLargestSum(N)
    print(max_sum)
 
# This code is contributed by Surendra_Gangwar


C#
// C# implementation to find the largest value
// of a + b satisfying the given condition
using System;
 
class GFG{
     
    // Function to return the maximum sum of
    // a + b satisfying the given condition
    static int getLargestSum(int N)
    {
        // Initialize max_sum
        int max_sum = 0;
     
        // Consider all the possible pairs
        for (int i = 1; i <= N; i++) {
            for (int j = i + 1; j <= N; j++) {
     
                // Check if the product is
                // divisible by the sum
                if (i * j % (i + j) == 0)
     
                    // Storing the maximum sum
                    // in the max_sum variable
                    max_sum = Math.Max(max_sum, i + j);
            }
        }
     
        // Return the max_sum value
        return max_sum;
    }
     
    // Driver code
    public static void Main(string[] args)
    {
        int N = 25;
     
        int max_sum = getLargestSum(N);
     
        Console.WriteLine(max_sum );
    }
}
 
// This code is contributed by AnkitRai01


Javascript


C++
// C++ implementation to  find the largest value
// of a + b satisfying the given condition
#include 
using namespace std;
 
// Function to return the maximum sum of
// a + b satisfying the given condition
int getLargestSum(int N)
{
    int max_sum = 0; // Initialize max_sum
 
    // Consider all possible pairs and check
    // the sum divides product property
    for (int i = 1; i * i <= N; i++) {
        for (int j = i + 1; j * j <= N; j++) {
 
            // To find the largest factor k
            int k = N / j;
            int a = k * i;
            int b = k * j;
 
            // Check if the product is
            // divisible by the sum
            if (a <= N && b <= N
                && a * b % (a + b) == 0)
 
                // Storing the maximum sum
                // in the max_sum variable
                max_sum = max(max_sum, a + b);
        }
    }
 
    // Return the max_sum value
    return max_sum;
}
 
// Driver code
int main()
{
    int N = 25;
    int max_sum = getLargestSum(N);
 
    cout << max_sum << endl;
    return 0;
}


Java
// Java implementation to find the largest value
// of a + b satisfying the given condition
 
class GFG{
 
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
    // Initialize max_sum
    int max_sum = 0;
 
    // Consider all possible pairs and check
    // the sum divides product property
    for (int i = 1; i * i <= N; i++) {
         for (int j = i + 1; j * j <= N; j++) {
 
              // To find the largest factor k
              int k = N / j;
              int a = k * i;
              int b = k * j;
 
               // Check if the product is
               // divisible by the sum
               if (a <= N && b <= N &&
                   a * b % (a + b) == 0)
 
                   // Storing the maximum sum
                   // in the max_sum variable
                   max_sum = Math.max(max_sum, a + b);
        }
    }
 
    // Return the max_sum value
    return max_sum;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 25;
    int max_sum = getLargestSum(N);
    System.out.print(max_sum + "\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to find the largest value
# of a + b satisfying the given condition
 
# Function to return the maximum sum of
# a + b satisfying the given condition
def getLargestSum(N) :
 
    max_sum = 0; # Initialize max_sum
 
    # Consider all possible pairs and check
    # the sum divides product property
    for i in range(1, int(N ** (1/2))+1) :
        for j in range(i + 1, int(N ** (1/2)) + 1) :
             
            # To find the largest factor k
            k = N // j;
            a = k * i;
            b = k * j;
             
            # Check if the product is
            # divisible by the sum
            if (a <= N and b <= N and a * b % (a + b) == 0) :
                # Storing the maximum sum
                # in the max_sum variable
                max_sum = max(max_sum, a + b);
                 
    # Return the max_sum value
    return max_sum;
     
# Driver code
if __name__ == "__main__" :
    N = 25;
    max_sum = getLargestSum(N);
    print(max_sum);
 
# This code is contributed by AnkitRai01


C#
// C# implementation to find the largest value
// of a + b satisfying the given condition
using System;
 
class GFG{
 
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
     
    // Initialize max_sum
    int max_sum = 0;
 
    // Consider all possible pairs and check
    // the sum divides product property
    for(int i = 1; i * i <= N; i++)
    {
       for(int j = i + 1; j * j <= N; j++)
       {
          // To find the largest factor k
          int k = N / j;
          int a = k * i;
          int b = k * j;
           
          // Check if the product is
          // divisible by the sum
          if (a <= N && b <= N &&
              a * b % (a + b) == 0)
               
              // Storing the maximum sum
              // in the max_sum variable
              max_sum = Math.Max(max_sum, a + b);
        }
    }
 
    // Return the max_sum value
    return max_sum;
}
 
// Driver code
static public void Main(String[] args)
{
    int N = 25;
    int max_sum = getLargestSum(N);
     
    Console.Write(max_sum + "\n");
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
36

时间复杂度:由于嵌套的 for 循环运行 (N * (N + 1)) / 2 次,因此上述解决方案的时间复杂度为O(N 2 )
有效的方法:可以观察到的一个现象是,如果存在两个数ab ,并且它们的乘积可以被它们的和整除,那么它们就不是素数,即它们的 GCD 不是 1。这可以使用欧几里得算法来证明。
因此,通过使用上述观察,可以按照以下步骤计算结果:

1. 如果a可以表示为k * x并且b可以表示为k * y使得xy互质,那么:

a + b = k(x + y)
a * b = k2

2. 现在,除以上述值后:

(a * b) /(a + b) = k * ((x * y)/(x + y)). 

3. 由于已知 x 和 y 互质,因此 ( x * y ) 不能被 ( x + y ) 整除。这意味着k必须能被 x + y 整除。

4. 因此, k是 ( k * x ) 和 ( k * y ) 保持小于N的最大可能因子。

5. 显然,可被 (x + y) 整除的k的最小值是 ( x + y )。这意味着 (x + y) * y ≤ N 和 (x + y) * x ≤ N。

6. 因此,所有因素都从1 到 N 0.5进行检查。

下面是上述方法的实现:

C++

// C++ implementation to  find the largest value
// of a + b satisfying the given condition
#include 
using namespace std;
 
// Function to return the maximum sum of
// a + b satisfying the given condition
int getLargestSum(int N)
{
    int max_sum = 0; // Initialize max_sum
 
    // Consider all possible pairs and check
    // the sum divides product property
    for (int i = 1; i * i <= N; i++) {
        for (int j = i + 1; j * j <= N; j++) {
 
            // To find the largest factor k
            int k = N / j;
            int a = k * i;
            int b = k * j;
 
            // Check if the product is
            // divisible by the sum
            if (a <= N && b <= N
                && a * b % (a + b) == 0)
 
                // Storing the maximum sum
                // in the max_sum variable
                max_sum = max(max_sum, a + b);
        }
    }
 
    // Return the max_sum value
    return max_sum;
}
 
// Driver code
int main()
{
    int N = 25;
    int max_sum = getLargestSum(N);
 
    cout << max_sum << endl;
    return 0;
}

Java

// Java implementation to find the largest value
// of a + b satisfying the given condition
 
class GFG{
 
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
    // Initialize max_sum
    int max_sum = 0;
 
    // Consider all possible pairs and check
    // the sum divides product property
    for (int i = 1; i * i <= N; i++) {
         for (int j = i + 1; j * j <= N; j++) {
 
              // To find the largest factor k
              int k = N / j;
              int a = k * i;
              int b = k * j;
 
               // Check if the product is
               // divisible by the sum
               if (a <= N && b <= N &&
                   a * b % (a + b) == 0)
 
                   // Storing the maximum sum
                   // in the max_sum variable
                   max_sum = Math.max(max_sum, a + b);
        }
    }
 
    // Return the max_sum value
    return max_sum;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 25;
    int max_sum = getLargestSum(N);
    System.out.print(max_sum + "\n");
}
}
 
// This code is contributed by 29AjayKumar

蟒蛇3

# Python3 implementation to find the largest value
# of a + b satisfying the given condition
 
# Function to return the maximum sum of
# a + b satisfying the given condition
def getLargestSum(N) :
 
    max_sum = 0; # Initialize max_sum
 
    # Consider all possible pairs and check
    # the sum divides product property
    for i in range(1, int(N ** (1/2))+1) :
        for j in range(i + 1, int(N ** (1/2)) + 1) :
             
            # To find the largest factor k
            k = N // j;
            a = k * i;
            b = k * j;
             
            # Check if the product is
            # divisible by the sum
            if (a <= N and b <= N and a * b % (a + b) == 0) :
                # Storing the maximum sum
                # in the max_sum variable
                max_sum = max(max_sum, a + b);
                 
    # Return the max_sum value
    return max_sum;
     
# Driver code
if __name__ == "__main__" :
    N = 25;
    max_sum = getLargestSum(N);
    print(max_sum);
 
# This code is contributed by AnkitRai01

C#

// C# implementation to find the largest value
// of a + b satisfying the given condition
using System;
 
class GFG{
 
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
     
    // Initialize max_sum
    int max_sum = 0;
 
    // Consider all possible pairs and check
    // the sum divides product property
    for(int i = 1; i * i <= N; i++)
    {
       for(int j = i + 1; j * j <= N; j++)
       {
          // To find the largest factor k
          int k = N / j;
          int a = k * i;
          int b = k * j;
           
          // Check if the product is
          // divisible by the sum
          if (a <= N && b <= N &&
              a * b % (a + b) == 0)
               
              // Storing the maximum sum
              // in the max_sum variable
              max_sum = Math.Max(max_sum, a + b);
        }
    }
 
    // Return the max_sum value
    return max_sum;
}
 
// Driver code
static public void Main(String[] args)
{
    int N = 25;
    int max_sum = getLargestSum(N);
     
    Console.Write(max_sum + "\n");
}
}
 
// This code is contributed by gauravrajput1

Javascript


输出:
36

时间复杂度:虽然有两个循环,但每个循环最多运行 sqrt(N) 次。因此,整体时间复杂度为O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程