Codeforces Round #633 (Div. 1)C. Perfect Triples

C. Perfect Triples

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Consider the infinite sequence ss of positive integers, created by repeating the following steps:

  1. Find the lexicographically smallest triple of positive integers (a,b,c)(a,b,c) such that
    • a⊕b⊕c=0a⊕b⊕c=0, where ⊕⊕ denotes the bitwise XOR operation.
    • aa, bb, cc are not in ss.
    Here triple of integers (a1,b1,c1)(a1,b1,c1) is considered to be lexicographically smaller than triple (a2,b2,c2)(a2,b2,c2) if sequence [a1,b1,c1][a1,b1,c1] is lexicographically smaller than sequence [a2,b2,c2][a2,b2,c2].
  2. Append aa, bb, cc to ss in this order.
  3. Go back to the first step.

You have integer nn. Find the nn-th element of ss.

You have to answer tt independent test cases.

A sequence aa is lexicographically smaller than a sequence bb if in the first position where aa and bb differ, the sequence aa has a smaller element than the corresponding element in bb.

Input

The first line contains a single integer tt (1≤t≤1051≤t≤105) — the number of test cases.

Each of the next tt lines contains a single integer nn (1≤n≤10161≤n≤1016) — the position of the element you want to know.

Output

In each of the tt lines, output the answer to the corresponding test case.

Example

input

Copy

9
1
2
3
4
5
6
7
8
9

output

Copy

1
2
3
4
8
12
5
10
15

Note

The first elements of ss are 1,2,3,4,8,12,5,10,15,…

 

 

题意:构造一个序列s,每次从正整数中找出字典序最小的,且不存在于s中的一个三元组(a,b,c)使其异或和为0,然后将abc依次放入s中。多次询问,每次询问s中的第n个数是多少。

 

分析:遇事不决先打表。

/*
    1 2 3       00 00 01    00 00 10    00 00 11

    4 8 12      00 01 00    00 10 00    00 11 00
    5 10 15     00 01 01    00 10 10    00 11 11
    6 11 13     00 01 10    00 10 11    00 11 01
    7 9 14      00 01 11    00 10 01    00 11 10

    16 32 48    01 00 00    10 00 00    11 00 00
    17 34 51    01 00 01    10 00 10    11 00 11
    18 35 49    01 00 10    10 00 11    11 00 01
    19 33 50    01 00 11    10 00 01    11 00 10
    20 40 60    01 01 00    10 10 00    11 11 00
    21 42 63    01 01 01    10 10 10    11 11 11
    22 43 61    01 01 10    10 10 11    11 11 01
    23 41 62    01 01 11    10 10 01    11 11 10
    24 44 52    01 10 00    10 11 00    11 01 00
    25 46 55    01 10 01    10 11 10    11 01 11
    26 47 53    01 10 10    10 11 11    11 01 01
    27 45 54    01 10 11    10 11 01    11 01 10
    28 36 56    01 11 00    10 01 00    11 10 00
    29 38 59    01 11 01    10 01 10    11 10 11
    30 39 57    01 11 10    10 01 11    11 10 01
    31 37 58    01 11 11    10 01 01    11 10 10
 */
#include 

using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        long long n;
        scanf("%lld", &n);
        long long dw = 1, up = 1;
        while (up <= n)up <<= 2;
        dw = up >> 2;
        if (n % 3 == 1) {
            cout << (n - dw + 3) / 3 + dw - 1 << endl;
        } else if (n % 3 == 2) {
            long long ans = 0;
            int base = 0;
            n -= dw - 1;
            n = (n + 2) / 3;
            n--;
            while (n) {
                if (n % 4 == 1)ans += (2LL << base);
                else if (n % 4 == 2)ans += (3LL << base);
                else if (n % 4 == 3)ans += (1LL << base);
                base += 2;
                n /= 4;
            }
            cout << ans + dw * 2 << endl;
        } else if (n % 3 == 0) {
            long long ans = 0;
            int base = 0;
            n -= dw - 1;
            n = (n + 2) / 3;
            n--;
            while (n) {
                if (n % 4 == 1)ans += (3LL << base);
                else if (n % 4 == 2)ans += (1LL << base);
                else if (n % 4 == 3)ans += (2LL << base);
                base += 2;
                n /= 4;
            }
            cout << ans + dw * 3 << endl;
        }
    }
}

 

你可能感兴趣的:(Codeforces Round #633 (Div. 1)C. Perfect Triples)