// 演算法與 C 版相同：去重 → 兩數 XOR 集合 → 三元組 XOR 集合 → 計數。
// Same algorithm as the C version: dedup → pairwise XOR set → triplet XOR set → count.
#include <vector>
#include <bitset>
using namespace std;

class Solution {
public:
    int uniqueXorTriplets(vector<int>& nums) {
        const int LIM = 2048;                // 值域上限（2^11），因為值 < 2048 / value bound, since values < 2048.

        // bitset<LIM> 是固定大小的位元集合，每個位置只佔 1 bit，且自動歸零。
        // bitset<LIM> is a fixed-size bit container; each slot is 1 bit and starts as 0.
        bitset<LIM> present;                 // present[v]=1 表示 v 出現過 / v appears in nums.
        for (int v : nums)                   // range-for：依序取出每個元素 / iterate each element.
            present[v] = 1;                  // 標記出現 / mark as seen.

        // 把出現過的值收集成緊湊清單，之後迴圈只跑不同值 / compact list of distinct values.
        vector<int> distinct;                // vector 是可自動增長的動態陣列 / a growable dynamic array.
        for (int v = 0; v < LIM; v++)
            if (present[v]) distinct.push_back(v);   // push_back 在尾端加入一個元素 / append at the end.

        // pair[x]=1 表示 x 可由某對不同值 XOR 得到 / x achievable as a^b.
        bitset<LIM> pair;
        for (size_t a = 0; a < distinct.size(); a++)         // 第一個值 / first value.
            for (size_t b = a; b < distinct.size(); b++)     // 第二個值，b>=a 允許重複 / second value, repeats allowed.
                pair[distinct[a] ^ distinct[b]] = 1;         // ^ 為位元 XOR / bitwise XOR, mark result.

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

        // bitset 的 count() 直接回傳有幾個位元是 1 / count() returns the number of set bits.
        return (int)triple.count();          // 即不同三元組 XOR 值的數量 / the answer.
    }
};
