生成一个 N 大小的唯一元素数组,其中相邻对的 GCD 为 X
给定两个整数N (总是偶数)和X ,任务是找到一个大小为N且具有不同数字的数组,使得相邻对的GCD之和(其中每个元素仅是一对的一部分)为X 。如果没有这样的数组是可能的,则返回 -1。
注意:如果有超过 1 个可能的序列,则返回其中任何一个
例子 :
Input: N = 6, X = 6
Output: 4 8 9 10 11 12
Explanation: Starting with first two numbers, gcd of (4, 8) = 4, then gcd of (9, 10) = 1, gcd of (11, 12) = 1.
Thus, gcd sums up to 4 + 1 + 1 = 6 which is equal to X.
Input: N = 4, X = 1
Output: -1
方法:这个想法是
If the value of X is less than N/2, it’s obvious that it’s not possible to make a minimum GCD of N/2.
So, if X < N/2, answer = -1.
Otherwise, lets denote extra = X – N/2 + 1, which signifies how much is the difference between X and sum (sum = the sum of GCD when all the adjacent pairs have GCD = 1 except for the first one) i.e. this must be GCD of the first pair
Thus, based upon the above fact, extra + sum of GCD of other N/2-1 pairs would be equal to X.
extra 的公式可以推导如下:
For N numbers there are a total of N/2 pairs.
If the first pair is left and all the other pairs are made to have a GCD of 1.
So the total sum of GCDs of these pairs are (N/2 -1)*1 = (N/2 -1)
So extra = X – (N/2 – 1) = X – N/2 + 1
请按照以下步骤解决问题:
- 将第一对设为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)