问题是找到所有子集的XOR的XOR。i、 如果集合为{1,2,3},则为e。所有子集都是:[{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}]。求每个子集的异或,然后求每个子集结果的异或。 我们强烈建议您尽量减少浏览器,并先自己尝试。 如果我们正确地迈出第一步(也是唯一一步),这是一个非常简单的问题。当n>1时,XOR始终为0,当n为1时,设置[0]。
null
C++
// C++ program to find XOR of XOR's of all subsets #include <bits/stdc++.h> using namespace std; // Returns XOR of all XOR's of given subset int findXOR( int Set[], int n) { // XOR is 1 only when n is 1, else 0 if (n == 1) return Set[0]; else return 0; } // Driver program int main() { int Set[] = {1, 2, 3}; int n = sizeof (Set)/ sizeof (Set[0]); cout << "XOR of XOR's of all subsets is " << findXOR(Set, n); return 0; } |
JAVA
// Java program to find XOR of // XOR's of all subsets import java.util.*; class GFG { // Returns XOR of all XOR's of given subset static int findXOR( int Set[], int n) { // XOR is 1 only when n is 1, else 0 if (n == 1 ) return Set[ 0 ]; else return 0 ; } // Driver code public static void main(String arg[]) { int Set[] = { 1 , 2 , 3 }; int n = Set.length; System.out.print( "XOR of XOR's of all subsets is " + findXOR(Set, n)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python program to find # XOR of XOR's of all subsets # Returns XOR of all # XOR's of given subset def findXOR( Set , n): # XOR is 1 only when # n is 1, else 0 if (n = = 1 ): return Set [ 0 ] else : return 0 # Driver code Set = [ 1 , 2 , 3 ] n = len ( Set ) print ( "XOR of XOR's of all subsets is " , findXOR( Set , n)); # This code is contributed # by Anant Agarwal. |
C#
// C# program to find XOR of // XOR's of all subsets using System; class GFG { // Returns XOR of all // XOR's of given subset static int findXOR( int []Set, int n) { // XOR is 1 only when n // is 1, else 0 if (n == 1) return Set[0]; else return 0; } // Driver code public static void Main() { int []Set = {1, 2, 3}; int n = Set.Length; Console.Write( "XOR of XOR's of all subsets is " + findXOR(Set, n)); } } // This code is contributed by nitin mittal |
PHP
<?php // PHP program to find XOR // of XOR's of all subsets // Returns XOR of all // XOR's of given subset function findXOR( $Set , $n ) { // XOR is 1 only when // n is 1, else 0 if ( $n == 1) return $Set [0]; else return 0; } // Driver Code $Set = array (1, 2, 3); $n = count ( $Set ); echo "XOR of XOR's of all subsets is " , findXOR( $Set , $n ); // This code is contributed by anuj_67. ?> |
Javascript
<script> // JavaScript program to find XOR of XOR's of all subsets // Returns XOR of all XOR's of given subset function findXOR(Set, n) { // XOR is 1 only when n is 1, else 0 if (n == 1) return Set[0]; else return 0; } // Driver program let Set = [1, 2, 3]; let n = Set.length; document.write( "XOR of XOR's of all subsets is " + findXOR(Set, n)); // This code is contributed by Surbhi Tyagi </script> |
输出:
XOR of XOR's of all subsets is 0
时间复杂性: O(1)
辅助空间: O(1)
相关问题: 所有可能子集的XOR之和 这是怎么回事? 逻辑很简单。让我们考虑NTH元素,它可以包含在剩余(N-1)元素的所有子集中。(n-1)元素的子集数等于2 (n-1) 当n>1时总是偶数。因此,在XOR结果中,每个元素包含偶数次,任何数字偶数出现的XOR为0。 本文由 埃克塔·戈尔 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END