给定两个整数N和K ,任务是找到K个对数为N的因数,以使每对因数的GCD为1。
注意:对于给定的数字, K个素数因子始终存在
例子:
Input: N = 6, K = 1
Output: 2 3
Explanation:
Since 2 and 3 are both factors of 6 and gcd(2, 3) = 1.
Input: N = 120, K = 4
Output:
2 3
3 4
3 5
4 5
天真的方法:
最简单的方法是检查直到N的所有数字,并检查该对的GCD是否为1。
时间复杂度: O(N 2 )
空间复杂度: O(1)
线性进近:
查找N的所有可能的除数,并将其存储在另一个数组中。遍历数组以从数组中搜索所有可能的互质对,并打印它们。
时间复杂度: O(N)
空间复杂度: O(N)
高效方法:
请按照以下步骤解决问题:
- 可以观察到,如果任何数量的GCD,说x,其中1始终为1,即GCD(1,X)= 1。
- 由于1将始终是一个因子N,简单地打印的N中的任K系数与1作为互质对。
下面是上述方法的实现。
C++
// C++ implementation of
// the above approach
#include
using namespace std;
// Function prints the
// required pairs
void FindPairs(int n, int k)
{
// First co-prime pair
cout << 1 << " " << n << endl;
// As a pair (1 n) has
// already been Printed
k--;
for (long long i = 2;
i <= sqrt(n); i++) {
// If i is a factor of N
if (n % i == 0) {
cout << 1 << " "
<< i << endl;
k--;
if (k == 0)
break;
// Since (i, i) won't form
// a coprime pair
if (i != n / i) {
cout << 1 << " "
<< n / i << endl;
k--;
}
if (k == 0)
break;
}
}
}
// Driver Code
int main()
{
int N = 100;
int K = 5;
FindPairs(N, K);
return 0;
}
Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
// Function prints the
// required pairs
static void FindPairs(int n, int k)
{
// First co-prime pair
System.out.print(1 + " " + n + "\n");
// As a pair (1 n) has
// already been Printed
k--;
for(long i = 2; i <= Math.sqrt(n); i++)
{
// If i is a factor of N
if (n % i == 0)
{
System.out.print(1 + " " + i + "\n");
k--;
if (k == 0)
break;
// Since (i, i) won't form
// a coprime pair
if (i != n / i)
{
System.out.print(1 + " " +
n / i + "\n");
k--;
}
if (k == 0)
break;
}
}
}
// Driver Code
public static void main(String[] args)
{
int N = 100;
int K = 5;
FindPairs(N, K);
}
}
// This code is contributed by princiraj1992
Python3
# Python3 implementation of
# the above approach
from math import sqrt
# Function prints the
# required pairs
def FindPairs(n, k):
# First co-prime pair
print(1, n)
# As a pair (1 n) has
# already been Printed
k -= 1
for i in range(2, int(sqrt(n)) + 1):
# If i is a factor of N
if(n % i == 0):
print(1, i)
k -= 1
if(k == 0):
break
# Since (i, i) won't form
# a coprime pair
if(i != n // i):
print(1, n // i)
k -= 1
if(k == 0):
break
# Driver Code
if __name__ == '__main__':
N = 100
K = 5
FindPairs(N, K)
# This code is contributed by Shivam Singh
C#
// C# implementation of
// the above approach
using System;
class GFG{
// Function prints the
// required pairs
static void FindPairs(int n, int k)
{
// First co-prime pair
Console.Write(1 + " " + n + "\n");
// As a pair (1 n) has
// already been Printed
k--;
for(long i = 2; i <= Math.Sqrt(n); i++)
{
// If i is a factor of N
if (n % i == 0)
{
Console.Write(1 + " " + i + "\n");
k--;
if (k == 0)
break;
// Since (i, i) won't form
// a coprime pair
if (i != n / i)
{
Console.Write(1 + " " +
n / i + "\n");
k--;
}
if (k == 0)
break;
}
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 100;
int K = 5;
FindPairs(N, K);
}
}
// This code is contributed by Rajput-Ji
输出:
1 100
1 2
1 50
1 4
1 25
时间复杂度: O(sqrt(N))
辅助空间: O(1)