Skip to content

P1863, leetcode ​

Theorem 1 ​

Let AA be a binary sequence of length nn consisting of 0s and 1s.

  1. If AA contains at least one 1, then the sum of XOR sums of all subsets of AA is 2n−12^{n-1}.
  2. If AA contains only 0s, then the sum is 0.

Proof ​

Definitions and Setup ​

  • XOR sum of a subset: The result of applying XOR (⊕\oplus) to all elements in the subset (equivalent to parity of 1s count).
  • Objective: Count the number of subsets with XOR sum = 1 (i.e., subsets with an odd number of 1s).

Case 1: All elements are 0 ​

  • Every subset has XOR sum 0.
  • Total sum: 00.

Case 2: At least one 1 is present ​

Let m≥1m \geq 1 be the count of 1s in AA, and n−mn-m be the count of 0s.

  1. Step 1: Choose an odd number of 1s from the mm available.

    • By the binomial theorem:

      ∑k odd(mk)=2m−1.\sum_{k \text{ odd}} \binom{m}{k} = 2^{m-1}.

      Derivation: From

      (1+1)m=∑k=0m(mk)(1+1)^m = \sum_{k=0}^m \binom{m}{k}

      and

      (1−1)m=∑k=0m(mk)(−1)k(1-1)^m = \sum_{k=0}^m \binom{m}{k}(-1)^k

      , subtract to get:

      2m=2∑k odd(mk)  ⟹  ∑k odd(mk)=2m−1.2^m = 2 \sum_{k \text{ odd}} \binom{m}{k} \implies \sum_{k \text{ odd}} \binom{m}{k} = 2^{m-1}.

  2. Step 2: Freely include any of the n−mn-m 0s (they do not affect the XOR sum).

    • Choices for 0s: 2n−m2^{n-m}.
  3. Total subsets with XOR sum 1:

    2m−1×2n−m=2n−1.2^{m-1} \times 2^{n-m} = 2^{n-1}.


Example Verification ​

  • For m=3m = 3 1s:
    • Subsets with odd 1s: (31)+(33)=4=23−1\binom{3}{1} + \binom{3}{3} = 4 = 2^{3-1}
  • For n=5n = 5 (with 2 extra 0s):
    • Total valid subsets: 4×22=16=25−14 \times 2^2 = 16 = 2^{5-1}

Conclusion ​

The proof leverages the binomial theorem to count parity-constrained subsets, establishing that the XOR sum over all subsets is 2n−12^{n-1} when 1s exist, and 0 otherwise.

Last updated at: