📌  相关文章
📜  用相同的按位AND,OR和XOR值计数子序列

📅  最后修改于: 2021-04-23 18:09:43             🧑  作者: Mango

我们给定了n个元素的数组arr。我们需要计算非空子序列的数量,以使这些单个子序列具有按位与,或和异或的相同值。例如,如果(x | y | z)等于(x&y&z)和(x ^ y ^ z),我们需要计算一个子序列(x,y,z)。对于单个元素子序列,我们将元素本身视为XOR,AND和OR的结果。因此,所有单元素子序列始终被视为结果的一部分。

例子:

Input :  a = [1, 3, 7]
Output : 3
Explanation: 
There are 7 non empty subsequence .
subsequence   OR  AND  XOR
{1}            1    1    1
{3}            3    3    3
{7}            7    7    7
{1, 3}         3    1    2
{1, 7}         7    1    6
{3, 7}         7    3    4
{1, 3, 7}      7    1    5
Out of 7, there are 3 subsequences {1}
{3} {7} which have same values of AND, 
OR and XOR. 

Input :  a[] = [0, 0, 0]
Output : 7
Explanation:  All 7 non empty subsequences 
have same values of AND, OR and XOR. 

Input : a[] = [2, 2, 2, 3, 4]
Output : 6
Explanation:  subsequence {2}, {2}, {2},
{2, 2, 2}, {3}, {4} have same values of
AND, OR and XOR. 

1)如果给定数组中出现n个零,那么由这些零贡献的n个子序列将为2 n – 1。
2)如果存在n个非零元素x,则将有2个n-1个子序列由该元素的出现贡献。请注意,在非零元素的情况下,对于位运算运算符,只有奇数次的出现会导致相同的结果。

查找数组中每个元素的计数,然后应用上述公式。

C++
#include 
using namespace std;
  
// function for finding count of  possible subsequence
int countSubseq(int arr[], int n)
{
    int count = 0;
  
    // creating a map to count the frequency of each element
    unordered_map mp;
  
    // store frequency of each element
    for (int i = 0; i < n; i++)
        mp[arr[i]]++;
  
    // iterate through the map
    for (auto i : mp) {
  
        // add all possible combination for key equal zero
        if (i.first == 0)
            count += pow(2, i.second) - 1;
  
        // add all (odd number of elements) possible 
        // combination for key other than zero
        else
            count += pow(2, i.second - 1);
    }
    return count;
}
  
// driver function
int main()
{
    int arr[] = { 2, 2, 2, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countSubseq(arr, n);
    return 0;
}


Java
import java .io.*; 
import java.util.*;
  
  
class GFG {
   
// function for finding count of  possible subsequence
static int countSubseq(int arr[], int n)
{
    int count = 0;
   
    // creating a map to count the frequency of each element
    HashMapmp=new HashMap();
   
    // store frequency of each element
    for (int i = 0; i < n; i++)
        if (mp.containsKey(arr[i]))
            mp.put(arr[i],mp.get(arr[i])+1);
        else
            mp.put(arr[i],1);
   
    // iterate through the map
    for (Map.Entryentry:mp.entrySet()) {
   
        // add all possible combination for key equal zero
        if (entry.getKey() == 0)
            count += Math.pow(2, entry.getValue()) - 1;
   
        // add all (odd number of elements) possible 
        // combination for key other than zero
        else
            count += Math.pow(2, entry.getValue()- 1);
    }
    return count;
}
   
// driver function
public static void main(String[] args)
{
    int arr[] = { 2, 2, 2, 5, 6 };
    int n=arr.length;
    System.out.println(countSubseq(arr, n));
}
}
  
// This code is contributed by apurva raj


C#
using System;
using System.Collections.Generic;
class GFG{
  
// function for finding count of possible subsequence
static int countSubseq(int []arr, int n)
{
    int count = 0;
  
    // creating a map to count the frequency of each element
     Dictionary mp = new Dictionary();
  
    // store frequency of each element
     for (int i = 0; i < n; i++) 
        { 
            if (mp.ContainsKey(arr[i]))  
            { 
                var val = mp[arr[i]]; 
                mp.Remove(arr[i]); 
                mp.Add(arr[i], val + 1);  
            }  
            else
            { 
                mp.Add(arr[i], 1); 
            } 
        }
  
    // iterate through the map
    foreach(KeyValuePair entry in mp) {
  
        // add all possible combination for key equal zero
        if (entry.Key == 0)
            count += (int)(Math.Pow(2, entry.Value - 1));
  
        // add all (odd number of elements) possible 
        // combination for key other than zero
        else
            count += (int)(Math.Pow(2, entry.Value - 1));
    }
    return count;
}
  
// Driver function
public static void Main(String []args)  
    {
    int []arr = { 2, 2, 2, 5, 6 };
    int n = arr.Length;
    Console.WriteLine(countSubseq(arr, n));
}
}
  
// This code is contributed by shivanisinghss2110


Python3
# function for finding count of possible subsequence
def countSubseq(arr, n):
    count = 0
  
    # creating a map to count the frequency of each element
    mp = {}
  
    # store frequency of each element
    for x in arr:
        if x in mp.keys():
            mp[x]+=1
        else:
            mp[x]=1
  
    # iterate through the map
    for i in mp.keys():
  
        # add all possible combination for key equal zero
        if (i == 0):
            count += pow(2, mp[i]) - 1
  
        # add all (odd number of elements) possible 
        # combination for key other than zero
        else:
            count += pow(2, mp[i] - 1)
    return count
  
# Driver function
arr= [2, 2, 2, 5, 6 ]
n = len(arr)
print(countSubseq(arr, n))
  
# This code is contributed by apurva raj


输出:
6