📌  相关文章
📜  用于查找所有偶数奇偶校验数之和的范围查询

📅  最后修改于: 2021-09-07 03:27:09             🧑  作者: Mango

给定Q查询,其中每个查询由两个数字LR 组成,表示范围[L, R] 。任务是找到位于给定范围[L, R]内的所有偶数奇偶校验数的总和

例子:

方法:
这个想法是使用前缀和数组。所有偶数奇偶校验数的总和直到该特定索引被预先计算并存储在数组pref[] 中,以便可以在O(1)时间内回答每个查询。

  1. 初始化前缀数组pref[]
  2. 从 1 到 N 迭代并检查数字是否具有偶校验:
    • 如果数字是偶校验数,则pref[]的当前索引将存储到目前为止找到的偶数奇偶校验数的总和。
    • 别的PREF的当前索引[]是相同的PREF先前索引处的值[]。
  3. 对于Q查询,范围[L, R]的所有偶数奇偶校验数的总和可以计算如下:
    sum = pref[R] - pref[L - 1]
    

下面是上述方法的实现

C++
// C++ program to find the sum
// of all Even Parity numbers
// in the given range
  
#include 
using namespace std;
  
// pref[] array to precompute
// the sum of all Even
// Parity Numbers
int pref[100001] = { 0 };
  
// Function that returns true
// if count of set bits in
// x is even
int isEvenParity(int num)
{
    // Parity will store the
    // count of set bits
    int parity = 0;
    int x = num;
    while (x != 0) {
        if (x & 1)
            parity++;
        x = x >> 1;
    }
  
    if (parity % 2 == 0)
        return num;
    else
        return 0;
}
  
// Function to precompute the
// sum of all even parity
// numbers upto 100000
void preCompute()
{
    for (int i = 1; i < 100001; i++) {
  
        // isEvenParity()
        // return the number i
        // if i has even parity
        // else return 0
        pref[i] = pref[i - 1]
                  + isEvenParity(i);
    }
}
  
// Function to print sum
// for each query
void printSum(int L, int R)
{
    cout << (pref[R] - pref[L - 1])
         << endl;
}
  
// Function to print sum of all
// even parity numbers between
// [L, R]
void printSum(int arr[2][2], int Q)
{
  
    // Function that pre computes
    // the sum of all even parity
    // numbers
    preCompute();
  
    // Iterate over all Queries
    // to print sum
    for (int i = 0; i < Q; i++) {
        printSum(arr[i][0],
                 arr[i][1]);
    }
}
// Driver code
int main()
{
    // Queries
    int N = 2;
    int Q[2][2] = { { 1, 10 },
                    { 121, 211 } };
  
    // Function that print
    // the sum of all even parity
    // numbers in Range [L, R]
    printSum(Q, N);
  
    return 0;
}


Java
// Java program to find the sum
// of all Even Parity numbers
// in the given range
import java.io.*; 
import java.util.*; 
  
class GFG { 
      
// pref[] array to precompute
// the sum of all Even
// Parity Numbers
static int[] pref = new int[100001];
  
// Function that returns true
// if count of set bits in
// x is even
static int isEvenParity(int num)
{
      
    // Parity will store the
    // count of set bits
    int parity = 0;
    int x = num;
      
    while (x != 0)
    {
        if ((x & 1) == 1)
            parity++;
              
        x = x >> 1;
    }
      
    if (parity % 2 == 0)
        return num;
    else
        return 0;
}
  
// Function to precompute the
// sum of all even parity
// numbers upto 100000
static void preCompute()
{
    for(int i = 1; i < 100001; i++)
    {
  
       // isEvenParity()
       // return the number i
       // if i has even parity
       // else return 0
       pref[i] = pref[i - 1] + isEvenParity(i);
    }
}
  
// Function to print sum
// for each query
static void printSum(int L, int R)
{
    System.out.println(pref[R] - pref[L - 1]);
}
  
// Function to print sum of all
// even parity numbers between
// [L, R]
static void printSum(int arr[][], int Q)
{
      
    // Function that pre computes
    // the sum of all even parity
    // numbers
    preCompute();
  
    // Iterate over all Queries
    // to print sum
    for(int i = 0; i < Q; i++) 
    {
       printSum(arr[i][0], arr[i][1]);
    }
}
      
// Driver code 
public static void main(String[] args) 
{ 
      
    // Queries
    int N = 2;
    int[][] Q = { { 1, 10 }, 
                  { 121, 211 } };
  
    // Function that print
    // the sum of all even parity
    // numbers in Range [L, R]
    printSum(Q, N);
} 
}
  
// This code is contributed by coder001


C#
// C# program to find the sum
// of all Even Parity numbers
// in the given range
using System;
  
class GFG { 
      
// pref[] array to precompute
// the sum of all Even
// Parity Numbers
static int[] pref = new int[100001];
  
// Function that returns true
// if count of set bits in
// x is even
static int isEvenParity(int num)
{
      
    // Parity will store the
    // count of set bits
    int parity = 0;
    int x = num;
      
    while (x != 0)
    {
        if ((x & 1) == 1)
            parity++;
              
        x = x >> 1;
    }
      
    if (parity % 2 == 0)
        return num;
    else
        return 0;
}
  
// Function to precompute the
// sum of all even parity
// numbers upto 100000
static void preCompute()
{
    for(int i = 1; i < 100001; i++)
    {
          
       // isEvenParity()
       // return the number i
       // if i has even parity
       // else return 0
       pref[i] = pref[i - 1] + isEvenParity(i);
    }
}
  
// Function to print sum
// for each query
static void printSum(int L, int R)
{
    Console.WriteLine(pref[R] - pref[L - 1]);
}
  
// Function to print sum of all
// even parity numbers between
// [L, R]
static void printSum(int[,] arr, int Q)
{
      
    // Function that pre computes
    // the sum of all even parity
    // numbers
    preCompute();
  
    // Iterate over all Queries
    // to print sum
    for(int i = 0; i < Q; i++) 
    {
       printSum(arr[i, 0], arr[i, 1]);
    }
}
      
// Driver code 
public static void Main() 
{ 
      
    // Queries
    int N = 2;
    int[,] Q = { { 1, 10 }, 
                 { 121, 211 } };
  
    // Function that print
    // the sum of all even parity
    // numbers in Range [L, R]
    printSum(Q, N);
} 
}
  
// This code is contributed by AbhiThakur


输出:
33
7493

时间复杂度: O(N) ,其中 N 是查询中的最大元素。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live