Problem 529175 · medium · Phase 05 Advanced Algorithms & Graphs

Sum of Every Team's XOR Badge

bit manipulation · combinatorics · modular arithmetic

Every possible team (subset) of the players in ids, including the empty team, gets a badge number equal to the XOR of its members' ids (the empty team gets 0). Return the sum of all 2**n badge numbers modulo 1_000_000_007.

Examples

Input:  ids = [1, 3]
Output: 6
Explanation: {} -> 0, {1} -> 1, {3} -> 3, {1, 3} -> 2. Sum 6.
Input:  ids = [2, 2]
Output: 4

Constraints

  • 0 <= len(ids) <= 10**5, 0 <= ids[i] < 2**30.
  • Target complexity: O(n); enumerating subsets is impossible.

Goals

  • Reason about each bit independently across all subsets
  • Show that a bit present anywhere is set in exactly half of the subsets
  • Reduce the answer modulo a prime with fast powering
Starting Python…