📌  相关文章
📜  作为两个不同质数的乘积的前 N 个自然数的按位异或

📅  最后修改于: 2021-09-02 07:28:16             🧑  作者: Mango

给定一个正整数N ,任务是计算前N 个数字的按位异或,这些数字恰好是两个不同素数的乘积。

例子:

朴素的方法:最简单的方法是将每个数字迭代到N,并使用素因数分解方法找到每个数字的素因数。发现不同质因数的个数为 2 的数,然后用答案计算它们的异或。检查所有数字后,打印得到的答案。
时间复杂度: O(N*√N)
辅助空间: O(1)

高效的方法:为了优化上述方法,想法是使用 Eratosthenes 的筛子并稍作修改。请按照以下步骤解决问题:

  • 将变量ans初始化为0以存储所需的结果。
  • 创建一个整数数组,编曲[大小为N + 1],并用全零,初始化其中ARR [i]表示的不同素数的数量。
  • 使用变量i在范围[2, N] 中迭代,如果arr[i] 的值为0,则使用变量j遍历i 的所有倍数并将arr[j]值增加1,因为ij 的主要因子。
  • 使用变量i在范围[2, N] 中迭代,如果arr[i]等于2 ,则对ians进行异或
  • 打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count prime factors
// using Sieve of Eratosthenes
void sieve(int arr[], int n)
{
    // Iterate in the [2, N]
    for (int i = 2; i <= n; i++) {
 
        // If the current number is prime
        if (arr[i] == 0)
 
            // Iterate over all multiples of i
            for (int j = 2 * i; j <= n; j += i) {
 
                // Increment arr[j] by 1 since
                // i is a prime factor of j
                arr[j]++;
            }
    }
}
 
// Function to find Bitwise XOR
// of first N natural numbers
// satisfying the given condition
void findXOR(int n)
{
    // arr[i]: Stores the number of
    // distinct prime factors of i
    int arr[n + 1] = { 0 };
 
    // Initialize the base cases
    arr[0] = arr[1] = 1;
 
    // Function Call to fill
    // the array, arr[]
    sieve(arr, n);
 
    // Store the required result
    int ans = 0;
 
    // Iterate over the range [2, N]
    for (int i = 2; i <= n; i++) {
 
        // Check if the i-th number has
        // exactly two distinct prime factor
        if (arr[i] == 2) {
 
            // If true, update the answer
            ans = (ans ^ i);
        }
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int n = 20;
 
    // Function Call
    findXOR(n);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG {
 
    // Function to count prime factors
    // using Sieve of Eratosthenes
    static void sieve(int arr[], int n)
    {
        // Iterate in the [2, N]
        for (int i = 2; i <= n; i++) {
 
            // If the current number is prime
            if (arr[i] == 0)
 
                // Iterate over all multiples of i
                for (int j = 2 * i; j <= n; j += i) {
 
                    // Increment arr[j] by 1 since
                    // i is a prime factor of j
                    arr[j]++;
                }
        }
    }
 
    // Function to find Bitwise XOR
    // of first N natural numbers
    // satisfying the given condition
    static void findXOR(int n)
    {
       
        // arr[i]: Stores the number of
        // distinct prime factors of i
        int arr[] = new int[n + 1];
 
        // Initialize the base cases
        arr[0] = arr[1] = 1;
 
        // Function Call to fill
        // the array, arr[]
        sieve(arr, n);
 
        // Store the required result
        int ans = 0;
 
        // Iterate over the range [2, N]
        for (int i = 2; i <= n; i++) {
 
            // Check if the i-th number has
            // exactly two distinct prime factor
            if (arr[i] == 2) {
 
                // If true, update the answer
                ans = (ans ^ i);
            }
        }
 
        // Print the result
        System.out.println(ans);
    }
 
    // Driver code
    public static void main(String[] args)
    {
      // Given Input
        int n = 20;
 
        // Function Call
        findXOR(n);
    }
}
 
// This code is contributed by abhinavjain194


输出:
7

时间复杂度: O(N*log(logN))
辅助空间: O(N)