📜  数不。具有特定 XOR 值的有序子集

📅  最后修改于: 2021-09-22 10:15:18             🧑  作者: Mango

给定一个由n 个元素组成的数组arr[]和一个数字K ,找到元素的异或为Karr[]有序子集的数量
这是此问题的修改版本。所以建议先试试那个问题。
例子:

朴素方法 O(2 n ):生成所有2 n个子集并找到所有具有XOR 值 K的子集,并为每个具有XOR 值 K 的子集添加 no。由于我们需要有序的子集,因此该子集的排列组合到答案中,但是这种方法对于 n 的大值来说效率不高。
Efficient Approach O(n 2 * m):这种方法使用动态规划,类似于本文中解释的方法。
唯一的修改是我们在解决方案中再添加一种状态。在那里我们有两个状态,其中dp[i][j]存储了编号。具有XOR 值 j数组 [0…i-1]的子集。现在,我们再添加一个状态k ,即存储子集长度的第三维。
因此, dp[i][j][k]将存储编号。具有XOR 值 j数组 [0…i-1]的长度为k的子集。
现在我们可以看到,

    $$ dp[i][j][k] = dp[i-1][j][k] + k*dp[i-1][a[i-1] XOR j][k-1] $$

dp[i][j][k]可以通过丢弃a[i]元素(它给出 dp[i-1][j][k] 子集)并将其放入子集中来找到(类似于子集的想法Sum Problem) 给出dp[i-1][a[i] ^ j][k-1]子集。现在我们必须将a[i]元素插入k – 1长度的子集中,这可以用k种方式完成,这解释了 k 的因子。
计算dp 数组后,我们的答案将是
$ \sum_{k=1}^{k=n} dp[n][K][k] $
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Returns count of ordered subsets of arr[]
// with XOR value = K
int subsetXOR(int arr[], int n, int K)
{
 
    // Find maximum element in arr[]
    int max_ele = arr[0];
    for (int i = 1; i < n; i++)
        if (arr[i] > max_ele)
            max_ele = arr[i];
 
    // Maximum possible XOR value
    int m = (1 << (int)(log2(max_ele) + 1)) - 1;
 
    // The value of dp[i][j][k] is the number
    // of subsets of length k having XOR of their
    // elements as j from the set arr[0...i-1]
    int dp[n + 1][m + 1][n + 1];
 
    // Initializing all the values of dp[i][j][k]
    // as 0
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= m; j++)
            for (int k = 0; k <= n; k++)
                dp[i][j][k] = 0;
 
    // The xor of empty subset is 0
    for (int i = 0; i <= n; i++)
        dp[i][0][0] = 1;
 
    // Fill the dp table
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            for (int k = 0; k <= n; k++) {
                dp[i][j][k] = dp[i - 1][j][k];
                if (k != 0) {
                    dp[i][j][k] += k * dp[i - 1][j ^
                                   arr[i - 1]][k - 1];
                }
            }
        }
    }
 
    // The answer is the number of subsets of all lengths
    // from set arr[0..n-1] having XOR of elements as k
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        ans += dp[n][K][i];
    }
    return ans;
}
 
// Driver program to test above function
int main()
{
    int arr[] = { 1, 2, 3 };
    int k = 1;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << subsetXOR(arr, n, k);
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
    // Returns count of ordered subsets of arr[]
    // with XOR value = K
    static int subsetXOR(int arr[], int n, int K)
    {
     
        // Find maximum element in arr[]
        int max_ele = arr[0];
        for (int i = 1; i < n; i++)
            if (arr[i] > max_ele)
                max_ele = arr[i];
     
        // Maximum possible XOR value
        int m = (1 << (int)(Math.log(max_ele) /
                        Math.log(2) + 1)) - 1;
     
        // The value of dp[i][j][k] is the number
        // of subsets of length k having XOR of their
        // elements as j from the set arr[0...i-1]
        int [][][] dp = new int[n + 1][m + 1][n + 1];
     
        // Initializing all the values of
        // dp[i][j][k] as 0
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= m; j++)
                for (int k = 0; k <= n; k++)
                    dp[i][j][k] = 0;
     
        // The xor of empty subset is 0
        for (int i = 0; i <= n; i++)
            dp[i][0][0] = 1;
     
        // Fill the dp table
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j <= m; j++)
            {
                for (int k = 0; k <= n; k++)
                {
                    dp[i][j][k] = dp[i - 1][j][k];
                    if (k != 0)
                    {
                        dp[i][j][k] += k * dp[i - 1][j ^
                                    arr[i - 1]][k - 1];
                    }
                }
            }
        }
     
        // The answer is the number of subsets
        // of all lengths from set arr[0..n-1]
        // having XOR of elements as k
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            ans += dp[n][K][i];
        }
        return ans;
    }
     
    // Driver code
    public static void main(String []args)
    {
        int arr[] = { 1, 2, 3 };
        int k = 1;
        int n = arr.length;
        System.out.println(subsetXOR(arr, n, k));
    }
}
 
// This code is contributed by ihritik


Python3
# Python 3implementation of the approach
from math import log2
 
