📜  生成 GCD 为 1 且没有 Coprime 对的不同元素数组

📅  最后修改于: 2022-05-13 01:56:06.093000             🧑  作者: Mango

生成 GCD 为 1 且没有 Coprime 对的不同元素数组

给定一个整数N ,任务是生成一个包含N不同整数的数组。使得数组所有元素的 GCD 为 1,但没有一对元素互质,即对于数组 GCD(A[i], A[ j]) ≠ 1。

注意:如果可能有超过 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 个素数的乘积。根据以下观察可以避免这种情况:

下面是上述方法的实现。

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)