给定一个整数N ,任务是找到[1, N]范围内所有奇数的按位异或。
例子:
Input: 11
Output: 2
Explanation: Bitwise XOR of all odd numbers up to 11 = 1 ^ 3 ^ 5 ^ 7 ^ 9 ^ 11 = 2.
Input: 10
Output: 9
Explanation: Bitwise XOR of all odd numbers up to 10 = 1 ^ 3 ^ 5 ^ 7 ^ 9 = 9.
朴素的方法:解决问题的最简单方法是迭代范围[1, N]并为每个值检查它是否为奇数。计算找到的每个奇数元素的按位异或并将其打印为所需的结果。
时间复杂度: O(N)
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是使用以下公式计算所有小于或等于N 的奇数的按位异或:
Let f(N) = 2 ^ 4 ^ 6 ^ … ^ (N − 2) ^ n and g(n) = 1 ^ 2 ^ 3 ^ … ^ (N − 2) / 2 ^ (N / 2)
=> f(N) = 2 * g(N)
Now, let k(N) = 1 ^ 2 ^ 3 ^ … ^ (N − 2) ^ N
XOR of all odd numbers less than or equal to N = k(N) ^ f(N) [Since all even numbers cancel their own bits leaving only odd numbers].
Substituting the value of f(N) = 2 * g(N), XOR of all odd numbers up to N = k(N) ^ (2 * g(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)