生成 GCD 为 1 且没有 Coprime 对的不同元素数组
给定一个整数N ,任务是生成一个包含N个不同整数的数组。使得数组所有元素的 GCD 为 1,但没有一对元素互质,即对于数组 GCD(A[i], A[ j]) ≠ 1。
注意:如果可能有超过 1 个答案,请打印其中任何一个。
例子:
Input: N = 4
Output: 30 70 42 105
Explanation: GCD(30, 70, 42, 105) = 1.
GCD(30, 70) = 10, GCD(30, 42) = 6, GCD(30, 105) = 15, GCD(70, 42) = 14, GCD(70, 105) = 35, GCD(42, 105) = 21.
Hence, it can be seen that GCD of all elements is 1, whereas GCD of all possible pairs is greater than 1.
Input: N = 6
Output: 10 15 6 12 24 48
方法:可以根据以下观察解决问题:
Suppose A1, A2, A3 . . . AN are N prime numbers. Let their product be P, i.e. P = A1 * A2 * A3 . . .*AN.
Then, the sequence P/A1 , P/A2 . . . P/AN = (A2 * A3 . . . AN), (A1 * A3 . . . AN), . . ., (A1 * A2 . . . AN-1)
This sequence have at least one common factor between any pair of elements but the overall GCD is 1
这可以按照以下步骤完成:
- 如果 N = 2,则返回 -1,因为 N = 2 不可能有这样的序列。
- 使用埃拉托色尼筛法存储前 N 个素数(例如在向量v中)。
- 计算向量“v”的所有元素的乘积并将其存储在一个变量中(比如“product”)。
- 现在,从 i = 0 迭代到 N-1 并在每次迭代时将答案的第 i 个元素存储为(product/v[i]) 。
- 返回结果向量。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
#define int unsigned long long
const int M = 100001;
bool primes[M + 1];
// Function to form an array such that
// pairwise gcd of all elements is not 1
// and gcd of whole sequence is 1
void printArray(int N)
{
// Such sequence is not possible for N=2
if (N == 2) {
cout << -1 << endl;
return;
}
// storing primes upto M using sieve
for (int i = 0; i < M; i++)
primes[i] = 1;
primes[0] = 0;
primes[1] = 0;
for (int i = 2; i * i <= M; i++) {
if (primes[i] == 1) {
for (int j = i * i; j <= M;
j += i) {
primes[j] = 0;
}
}
}
// Storing first N primes in vector v
vector v;
for (int i = 0; i < M; i++) {
if (v.size() < N
&& primes[i] == 1) {
v.push_back(i);
}
}
// Calculating product of
// first N prime numbers
int product = 1;
vector answer;
for (auto it : v) {
product *= it;
}
// Calculating answer sequence
for (int i = 0; i < N; i++) {
int num = product / v[i];
answer.push_back(num);
}
// Printing the answer
for (int i = 0; i < N; i++) {
cout << answer[i] << " ";
}
}
// Driver Code
int32_t main()
{
int N = 4;
// Function call
printArray(N);
return 0;
}
Java
// Java code to implement the approach
import java.util.ArrayList;
class GFG {
static int M = 100001;
static int[] primes = new int[M + 1];
// Function to form an array such that
// pairwise gcd of all elements is not 1
// and gcd of whole sequence is 1
static void printArray(int N)
{
// Such sequence is not possible for N=2
if (N == 2) {
System.out.println(-1);
return;
}
// storing primes upto M using sieve
for (int i = 0; i < M; i++)
primes[i] = 1;
primes[0] = 0;
primes[1] = 0;
for (int i = 2; i * i <= M; i++) {
if (primes[i] == 1) {
for (int j = i * i; j <= M; j += i) {
primes[j] = 0;
}
}
}
// Storing first N primes in arraylist v
ArrayList v = new ArrayList();
for (int i = 0; i < M; i++) {
if (v.size() < N && primes[i] == 1) {
v.add(i);
}
}
// Calculating product of
// first N prime numbers
int product = 1;
ArrayList answer
= new ArrayList();
;
for (int i = 0; i < v.size(); i++) {
product *= v.get(i);
}
// Calculating answer sequence
for (int i = 0; i < N; i++) {
int num = product / v.get(i);
answer.add(num);
}
// Printing the answer
for (int i = 0; i < N; i++) {
System.out.print(answer.get(i) + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
// Function call
printArray(N);
}
}
// This code is contributed by phasing.
Python3
# Python code to implement the approach
M = 100001
primes = [0]*(M + 1)
# Function to form an array such that
# pairwise gcd of all elements is not 1
# and gcd of whole sequence is 1
def printArrayint(N):
# Such sequence is not possible for N=2
if N == 2:
print(-1)
return
# storing primes upto M using sieve
for i in range(M):
primes[i] = 1
primes[0] = 0
primes[1] = 0
i = 2
while i * i <= M:
if primes[i] == 1:
j = i*i
while j <= M:
primes[j] = 0
j += i
i += 1
# Storing first N primes in vector v
v = []
for i in range(M):
if len(v) < N and primes[i] == 1:
v.append(i)
# Calculating product of
# first N prime numbers
product = 1
answer = []
for it in v:
product *= it
# Calculating answer sequence
for i in range(N):
num = product // v[i]
answer.append(num)
# Printing the answer
for i in range(N):
print(answer[i], end=" ")
# Driver Code
if __name__ == "__main__":
N = 4
printArrayint(N)
# This code is contributed by amnindersingh1414.
C#
// C# code to implement the approach
using System;
using System.Collections;
class GFG {
static int M = 100001;
static int[] primes = new int[M + 1];
// Function to form an array such that
// pairwise gcd of all elements is not 1
// and gcd of whole sequence is 1
static void printArray(int N)
{
// Such sequence is not possible for N=2
if (N == 2) {
Console.WriteLine(-1);
return;
}
// storing primes upto M using sieve
for (int i = 0; i < M; i++)
primes[i] = 1;
primes[0] = 0;
primes[1] = 0;
for (int i = 2; i * i <= M; i++) {
if (primes[i] == 1) {
for (int j = i * i; j <= M; j += i) {
primes[j] = 0;
}
}
}
// Storing first N primes in vector v
ArrayList v = new ArrayList();
for (int i = 0; i < M; i++) {
if (v.Count < N && primes[i] == 1) {
v.Add(i);
}
}
// Calculating product of
// first N prime numbers
int product = 1;
ArrayList answer = new ArrayList();
foreach(int it in v) { product *= (int)it; }
// Calculating answer sequence
for (int i = 0; i < N; i++) {
int num = product / (int)v[i];
answer.Add(num);
}
// Printing the answer
for (int i = 0; i < N; i++) {
Console.Write(answer[i] + " ");
}
}
// Driver Code
public static void Main()
{
int N = 4;
// Function call
printArray(N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
C++
// C++ code for above approach
#include
using namespace std;
#define int unsigned long long
// Function to form an array such that
// pairwise gcd of all elements is not 1
// and gcd of whole sequence is 1
void printArray(int N)
{
if (N == 1) {
cout << 2 << endl;
}
// Such sequence is not possible for N = 2
if (N == 2) {
cout << -1 << endl;
return;
}
int ans[N];
// Initializing initial 3 terms
// of the sequence
ans[0] = 10, ans[1] = 15, ans[2] = 6;
// Calculating next terms of the sequence
// by multiplying previous terms by 2
for (int i = 3; i < N; i++) {
ans[i] = 2LL * ans[i - 1];
}
// Printing the final sequence
for (int i = 0; i < N; i++) {
cout << ans[i] << " ";
}
}
// Driver Code
int32_t main()
{
int N = 4;
// Function call
printArray(N);
return 0;
}
Java
// JAVA code for above approach
import java.util.*;
class GFG {
// Function to form an array such that
// pairwise gcd of all elements is not 1
// and gcd of whole sequence is 1
public static void printArray(int N)
{
if (N == 1) {
System.out.println(2);
}
// Such sequence is not possible for N = 2
if (N == 2) {
System.out.println(-1);
return;
}
int ans[] = new int[N];
// Initializing initial 3 terms
// of the sequence
ans[0] = 10;
ans[1] = 15;
ans[2] = 6;
// Calculating next terms of the sequence
// by multiplying previous terms by 2
for (int i = 3; i < N; i++) {
ans[i] = 2 * ans[i - 1];
}
// Printing the final sequence
for (int i = 0; i < N; i++) {
System.out.print(ans[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
// Function call
printArray(N);
}
}
// This code is contributed by Taranpreet
Python3
# Python code for above approach
# Function to form an array such that
# pairwise gcd of all elements is not 1
# and gcd of whole sequence is 1
def printArray(N):
if (N == 1):
print(2)
# Such sequence is not possible for N = 2
if (N == 2):
print(-1)
return
ans = [0]*N
# Initializing initial 3 terms
# of the sequence
ans[0] = 10
ans[1] = 15
ans[2] = 6
# Calculating next terms of the sequence
# by multiplying previous terms by 2
for i in range(3, N):
ans[i] = 2 * ans[i - 1]
# Printing the final sequence
for i in range(N):
print(ans[i], end=" ")
# Driver Code
if __name__ == '__main__':
N = 4
printArray(N)
# This code is contributed by amnindersingh1414.
105 70 42 30
时间复杂度: O(M*(log(logM))) 其中 M 是一个很大的正数 [这里,100000]
辅助空间: O(M)
更好的方法:对于较大的 N 值,上述方法将失败,因为无法存储超过 15 个素数的乘积。根据以下观察可以避免这种情况:
Fix the first three distinct numbers with all possible products formed taking two among first three primes (say the numbers formed are X, Y and Z).
Then for the next elements keep on multiplying any one (say X) by one of the primes (say it becomes X’) and changing X to X’.
As the GCD of X, Y and Z is 1 the overall GCD will be one but no two elements will be coprime to each other.
For own convenience use the first 3 primes 2, 3, 5 and make the first 3 numbers as 10, 15, 6, and the others by multiplying with 2 i.e. 12, 24, 48, . . .
下面是上述方法的实现。
C++
// C++ code for above approach
#include
using namespace std;
#define int unsigned long long
// Function to form an array such that
// pairwise gcd of all elements is not 1
// and gcd of whole sequence is 1
void printArray(int N)
{
if (N == 1) {
cout << 2 << endl;
}
// Such sequence is not possible for N = 2
if (N == 2) {
cout << -1 << endl;
return;
}
int ans[N];
// Initializing initial 3 terms
// of the sequence
ans[0] = 10, ans[1] = 15, ans[2] = 6;
// Calculating next terms of the sequence
// by multiplying previous terms by 2
for (int i = 3; i < N; i++) {
ans[i] = 2LL * ans[i - 1];
}
// Printing the final sequence
for (int i = 0; i < N; i++) {
cout << ans[i] << " ";
}
}
// Driver Code
int32_t main()
{
int N = 4;
// Function call
printArray(N);
return 0;
}
Java
// JAVA code for above approach
import java.util.*;
class GFG {
// Function to form an array such that
// pairwise gcd of all elements is not 1
// and gcd of whole sequence is 1
public static void printArray(int N)
{
if (N == 1) {
System.out.println(2);
}
// Such sequence is not possible for N = 2
if (N == 2) {
System.out.println(-1);
return;
}
int ans[] = new int[N];
// Initializing initial 3 terms
// of the sequence
ans[0] = 10;
ans[1] = 15;
ans[2] = 6;
// Calculating next terms of the sequence
// by multiplying previous terms by 2
for (int i = 3; i < N; i++) {
ans[i] = 2 * ans[i - 1];
}
// Printing the final sequence
for (int i = 0; i < N; i++) {
System.out.print(ans[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
// Function call
printArray(N);
}
}
// This code is contributed by Taranpreet
Python3
# Python code for above approach
# Function to form an array such that
# pairwise gcd of all elements is not 1
# and gcd of whole sequence is 1
def printArray(N):
if (N == 1):
print(2)
# Such sequence is not possible for N = 2
if (N == 2):
print(-1)
return
ans = [0]*N
# Initializing initial 3 terms
# of the sequence
ans[0] = 10
ans[1] = 15
ans[2] = 6
# Calculating next terms of the sequence
# by multiplying previous terms by 2
for i in range(3, N):
ans[i] = 2 * ans[i - 1]
# Printing the final sequence
for i in range(N):
print(ans[i], end=" ")
# Driver Code
if __name__ == '__main__':
N = 4
printArray(N)
# This code is contributed by amnindersingh1414.
10 15 6 12
时间复杂度: O(N)
辅助空间: O(N)