📜  GCD等于1的前N个数字的隔离组

📅  最后修改于: 2021-05-07 09:23:41             🧑  作者: Mango

给定数字N。任务是将所有数字从1到N进行分组,因此每组中所有数字的GCD为1,因为必须将组的数量减到最少。
例子:

方法:我们知道每个连续数字的GCD为1的事实,因此我们将使用此事实来解决此问题。步骤如下:

  1. 如果数字为3,则GCD(1、2、3)只能为1的一组{1,2,3}
  2. [1,N / 2]上进行其他迭代(例如i ) 并使用(2 * i – 1,2 * i)形成组并将其存储在向量数组中(例如arr [] [] )。
  3. 存储在arr [] []中的组是所需的组。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the groups
void printGroups(vector A[], int N)
{
 
    // Interate over [1, N/2];
    for (int i = 1; i <= N / 2 + 1; i++) {
 
        // Print all pairs
        for (int j = 0; j < A[i].size(); j++) {
            cout << A[i][j] << ' ';
        }
        cout << endl;
    }
}
 
// Function to form a groups with GCD 1
void formGroups(int N)
{
    int i;
 
    // Corner case
    if (N == 3) {
        cout << "1 2 3";
        return;
    }
 
    // Array of vectors to store the
    // possible pairs
    vector arr[N / 2 + 2];
 
    for (i = 1; i <= N / 2; i++) {
 
        // Push the pair (2*i - 1, 2*i)
        arr[i].push_back(2 * i - 1);
        arr[i].push_back(2 * i);
    }
 
    // If N is odd then push the
    // last element N
    if (N & 1) {
        arr[i].push_back(N);
    }
 
    // Function Call
    printGroups(arr, N);
 
    return;
}
 
// Driver Code
int main()
{
    int N = 10;
 
    // Function Call
    formGroups(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to print the groups
static void printGroups(Vector A[],
                        int N)
{
     
    // Interate over [1, N/2];
    for(int i = 1; i <= N / 2 + 1; i++)
    {
         
        // Print all pairs
        for(int j = 0; j < A[i].size(); j++)
        {
            System.out.print(A[i].get(j) + " ");
        }
        System.out.println();
    }
}
 
// Function to form a groups with GCD 1
static void formGroups(int N)
{
    int i;
 
    // Corner case
    if (N == 3)
    {
        System.out.print("1 2 3");
        return;
    }
 
    // Array of vectors to store the
    // possible pairs
    @SuppressWarnings("unchecked")
    Vector []arr = new Vector[N / 2 + 2];
    for(int k = 0; k < arr.length; k++)
        arr[k] = new Vector();
         
    for(i = 1; i <= N / 2; i++)
    {
         
        // Push the pair (2*i - 1, 2*i)
        arr[i].add(2 * i - 1);
        arr[i].add(2 * i);
    }
 
    // If N is odd then push the
    // last element N
    if ((N & 1) != 0)
    {
        arr[i].add(N);
    }
     
    // Function call
    printGroups(arr, N);
 
    return;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 10;
 
    // Function call
    formGroups(N);
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program for
# the above approach
 
# Function to print the groups
def printGroups(A, N):
 
    # Interate over [1, N / 2];
    for i in range (0, N // 2 ):
 
        # Print all pairs
        for j in range (len(A[i])):
            print (A[i][j], end = ' ')
        
        print ()
 
# Function to form a
# groups with GCD 1
def formGroups(N):
 
    # Corner case
    if (N == 3):
        print ("1 2 3")
        return
   
    # Array of vectors
    # to store the
    # possible pairs
    arr = []
 
    for i in range (1, N // 2 + 1):
        P = []
         
        # Push the pair
        # (2 * i - 1, 2 * i)
        P.append(2 * i - 1)
        P.append(2 * i)
        arr.append(P)
    
    # If N is odd then push the
    # last element N
    if (N & 1):
        arr.append(N)
    
    # Function Call
    printGroups(arr, N)
 
    return
 
# Driver Code
if __name__ == "__main__":
 
    N = 10
 
    # Function Call
    formGroups(N)
    
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to print the groups
static void printGroups(List []A,
                        int N)
{
     
    // Interate over [1, N/2];
    for(int i = 1; i <= N / 2 + 1; i++)
    {
         
        // Print all pairs
        for(int j = 0; j < A[i].Count; j++)
        {
            Console.Write(A[i][j] + " ");
        }
        Console.WriteLine();
    }
}
 
// Function to form a groups with GCD 1
static void formGroups(int N)
{
    int i;
 
    // Corner case
    if (N == 3)
    {
        Console.Write("1 2 3");
        return;
    }
 
    // Array of vectors to store the
    // possible pairs
    List []arr = new List[N / 2 + 2];
    for(int k = 0; k < arr.Length; k++)
        arr[k] = new List();
         
    for(i = 1; i <= N / 2; i++)
    {
         
        // Push the pair (2*i - 1, 2*i)
        arr[i].Add(2 * i - 1);
        arr[i].Add(2 * i);
    }
 
    // If N is odd then push the
    // last element N
    if ((N & 1) != 0)
    {
        arr[i].Add(N);
    }
     
    // Function call
    printGroups(arr, N);
 
    return;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 10;
 
    // Function call
    formGroups(N);
}
}
 
// This code is contributed by amal kumar choubey


输出:
1 2 
3 4 
5 6 
7 8 
9 10

时间复杂度: O(N),其中N是给定的数字。