找出序列的 GCD 为 1 且每对的 GCD 大于 1 的 N 个不同整数
给定一个整数N ,任务是找到一个由N个不同的正整数组成的序列,使得该序列的最大公约数为 1,并且所有可能的元素对的 GCD 大于 1。
Input: N = 4
Output: 84 60 105 70
Explanation: The GCD(84, 60, 105, 70) is 1 and the GCD of all possible pair of elements i.e, {(84, 60), (84, 105), (84, 70), (60, 105), (60, 70), (105, 70)} is greater than 1.
Input: N = 3
Output: 6 10 15
方法:这个问题可以通过使用Set Data Structure来解决。想法是选择(a*b, b*c, c*a)形式的三个整数,因为三个的 GCD 为 1,并且三个的成对GCD始终大于 1。此外,只需将三个整数,直到序列包含所需数量的整数。 (a*b, b*c, c*a)形式的一组整数是(6, 10, 15) 。因此,将6 、 10和15的倍数添加到序列中并打印所需的整数个数。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to find the sequence of
// distinct integers with GCD equal
// to 1 and every pair has GCD > 1.
void findSequence(int N)
{
// Special case
if (N == 3) {
cout << "6 10 15" << endl;
return;
}
// Set to avoid duplicates
set s;
s.insert(6);
s.insert(10);
s.insert(15);
// Add multiples of 6
for (int i = 12; i <= 10000; i += 6)
s.insert(i);
// Add multiples of 10
for (int i = 20; i <= 10000; i += 10)
s.insert(i);
// Add multiples of 15
for (int i = 30; i <= 10000; i += 15)
s.insert(i);
int cnt = 0;
// Print first N numbers of set
for (int x : s) {
cout << x << " ";
cnt++;
if (cnt == N) {
break;
}
}
}
// Driver Code
int main()
{
int N = 3;
findSequence(N);
return 0;
}
Java
// Java program for above approach
import java.util.*;
class GFG{
// Function to find the sequence of
// distinct integers with GCD equal
// to 1 and every pair has GCD > 1.
static void findSequence(int N)
{
// Special case
if (N == 3)
{
System.out.println("6 10 15");
return;
}
// Set to avoid duplicates
Set s = new HashSet();
s.add(6);
s.add(10);
s.add(15);
// Add multiples of 6
for(int i = 12; i <= 10000; i += 6)
s.add(i);
// Add multiples of 10
for(int i = 20; i <= 10000; i += 10)
s.add(i);
// Add multiples of 15
for(int i = 30; i <= 10000; i += 15)
s.add(i);
int cnt = 0;
// Print first N numbers of set
for(Integer x : s)
{
System.out.print(x + " ");
cnt++;
if (cnt == N)
{
break;
}
}
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
findSequence(N);
}
}
// This code is contributed by Potta Lokesh
Python3
# python program for above approach
# Function to find the sequence of
# distinct integers with GCD equal
# to 1 and every pair has GCD > 1.
def findSequence(N):
# Special case
if (N == 3):
print("6 10 15")
return
# Set to avoid duplicates
s = set()
s.add(6)
s.add(10)
s.add(15)
# Add multiples of 6
for i in range(12, 10001, 6):
s.add(i)
# Add multiples of 10
for i in range(20, 10001, 10):
s.add(i)
# Add multiples of 15
for i in range(30, 10001, 15):
s.add(i)
cnt = 0
# Print first N numbers of set
for x in s:
print(x, end=" ")
cnt += 1
if (cnt == N):
break
# Driver Code
if __name__ == "__main__":
N = 3
findSequence(N)
# This code is contributed by rakeshsahni
C#
// C# program for above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find the sequence of
// distinct integers with GCD equal
// to 1 and every pair has GCD > 1.
static void findSequence(int N)
{
// Special case
if (N == 3) {
Console.WriteLine("6 10 15");
return;
}
// Set to avoid duplicates
HashSet s = new HashSet();
s.Add(6);
s.Add(10);
s.Add(15);
// Add multiples of 6
for (int i = 12; i <= 10000; i += 6)
s.Add(i);
// Add multiples of 10
for (int i = 20; i <= 10000; i += 10)
s.Add(i);
// Add multiples of 15
for (int i = 30; i <= 10000; i += 15)
s.Add(i);
int cnt = 0;
// Print first N numbers of set
foreach(int x in s)
{
Console.Write(x + " ");
cnt++;
if (cnt == N) {
break;
}
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 3;
findSequence(N);
}
}
// Thiss code is contributed by ukasp.
Javascript
输出
6 10 15
时间复杂度: O(N*log N)
辅助空间: O(N)