📌  相关文章
📜  生成一个 N 大小的唯一元素数组,其中相邻对的 GCD 为 X

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

生成一个 N 大小的唯一元素数组,其中相邻对的 GCD 为 X

给定两个整数N (总是偶数)和X ,任务是找到一个大小为N且具有不同数字的数组,使得相邻对的GCD之和(其中每个元素仅是一对的一部分)为X 。如果没有这样的数组是可能的,则返回 -1。

注意:如果有超过 1 个可能的序列,则返回其中任何一个

例子 :

方法:这个想法是

extra 的公式可以推导如下:

请按照以下步骤解决问题:

  • 将第一对设为extra(extra*2)
  • 使其他对由连续整数组成,使得它们的 GCD = 1。

下面是上述方法的实现:

C++
// C++ Program of the above approach.
 
#include 
using namespace std;
 
// Function to get the
// Resultant array
void solve(int n, int x)
{
    // Formula generated to calculate
    // Extra gcd required
    int extra = x - n / 2 + 1;
 
    // Print extra required in the
    // Starting of the array sequence
    cout << extra << " ";
 
    // Multiplied starting element by 2
    // Such that after taking pair of both
    // Starting and next - starting
    // Element it will be equal to extra
    extra = extra * 2;
 
    // Printing the leftover pairs
    // Or elements with gcd = 1
    for (int i = 1; i < n; i++) {
        cout << (extra++) << " ";
    }
}
 
// Driver Code
int main()
{
    int N = 4, X = 3;
    solve(N, X);
    return 0;
}


Java
// Java Program of the above approach.
import java.util.*;
public class GFG {
 
  // Function to get the
  // Resultant array
  static void solve(int n, int x)
  {
 
    // Formula generated to calculate
    // Extra gcd required
    int extra = x - n / 2 + 1;
 
    // Print extra required in the
    // Starting of the array sequence
    System.out.print(extra + " ");
 
    // Multiplied starting element by 2
    // Such that after taking pair of both
    // Starting and next - starting
    // Element it will be equal to extra
    extra = extra * 2;
 
    // Printing the leftover pairs
    // Or elements with gcd = 1
    for (int i = 1; i <= n; i++) {
      System.out.print((extra++) + " ");
    }
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int N = 4, X = 3;
    solve(N, X);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python code for the above approach
 
# Function to get the
# Resultant array
def solve( n,  x):
 
    # Formula generated to calculate
    # Extra gcd required
    extra = x - (int)(n / 2) + 1;
 
    # Print extra required in the
    # Starting of the array sequence
    print(extra, end= " ");
 
    # Multiplied starting element by 2
    # Such that after taking pair of both
    # Starting and next - starting
    # Element it will be equal to extra
    extra = extra * 2;
 
    # Printing the leftover pairs
    # Or elements with gcd = 1
    for i in range(1,n+1):
        print(extra, end = " ");
        extra = extra + 1;
 
# Driver Code
N = 4
X = 3
solve(N, X);
    
# This code is contributed by Potta Lokesh


C#
// C# Program of the above approach.
using System;
class GFG {
 
  // Function to get the
  // Resultant array
  static void solve(int n, int x)
  {
 
    // Formula generated to calculate
    // Extra gcd required
    int extra = x - n / 2 + 1;
 
    // Print extra required in the
    // Starting of the array sequence
    Console.Write(extra + " ");
 
    // Multiplied starting element by 2
    // Such that after taking pair of both
    // Starting and next - starting
    // Element it will be equal to extra
    extra = extra * 2;
 
    // Printing the leftover pairs
    // Or elements with gcd = 1
    for (int i = 1; i <= n; i++) {
      Console.Write((extra++) + " ");
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 4, X = 3;
    solve(N, X);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
2 4 5 6 7 

时间复杂度: O(N)
空间复杂度: O(1)