📌  相关文章
📜  从 L 开始计算范围 [L, R] 中连续数字的偶数和奇数按位异或

📅  最后修改于: 2021-10-25 10:34:51             🧑  作者: Mango

给定两个整数LR ,任务是从L开始的范围[L, R] 中找到连续数字的偶数和奇数按位异或值的计数。

例子:

朴素的方法:最简单的方法是遍历[L, R]范围内的所有数字 并对从L开始的连续数字执行按位异或。最后,统计得到的偶数和奇数的Bitwise XOR值。
时间复杂度: O(R – L)
辅助空间: O(1)

高效方法:为了优化上述方法,该想法基于以下观察,即可以在恒定时间内计算从1N的按位异或:

  • N 的除以4的余数
  • 如果获得的余数为0 ,则 XOR 将等于N
  • 如果获得的余数为1 ,则 XOR 将等于1
  • 如果获得的余数为2 ,则 XOR 将等于N+1
  • 如果获得的余数为3 ,则获得的 XOR 将为0

由以上观察,可以得出偶数异或值不是0就是4的倍数。按照以下步骤解决问题:

  • 将范围[L, R]中的元素数量存储在一个变量中,比如X
  • 通过将X除以4并乘以2来存储偶数XOR值的计数,例如Even
  • 如果L为奇数且X % 4等于3 ,则将Even增加1
  • 否则,如果L是偶数且X % 4大于0 ,则将Even增加1
  • 将奇数XOR值的计数存储在变量Odd = X – Even 中
  • 完成上述步骤后,打印EvenOdd的值作为结果。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Print count of even and odd numbers
// of XOR value from L to R
void countEvenOdd(int L, int R)
{
    // Store the number of elements
    // between L and R
    int range = R - L + 1;
 
    // Count of even XOR values
    int even = (range / 4) * 2;
 
    // If L is odd and range % 4 = 3
    if ((L & 1) && (range % 4 == 3)) {
 
        // Increment even by 1
        even++;
    }
 
    // If L is even and range % 4 !=0
    else if (!(L & 1) && (range % 4)) {
 
        // Increment even by 1
        even++;
    }
 
    // Print the answer
    cout << "Even = " << even
         << ", Odd = " << range - even;
}
 
// Driver Code
int main()
{
    int L = 2, R = 7;
    countEvenOdd(L, R);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
  // Print count of even and odd numbers
  // of XOR value from L to R
  static void countEvenOdd(int L, int R)
  {
 
    // Store the number of elements
    // between L and R
    int range = R - L + 1;
 
    // Count of even XOR values
    int even = (range / 4) * 2;
 
    // If L is odd and range % 4 = 3
    if ((L & 1) != 0 && (range % 4 == 3))
    {
 
      // Increment even by 1
      even++;
    }
 
    // If L is even and range % 4 !=0
    else if ((L & 1) == 0 && (range % 4 != 0))
    {
 
      // Increment even by 1
      even++;
    }
 
    // Print the answer
    System.out.print("Even = " + even +
                     ", Odd = " + (range - even));
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int L = 2, R = 7;
    countEvenOdd(L, R);
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python program for the above approach
 
# Prcount of even and odd numbers
# of XOR value from L to R
def countEvenOdd(L, R):
   
    # Store the number of elements
    # between L and R
    range = R - L + 1;
 
    # Count of even XOR values
    even = (range // 4) * 2;
 
    # If L is odd and range % 4 = 3
    if ((L & 1) != 0 and (range % 4 == 3)):
 
        # Increment even by 1
        even += 1;
 
    # If L is even and range % 4 !=0
    elif ((L & 1) == 0 and (range % 4 != 0)):
 
        # Increment even by 1
        even += 1;
 
    # Prthe answer
    print("Even = " , even ,\
          ", Odd = " , (range - even));
 
# Driver Code
if __name__ == '__main__':
    L = 2; R = 7;
    countEvenOdd(L, R);
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
class GFG
{
     
    // Print count of even and odd numbers
    // of XOR value from L to R
    static void countEvenOdd(int L, int R)
    {
       
        // Store the number of elements
        // between L and R
        int range = R - L + 1;
       
        // Count of even XOR values
        int even = (range / 4) * 2;
       
        // If L is odd and range % 4 = 3
        if ((L & 1) != 0 && (range % 4 == 3))
        {
       
            // Increment even by 1
            even++;
        }
       
        // If L is even and range % 4 !=0
        else if ((L & 1) == 0 && (range % 4 != 0))
        {
       
            // Increment even by 1
            even++;
        }
       
        // Print the answer
        Console.Write("Even = " + even +
                      ", Odd = " + (range - even));
    }
 
  // Driver code
  static void Main()
  {
    int L = 2, R = 7;
    countEvenOdd(L, R);
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript


输出:
Even = 3, Odd = 3

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