// 演算法 / Algorithm:
// 1) 去重，得到不同的數值 distinct[] / dedup to distinct values.
// 2) 對每對不同值做 XOR，記錄所有「兩數 XOR」結果 / all pairwise XORs.
// 3) 再把每個兩數結果與每個不同值 XOR，記錄「三元組 XOR」/ all triplet XORs.
// 4) 數三元組集合中被標記的值 / count marked triplet values.

// 值域上限：nums[i] <= 1500 < 2048，三個值 XOR 後仍 < 2048。
// Value bound: nums[i] < 2048, so any XOR of them stays < 2048.
#define LIM 2048

int uniqueXorTriplets(int* nums, int numsSize) {
    // present[v]=1 表示數值 v 在 nums 裡出現過 / v appears in nums.
    // 用值當索引的布林陣列，比雜湊表更快 / value-indexed boolean array, faster than a hash set.
    char present[LIM] = {0};                 // {0} 把整個陣列初始化為 0 / initialize all to 0.
    for (int i = 0; i < numsSize; i++)       // 掃過每個元素 / scan each element.
        present[nums[i]] = 1;                // 標記它出現過 / mark it as seen.

    // 把出現過的值收集成一個緊湊的清單 / collect seen values into a compact list.
    int distinct[LIM];                       // 最多 2048 個不同值 / at most 2048 distinct values.
    int m = 0;                               // m 是不同值的個數 / m = count of distinct values.
    for (int v = 0; v < LIM; v++)            // 依序檢查每個可能值 / check every possible value.
        if (present[v]) distinct[m++] = v;   // 若出現過就加入清單 / append if seen.

    // pair[x]=1 表示某對不同值的 XOR 等於 x / x is achievable as a^b.
    char pair[LIM] = {0};
    for (int a = 0; a < m; a++)              // 選第一個值 distinct[a] / pick first value.
        for (int b = a; b < m; b++)          // 選第二個值（b 從 a 開始，允許 a==b）/ second value, b>=a allows repeat.
            pair[distinct[a] ^ distinct[b]] = 1;   // ^ 是位元 XOR，標記這個配對結果 / mark this pairwise XOR.

    // triple[x]=1 表示某個三元組的 XOR 等於 x / x is achievable as a^b^c.
    char triple[LIM] = {0};
    for (int p = 0; p < LIM; p++) {          // 掃過所有可能的「兩數 XOR」值 / scan all pair values.
        if (!pair[p]) continue;              // 跳過沒被標記的值 / skip values never produced.
        for (int c = 0; c < m; c++)          // 再 XOR 上每個不同值 / XOR with each distinct value.
            triple[p ^ distinct[c]] = 1;     // 標記三元組結果 / mark this triplet XOR.
    }

    // 數出被標記的三元組值有幾個 / count how many triplet values are marked.
    int count = 0;
    for (int x = 0; x < LIM; x++)            // 掃過整個值域 / scan the whole range.
        if (triple[x]) count++;              // 每個被標記的值代表一個不同答案 / each marked value = one unique result.
    return count;                            // 這就是不同三元組 XOR 值的數量 / the answer.
}
