1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <cstdlib>
#include <iostream>
#include <unordered_map>

#include "dzilib.h"

int64_t next_power_of_2(int64_t n) {
    int ret = 1;
    while (n >= ret) {
        ret *= 2;
    }
    return ret;
}

void one() {
    int64_t n = GetN();
    int64_t rem = 0;
    std::unordered_map<int64_t, int64_t> cache;

    for (int i = 0; ; ++i) {
        int64_t pow = next_power_of_2(n);
        int64_t step = (int64_t)1 << i;
        if (step > n) {
            break;
        }

        int64_t tested = pow + step - rem;
        for (;;) {
            while (tested >= pow * 2 - n) {
                pow *= 2;
                tested = pow + step - rem;
            }
            if (!cache.count(tested)) {
                cache[tested] = Ask(tested);
            }
            int64_t ret = cache[tested];
            if (ret == (i + 1) * 2) {
                break;
            }
            tested += step;
        }
        rem += !(((tested + rem) / step) % 2) * step;
    }
    Answer(rem);
}

int main() {
    int t = GetT();
    for (int i = 0; i < t; ++i) {
        one();
    }
}