📌  相关文章
📜  在给定条件下生成数组的元素

📅  最后修改于: 2021-04-22 05:53:33             🧑  作者: Mango

给定一个整数N,对于2N范围内的每个整数i ,分配一个正整数a_i满足以下条件:

  • 对于任何一对索引(i,j) ,如果ij是互质的,则a_i \neq a_j
  • 全部的最大值a_i应该最小化(即最大值应该尽可能小)。

    如果一对整数的gcd(i,j)= 1,则称它们为互质。

    例子:

    方法:在从2n的索引处分配值时,我们必须牢记两件事。首先,对于任何一对(i,j) ,如果ij是互质的,那么我们就不能为这对索引分配相同的值。第二,我们必须最小化分配给每个索引的最大值从2n

    如果为每个素数分配了不同的数字,则可以实现此目的,因为所有素数都是互质数。然后,在所有合成位置上与其任意除数相同的位置上赋值。此解决方案适用于任何对(i,j)i被赋予相同数量的除数, j也是如此,因此,如果它们是互质的,则不能被赋予相同的数量。

    可以通过在构建Eratosthenes筛时进行一些小的更改来实现此方法。
    当我们第一次遇到素数时,请为其分配唯一的最小值及其所有倍数。这样,所有素数索引都应具有唯一值,所有综合索引都应具有与其任何素数除数相同的值。

    下面是上述方法的实现:

    C++
    // C++ implementation of the approach
    #include 
    using namespace std;
      
    // Function to generate the required array
    void specialSieve(int n)
    {
      
        // Initialize cnt variable for assigning
        // unique value to prime and its multiples
        int cnt = 0;
        int prime[n + 1];
      
        for (int i = 0; i <= n; i++)
            prime[i] = 0;
      
        for (int i = 2; i <= n; i++) {
      
            // When we get a prime for the first time
            // then assign a unique smallest value to
            // it and all of its multiples
            if (!prime[i]) {
                cnt++;
                for (int j = i; j <= n; j += i)
                    prime[j] = cnt;
            }
        }
      
        // Print the generated array
        for (int i = 2; i <= n; i++)
            cout << prime[i] << " ";
    }
      
    // Driver code
    int main()
    {
        int n = 6;
      
        specialSieve(n);
      
        return 0;
    }


    Java
    // Java implementation of the approach
    import java.io.*;
      
    class GFG
    {
      
    // Function to generate the required array
    static void specialSieve(int n)
    {
      
        // Initialize cnt variable for assigning
        // unique value to prime and its multiples
        int cnt = 0;
        int prime[] = new int[n+1];
      
        for (int i = 0; i <= n; i++)
            prime[i] = 0;
      
        for (int i = 2; i <= n; i++)
        {
      
            // When we get a prime for the first time
            // then assign a unique smallest value to
            // it and all of its multiples
            if (!(prime[i]>0)) 
            {
                cnt++;
                for (int j = i; j <= n; j += i)
                    prime[j] = cnt;
            }
        }
      
        // Print the generated array
        for (int i = 2; i <= n; i++)
            System.out.print(prime[i] + " ");
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int n = 6;
      
    specialSieve(n);
      
    }
    }
      
    // This code is contrubuted by anuj_67..


    Python3
    # Python3 implementation of the approach 
      
    # Function to generate the required array 
    def specialSieve(n) : 
      
        # Initialize cnt variable for assigning 
        # unique value to prime and its multiples 
        cnt = 0; 
        prime = [0]*(n + 1); 
      
        for i in range(2, n + 1) :
      
            # When we get a prime for the first time 
            # then assign a unique smallest value to 
            # it and all of its multiples 
            if (not prime[i]) :
                cnt += 1; 
                for j in range(i, n + 1, i) : 
                    prime[j] = cnt; 
      
        # Print the generated array 
        for i in range(2, n + 1) :
            print(prime[i],end = " "); 
      
      
    # Driver code 
    if __name__ == "__main__" : 
      
        n = 6; 
        specialSieve(n); 
          
    # This code is contributed by AnkitRai01


    C#
    // C# implementation of the approach
    using System;
      
    class GFG
    {
      
    // Function to generate the required array
    static void specialSieve(int n)
    {
      
        // Initialize cnt variable for assigning
        // unique value to prime and its multiples
        int cnt = 0;
        int []prime = new int[n+1];
      
        for (int i = 0; i <= n; i++)
            prime[i] = 0;
      
        for (int i = 2; i <= n; i++)
        {
      
            // When we get a prime for the first time
            // then assign a unique smallest value to
            // it and all of its multiples
            if (!(prime[i] > 0)) 
            {
                cnt++;
                for (int j = i; j <= n; j += i)
                    prime[j] = cnt;
            }
        }
      
        // Print the generated array
        for (int i = 2; i <= n; i++)
            Console.Write(prime[i] + " ");
    }
      
    // Driver code
    public static void Main ()
    {
        int n = 6;
      
        specialSieve(n);
      
    }
    }
      
    // This code is contrubuted by anuj_67..


    输出:
    1 2 1 3 2
    

    时间复杂度: O(N Log N)