<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

542. Gena vs Petya
Time limit per test: 1 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



Gena and Petya love playing the following game with each other. There are n piles of stones, the i-th pile contains ai stones. The players move in turns, Gena moves first. A player moves by choosing any non-empty pile and taking an arbitrary positive number of stones from it. If the move is impossible (that is, all piles are empty), then the game finishes and the current player is considered a loser.

Gena and Petya are the world famous experts in unusual games. We will assume that they play optimally.

Recently Petya started to notice that Gena wins too often. Petya decided that the problem is the unjust rules as Gena always gets to move first! To even their chances, Petya decided to cheat and take and hide some stones before the game begins. Since Petya does not want Gena to suspect anything, he will take the same number of stones x from each pile. This number x can be an arbitrary non-negative integer, strictly less that the minimum of ai values.

Your task is to find the number of distinct numbers x such that Petya will win the game.

Input
The first line contains the number of piles n (1 ≤ n ≤ 2 · 105). The second line contains n space-separated integers ai (1 ≤ ai ≤ 1018) — the piles' sizes.

Output
Print the number of ways to choose x so that Petya will win the resulting game considering that both players play optimally.

Example(s)
sample input
sample output
2
3 3
3

sample input
sample output
3
3 4 5
1

sample input
sample output
4
2 7 4 1
1

sample input
sample output
4
4 6 8 10
2



Note
Consider the first example. Petya can choose any x between 0 and 2. After it Gena starts the game with two piles of equal sizes and looses the game. In the second example there is a single possible value of x, equal to 2. In the third example the sought x is also only one — it's x=0. In the fourth example there are two possible values of x — they are 0 and 3.

<|response|>
1. Abridged problem statement  
   - You have n piles of stones with sizes a₁,…,aₙ.  
   - Petya may choose an integer x, 0 ≤ x < m, where m = min(aᵢ), and remove x stones from each pile.  
   - Then Gena and Petya play standard Nim: they alternate removing any positive number of stones from a single non-empty pile; the player who cannot move loses.  
   - In Nim, the first player loses if and only if the bitwise XOR of the pile sizes is 0.  
   - Petya moves “before” the game by choosing x so that after subtraction the XOR of (aᵢ–x) is 0—then Gena (the first Nim player) loses.  
   - Count the number of valid x ∈ [0, m–1] that make ⊕ᵢ(aᵢ–x)=0.  

2. Key observations  
   - We need to count x ∈ [0, m–1] with  
       XOR(x) ≔ (a₁–x)⊕(a₂–x)⊕…⊕(aₙ–x) = 0.  
   - Subtraction of x from each aᵢ in binary involves “borrows” that couple all bits together; you cannot treat each bit independently in a simple XOR formula.  
   - We can do a digit-DP on bits 0…60 (since aᵢ ≤ 10¹⁸ < 2⁶⁰).  
   - Define DP state:  
       D[k][c] = number of ways to choose the k least significant bits of x (bits 0..k–1) so that:  
       1. The XOR of bits 0..k–1 of all (aᵢ–x) is 0.  
       2. Exactly c piles generate a borrow into bit k.  
   - Transition from D[k][c] to D[k+1][*]: try xₖ∈{0,1}. For each pile i, given its k-th bit aᵢₖ and whether it had a borrow in bᵢ∈{0,1}, we compute:  
       • The resulting bit contribution rᵢ = (aᵢₖ – xₖ – bᵢ) mod 2.  
       • Whether it produces a borrow out of this bit (if aᵢₖ – xₖ – bᵢ < 0).  
     We require that the XOR across all piles of rᵢ is 0. The next carry count c′ is the total number of piles with a borrow out.  
   - To implement this efficiently, we only need to know, for the current bit k and incoming borrow group sizes c and n–c, how many piles have aᵢₖ=1 in each group. We can precompute, for each bit k, a permutation of piles grouped by their k-th bit and a prefix sum of 1-bits. This lets us in O(1) get the counts needed for all four combinations (borrow-in 0/1 × aᵢₖ 0/1).  
   - Finally, the DP will count all x ≥ 0 (with any number of bits). We must subtract 1 if x=m (the forbidden case) also satisfies the XOR=0 condition.  

