📌  相关文章
📜  给定范围内所有奇数的按位异或

📅  最后修改于: 2021-10-26 05:43:39             🧑  作者: Mango

给定一个整数N ,任务是找到[1, N]范围内所有奇数的按位异或。

例子:

朴素的方法:解决问题的最简单方法是迭代范围[1, N]并为每个值检查它是否为奇数。计算找到的每个奇数元素的按位异或并将其打印为所需的结果。
时间复杂度: O(N)
辅助空间: O(1)

高效方法:为了优化上述方法,其思想是使用以下公式计算所有小于或等于N 的奇数的按位异或:

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

  • 将范围[1, N]中数字的异或存储在变量中,例如K
  • 如果 N 是奇数,则将范围[1, (N – 1) / 2]中数字的 XOR 存储在变量中,例如g 。否则,将[1, N / 2]范围内数字的 XOR 存储在g 中
  • 使用导出的公式,将结果更新为K ^ (2 & g)
  • 完成上述步骤后,打印结果的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate Bitwise
// XOR of odd numbers in the range [1, N]
int findXOR(int n)
{
    // N & 3 is equivalent to n % 4
    switch (n & 3) {
 
    // If n is multiple of 4
    case 0:
        return n;
 
    // If n % 4 gives remainder 1
    case 1:
        return 1;
 
    // If n % 4 gives remainder 2
    case 2:
        return n + 1;
 
    // If n % 4 gives remainder 3
    case 3:
        return 0;
    }
}
 
// Function to find the XOR of odd
// numbers less than or equal to N
void findOddXOR(int n)
{
 
    // If number is even
    if (n % 2 == 0)
 
        // Print the answer
        cout << ((findXOR(n))
                 ^ (2 * findXOR(n / 2)));
 
    // If number is odd
    else
 
        // Print the answer
        cout << ((findXOR(n))
                 ^ (2 * findXOR((n - 1) / 2)));
}
 
// Driver Code
int main()
{
    int N = 11;
 
    // Function Call
    findOddXOR(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to calculate Bitwise
  // XOR of odd numbers in the range [1, N]
  static int findXOR(int n)
  {
 
    // N & 3 is equivalent to n % 4
    switch (n & 3)
    {
 
        // If n is multiple of 4
      case 0:
        return n;
 
        // If n % 4 gives remainder 1
      case 1:
        return 1;
 
        // If n % 4 gives remainder 2
      case 2:
        return n + 1;
    }
    // If n % 4 gives remainder 3
    return 0;
 
  }
 
  // Function to find the XOR of odd
  // numbers less than or equal to N
  static void findOddXOR(int n)
  {
 
    // If number is even
    if (n % 2 == 0)
 
      // Print the answer
      System.out.print(((findXOR(n))
                        ^ (2 * findXOR(n / 2))));
 
    // If number is odd
    else
 
      // Print the answer
      System.out.print(((findXOR(n))
                        ^ (2 * findXOR((n - 1) / 2))));
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 11;
 
    // Function Call
    findOddXOR(N);
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python program for the above approach
 
# Function to calculate Bitwise
# XOR of odd numbers in the range [1, N]
def findXOR(n):
   
    # N & 3 is equivalent to n % 4
    if (n % 4 == 0):
       
        # If n is multiple of 4
        return n;
    elif (n % 4 == 1):
       
        # If n % 4 gives remainder 1
        return 1;
 
    # If n % 4 gives remainder 2
    elif (n % 4 == 2):
        return n + 1;
 
    # If n % 4 gives remainder 3
    elif (n % 4 == 3):
        return 0;
 
# Function to find the XOR of odd
# numbers less than or equal to N
def findOddXOR(n):
   
    # If number is even
    if (n % 2 == 0):
 
        # Prthe answer
        print(((findXOR(n)) ^ (2 * findXOR(n // 2))));
 
    # If number is odd
    else:
 
        # Prthe answer
        print(((findXOR(n)) ^ (2 * findXOR((n - 1) // 2))));
 
# Driver Code
if __name__ == '__main__':
    N = 11;
 
    # Function Call
    findOddXOR(N);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to calculate Bitwise
  // XOR of odd numbers in the range [1, N]
  static int findXOR(int n)
  {
 
    // N & 3 is equivalent to n % 4
    switch (n & 3)
    {
 
        // If n is multiple of 4
      case 0:
        return n;
 
        // If n % 4 gives remainder 1
      case 1:
        return 1;
 
        // If n % 4 gives remainder 2
      case 2:
        return n + 1;
    }
    // If n % 4 gives remainder 3
    return 0;
 
  }
 
  // Function to find the XOR of odd
  // numbers less than or equal to N
  static void findOddXOR(int n)
  {
 
    // If number is even
    if (n % 2 == 0)
 
      // Print the answer
      Console.Write(((findXOR(n))
                     ^ (2 * findXOR(n / 2))));
 
    // If number is odd
    else
 
      // Print the answer
      Console.Write(((findXOR(n))
                     ^ (2 * findXOR((n - 1) / 2))));
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 11;
 
    // Function Call
    findOddXOR(N);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
2

时间复杂度: O(1)
辅助空间: O(1)