给定数字N ,任务是找到两个数字a和b,使得a + b = N且LCM(a,b)最小。
例子:
Input: N = 15
Output: a = 5, b = 10
Explanation:
The pair 5, 10 has a sum of 15 and their LCM is 10 which is the minimum possible.
Input: N = 4
Output: a = 2, b = 2
Explanation:
The pair 2, 2 has a sum of 4 and their LCM is 2 which is the minimum possible.
方法:想法是使用GCD和LCM的概念。步骤如下:
- 如果N是素数,则答案为1和N – 1,因为在任何其他情况下, a + b> N或LCM(a,b)都> N – 1 。这是因为如果N为质数,则意味着N为奇数。因此,a和b,它们中的任何一个都必须是奇数而其他偶数。因此,LCM(a,b)必须大于N (如果不是1且N – 1),因为2始终是一个因数。
- 如果N不是质数,则选择a,b,使它们的GCD最大,因为公式LCM(a,b)= a * b / GCD(a,b) 。因此,为了最小化LCM(a,b),我们必须 最大化GCD(a,b)。
- 如果x是N的除数,则通过简单的数学运算 a和b可以分别表示为N / x和N / x *(x – 1) 。现在,由于a = N / x和b = N / x *(x – 1) ,因此它们的GCD表示为N / x 。为了最大限度地提高本GCD,取可能的最小x或N个最小可能除数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if number is
// prime or not
bool prime(int n)
{
// As 1 is neither prime
// nor composite return false
if (n == 1)
return false;
// Check if it is divided by any
// number then it is not prime,
// return false
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
// Check if n is not divided
// by any number then it is
// prime and hence return true
return true;
}
// Function to find the pair (a, b)
// such that sum is N & LCM is minimum
void minDivisior(int n)
{
// Check if the number is prime
if (prime(n)) {
cout << 1 << " " << n - 1;
}
// Now, if it is not prime then
// find the least divisior
else {
for (int i = 2; i * i <= n; i++) {
// Check if divides n then
// it is a factor
if (n % i == 0) {
// Required output is
// a = n/i & b = n/i*(n-1)
cout << n / i << " "
<< n / i * (i - 1);
break;
}
}
}
}
// Driver Code
int main()
{
int N = 4;
// Function call
minDivisior(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check if number is
// prime or not
static boolean prime(int n)
{
// As 1 is neither prime
// nor composite return false
if (n == 1)
return false;
// Check if it is divided by any
// number then it is not prime,
// return false
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
// Check if n is not divided
// by any number then it is
// prime and hence return true
return true;
}
// Function to find the pair (a, b)
// such that sum is N & LCM is minimum
static void minDivisior(int n)
{
// Check if the number is prime
if (prime(n))
{
System.out.print(1 + " " + (n - 1));
}
// Now, if it is not prime then
// find the least divisior
else
{
for (int i = 2; i * i <= n; i++)
{
// Check if divides n then
// it is a factor
if (n % i == 0)
{
// Required output is
// a = n/i & b = n/i*(n-1)
System.out.print(n / i + " " +
(n / i * (i - 1)));
break;
}
}
}
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
// Function call
minDivisior(N);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function to check if number is
# prime or not
def prime(n):
# As 1 is neither prime
# nor composite return false
if (n == 1):
return False
# Check if it is divided by any
# number then it is not prime,
# return false
for i in range(2, n + 1):
if i * i > n:
break
if (n % i == 0):
return False
# Check if n is not divided
# by any number then it is
# prime and hence return true
return True
# Function to find the pair (a, b)
# such that sum is N & LCM is minimum
def minDivisior(n):
# Check if the number is prime
if (prime(n)):
print(1, n - 1)
# Now, if it is not prime then
# find the least divisior
else:
for i in range(2, n + 1):
if i * i > n:
break
# Check if divides n then
# it is a factor
if (n % i == 0):
# Required output is
# a = n/i & b = n/i*(n-1)
print(n // i, n // i * (i - 1))
break
# Driver Code
N = 4
# Function call
minDivisior(N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if number is
// prime or not
static bool prime(int n)
{
// As 1 is neither prime
// nor composite return false
if (n == 1)
return false;
// Check if it is divided by any
// number then it is not prime,
// return false
for(int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
// Check if n is not divided
// by any number then it is
// prime and hence return true
return true;
}
// Function to find the pair (a, b)
// such that sum is N & LCM is minimum
static void minDivisior(int n)
{
// Check if the number is prime
if (prime(n))
{
Console.Write(1 + " " + (n - 1));
}
// Now, if it is not prime then
// find the least divisior
else
{
for(int i = 2; i * i <= n; i++)
{
// Check if divides n then
// it is a factor
if (n % i == 0)
{
// Required output is
// a = n/i & b = n/i*(n-1)
Console.Write(n / i + " " +
(n / i * (i - 1)));
break;
}
}
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 4;
// Function call
minDivisior(N);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
2 2
时间复杂度: O(平方(N))
辅助空间: O(1)