找到两个数字 B 和 C 使得它们的乘积是 A 并且它们的 GCD 最大
给定一个正整数A 。任务是找到两个数字B和C 这样他们的产品是A ,他们的GCD应该是最大的。
例子:
Input: A = 72
Output: 12 6
Explanation: The product of 12 and 6 is 72 and GCD(12, 6) is 6 which is maximum possible.
Input: A = 54
Output: 6 9
Explanation: The product of 6 and 9 is 54, gcd(6, 9) is 3 which is maximum possible.
方法:这个问题可以通过生成A 的素数来解决。要使GCD最大化,唯一可能的方法是选择不同的素因子,这样它们的乘积就会得到最大的GCD 。请按照以下步骤解决问题。
- 创建一个函数,比如primeFactors()来查找一个数字的所有素因子。
- 首先调用primeFactors()并传递A并通过引用传递一个数组以以排序方式存储所有素数因子。
- 遍历素因子数组,并将A的所有因子交替分布到B和C ,使得,
- B = primefactor[0] * primefactor[2] * primefactor[4] – – – 以此类推。
- C = primefactor[1] * primefactor[3] * primefactor[5] – – – 以此类推。
- 输出用空格分隔的数字B和C。
For Example: N = 72
Prime Factorization of 72 = 2 * 2 * 2 * 3 * 3.
primefactor[] = {2, 2, 2, 3, 3}
B = primefactor[0] * primefactor[2] * primefactor[4] => 2 * 2 * 3 = 12.
C = primefactor[1] * primefactor[3] => 2 * 3 = 6.
Hence, B = 12 and C = 6.
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to stores prime factors
// of a number in a vector.
void primeFactors(int A, vector& prime)
{
while (A % 2 == 0) {
prime.push_back(2);
A = A / 2;
}
for (int i = 3; i <= sqrt(A); i += 2) {
while (A % i == 0) {
prime.push_back(i);
A = A / i;
}
}
if (A > 2)
prime.push_back(A);
}
// Find the numbers B and C
// such that gcd(B, C) is max
// and product is A.
void maxPairGCD(int A)
{
// Vector to store prime factors
vector prime;
primeFactors(A, prime);
int B = 1, C = 1;
int temp = 0;
for (int i = 0; i < prime.size(); i++) {
if (temp == 0)
B *= prime[i];
else
C *= prime[i];
temp = temp ^ 1;
}
cout << B << " " << C;
}
// Driver code
int main()
{
int A = 72;
// Function Call
maxPairGCD(A);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.ArrayList;
class GFG
{
// Function to stores prime factors
// of a number in a vector.
static void primeFactors(int A, ArrayList prime)
{
while (A % 2 == 0)
{
prime.add(2);
A = A / 2;
}
for (int i = 3; i <= Math.sqrt(A); i += 2)
{
while (A % i == 0)
{
prime.add(i);
A = A / i;
}
}
if (A > 2)
prime.add(A);
}
// Find the numbers B and C
// such that gcd(B, C) is max
// and product is A.
static void maxPairGCD(int A)
{
// Vector to store prime factors
ArrayList prime = new ArrayList();
primeFactors(A, prime);
int B = 1, C = 1;
int temp = 0;
for (int i = 0; i < prime.size(); i++) {
if (temp == 0)
B *= prime.get(i);
else
C *= prime.get(i);
temp = temp ^ 1;
}
System.out.println(B + " " + C);
}
// Driver code
public static void main(String args[])
{
int A = 72;
// Function Call
maxPairGCD(A);
}
}
// This code is contributed by Saurabh Jaiswal
Python3
# Python 3 program for above approach
import math
# Function to stores prime factors
# of a number in a vector.
def primeFactors(A, prime):
while (A % 2 == 0):
prime.append(2)
A = A / 2
for i in range(3, int(math.sqrt(A)) + 1, 2):
while (A % i == 0):
prime.append(i)
A = A / i
if (A > 2):
prime.append(A)
# Find the numbers B and C
# such that gcd(B, C) is max
# and product is A.
def maxPairGCD(A):
# Vector to store prime factors
prime = []
primeFactors(A, prime)
B = 1
C = 1
temp = 0
for i in range(len(prime)):
if (temp == 0):
B *= prime[i]
else:
C *= prime[i]
temp = temp ^ 1
print(B, C)
# Driver code
if __name__ == "__main__":
A = 72
# Function Call
maxPairGCD(A)
# This code is contributed by ukasp.
C#
// C# program to implement
// the above approach
using System;
using System.Collections;
class GFG
{
// Function to stores prime factors
// of a number in a vector.
static void primeFactors(int A, ArrayList prime)
{
while (A % 2 == 0) {
prime.Add(2);
A = A / 2;
}
for (int i = 3; i <= Math.Sqrt(A); i += 2) {
while (A % i == 0) {
prime.Add(i);
A = A / i;
}
}
if (A > 2)
prime.Add(A);
}
// Find the numbers B and C
// such that gcd(B, C) is max
// and product is A.
static void maxPairGCD(int A)
{
// Vector to store prime factors
ArrayList prime = new ArrayList();
primeFactors(A, prime);
int B = 1, C = 1;
int temp = 0;
for (int i = 0; i < prime.Count; i++) {
if (temp == 0)
B *= (int)prime[i];
else
C *= (int)prime[i];
temp = temp ^ 1;
}
Console.Write(B + " " + C);
}
// Driver code
public static void Main()
{
int A = 72;
// Function Call
maxPairGCD(A);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
12 6
时间复杂度: O(Sqrt(A) )
辅助空间: O(log(A))