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

📅  最后修改于: 2021-05-25 03:03:43             🧑  作者: Mango

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

例子:

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

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

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

从以上观察结果可以得出结论,偶数XOR值为0或4的倍数。请按照以下步骤解决问题:

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

下面是上述方法的实现。

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)