# Returns count of ordered subsets of arr[]
# with XOR value = K
def subsetXOR(arr, n, K):
     
    # Find maximum element in arr[]
    max_ele = arr[0]
    for i in range(1, n):
        if (arr[i] > max_ele):
            max_ele = arr[i]
 
    # Maximum possible XOR value
    m = (1 << int(log2(max_ele) + 1)) - 1
 
    # The value of dp[i][j][k] is the number
    # of subsets of length k having XOR of their
    # elements as j from the set arr[0...i-1]
    dp = [[[0 for i in range(n + 1)]
              for j in range(m + 1)]
              for k in range(n + 1)]
 
    # Initializing all the values
    # of dp[i][j][k] as 0
    for i in range(n + 1):
        for j in range(m + 1):
            for k in range(n + 1):
                dp[i][j][k] = 0
 
    # The xor of empty subset is 0
    for i in range(n + 1):
        dp[i][0][0] = 1
 
    # Fill the dp table
    for i in range(1, n + 1):
        for j in range(m + 1):
            for k in range(n + 1):
                dp[i][j][k] = dp[i - 1][j][k]
                if (k != 0):
                    dp[i][j][k] += k * dp[i - 1][j ^ arr[i - 1]][k - 1]
 
    # The answer is the number of subsets of all lengths
    # from set arr[0..n-1] having XOR of elements as k
    ans = 0
    for i in range(1, n + 1):
        ans += dp[n][K][i]
     
    return ans
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3]
    k = 1
    n = len(arr)
    print(subsetXOR(arr, n, k))
     
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
    // Returns count of ordered subsets of arr[]
    // with XOR value = K
    static int subsetXOR(int []arr, int n, int K)
    {
     
        // Find maximum element in arr[]
        int max_ele = arr[0];
        for (int i = 1; i < n; i++)
            if (arr[i] > max_ele)
                max_ele = arr[i];
     
        // Maximum possible XOR value
        int m = (1 << (int)(Math.Log(max_ele) /
                        Math.Log(2) + 1)) - 1;
     
        // The value of dp[i][j][k] is the number
        // of subsets of length k having XOR of their
        // elements as j from the set arr[0...i-1]
        int [ , , ] dp = new int[n + 1 , m + 1 ,n + 1];
     
        // Initializing all the values of
        // dp[i][j][k] as 0
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= m; j++)
                for (int k = 0; k <= n; k++)
                    dp[i, j, k] = 0;
     
        // The xor of empty subset is 0
        for (int i = 0; i <= n; i++)
            dp[i, 0, 0] = 1;
     
        // Fill the dp table
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j <= m; j++)
            {
                for (int k = 0; k <= n; k++)
                {
                    dp[i, j, k] = dp[i - 1, j, k];
                    if (k != 0) {
                        dp[i, j, k] += k * dp[i - 1, j ^
                                    arr[i - 1], k - 1];
                    }
                }
            }
        }
     
        // The answer is the number of subsets
        // of all lengths from set arr[0..n-1]
        // having XOR of elements as k
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            ans += dp[n, K, i];
        }
        return ans;
    }
     
    // Driver code
    public static void Main()
    {
        int []arr = { 1, 2, 3 };
        int k = 1;
        int n = arr.Length;
        Console.WriteLine(subsetXOR(arr, n, k));
    }
}
 
// This code is contributed by ihritik


PHP
 $max_ele)
            $max_ele = $arr[$i];
 
    // Maximum possible XOR value
    $m = (1 << (floor(log($max_ele, 2))+ 1)) - 1;
 
    // The value of dp[i][j][k] is the number
    // of subsets of length k having XOR of their
    // elements as j from the set arr[0...i-1]
    $dp = array(array(array())) ;
 
    // Initializing all the values
    // of dp[i][j][k] as 0
    for ($i = 0; $i <= $n; $i++)
        for ($j = 0; $j <= $m; $j++)
            for ($k = 0; $k <= $n; $k++)
                $dp[$i][$j][$k] = 0;
 
    // The xor of empty subset is 0
    for ($i = 0; $i <= $n; $i++)
        $dp[$i][0][0] = 1;
 
    // Fill the dp table
    for ($i = 1; $i <= $n; $i++)
    {
        for ($j = 0; $j <= $m; $j++)
        {
            for ($k = 0; $k <= $n; $k++)
            {
                $dp[$i][$j][$k] = $dp[$i - 1][$j][$k];
                if ($k != 0)
                {
                    $dp[$i][$j][$k] += $k * $dp[$i - 1][$j ^
                                       $arr[$i - 1]][$k - 1];
                }
            }
        }
    }
 
    // The answer is the number of subsets
    // of all lengths from set arr[0..n-1]
    // having XOR of elements as k
    $ans = 0;
    for ($i = 1; $i <= $n; $i++)
    {
        $ans += $dp[$n][$K][$i];
    }
    return $ans;
}
 
// Driver Code
$arr = [ 1, 2, 3 ];
$k = 1;
$n = sizeof($arr);
echo subsetXOR($arr, $n, $k);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程