3. Full solution approach  
   1. Read n and array a. Compute m = min(a).  
   2. Preprocess for each bit k=0..60:  
      - Maintain an array order[] initially 0,1,…,n–1.  
      - Stable-partition order[] by the k-th bit of a[order[i]] (0-bits first, then 1-bits).  
      - While partitioning, build pref1[k][i] = number of 1-bits among the first i piles in this order.  
      After this, for any c, the first c indices in order[] represent the piles with borrow-in=1, and pref1[k][c] tells how many of those have aᵢₖ=1; similarly (pref1[k][n]–pref1[k][c]) is how many of the other n–c piles have bit=1.  
   3. Let DP be a 2-D array of size (61)×(n+1), initialized to 0. Set DP[0][0] = 1.  
   4. For each bit k = 0…60, for each carry-in c = 0…n with DP[k][c] > 0, do:  
       for xₖ in {0,1}:  
         • Compute:  
             ones1 = pref1[k][c]              // among carry-in=1 piles how many have aᵢₖ=1  
             zeros1 = c – ones1  
             ones0 = pref1[k][n] – ones1     // among carry-in=0 piles how many have bit=1  
             zeros0 = (n–c) – ones0  
           Initialize xor_parity=0, new_carry=0.  
         • For group (b=0, v=0), count = zeros0:  
             r = (0 – xₖ – 0) & 1; borrow_out? = (0 – xₖ < 0)  
             xor_parity ^= r * (count&1); if borrow_out add count to new_carry.  
         • For (b=0, v=1), count = ones0: r=(1–xₖ)&1; borrow_out?=(1–xₖ<0) …  
         • For (b=1, v=0), count = zeros1: r=(0–xₖ–1)&1; borrow_out?=(0–xₖ–1<0) …  
         • For (b=1, v=1), count = ones1: r=(1–xₖ–1)&1; borrow_out?=(1–xₖ–1<0) …  
         • If xor_parity == 0, then DP[k+1][new_carry] += DP[k][c].  
   5. The total number of nonnegative x with XOR=0 is sum over c of DP[61][c] but only c=0 is valid (no borrow out of the top bit), so answer_all = DP[61][0].  
   6. Compute XOR_m = ⊕ᵢ(aᵢ–m). If XOR_m==0, decrement answer_all by 1 (to exclude x=m).  
   7. Print the result.  

4. C++ implementation with detailed comments  

```cpp
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<int64> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    // m = minimum pile size
    int64 m = *min_element(a.begin(), a.end());

    // pref1[k][i] = number of piles among the first i in 'order' whose k-th bit is 1
    vector<vector<int>> pref1(61, vector<int>(n+1, 0));

    // Build an ordering of piles for each bit so we can query "in the first c piles, how many have bit=1?"
    vector<int> order(n);
    iota(order.begin(), order.end(), 0);

    for(int bit = 0; bit < 61; bit++){
        vector<int> zero_bucket, one_bucket;
        zero_bucket.reserve(n);
        one_bucket.reserve(n);
        // Build prefix sums of 1-bits in this bit-position
        for(int i = 0; i < n; i++){
            int idx = order[i];
            int b = (a[idx] >> bit) & 1;
            pref1[bit][i+1] = pref1[bit][i] + b;
            if(b) one_bucket.push_back(idx);
            else  zero_bucket.push_back(idx);
        }
        // New order: zeros first, then ones
        order.clear();
        order.insert(order.end(), zero_bucket.begin(), zero_bucket.end());
        order.insert(order.end(), one_bucket.begin(), one_bucket.end());
    }

    // DP[k][c] = number of ways to choose bits 0..k-1 of x with c borrows into bit k
    vector<vector<int64>> DP(62, vector<int64>(n+1, 0));
    DP[0][0] = 1;

    // Iterate bits 0..60
    for(int bit = 0; bit < 61; bit++){
        for(int c = 0; c <= n; c++){
            int64 ways = DP[bit][c];
            if(ways == 0) continue;

            // How many piles have k-th bit =1 among those with borrow-in=1 and borrow-in=0
            int ones1 = pref1[bit][c];
            int zeros1 = c - ones1;
            int total1 = pref1[bit][n];
            int ones0 = total1 - ones1;
            int zeros0 = (n - c) - ones0;

            // Try x_k = 0 or 1
            for(int xk = 0; xk < 2; xk++){
                int xor_par = 0;
                int new_carry = 0;

                // group b=0, v=0
                {
                    int cnt = zeros0;
                    int r   = (0 - xk) & 1;
                    bool br = (0 - xk < 0);
                    xor_par ^= r * (cnt & 1);
                    if(br) new_carry += cnt;
                }
                // b=0, v=1
                {
                    int cnt = ones0;
                    int r   = (1 - xk) & 1;
                    bool br = (1 - xk < 0);
                    xor_par ^= r * (cnt & 1);
                    if(br) new_carry += cnt;
                }
                // b=1, v=0
                {
                    int cnt = zeros1;
                    int r   = (0 - xk - 1) & 1;
                    bool br = (0 - xk - 1 < 0);
                    xor_par ^= r * (cnt & 1);
                    if(br) new_carry += cnt;
                }
                // b=1, v=1
                {
                    int cnt = ones1;
                    int r   = (1 - xk - 1) & 1;
                    bool br = (1 - xk - 1 < 0);
                    xor_par ^= r * (cnt & 1);
                    if(br) new_carry += cnt;
                }

                // If this bit's XOR is zero, accumulate DP
                if(xor_par == 0 && new_carry <= n){
                    DP[bit+1][new_carry] += ways;
                }
            }
        }
    }

    // Valid x produce no borrow out of bit 60 ⇒ carry=0 at bit=61
    int64 answer = DP[61][0];

    // Exclude x = m if it was counted
    int64 xor_m = 0;
    for(int i = 0; i < n; i++){
        xor_m ^= (a[i] - m);
    }
    if(xor_m == 0) answer--;

    cout << answer << "\n";
    return 0;
}
```

5. Python implementation with detailed comments  

```python
import sys
sys.setrecursionlimit(10**7)

def main():
    data = sys.stdin.read().split()
    n = int(data[0])
    a = list(map(int, data[1:1+n]))
    m = min(a)

    # pref1[bit][i] = # of piles among the first i in 'order' whose bit-th bit is 1
    pref1 = [[0]*(n+1) for _ in range(61)]

    # Build initial order = [0..n-1]
    order = list(range(n))
    for bit in range(61):
        zero_bucket = []
        one_bucket  = []
        cnt1 = 0
        for i, idx in enumerate(order):
            b = (a[idx] >> bit) & 1
            cnt1 += b
            pref1[bit][i+1] = cnt1
            if b:
                one_bucket.append(idx)
            else:
                zero_bucket.append(idx)
        # New order: zeros first, then ones
        order = zero_bucket + one_bucket

    # DP table: dp[bit][carry] = #ways for bits 0..bit-1, with 'carry' borrows into this bit
    dp = [ [0]*(n+1) for _ in range(62) ]
    dp[0][0] = 1

    for bit in range(61):
        total1 = pref1[bit][n]
        for carry in range(n+1):
            ways = dp[bit][carry]
            if ways == 0:
                continue

            # Among carry-in piles, how many have bit=1?
            ones1  = pref1[bit][carry]
            zeros1 = carry - ones1
            # Among carry-out piles, how many have bit=1?
            ones0  = total1 - ones1
            zeros0 = (n - carry) - ones0

            # Try x_k = 0 or 1
            for xk in (0, 1):
                xor_par = 0
                new_c = 0

                # Four groups: (b=0,v=0), (b=0,v=1), (b=1,v=0), (b=1,v=1)
                for b, v, cnt in ((0,0,zeros0),
                                  (0,1,ones0),
                                  (1,0,zeros1),
                                  (1,1,ones1)):
                    # resulting bit
                    r = (v - xk - b) & 1
                    xor_par ^= r * (cnt & 1)
                    # does this subtraction produce a borrow?
                    if v - xk - b < 0:
                        new_c += cnt

                # accept this choice if XOR at this bit is 0
                if xor_par == 0 and new_c <= n:
                    dp[bit+1][new_c] += ways

    # Answer is dp[61][0], minus 1 if x=m is illegally counted
    answer = dp[61][0]
    xor_m = 0
    for v in a:
        xor_m ^= (v - m)
    if xor_m == 0:
        answer -= 1

    print(answer)

if __name__ == "__main__":
    main()
```