output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
char buf[1 << 15], *fs, *ft;
inline char getc() {
return (fs == ft &&
(ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), fs == ft))
? 0
: *fs++;
}
inline long long read() {
register long long x = 0, f = 1;
register char ch = getc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getc();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getc();
}
return x * f;
}
const long long MAXN = 100010;
long long n, T, top;
long long s[MAXN], a, b, c;
long long p[MAXN], maxx, sum[MAXN];
int main() {
T = read();
p[0] = 1;
maxx = 27;
sum[0] = 1;
for (register long long i = 1; i <= maxx; ++i)
p[i] = p[i - 1] * 4, sum[i] += sum[i - 1] + p[i];
while (T--) {
n = read();
top = 0;
long long ww = (n - 1) / 3 + 1;
long long cc = n % 3 == 0 ? 3 : n % 3;
long long w1 = 0;
while (sum[w1] < ww) ++w1;
ww = ww - (w1 == 0 ? 0 : sum[w1 - 1]);
while (w1) {
long long p1 = (ww - 1) / 4 + 1;
s[++top] = ww - (p1 - 1) * 4;
ww = p1;
--w1;
}
a = 1;
b = 2;
c = 3;
while (top) {
a = a << 2;
b = b << 2;
c = c << 2;
if (s[top] == 2) a += 1, b += 2, c += 3;
if (s[top] == 3) a += 2, b += 3, c += 1;
if (s[top] == 4) a += 3, b += 1, c += 2;
--top;
}
if (cc == 1) printf("%lld\n", a);
if (cc == 2) printf("%lld\n", b);
if (cc == 3) printf("%lld\n", c);
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char buf[1 << 15], *fs, *ft;
inline char getc() {
return (fs == ft &&
(ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), fs == ft))
? 0
: *fs++;
}
inline long long read() {
register long long x = 0, f = 1;
register char ch = getc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getc();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getc();
}
return x * f;
}
const long long MAXN = 100010;
long long n, T, top;
long long s[MAXN], a, b, c;
long long p[MAXN], maxx, sum[MAXN];
int main() {
T = read();
p[0] = 1;
maxx = 27;
sum[0] = 1;
for (register long long i = 1; i <= maxx; ++i)
p[i] = p[i - 1] * 4, sum[i] += sum[i - 1] + p[i];
while (T--) {
n = read();
top = 0;
long long ww = (n - 1) / 3 + 1;
long long cc = n % 3 == 0 ? 3 : n % 3;
long long w1 = 0;
while (sum[w1] < ww) ++w1;
ww = ww - (w1 == 0 ? 0 : sum[w1 - 1]);
while (w1) {
long long p1 = (ww - 1) / 4 + 1;
s[++top] = ww - (p1 - 1) * 4;
ww = p1;
--w1;
}
a = 1;
b = 2;
c = 3;
while (top) {
a = a << 2;
b = b << 2;
c = c << 2;
if (s[top] == 2) a += 1, b += 2, c += 3;
if (s[top] == 3) a += 2, b += 3, c += 1;
if (s[top] == 4) a += 3, b += 1, c += 2;
--top;
}
if (cc == 1) printf("%lld\n", a);
if (cc == 2) printf("%lld\n", b);
if (cc == 3) printf("%lld\n", c);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long r(long long n, long long s[], long long a[], long long su[],
long long ti[][3]) {
long long i, max, sum, min, f;
f = (n - 1) % 3 + 1;
n = (n - f) / 3;
for (i = 0; i < 28; i++) {
if (n < su[i]) {
break;
}
}
n = n - su[i - 1];
sum = 0;
sum += f * s[i - 1];
for (i = 27; i >= 0; i--) {
a[i] = n / s[i];
n -= a[i] * s[i];
}
for (i = 0; i < 28; i++) {
sum += ti[a[i]][f - 1] * s[i];
}
return sum;
}
int main() {
std::ios::sync_with_stdio(false);
long long t, n, i, h;
long long s[28];
long long a[28];
long long su[28];
long long ti[4][3];
ti[0][0] = 0;
ti[0][1] = 0;
ti[0][2] = 0;
ti[1][0] = 1;
ti[1][1] = 2;
ti[1][2] = 3;
ti[2][0] = 2;
ti[2][1] = 3;
ti[2][2] = 1;
ti[3][0] = 3;
ti[3][1] = 1;
ti[3][2] = 2;
cin >> t;
s[0] = 1;
su[0] = 0;
for (i = 1; i < 28; i++) {
s[i] = s[i - 1] * 4;
su[i] = su[i - 1] + s[i - 1];
}
while (t--) {
cin >> n;
cout << r(n, s, a, su, ti) << endl;
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long r(long long n, long long s[], long long a[], long long su[],
long long ti[][3]) {
long long i, max, sum, min, f;
f = (n - 1) % 3 + 1;
n = (n - f) / 3;
for (i = 0; i < 28; i++) {
if (n < su[i]) {
break;
}
}
n = n - su[i - 1];
sum = 0;
sum += f * s[i - 1];
for (i = 27; i >= 0; i--) {
a[i] = n / s[i];
n -= a[i] * s[i];
}
for (i = 0; i < 28; i++) {
sum += ti[a[i]][f - 1] * s[i];
}
return sum;
}
int main() {
std::ios::sync_with_stdio(false);
long long t, n, i, h;
long long s[28];
long long a[28];
long long su[28];
long long ti[4][3];
ti[0][0] = 0;
ti[0][1] = 0;
ti[0][2] = 0;
ti[1][0] = 1;
ti[1][1] = 2;
ti[1][2] = 3;
ti[2][0] = 2;
ti[2][1] = 3;
ti[2][2] = 1;
ti[3][0] = 3;
ti[3][1] = 1;
ti[3][2] = 2;
cin >> t;
s[0] = 1;
su[0] = 0;
for (i = 1; i < 28; i++) {
s[i] = s[i - 1] * 4;
su[i] = su[i - 1] + s[i - 1];
}
while (t--) {
cin >> n;
cout << r(n, s, a, su, ti) << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
long long n, ans, j;
int t, p, a[] = {0, 0, 0, 1, 2, 3, 2, 3, 1, 3, 1, 2};
int main() {
for (scanf("%d", &t); t--; ans = 0) {
scanf("%lld", &n);
p = --n % 3, n = n / 3 - 1;
for (j = 1; n + 1; j *= 4, n = n / 4 - 1) ans += j * a[n % 4 * 3 + p];
printf("%lld\n", ans + j * (p + 1));
}
}
| ### Prompt
Create a solution in CPP for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
long long n, ans, j;
int t, p, a[] = {0, 0, 0, 1, 2, 3, 2, 3, 1, 3, 1, 2};
int main() {
for (scanf("%d", &t); t--; ans = 0) {
scanf("%lld", &n);
p = --n % 3, n = n / 3 - 1;
for (j = 1; n + 1; j *= 4, n = n / 4 - 1) ans += j * a[n % 4 * 3 + p];
printf("%lld\n", ans + j * (p + 1));
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long p4 = 1;
while (p4 * 4 <= n) p4 *= 4;
long long k = p4 + (n - p4) / 3;
if (n % 3 == 1) {
cout << k << endl;
} else if (n % 3 == 2) {
long long ans = 2;
k ^= p4;
while (p4 > 1) {
ans *= 4;
p4 /= 4;
long long x = k / p4;
k %= p4;
switch (x) {
case 0:
break;
case 1:
ans += 2;
break;
case 2:
ans += 3;
break;
case 3:
ans += 1;
break;
}
}
cout << ans << '\n';
} else {
long long ans = 3;
k ^= p4;
while (p4 > 1) {
ans *= 4;
p4 /= 4;
long long x = k / p4;
k %= p4;
switch (x) {
case 0:
break;
case 1:
ans += 3;
break;
case 2:
ans += 1;
break;
case 3:
ans += 2;
break;
}
}
cout << ans << '\n';
}
}
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long p4 = 1;
while (p4 * 4 <= n) p4 *= 4;
long long k = p4 + (n - p4) / 3;
if (n % 3 == 1) {
cout << k << endl;
} else if (n % 3 == 2) {
long long ans = 2;
k ^= p4;
while (p4 > 1) {
ans *= 4;
p4 /= 4;
long long x = k / p4;
k %= p4;
switch (x) {
case 0:
break;
case 1:
ans += 2;
break;
case 2:
ans += 3;
break;
case 3:
ans += 1;
break;
}
}
cout << ans << '\n';
} else {
long long ans = 3;
k ^= p4;
while (p4 > 1) {
ans *= 4;
p4 /= 4;
long long x = k / p4;
k %= p4;
switch (x) {
case 0:
break;
case 1:
ans += 3;
break;
case 2:
ans += 1;
break;
case 3:
ans += 2;
break;
}
}
cout << ans << '\n';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a[200000];
long long b[200000];
long long c[200000];
for (long long i = 0; i < (long long)(200000); i++) a[i] = 0;
long long n, k, t;
long long x, y, z;
long long xx, yy, zz;
long long s, ans;
bool v = true;
ans = 0;
a[0] = 1;
for (long long i = 0; i < (long long)(30); i++) a[i + 1] = a[i] * 4;
scanf("%lld", &t);
for (long long tt = 0; tt < (long long)(t); tt++) {
scanf("%lld", &n);
k = (n + 2) % 3;
n = (n + 2) / 3;
x = 0;
y = 0;
while (true) {
y += a[x];
if (n <= y) break;
x++;
}
xx = a[x] * 2 - (y - n) - 1;
z = a[x] - (y - n) - 1;
yy = 2 * a[x];
for (long long i = 0; i < (long long)(x); i++) {
c[i] = z % 4;
z /= 4;
if (c[i] == 1)
c[i] = 2;
else if (c[i] == 2)
c[i] = 3;
else if (c[i] == 3)
c[i] = 1;
yy += (c[i] * a[i]);
}
zz = xx ^ yy;
if (k == 0)
printf("%lld\n", xx);
else if (k == 1)
printf("%lld\n", yy);
else if (k == 2)
printf("%lld\n", zz);
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a[200000];
long long b[200000];
long long c[200000];
for (long long i = 0; i < (long long)(200000); i++) a[i] = 0;
long long n, k, t;
long long x, y, z;
long long xx, yy, zz;
long long s, ans;
bool v = true;
ans = 0;
a[0] = 1;
for (long long i = 0; i < (long long)(30); i++) a[i + 1] = a[i] * 4;
scanf("%lld", &t);
for (long long tt = 0; tt < (long long)(t); tt++) {
scanf("%lld", &n);
k = (n + 2) % 3;
n = (n + 2) / 3;
x = 0;
y = 0;
while (true) {
y += a[x];
if (n <= y) break;
x++;
}
xx = a[x] * 2 - (y - n) - 1;
z = a[x] - (y - n) - 1;
yy = 2 * a[x];
for (long long i = 0; i < (long long)(x); i++) {
c[i] = z % 4;
z /= 4;
if (c[i] == 1)
c[i] = 2;
else if (c[i] == 2)
c[i] = 3;
else if (c[i] == 3)
c[i] = 1;
yy += (c[i] * a[i]);
}
zz = xx ^ yy;
if (k == 0)
printf("%lld\n", xx);
else if (k == 1)
printf("%lld\n", yy);
else if (k == 2)
printf("%lld\n", zz);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long t, n, x, y;
int tmp[3];
long long calc2(long long x, long long t) {
if (t == 1) {
return 0;
}
long long nt = t / 4;
if (x < nt) {
return calc2(x, nt);
}
if (x < nt * 2) {
return calc2(x - nt, nt) + 2 * nt;
}
if (x < nt * 3) {
return calc2(x - nt * 2, nt) + 3 * nt;
}
if (x < nt * 4) {
return calc2(x - nt * 3, nt) + 1 * nt;
}
}
long long calc3(long long x, long long t) {
if (t == 1) {
return 0;
}
long long nt = t / 4;
if (x < nt) {
return calc3(x, nt);
}
if (x < nt * 2) {
return calc3(x - nt, nt) + 3 * nt;
}
if (x < nt * 3) {
return calc3(x - nt * 2, nt) + 1 * nt;
}
if (x < nt * 4) {
return calc3(x - nt * 3, nt) + 2 * nt;
}
}
int main() {
scanf("%lld", &t);
while (t--) {
scanf("%lld", &n);
x = (n - 1) / 3;
y = (n - 1) % 3 + 1;
long long t = 1, v = 1;
while (v <= x) {
x -= v;
v *= 4;
t++;
}
long long ans = y * v;
if (y == 1) {
ans += x;
}
if (y == 2) {
ans += calc2(x, v);
}
if (y == 3) {
ans += calc3(x, v);
}
printf("%lld\n", ans);
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long t, n, x, y;
int tmp[3];
long long calc2(long long x, long long t) {
if (t == 1) {
return 0;
}
long long nt = t / 4;
if (x < nt) {
return calc2(x, nt);
}
if (x < nt * 2) {
return calc2(x - nt, nt) + 2 * nt;
}
if (x < nt * 3) {
return calc2(x - nt * 2, nt) + 3 * nt;
}
if (x < nt * 4) {
return calc2(x - nt * 3, nt) + 1 * nt;
}
}
long long calc3(long long x, long long t) {
if (t == 1) {
return 0;
}
long long nt = t / 4;
if (x < nt) {
return calc3(x, nt);
}
if (x < nt * 2) {
return calc3(x - nt, nt) + 3 * nt;
}
if (x < nt * 3) {
return calc3(x - nt * 2, nt) + 1 * nt;
}
if (x < nt * 4) {
return calc3(x - nt * 3, nt) + 2 * nt;
}
}
int main() {
scanf("%lld", &t);
while (t--) {
scanf("%lld", &n);
x = (n - 1) / 3;
y = (n - 1) % 3 + 1;
long long t = 1, v = 1;
while (v <= x) {
x -= v;
v *= 4;
t++;
}
long long ans = y * v;
if (y == 1) {
ans += x;
}
if (y == 2) {
ans += calc2(x, v);
}
if (y == 3) {
ans += calc3(x, v);
}
printf("%lld\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod1 = 19260817;
const long long mod2 = 19660813;
long long n;
long long cur;
long long R;
void solve() {
--n;
cur = n % 3;
n = n / 3;
while ((1LL << R) <= n) {
n -= (1LL << R);
R += (1 << 1);
}
for (long long i = 0; i < R; i += 2) {
long long tmp = n >> i & 3;
if (tmp > 0) {
n ^= tmp << i;
tmp = (tmp + cur) % 3;
if (tmp == 0) tmp = 3;
n = (n ^ (tmp << i));
}
}
if (cur == 1)
n |= 1LL << (R + 1);
else if (cur == 0)
n |= (1LL << R);
else {
n = n | (1LL << R);
n = n | (1LL << (R + 1));
}
printf("%lld\n", n);
}
int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
R = 0;
scanf("%lld", &n);
solve();
}
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod1 = 19260817;
const long long mod2 = 19660813;
long long n;
long long cur;
long long R;
void solve() {
--n;
cur = n % 3;
n = n / 3;
while ((1LL << R) <= n) {
n -= (1LL << R);
R += (1 << 1);
}
for (long long i = 0; i < R; i += 2) {
long long tmp = n >> i & 3;
if (tmp > 0) {
n ^= tmp << i;
tmp = (tmp + cur) % 3;
if (tmp == 0) tmp = 3;
n = (n ^ (tmp << i));
}
}
if (cur == 1)
n |= 1LL << (R + 1);
else if (cur == 0)
n |= (1LL << R);
else {
n = n | (1LL << R);
n = n | (1LL << (R + 1));
}
printf("%lld\n", n);
}
int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
R = 0;
scanf("%lld", &n);
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
long long md = 1000000007;
using namespace std;
template <typename T>
T pw(T a, T b) {
T c = 1, m = a;
while (b) {
if (b & 1) c = (c * m);
m = (m * m), b /= 2;
}
return c;
}
template <typename T>
T ceel(T a, T b) {
if (a % b == 0)
return a / b;
else
return a / b + 1;
}
template <typename T>
T my_log(T n, T b) {
T i = 1, ans = 0;
while (1) {
if (i > n) {
ans--;
break;
}
if (i == n) break;
i *= b, ans++;
}
return ans;
}
template <typename T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long pwmd(long long a, long long b) {
long long c = 1, m = a;
while (b) {
if (b & 1) c = (c * m) % md;
m = (m * m) % md;
b /= 2;
}
return c;
}
long long modinv(long long n) { return pwmd(n, md - 2); }
long long inverse(long long i) {
if (i == 1) return 1;
return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long t, n;
cin >> t;
while (t--) {
cin >> n;
if (n <= 4) {
cout << n << "\n";
continue;
}
long long q = 1;
while (n >= 4ll * q) q *= 4ll;
n -= (q - 1);
long long e = n % 3, ans = 0;
n /= 3;
deque<long long> v;
if (e == 0) {
n--;
long long o = n;
if (n == 0) {
cout << ((q + n) ^ (2ll * q + n)) << "\n";
continue;
}
while (n > 0) {
v.push_back(n & 1);
n = (n >> 1ll);
}
reverse((v).begin(), (v).end());
if (v.size() % 2) v.push_front(0);
vector<long long> we(v.size());
for (long long i = 1; i < v.size(); i++) {
if (v[i] == 0 && v[i - 1] == 0) {
we[i] = we[i - 1] = 0;
} else if (v[i - 1] == 0 && v[i] == 1) {
we[i - 1] = 1, we[i] = 0;
} else if (v[i - 1] == 1 && v[i] == 0) {
we[i] = we[i - 1] = 1;
} else
we[i] = 1, we[i - 1] = 0;
i++;
}
ans = 0;
reverse((we).begin(), (we).end());
for (long long i = 0; i < we.size(); i++) {
if (we[i]) ans += (1ll << i);
}
cout << ((q + o) ^ (2ll * q + ans)) << "\n";
} else if (e == 1)
cout << (q + n) << "\n";
else {
if (n == 0) {
cout << (2ll * q + n) << "\n";
continue;
}
while (n > 0) {
v.push_back(n & 1);
n = (n >> 1ll);
}
reverse((v).begin(), (v).end());
if (v.size() % 2) v.push_front(0);
vector<long long> w(v.size());
for (long long i = 1; i < v.size(); i += 2) {
if (v[i] == 0 && v[i - 1] == 0) {
w[i] = w[i - 1] = 0;
} else if (v[i - 1] == 0 && v[i] == 1) {
w[i] = 0, w[i - 1] = 1;
} else if (v[i - 1] == 1 && v[i] == 0) {
w[i] = w[i - 1] = 1;
} else
w[i] = 1, w[i - 1] = 0;
}
reverse((w).begin(), (w).end());
for (long long i = 0; i < w.size(); i++) {
if (w[i]) ans += (1ll << i);
}
cerr << "ans"
<< " = " << ans << endl;
;
cerr << "w[0]"
<< " = " << w[0] << endl;
;
cerr << "w[1]"
<< " = " << w[1] << endl;
;
cout << (2ll * q + ans) << "\n";
}
}
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
long long md = 1000000007;
using namespace std;
template <typename T>
T pw(T a, T b) {
T c = 1, m = a;
while (b) {
if (b & 1) c = (c * m);
m = (m * m), b /= 2;
}
return c;
}
template <typename T>
T ceel(T a, T b) {
if (a % b == 0)
return a / b;
else
return a / b + 1;
}
template <typename T>
T my_log(T n, T b) {
T i = 1, ans = 0;
while (1) {
if (i > n) {
ans--;
break;
}
if (i == n) break;
i *= b, ans++;
}
return ans;
}
template <typename T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long pwmd(long long a, long long b) {
long long c = 1, m = a;
while (b) {
if (b & 1) c = (c * m) % md;
m = (m * m) % md;
b /= 2;
}
return c;
}
long long modinv(long long n) { return pwmd(n, md - 2); }
long long inverse(long long i) {
if (i == 1) return 1;
return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long t, n;
cin >> t;
while (t--) {
cin >> n;
if (n <= 4) {
cout << n << "\n";
continue;
}
long long q = 1;
while (n >= 4ll * q) q *= 4ll;
n -= (q - 1);
long long e = n % 3, ans = 0;
n /= 3;
deque<long long> v;
if (e == 0) {
n--;
long long o = n;
if (n == 0) {
cout << ((q + n) ^ (2ll * q + n)) << "\n";
continue;
}
while (n > 0) {
v.push_back(n & 1);
n = (n >> 1ll);
}
reverse((v).begin(), (v).end());
if (v.size() % 2) v.push_front(0);
vector<long long> we(v.size());
for (long long i = 1; i < v.size(); i++) {
if (v[i] == 0 && v[i - 1] == 0) {
we[i] = we[i - 1] = 0;
} else if (v[i - 1] == 0 && v[i] == 1) {
we[i - 1] = 1, we[i] = 0;
} else if (v[i - 1] == 1 && v[i] == 0) {
we[i] = we[i - 1] = 1;
} else
we[i] = 1, we[i - 1] = 0;
i++;
}
ans = 0;
reverse((we).begin(), (we).end());
for (long long i = 0; i < we.size(); i++) {
if (we[i]) ans += (1ll << i);
}
cout << ((q + o) ^ (2ll * q + ans)) << "\n";
} else if (e == 1)
cout << (q + n) << "\n";
else {
if (n == 0) {
cout << (2ll * q + n) << "\n";
continue;
}
while (n > 0) {
v.push_back(n & 1);
n = (n >> 1ll);
}
reverse((v).begin(), (v).end());
if (v.size() % 2) v.push_front(0);
vector<long long> w(v.size());
for (long long i = 1; i < v.size(); i += 2) {
if (v[i] == 0 && v[i - 1] == 0) {
w[i] = w[i - 1] = 0;
} else if (v[i - 1] == 0 && v[i] == 1) {
w[i] = 0, w[i - 1] = 1;
} else if (v[i - 1] == 1 && v[i] == 0) {
w[i] = w[i - 1] = 1;
} else
w[i] = 1, w[i - 1] = 0;
}
reverse((w).begin(), (w).end());
for (long long i = 0; i < w.size(); i++) {
if (w[i]) ans += (1ll << i);
}
cerr << "ans"
<< " = " << ans << endl;
;
cerr << "w[0]"
<< " = " << w[0] << endl;
;
cerr << "w[1]"
<< " = " << w[1] << endl;
;
cout << (2ll * q + ans) << "\n";
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int T;
cin >> T;
for (int t = 0; t < T; t++) {
long long n;
cin >> n;
long long currentK = 0;
long long seen = 0;
n--;
long long transformations[3][4] = {
{0, 1, 2, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}};
while (true) {
if (3 * (1ll << currentK) > n) {
long long solution = 0;
long long rem = n % 3;
n /= 3;
solution += ((rem + 1) << currentK);
for (int i = 0; i <= currentK + 2; i += 2) {
solution |= ((transformations[rem % 3][(n >> (i)) & 3]) << i);
}
cout << solution << "\n";
break;
} else {
n -= 3 * (1ll << currentK);
currentK += 2;
}
}
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int T;
cin >> T;
for (int t = 0; t < T; t++) {
long long n;
cin >> n;
long long currentK = 0;
long long seen = 0;
n--;
long long transformations[3][4] = {
{0, 1, 2, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}};
while (true) {
if (3 * (1ll << currentK) > n) {
long long solution = 0;
long long rem = n % 3;
n /= 3;
solution += ((rem + 1) << currentK);
for (int i = 0; i <= currentK + 2; i += 2) {
solution |= ((transformations[rem % 3][(n >> (i)) & 3]) << i);
}
cout << solution << "\n";
break;
} else {
n -= 3 * (1ll << currentK);
currentK += 2;
}
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n = 500;
vector<int> res;
set<int> s;
for (int i = 0; i < 1000 * n; ++i) s.insert(i + 1);
while (res.size() < n) {
int a = *s.begin();
int b, c;
s.erase(s.begin());
for (int i : s) {
if (s.find(a ^ i) != s.end()) {
b = i, c = a ^ b;
break;
}
}
s.erase(b);
s.erase(c);
res.push_back(a);
res.push_back(b);
res.push_back(c);
}
int t;
cin >> t;
while (t--) {
cin >> n;
if (n < 100) {
cout << res[n - 1] << "\n";
continue;
}
auto first = [](long long n) {
long long x = 1;
while (x * 4 <= n) x *= 4;
return x + (n - x) / 3;
};
auto secsec = [](long long ind) {
int d[] = {0, 2, 3, 1, 8, 10, 11, 9, 12, 14, 15, 13, 4, 6, 7, 5};
return d[ind];
};
auto second = [secsec](long long n) {
long long x = 1;
while (x * 4 <= n) x *= 4;
long long ind = (n - x) / 3;
long long t = 1LL << 60;
long long res = x * 2;
while (t > 0) {
res += t * secsec(ind / t), ind = ind % t;
t /= 16;
}
return res;
};
if (n % 3 == 1) {
cout << first(n) << "\n";
} else if (n % 3 == 2) {
cout << second(n) << "\n";
} else {
cout << (first(n) ^ second(n)) << "\n";
}
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n = 500;
vector<int> res;
set<int> s;
for (int i = 0; i < 1000 * n; ++i) s.insert(i + 1);
while (res.size() < n) {
int a = *s.begin();
int b, c;
s.erase(s.begin());
for (int i : s) {
if (s.find(a ^ i) != s.end()) {
b = i, c = a ^ b;
break;
}
}
s.erase(b);
s.erase(c);
res.push_back(a);
res.push_back(b);
res.push_back(c);
}
int t;
cin >> t;
while (t--) {
cin >> n;
if (n < 100) {
cout << res[n - 1] << "\n";
continue;
}
auto first = [](long long n) {
long long x = 1;
while (x * 4 <= n) x *= 4;
return x + (n - x) / 3;
};
auto secsec = [](long long ind) {
int d[] = {0, 2, 3, 1, 8, 10, 11, 9, 12, 14, 15, 13, 4, 6, 7, 5};
return d[ind];
};
auto second = [secsec](long long n) {
long long x = 1;
while (x * 4 <= n) x *= 4;
long long ind = (n - x) / 3;
long long t = 1LL << 60;
long long res = x * 2;
while (t > 0) {
res += t * secsec(ind / t), ind = ind % t;
t /= 16;
}
return res;
};
if (n % 3 == 1) {
cout << first(n) << "\n";
} else if (n % 3 == 2) {
cout << second(n) << "\n";
} else {
cout << (first(n) ^ second(n)) << "\n";
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int T;
long long mp[4] = {0, 2, 3, 1};
long long n, base, mod;
long long ans[3];
int main() {
ios::sync_with_stdio(false);
scanf("%d", &T);
while (T--) {
ans[1] = 0;
base = 1;
scanf("%lld", &n);
--n;
mod = n % 3;
n /= 3;
if (n == 0) {
base = 1;
n = 0;
} else {
do {
n -= base;
base <<= 2;
} while (n >= base);
}
ans[0] = n + base;
for (int i = 0; i < 60; i += 2) {
ans[1] |= mp[(ans[0] >> i) & 3] << i;
}
ans[2] = ans[0] ^ ans[1];
printf("%lld\n", ans[mod]);
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int T;
long long mp[4] = {0, 2, 3, 1};
long long n, base, mod;
long long ans[3];
int main() {
ios::sync_with_stdio(false);
scanf("%d", &T);
while (T--) {
ans[1] = 0;
base = 1;
scanf("%lld", &n);
--n;
mod = n % 3;
n /= 3;
if (n == 0) {
base = 1;
n = 0;
} else {
do {
n -= base;
base <<= 2;
} while (n >= base);
}
ans[0] = n + base;
for (int i = 0; i < 60; i += 2) {
ans[1] |= mp[(ans[0] >> i) & 3] << i;
}
ans[2] = ans[0] ^ ans[1];
printf("%lld\n", ans[mod]);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long T;
long long wei(long long a) {
long long t = 0;
while (a) {
t++;
a >>= 1;
}
return t;
}
long long suan(long long now, long long xuyao) {
if (xuyao == 1) return now;
long long A = wei(now);
long long ss = 0;
for (long long i = A - 1; i >= 2; i -= 2) {
long long a = (1ll << (i - 1)) & now, b = (1ll << (i - 2)) & now;
if (!a && !b) continue;
if (a && !b) ss += (1ll << (i - 1)) + (1ll << (i - 2));
if (!a && b) ss += 1ll << (i - 1);
if (a && b) ss += 1ll << (i - 2);
}
ss += 1ll << A;
return xuyao == 2 ? ss : now ^ ss;
}
signed main() {
cin >> T;
while (T--) {
cin >> n;
long long gg = (n - 1) / 3 + 1;
long long sum = 0;
for (long long i = 1; i <= 63; i += 2) {
sum += 1ll << (i - 1);
if (sum >= gg) {
sum -= 1ll << (i - 1);
gg -= sum + 1;
gg += 1ll << (i - 1);
cout << suan(gg, (n - 1) % 3 + 1) << '\n';
break;
}
}
}
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n;
long long T;
long long wei(long long a) {
long long t = 0;
while (a) {
t++;
a >>= 1;
}
return t;
}
long long suan(long long now, long long xuyao) {
if (xuyao == 1) return now;
long long A = wei(now);
long long ss = 0;
for (long long i = A - 1; i >= 2; i -= 2) {
long long a = (1ll << (i - 1)) & now, b = (1ll << (i - 2)) & now;
if (!a && !b) continue;
if (a && !b) ss += (1ll << (i - 1)) + (1ll << (i - 2));
if (!a && b) ss += 1ll << (i - 1);
if (a && b) ss += 1ll << (i - 2);
}
ss += 1ll << A;
return xuyao == 2 ? ss : now ^ ss;
}
signed main() {
cin >> T;
while (T--) {
cin >> n;
long long gg = (n - 1) / 3 + 1;
long long sum = 0;
for (long long i = 1; i <= 63; i += 2) {
sum += 1ll << (i - 1);
if (sum >= gg) {
sum -= 1ll << (i - 1);
gg -= sum + 1;
gg += 1ll << (i - 1);
cout << suan(gg, (n - 1) % 3 + 1) << '\n';
break;
}
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long cost[4][2] = {{0, 0}, {2, 3}, {3, 1}, {1, 2}};
long long get_a(long long N) {
long long cnt = 1;
while (cnt <= N) {
N -= cnt;
cnt *= 4;
}
return cnt + N;
}
void solve() {
long long N;
cin >> N;
long long x = get_a((N - 1) / 3);
long long f[3] = {x, 0, 0};
for (long long pw = 1; pw <= x; pw *= 4) {
int p = (x & (3 * pw)) / pw;
f[1] += (cost[p][0] * pw);
f[2] += (cost[p][1] * pw);
}
cout << f[(N - 1) % 3] << endl;
}
int main() {
int tests;
cin >> tests;
while (tests--) {
solve();
}
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long cost[4][2] = {{0, 0}, {2, 3}, {3, 1}, {1, 2}};
long long get_a(long long N) {
long long cnt = 1;
while (cnt <= N) {
N -= cnt;
cnt *= 4;
}
return cnt + N;
}
void solve() {
long long N;
cin >> N;
long long x = get_a((N - 1) / 3);
long long f[3] = {x, 0, 0};
for (long long pw = 1; pw <= x; pw *= 4) {
int p = (x & (3 * pw)) / pw;
f[1] += (cost[p][0] * pw);
f[2] += (cost[p][1] * pw);
}
cout << f[(N - 1) % 3] << endl;
}
int main() {
int tests;
cin >> tests;
while (tests--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops, no-stack-protector")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long long maxn = 2e5 + 30;
const long long mod = 1e9 + 7;
const long long base = 1e9;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("t.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long nw = 1;
while (n >= nw) nw *= 4;
nw /= 4;
long long cl = n - (nw - 1);
long long pos = (cl - 1) / 3 + 1;
long long a = pos + nw - 1;
long long b = 0;
for (int i = 0; i < 60; i += 2) {
long long nw = 0;
if (a & (1ll << i)) nw++;
if (a & (1ll << (i + 1))) nw += 2;
if (nw == 1)
b += (1ll << (i + 1));
else if (nw == 2)
b += (1ll << (i)), b += (1ll << (i + 1));
else if (nw == 3)
b += (1ll << (i));
}
long long c = a ^ b;
if (n % 3 == 1)
cout << a << "\n";
else if (n % 3 == 2)
cout << b << "\n";
else
cout << c << "\n";
}
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops, no-stack-protector")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long long maxn = 2e5 + 30;
const long long mod = 1e9 + 7;
const long long base = 1e9;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("t.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long nw = 1;
while (n >= nw) nw *= 4;
nw /= 4;
long long cl = n - (nw - 1);
long long pos = (cl - 1) / 3 + 1;
long long a = pos + nw - 1;
long long b = 0;
for (int i = 0; i < 60; i += 2) {
long long nw = 0;
if (a & (1ll << i)) nw++;
if (a & (1ll << (i + 1))) nw += 2;
if (nw == 1)
b += (1ll << (i + 1));
else if (nw == 2)
b += (1ll << (i)), b += (1ll << (i + 1));
else if (nw == 3)
b += (1ll << (i));
}
long long c = a ^ b;
if (n % 3 == 1)
cout << a << "\n";
else if (n % 3 == 2)
cout << b << "\n";
else
cout << c << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
set<long long> ss;
vector<long long> s;
tuple<long long, long long, long long> find() {
tuple<long long, long long, long long> mn;
for (long long a = 1; a <= 300; a++) {
for (long long b = a + 1; b <= 300; b++) {
for (long long c = b + 1; c <= 300; c++) {
if (!ss.count(a) && !ss.count(b) && !ss.count(c) &&
((a ^ b) ^ c) == 0) {
return {a, b, c};
}
}
}
}
}
long long find_a(long long n) {
long long s = 1, pw = 0, li = 1, ri = 1;
while (1) {
if (li <= n && n <= ri) break;
pw += 2;
s = (1ll << pw);
long long len = (ri - li + 1);
li += (len);
long long new_len = (1ll << (pw + 1)) - (1ll << pw);
ri = li + new_len - 1;
}
return s + (n - li);
}
long long gett(long long len, long long n, long long li, long long pw) {
if (len == 1) return 0;
long long i = n - li;
long long b_id = (i / (len / 4));
long long buff = 0;
if (b_id > 1) buff += (1ll << pw);
if (b_id > 2) buff += (1ll << (pw + 2));
i %= (len / 4);
return buff + gett(len / 4, i + li - (len / 4), li - (len / 4), pw - 2);
}
long long find_b(long long n) {
long long li = 1, ri = 1, len = 1, pw = -2;
while (1) {
if (li <= n && n <= ri) {
return gett(len, n, li, pw);
} else {
len *= 4;
li = ri + 1;
ri = li + len - 1;
pw += 2;
}
}
}
void PUNKS_NOT_DEAD() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long num = (n - 1) / 3 + 1;
long long a = find_a(num);
long long b = -(find_b(num) - a * 2);
long long c = (a ^ b);
if ((n - 1) % 3 == 0)
cout << a << '\n';
else if ((n - 1) % 3 == 1)
cout << b << '\n';
else
cout << c << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
PUNKS_NOT_DEAD();
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
set<long long> ss;
vector<long long> s;
tuple<long long, long long, long long> find() {
tuple<long long, long long, long long> mn;
for (long long a = 1; a <= 300; a++) {
for (long long b = a + 1; b <= 300; b++) {
for (long long c = b + 1; c <= 300; c++) {
if (!ss.count(a) && !ss.count(b) && !ss.count(c) &&
((a ^ b) ^ c) == 0) {
return {a, b, c};
}
}
}
}
}
long long find_a(long long n) {
long long s = 1, pw = 0, li = 1, ri = 1;
while (1) {
if (li <= n && n <= ri) break;
pw += 2;
s = (1ll << pw);
long long len = (ri - li + 1);
li += (len);
long long new_len = (1ll << (pw + 1)) - (1ll << pw);
ri = li + new_len - 1;
}
return s + (n - li);
}
long long gett(long long len, long long n, long long li, long long pw) {
if (len == 1) return 0;
long long i = n - li;
long long b_id = (i / (len / 4));
long long buff = 0;
if (b_id > 1) buff += (1ll << pw);
if (b_id > 2) buff += (1ll << (pw + 2));
i %= (len / 4);
return buff + gett(len / 4, i + li - (len / 4), li - (len / 4), pw - 2);
}
long long find_b(long long n) {
long long li = 1, ri = 1, len = 1, pw = -2;
while (1) {
if (li <= n && n <= ri) {
return gett(len, n, li, pw);
} else {
len *= 4;
li = ri + 1;
ri = li + len - 1;
pw += 2;
}
}
}
void PUNKS_NOT_DEAD() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long num = (n - 1) / 3 + 1;
long long a = find_a(num);
long long b = -(find_b(num) - a * 2);
long long c = (a ^ b);
if ((n - 1) % 3 == 0)
cout << a << '\n';
else if ((n - 1) % 3 == 1)
cout << b << '\n';
else
cout << c << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
PUNKS_NOT_DEAD();
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long one = 1;
long long mp[] = {0, 2, 3, 1};
long long solve(long long n) {
if (n < 4) return n;
long long _n = n;
n -= 4;
long long a, b, c;
for (int i = 2;; i += 2) {
long long can = 3 * (one << i);
if (n < can) {
a = (one << i) | (n / 3);
b = one << (i + 1);
n /= 3;
for (int j = i - 2; j >= 0; j -= 2) {
long long x = n / (one << j);
b |= mp[x] << j;
n -= x * (one << j);
}
break;
}
n -= can;
}
assert(n == 0);
c = a ^ b;
if (_n % 3 == 1) return a;
if (_n % 3 == 2) return b;
return c;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << solve(n) << "\n";
}
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long one = 1;
long long mp[] = {0, 2, 3, 1};
long long solve(long long n) {
if (n < 4) return n;
long long _n = n;
n -= 4;
long long a, b, c;
for (int i = 2;; i += 2) {
long long can = 3 * (one << i);
if (n < can) {
a = (one << i) | (n / 3);
b = one << (i + 1);
n /= 3;
for (int j = i - 2; j >= 0; j -= 2) {
long long x = n / (one << j);
b |= mp[x] << j;
n -= x * (one << j);
}
break;
}
n -= can;
}
assert(n == 0);
c = a ^ b;
if (_n % 3 == 1) return a;
if (_n % 3 == 2) return b;
return c;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << solve(n) << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(long long &x, long long y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(long long &x, long long y) { return (y > x) ? x = y, 1 : 0; }
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const long double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
string bin_str(int x) {
string res;
while (x) {
res += char('0' + x % 2);
x /= 2;
}
reverse(res.begin(), res.end());
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.precision(20);
cout << fixed;
int T;
cin >> T;
while (T--) {
long long x;
cin >> x;
x--;
long long t = x % 3;
x /= 3;
for (long long b = 0;; b += 2) {
if ((1LL << b) <= x) {
x -= (1LL << b);
continue;
}
long long ans = 0;
long long pw = 0;
while (x > 0) {
long long v = x % 4;
if (v == 0) {
v = 0;
} else {
v += t;
if (v > 3) {
v -= 3;
}
}
ans += (1LL << pw) * v;
pw += 2;
x /= 4;
}
ans += (t + 1) * (1LL << b);
cout << ans << "\n";
break;
}
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(long long &x, long long y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(long long &x, long long y) { return (y > x) ? x = y, 1 : 0; }
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const long double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
string bin_str(int x) {
string res;
while (x) {
res += char('0' + x % 2);
x /= 2;
}
reverse(res.begin(), res.end());
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.precision(20);
cout << fixed;
int T;
cin >> T;
while (T--) {
long long x;
cin >> x;
x--;
long long t = x % 3;
x /= 3;
for (long long b = 0;; b += 2) {
if ((1LL << b) <= x) {
x -= (1LL << b);
continue;
}
long long ans = 0;
long long pw = 0;
while (x > 0) {
long long v = x % 4;
if (v == 0) {
v = 0;
} else {
v += t;
if (v > 3) {
v -= 3;
}
}
ans += (1LL << pw) * v;
pw += 2;
x /= 4;
}
ans += (t + 1) * (1LL << b);
cout << ans << "\n";
break;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long md = 998244353;
const int xn = 1e5 + 10;
const int xm = -20 + 10;
const int SQ = 450;
const int sq = 1e3 + 10;
const int inf = 1e9 + 10;
const long long INF = 1e18 + 10;
long long power(long long a, long long b) {
return (!b ? 1
: (b & 1 ? a * power(a * a % md, b / 2) % md
: power(a * a % md, b / 2) % md));
}
long long zarb(long long a, long long b) { return (a * b + 10 * md) % md; }
long long jaam(long long a, long long b) { return (a + b + 10 * md) % md; }
string abc = "abcdefghijklmnopqrstuvwxyz";
long long n, qq, k, ind, sum, m = 0;
int main() {
cin >> qq;
while (qq--) {
cin >> n;
n--;
ind = n / 3 + 1;
sum = 0;
k = 0;
while (true) {
sum += (1LL << (2 * k));
if (ind <= sum) break;
++k;
}
sum -= (1LL << (2 * k));
long long a = ind - sum - 1 + (1LL << (2 * k));
long long last = (1LL << (k + k + 1));
long long dx = (ind - sum - 1) / 4;
long long x = (ind - sum - 1);
long long b = last;
long long kk = 0;
while (x) {
long long t = x % 4;
if (t % 4 < 3 && t % 4)
++t;
else if (t % 4)
t -= 2;
b += t * (1LL << (2 * kk));
++kk;
x /= 4;
}
long long c = a ^ b;
if (n % 3 == 0)
cout << a << '\n';
else if (n % 3 == 1)
cout << b << '\n';
else
cout << c << '\n';
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long md = 998244353;
const int xn = 1e5 + 10;
const int xm = -20 + 10;
const int SQ = 450;
const int sq = 1e3 + 10;
const int inf = 1e9 + 10;
const long long INF = 1e18 + 10;
long long power(long long a, long long b) {
return (!b ? 1
: (b & 1 ? a * power(a * a % md, b / 2) % md
: power(a * a % md, b / 2) % md));
}
long long zarb(long long a, long long b) { return (a * b + 10 * md) % md; }
long long jaam(long long a, long long b) { return (a + b + 10 * md) % md; }
string abc = "abcdefghijklmnopqrstuvwxyz";
long long n, qq, k, ind, sum, m = 0;
int main() {
cin >> qq;
while (qq--) {
cin >> n;
n--;
ind = n / 3 + 1;
sum = 0;
k = 0;
while (true) {
sum += (1LL << (2 * k));
if (ind <= sum) break;
++k;
}
sum -= (1LL << (2 * k));
long long a = ind - sum - 1 + (1LL << (2 * k));
long long last = (1LL << (k + k + 1));
long long dx = (ind - sum - 1) / 4;
long long x = (ind - sum - 1);
long long b = last;
long long kk = 0;
while (x) {
long long t = x % 4;
if (t % 4 < 3 && t % 4)
++t;
else if (t % 4)
t -= 2;
b += t * (1LL << (2 * kk));
++kk;
x /= 4;
}
long long c = a ^ b;
if (n % 3 == 0)
cout << a << '\n';
else if (n % 3 == 1)
cout << b << '\n';
else
cout << c << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int pow1(long long int a, long long int b,
long long int m = 1000000007) {
if (b == 0)
return 1ll;
else if (b == 1)
return a;
else {
long long int x = pow1(a, b / 2, m);
x *= x;
x %= m;
if (b % 2) {
x *= a;
x %= m;
}
return x;
}
}
template <class avgType>
avgType avg(avgType a, avgType b) {
return (a + b) / 2;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int randInt() { return rng() % INT_MAX; }
long long int randLL() { return rng64() % LLONG_MAX; }
vector<int> hashmods = {1000000007, 1000000009, 1000000021,
1000000033, 1000000087, 1000000093};
int t;
long long int n;
long long int solve(long long int x) {
if (x <= 4) return x;
int fl = 0;
if (x % 3 == 0) {
x--;
fl = 1;
}
long long int x1 = 1, x2 = 2, curr = 0;
while (true) {
curr += (x1 + x2);
if (curr >= x) break;
x1 *= 4ll;
x2 *= 4ll;
}
long long int prev = curr - (x1 + x2);
long long int rem = x - prev;
long long int div = (x1 + x2) / 4ll;
long long int quad = rem / div;
if (rem % div) quad++;
long long int x11 = x1 / 4ll, x22 = x2 / 4ll;
long long int quadstr = x2;
if (quad >= 2) quadstr += x22;
if (quad >= 3) quadstr += x11;
if (quad == 4) quadstr -= x22;
if (x % 3 == 1) {
return (x1 + (x - prev) / 3);
} else {
long long int qd = quad, rem1 = rem, ans = quadstr, iter = 0;
while (x11 > 1) {
long long int within = rem1 - (qd - 1) * x11 * 3ll;
long long int div1 = (x11 + x22) / 4ll;
long long int qd1 = within / div1;
if (within % div1) qd1++;
x11 /= 4;
x22 /= 4;
if (qd1 == 2)
ans += x22;
else if (qd1 == 3)
ans += (x22 + x11);
else if (qd1 == 4)
ans += (x11);
qd = qd1;
rem1 = within;
}
rem1 = rem1 - (qd - 1) * x11 * 3;
if (x11 == 1) {
if (rem1 == 5)
ans += 2;
else if (rem1 == 8)
ans += 3;
else if (rem1 == 11)
ans += 1;
}
if (fl == 0)
return ans;
else {
return ans ^ (x1 + ((x - 1) - prev) / 3);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> t;
while (t--) {
cin >> n;
cout << solve(n) << "\n";
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int pow1(long long int a, long long int b,
long long int m = 1000000007) {
if (b == 0)
return 1ll;
else if (b == 1)
return a;
else {
long long int x = pow1(a, b / 2, m);
x *= x;
x %= m;
if (b % 2) {
x *= a;
x %= m;
}
return x;
}
}
template <class avgType>
avgType avg(avgType a, avgType b) {
return (a + b) / 2;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int randInt() { return rng() % INT_MAX; }
long long int randLL() { return rng64() % LLONG_MAX; }
vector<int> hashmods = {1000000007, 1000000009, 1000000021,
1000000033, 1000000087, 1000000093};
int t;
long long int n;
long long int solve(long long int x) {
if (x <= 4) return x;
int fl = 0;
if (x % 3 == 0) {
x--;
fl = 1;
}
long long int x1 = 1, x2 = 2, curr = 0;
while (true) {
curr += (x1 + x2);
if (curr >= x) break;
x1 *= 4ll;
x2 *= 4ll;
}
long long int prev = curr - (x1 + x2);
long long int rem = x - prev;
long long int div = (x1 + x2) / 4ll;
long long int quad = rem / div;
if (rem % div) quad++;
long long int x11 = x1 / 4ll, x22 = x2 / 4ll;
long long int quadstr = x2;
if (quad >= 2) quadstr += x22;
if (quad >= 3) quadstr += x11;
if (quad == 4) quadstr -= x22;
if (x % 3 == 1) {
return (x1 + (x - prev) / 3);
} else {
long long int qd = quad, rem1 = rem, ans = quadstr, iter = 0;
while (x11 > 1) {
long long int within = rem1 - (qd - 1) * x11 * 3ll;
long long int div1 = (x11 + x22) / 4ll;
long long int qd1 = within / div1;
if (within % div1) qd1++;
x11 /= 4;
x22 /= 4;
if (qd1 == 2)
ans += x22;
else if (qd1 == 3)
ans += (x22 + x11);
else if (qd1 == 4)
ans += (x11);
qd = qd1;
rem1 = within;
}
rem1 = rem1 - (qd - 1) * x11 * 3;
if (x11 == 1) {
if (rem1 == 5)
ans += 2;
else if (rem1 == 8)
ans += 3;
else if (rem1 == 11)
ans += 1;
}
if (fl == 0)
return ans;
else {
return ans ^ (x1 + ((x - 1) - prev) / 3);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> t;
while (t--) {
cin >> n;
cout << solve(n) << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
struct result {
long long level;
long long sum_up_to;
};
result find_level(long long n) {
result res;
res.sum_up_to = 0;
for (long long level = 0; level <= 100; level += 2) {
long long this_level = (1ll << level);
if (n < res.sum_up_to + this_level) {
res.level = level;
return res;
}
res.sum_up_to += this_level;
}
return res;
}
long long solve1(long long n) {
auto lvl = find_level(n);
return (n - lvl.sum_up_to) + (1ll << lvl.level);
}
long long solve2(long long n) {
static const long long mp[] = {0, 2, 3, 1};
auto lvl = find_level(n);
long long offset = (n - lvl.sum_up_to);
long long base = ((1ll << lvl.level) << 1);
long long spec_offset = 0;
for (long long level = lvl.level - 2; level >= 0; level -= 2) {
long long bitmask = 3 * (1ll << level);
long long idx = (offset & bitmask) >> level;
spec_offset += mp[idx] * (1ll << level);
}
return base + spec_offset;
}
long long solve3(long long n) {
static const long long mp[] = {0, 3, 1, 2};
auto lvl = find_level(n);
long long offset = (n - lvl.sum_up_to);
long long base = ((1ll << lvl.level) << 1) + ((1ll << lvl.level));
long long spec_offset = 0;
for (long long level = lvl.level - 2; level >= 0; level -= 2) {
long long bitmask = 3 * (1ll << level);
long long idx = (offset & bitmask) >> level;
spec_offset += mp[idx] * (1ll << level);
}
return base + spec_offset;
}
void solve() {
long long n;
std::cin >> n;
long long ans = 0;
if (n % 3 == 1) {
ans = solve1((n - 1) / 3);
} else if (n % 3 == 2) {
ans = solve2((n - 2) / 3);
} else if (n % 3 == 0) {
ans = solve3((n - 3) / 3);
}
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) solve();
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
struct result {
long long level;
long long sum_up_to;
};
result find_level(long long n) {
result res;
res.sum_up_to = 0;
for (long long level = 0; level <= 100; level += 2) {
long long this_level = (1ll << level);
if (n < res.sum_up_to + this_level) {
res.level = level;
return res;
}
res.sum_up_to += this_level;
}
return res;
}
long long solve1(long long n) {
auto lvl = find_level(n);
return (n - lvl.sum_up_to) + (1ll << lvl.level);
}
long long solve2(long long n) {
static const long long mp[] = {0, 2, 3, 1};
auto lvl = find_level(n);
long long offset = (n - lvl.sum_up_to);
long long base = ((1ll << lvl.level) << 1);
long long spec_offset = 0;
for (long long level = lvl.level - 2; level >= 0; level -= 2) {
long long bitmask = 3 * (1ll << level);
long long idx = (offset & bitmask) >> level;
spec_offset += mp[idx] * (1ll << level);
}
return base + spec_offset;
}
long long solve3(long long n) {
static const long long mp[] = {0, 3, 1, 2};
auto lvl = find_level(n);
long long offset = (n - lvl.sum_up_to);
long long base = ((1ll << lvl.level) << 1) + ((1ll << lvl.level));
long long spec_offset = 0;
for (long long level = lvl.level - 2; level >= 0; level -= 2) {
long long bitmask = 3 * (1ll << level);
long long idx = (offset & bitmask) >> level;
spec_offset += mp[idx] * (1ll << level);
}
return base + spec_offset;
}
void solve() {
long long n;
std::cin >> n;
long long ans = 0;
if (n % 3 == 1) {
ans = solve1((n - 1) / 3);
} else if (n % 3 == 2) {
ans = solve2((n - 2) / 3);
} else if (n % 3 == 0) {
ans = solve3((n - 3) / 3);
}
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) solve();
return 0;
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
long long md = 1000000007;
using namespace std;
template <typename T>
T pw(T a, T b) {
T c = 1, m = a;
while (b) {
if (b & 1) c = (c * m);
m = (m * m), b /= 2;
}
return c;
}
template <typename T>
T ceel(T a, T b) {
if (a % b == 0)
return a / b;
else
return a / b + 1;
}
template <typename T>
T my_log(T n, T b) {
T i = 1, ans = 0;
while (1) {
if (i > n) {
ans--;
break;
}
if (i == n) break;
i *= b, ans++;
}
return ans;
}
template <typename T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long pwmd(long long a, long long b) {
long long c = 1, m = a;
while (b) {
if (b & 1) c = (c * m) % md;
m = (m * m) % md;
b /= 2;
}
return c;
}
long long modinv(long long n) { return pwmd(n, md - 2); }
long long inverse(long long i) {
if (i == 1) return 1;
return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long t, n;
cin >> t;
while (t--) {
cin >> n;
if (n <= 4) {
cout << n << "\n";
continue;
}
long long q = 1;
while (n >= 4ll * q) q *= 4ll;
n -= (q - 1);
long long e = n % 3, ans = 0;
n /= 3;
deque<long long> v;
if (e == 0) {
n--;
long long o = n;
if (n == 0) {
cout << ((q + n) ^ (2ll * q + n)) << "\n";
continue;
}
while (n > 0) {
v.push_back(n & 1);
n = (n >> 1ll);
}
reverse((v).begin(), (v).end());
if (v.size() % 2) v.push_front(0);
vector<long long> we(v.size());
for (long long i = 1; i < v.size(); i++) {
if (v[i] == 0 && v[i - 1] == 0) {
we[i] = we[i - 1] = 0;
} else if (v[i - 1] == 0 && v[i] == 1) {
we[i - 1] = 1, we[i] = 0;
} else if (v[i - 1] == 1 && v[i] == 0) {
we[i] = we[i - 1] = 1;
} else
we[i] = 1, we[i - 1] = 0;
i++;
}
ans = 0;
reverse((we).begin(), (we).end());
for (long long i = 0; i < we.size(); i++) {
if (we[i]) ans += (1ll << i);
}
cout << ((q + o) ^ (2ll * q + ans)) << "\n";
} else if (e == 1)
cout << (q + n) << "\n";
else {
if (n == 0) {
cout << (2ll * q + n) << "\n";
continue;
}
while (n > 0) {
v.push_back(n & 1);
n = (n >> 1ll);
}
reverse((v).begin(), (v).end());
if (v.size() % 2) v.push_front(0);
vector<long long> w(v.size());
for (long long i = 1; i < v.size(); i++) {
if (v[i] == 0 && v[i - 1] == 0) {
w[i] = w[i - 1] = 0;
} else if (v[i - 1] == 0 && v[i] == 1) {
w[i] = 0, w[i - 1] = 1;
} else if (v[i - 1] == 1 && v[i] == 0) {
w[i] = w[i - 1] = 1;
} else
w[i] = 1, w[i - 1] = 0;
i++;
}
reverse((w).begin(), (w).end());
for (long long i = 0; i < w.size(); i++) {
if (w[i]) ans += (1ll << i);
}
cerr << "ans"
<< " = " << ans << endl;
;
cerr << "w[0]"
<< " = " << w[0] << endl;
;
cerr << "w[1]"
<< " = " << w[1] << endl;
;
cout << (2ll * q + ans) << "\n";
}
}
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
long long md = 1000000007;
using namespace std;
template <typename T>
T pw(T a, T b) {
T c = 1, m = a;
while (b) {
if (b & 1) c = (c * m);
m = (m * m), b /= 2;
}
return c;
}
template <typename T>
T ceel(T a, T b) {
if (a % b == 0)
return a / b;
else
return a / b + 1;
}
template <typename T>
T my_log(T n, T b) {
T i = 1, ans = 0;
while (1) {
if (i > n) {
ans--;
break;
}
if (i == n) break;
i *= b, ans++;
}
return ans;
}
template <typename T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long pwmd(long long a, long long b) {
long long c = 1, m = a;
while (b) {
if (b & 1) c = (c * m) % md;
m = (m * m) % md;
b /= 2;
}
return c;
}
long long modinv(long long n) { return pwmd(n, md - 2); }
long long inverse(long long i) {
if (i == 1) return 1;
return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long t, n;
cin >> t;
while (t--) {
cin >> n;
if (n <= 4) {
cout << n << "\n";
continue;
}
long long q = 1;
while (n >= 4ll * q) q *= 4ll;
n -= (q - 1);
long long e = n % 3, ans = 0;
n /= 3;
deque<long long> v;
if (e == 0) {
n--;
long long o = n;
if (n == 0) {
cout << ((q + n) ^ (2ll * q + n)) << "\n";
continue;
}
while (n > 0) {
v.push_back(n & 1);
n = (n >> 1ll);
}
reverse((v).begin(), (v).end());
if (v.size() % 2) v.push_front(0);
vector<long long> we(v.size());
for (long long i = 1; i < v.size(); i++) {
if (v[i] == 0 && v[i - 1] == 0) {
we[i] = we[i - 1] = 0;
} else if (v[i - 1] == 0 && v[i] == 1) {
we[i - 1] = 1, we[i] = 0;
} else if (v[i - 1] == 1 && v[i] == 0) {
we[i] = we[i - 1] = 1;
} else
we[i] = 1, we[i - 1] = 0;
i++;
}
ans = 0;
reverse((we).begin(), (we).end());
for (long long i = 0; i < we.size(); i++) {
if (we[i]) ans += (1ll << i);
}
cout << ((q + o) ^ (2ll * q + ans)) << "\n";
} else if (e == 1)
cout << (q + n) << "\n";
else {
if (n == 0) {
cout << (2ll * q + n) << "\n";
continue;
}
while (n > 0) {
v.push_back(n & 1);
n = (n >> 1ll);
}
reverse((v).begin(), (v).end());
if (v.size() % 2) v.push_front(0);
vector<long long> w(v.size());
for (long long i = 1; i < v.size(); i++) {
if (v[i] == 0 && v[i - 1] == 0) {
w[i] = w[i - 1] = 0;
} else if (v[i - 1] == 0 && v[i] == 1) {
w[i] = 0, w[i - 1] = 1;
} else if (v[i - 1] == 1 && v[i] == 0) {
w[i] = w[i - 1] = 1;
} else
w[i] = 1, w[i - 1] = 0;
i++;
}
reverse((w).begin(), (w).end());
for (long long i = 0; i < w.size(); i++) {
if (w[i]) ans += (1ll << i);
}
cerr << "ans"
<< " = " << ans << endl;
;
cerr << "w[0]"
<< " = " << w[0] << endl;
;
cerr << "w[1]"
<< " = " << w[1] << endl;
;
cout << (2ll * q + ans) << "\n";
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long N;
cin >> N;
long long n = (N - 1) / 3;
if (n == 0) {
cout << N << "\n";
return;
}
int dig = 1;
long long cnt = 1;
while (true) {
long long add = (1ll << (2 * dig));
if (n < cnt + add) break;
dig++;
cnt += add;
}
long long rel = n - cnt;
vector<int> pl;
while (rel) {
pl.push_back(rel % 4);
rel /= 4;
}
while (pl.size() != dig) pl.push_back(0);
pl.push_back(1);
vector<vector<long long>> its{{0, 1, 2, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}};
vector<long long> mine = its[(N - 1) % 3];
long long ans = 0;
for (int i = 0; i < (int)pl.size(); i++) {
ans += (mine[pl[i]] << (2 * i));
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
}
| ### Prompt
Please create a solution in cpp to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long N;
cin >> N;
long long n = (N - 1) / 3;
if (n == 0) {
cout << N << "\n";
return;
}
int dig = 1;
long long cnt = 1;
while (true) {
long long add = (1ll << (2 * dig));
if (n < cnt + add) break;
dig++;
cnt += add;
}
long long rel = n - cnt;
vector<int> pl;
while (rel) {
pl.push_back(rel % 4);
rel /= 4;
}
while (pl.size() != dig) pl.push_back(0);
pl.push_back(1);
vector<vector<long long>> its{{0, 1, 2, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}};
vector<long long> mine = its[(N - 1) % 3];
long long ans = 0;
for (int i = 0; i < (int)pl.size(); i++) {
ans += (mine[pl[i]] << (2 * i));
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/STACK:2000000")
using namespace std;
using ll = long long;
using db = long double;
using ii = pair<int, int>;
const int N = 2e5 + 5, LG = 19, MOD = 1e9 + 7;
const int SQ = 320;
const long double EPS = 1e-7;
vector<int> adj[100005];
int cnt = 0;
bitset<1024> bt;
int ans = 0;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
ll n;
cin >> n;
for (ll i = 0;; i++) {
if (n > 3 * (1ll << (i + i))) {
n -= (1ll << (i + i)) * 3;
} else {
if (n % 3 == 1) {
n = 1 + n / 3;
cout << (1ll << (i + i)) + n - 1 << '\n';
} else if (n % 3 == 2) {
n = 1 + n / 3;
ll offset = (1ll << (i + i + 1));
ll ord[] = {0, 2, 3, 1};
n--;
int bits = i + i;
for (int j = 0; j < bits; j += 2) {
offset += ord[n % 4] << j;
n >>= 2;
}
cout << offset << '\n';
} else if (n % 3 == 0) {
n /= 3;
ll offset = 3 * (1ll << (i + i));
n--;
ll ord[] = {0, 3, 1, 2};
int bits = i + i;
for (int j = 0; j < bits; j += 2) {
offset += ord[n % 4] << j;
n >>= 2;
}
cout << offset << '\n';
}
break;
}
}
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/STACK:2000000")
using namespace std;
using ll = long long;
using db = long double;
using ii = pair<int, int>;
const int N = 2e5 + 5, LG = 19, MOD = 1e9 + 7;
const int SQ = 320;
const long double EPS = 1e-7;
vector<int> adj[100005];
int cnt = 0;
bitset<1024> bt;
int ans = 0;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
ll n;
cin >> n;
for (ll i = 0;; i++) {
if (n > 3 * (1ll << (i + i))) {
n -= (1ll << (i + i)) * 3;
} else {
if (n % 3 == 1) {
n = 1 + n / 3;
cout << (1ll << (i + i)) + n - 1 << '\n';
} else if (n % 3 == 2) {
n = 1 + n / 3;
ll offset = (1ll << (i + i + 1));
ll ord[] = {0, 2, 3, 1};
n--;
int bits = i + i;
for (int j = 0; j < bits; j += 2) {
offset += ord[n % 4] << j;
n >>= 2;
}
cout << offset << '\n';
} else if (n % 3 == 0) {
n /= 3;
ll offset = 3 * (1ll << (i + i));
n--;
ll ord[] = {0, 3, 1, 2};
int bits = i + i;
for (int j = 0; j < bits; j += 2) {
offset += ord[n % 4] << j;
n >>= 2;
}
cout << offset << '\n';
}
break;
}
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long ads[4][3];
pair<long long, pair<long long, long long> > ini_ans[256];
pair<long long, pair<long long, long long> > solve(long long x) {
if (x < 256) return ini_ans[x];
int r = x & 3;
pair<long long, pair<long long, long long> > r1 = solve(x / 4);
long long x1 = r1.first * 4 + ads[r][0];
long long x2 = r1.second.first * 4 + ads[r][1];
long long x3 = r1.second.second * 4 + ads[r][2];
return pair<long long, pair<long long, long long> >(
x1, pair<long long, long long>(x2, x3));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
set<int> s;
vector<int> v;
for (int i = 1; i < 150; i++) {
if (s.find(i) != s.end()) continue;
for (int j = i + 1; 1; j++) {
if (s.find(j) != s.end()) continue;
if (s.find(i ^ j) != s.end()) continue;
s.insert(i);
s.insert(j);
s.insert(j ^ i);
v.push_back(i);
v.push_back(j);
v.push_back(j ^ i);
ini_ans[i] = pair<long long, pair<long long, long long> >(
i, pair<long long, long long>(j, j ^ i));
break;
}
}
ads[1][0] = 1;
ads[1][1] = 2;
ads[1][2] = 3;
ads[2][0] = 2;
ads[2][1] = 3;
ads[2][2] = 1;
ads[3][0] = 3;
ads[3][1] = 1;
ads[3][2] = 2;
int t;
cin >> t;
while (t--) {
long long x;
cin >> x;
if (x < 256)
cout << (v[x - 1]) << '\n';
else {
long long v = 256;
while (v <= x) v *= 4;
v /= 4;
x -= v;
int t = x % 3;
long long vv = x / 3 + v;
pair<long long, pair<long long, long long> > res = solve(vv);
if (t == 0)
cout << (res.first) << '\n';
else if (t == 1)
cout << (res.second.first) << '\n';
else
cout << (res.second.second) << '\n';
}
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long ads[4][3];
pair<long long, pair<long long, long long> > ini_ans[256];
pair<long long, pair<long long, long long> > solve(long long x) {
if (x < 256) return ini_ans[x];
int r = x & 3;
pair<long long, pair<long long, long long> > r1 = solve(x / 4);
long long x1 = r1.first * 4 + ads[r][0];
long long x2 = r1.second.first * 4 + ads[r][1];
long long x3 = r1.second.second * 4 + ads[r][2];
return pair<long long, pair<long long, long long> >(
x1, pair<long long, long long>(x2, x3));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
set<int> s;
vector<int> v;
for (int i = 1; i < 150; i++) {
if (s.find(i) != s.end()) continue;
for (int j = i + 1; 1; j++) {
if (s.find(j) != s.end()) continue;
if (s.find(i ^ j) != s.end()) continue;
s.insert(i);
s.insert(j);
s.insert(j ^ i);
v.push_back(i);
v.push_back(j);
v.push_back(j ^ i);
ini_ans[i] = pair<long long, pair<long long, long long> >(
i, pair<long long, long long>(j, j ^ i));
break;
}
}
ads[1][0] = 1;
ads[1][1] = 2;
ads[1][2] = 3;
ads[2][0] = 2;
ads[2][1] = 3;
ads[2][2] = 1;
ads[3][0] = 3;
ads[3][1] = 1;
ads[3][2] = 2;
int t;
cin >> t;
while (t--) {
long long x;
cin >> x;
if (x < 256)
cout << (v[x - 1]) << '\n';
else {
long long v = 256;
while (v <= x) v *= 4;
v /= 4;
x -= v;
int t = x % 3;
long long vv = x / 3 + v;
pair<long long, pair<long long, long long> > res = solve(vv);
if (t == 0)
cout << (res.first) << '\n';
else if (t == 1)
cout << (res.second.first) << '\n';
else
cout << (res.second.second) << '\n';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int conv[][3] = {{1, 2, 3}, {2, 3, 1}, {3, 1, 2}};
void solve(long long int n) {
int r = n % 3;
if (r != 0) n = (n / 3 + 1) * 3;
if (r == 0)
r = 2;
else
r = r - 1;
int k = 0;
long long int cnt = 3;
long long int tmp;
while (cnt < n) {
k++;
tmp = 3 * pow(4, k);
cnt += tmp;
}
cnt -= tmp;
cnt = n - cnt;
int i = 0;
long long int ans = 0;
ans += conv[0][r] << (2 * (k - i));
int j;
for (i = 1; i < k + 1; i++) {
for (j = 0; j < 4; j++) {
tmp = 3 * pow(4, k - i);
cnt -= tmp;
if (cnt <= 0) {
cnt += tmp;
break;
}
}
if (j > 0) ans += conv[j - 1][r] << (2 * (k - i));
}
printf("%lld\n", ans);
}
int main() {
int t;
long long int n;
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
solve(n);
}
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int conv[][3] = {{1, 2, 3}, {2, 3, 1}, {3, 1, 2}};
void solve(long long int n) {
int r = n % 3;
if (r != 0) n = (n / 3 + 1) * 3;
if (r == 0)
r = 2;
else
r = r - 1;
int k = 0;
long long int cnt = 3;
long long int tmp;
while (cnt < n) {
k++;
tmp = 3 * pow(4, k);
cnt += tmp;
}
cnt -= tmp;
cnt = n - cnt;
int i = 0;
long long int ans = 0;
ans += conv[0][r] << (2 * (k - i));
int j;
for (i = 1; i < k + 1; i++) {
for (j = 0; j < 4; j++) {
tmp = 3 * pow(4, k - i);
cnt -= tmp;
if (cnt <= 0) {
cnt += tmp;
break;
}
}
if (j > 0) ans += conv[j - 1][r] << (2 * (k - i));
}
printf("%lld\n", ans);
}
int main() {
int t;
long long int n;
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
solve(n);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long SZ = 1e5 + 4;
const double EPS = 1e-7;
const long long mod = 1e9 + 7;
const long long hashpow = 2;
const long long inf = 2e9;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
long long n;
void read() {
cin >> n;
--n;
}
void solve() {
long long x = 1;
long long m = n % 3;
n /= 3;
long long cnt = 0;
while (n >= x) {
n -= x;
x *= 4;
++cnt;
}
long long a = 1;
long long b = 2;
long long c = 3;
vector<long long> y;
while (n) {
y.push_back(n % 4);
n /= 4;
}
while (y.size() < cnt) {
y.push_back(0);
}
for (long long i = (long long)(y.size()) - 1; i >= 0; --i) {
a *= 4;
b *= 4;
c *= 4;
if (y[i] == 1) {
a += 1;
b += 2;
c += 3;
} else if (y[i] == 2) {
a += 2;
b += 3;
c += 1;
} else if (y[i] == 3) {
a += 3;
b += 1;
c += 2;
}
}
if (m == 0) {
cout << a << "\n";
} else if (m == 1) {
cout << b << "\n";
} else {
cout << c << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t;
cin >> t;
for (long long i = 0; i < t; ++i) {
read();
solve();
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long SZ = 1e5 + 4;
const double EPS = 1e-7;
const long long mod = 1e9 + 7;
const long long hashpow = 2;
const long long inf = 2e9;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
long long n;
void read() {
cin >> n;
--n;
}
void solve() {
long long x = 1;
long long m = n % 3;
n /= 3;
long long cnt = 0;
while (n >= x) {
n -= x;
x *= 4;
++cnt;
}
long long a = 1;
long long b = 2;
long long c = 3;
vector<long long> y;
while (n) {
y.push_back(n % 4);
n /= 4;
}
while (y.size() < cnt) {
y.push_back(0);
}
for (long long i = (long long)(y.size()) - 1; i >= 0; --i) {
a *= 4;
b *= 4;
c *= 4;
if (y[i] == 1) {
a += 1;
b += 2;
c += 3;
} else if (y[i] == 2) {
a += 2;
b += 3;
c += 1;
} else if (y[i] == 3) {
a += 3;
b += 1;
c += 2;
}
}
if (m == 0) {
cout << a << "\n";
} else if (m == 1) {
cout << b << "\n";
} else {
cout << c << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t;
cin >> t;
for (long long i = 0; i < t; ++i) {
read();
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
for (int z = 0; z < t; ++z) {
long long n;
cin >> n;
long long k = 0;
long long cur = 0;
long long m = (n + 2) / 3;
while (cur + (1ll << (2 * k)) < m) {
cur += (1ll << (2 * k));
k++;
}
m -= cur;
vector<int> b;
m--;
while (m > 0) {
b.push_back(m % 2);
m /= 2;
}
while (b.size() < 2 * k) b.push_back(0);
reverse(b.begin(), b.end());
vector<int> b1(2 * k), b2(2 * k);
for (int i = 0; i < k; ++i) {
if (b[i * 2] == 0 && b[i * 2 + 1] == 0) {
b1[i * 2] = 0;
b1[i * 2 + 1] = 0;
b2[i * 2] = 0;
b2[i * 2 + 1] = 0;
} else if (b[i * 2] == 0 && b[i * 2 + 1] == 1) {
b1[i * 2] = 1;
b1[i * 2 + 1] = 0;
b2[i * 2] = 1;
b2[i * 2 + 1] = 1;
} else if (b[i * 2] == 1 && b[i * 2 + 1] == 0) {
b1[i * 2] = 1;
b1[i * 2 + 1] = 1;
b2[i * 2] = 0;
b2[i * 2 + 1] = 1;
} else if (b[i * 2] == 1 && b[i * 2 + 1] == 1) {
b1[i * 2] = 0;
b1[i * 2 + 1] = 1;
b2[i * 2] = 1;
b2[i * 2 + 1] = 0;
}
}
long long ans;
if (n % 3 == 1) {
ans = 1;
for (int i = 0; i < 2 * k; ++i) {
ans *= 2;
ans += b[i];
}
} else if (n % 3 == 2) {
ans = 2;
for (int i = 0; i < 2 * k; ++i) {
ans *= 2;
ans += b1[i];
}
} else if (n % 3 == 0) {
ans = 3;
for (int i = 0; i < 2 * k; ++i) {
ans *= 2;
ans += b2[i];
}
}
cout << ans << "\n";
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
for (int z = 0; z < t; ++z) {
long long n;
cin >> n;
long long k = 0;
long long cur = 0;
long long m = (n + 2) / 3;
while (cur + (1ll << (2 * k)) < m) {
cur += (1ll << (2 * k));
k++;
}
m -= cur;
vector<int> b;
m--;
while (m > 0) {
b.push_back(m % 2);
m /= 2;
}
while (b.size() < 2 * k) b.push_back(0);
reverse(b.begin(), b.end());
vector<int> b1(2 * k), b2(2 * k);
for (int i = 0; i < k; ++i) {
if (b[i * 2] == 0 && b[i * 2 + 1] == 0) {
b1[i * 2] = 0;
b1[i * 2 + 1] = 0;
b2[i * 2] = 0;
b2[i * 2 + 1] = 0;
} else if (b[i * 2] == 0 && b[i * 2 + 1] == 1) {
b1[i * 2] = 1;
b1[i * 2 + 1] = 0;
b2[i * 2] = 1;
b2[i * 2 + 1] = 1;
} else if (b[i * 2] == 1 && b[i * 2 + 1] == 0) {
b1[i * 2] = 1;
b1[i * 2 + 1] = 1;
b2[i * 2] = 0;
b2[i * 2 + 1] = 1;
} else if (b[i * 2] == 1 && b[i * 2 + 1] == 1) {
b1[i * 2] = 0;
b1[i * 2 + 1] = 1;
b2[i * 2] = 1;
b2[i * 2 + 1] = 0;
}
}
long long ans;
if (n % 3 == 1) {
ans = 1;
for (int i = 0; i < 2 * k; ++i) {
ans *= 2;
ans += b[i];
}
} else if (n % 3 == 2) {
ans = 2;
for (int i = 0; i < 2 * k; ++i) {
ans *= 2;
ans += b1[i];
}
} else if (n % 3 == 0) {
ans = 3;
for (int i = 0; i < 2 * k; ++i) {
ans *= 2;
ans += b2[i];
}
}
cout << ans << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long N;
cin >> N;
N--;
long long g = N / 3;
long long pow = 1;
while (g >= pow) {
g -= pow;
pow <<= 2;
}
long long a = pow + g;
long long trans[4] = {0, 2, 3, 1};
long long b = 0;
for (int i = 0; i < 60; i += 2) {
b += trans[a >> i & 3] * (1LL << i);
}
if (N % 3 == 0) {
cout << a;
} else if (N % 3 == 1) {
cout << b;
} else {
cout << (a ^ b);
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long N;
cin >> N;
N--;
long long g = N / 3;
long long pow = 1;
while (g >= pow) {
g -= pow;
pow <<= 2;
}
long long a = pow + g;
long long trans[4] = {0, 2, 3, 1};
long long b = 0;
for (int i = 0; i < 60; i += 2) {
b += trans[a >> i & 3] * (1LL << i);
}
if (N % 3 == 0) {
cout << a;
} else if (N % 3 == 1) {
cout << b;
} else {
cout << (a ^ b);
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long pow4[31];
long long psa[32];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
pow4[0] = 1;
psa[0] = 0;
psa[1] = 1;
for (int i = 1; i < 31; i++) {
pow4[i] = pow4[i - 1] * 4;
psa[i + 1] = psa[i] + pow4[i];
}
int tt;
cin >> tt;
while (tt--) {
long long n;
cin >> n;
if (n <= 3) {
cout << n << '\n';
continue;
}
n -= 3;
long long m = (n + 2) / 3;
int e = 31;
while (m < psa[e]) {
e--;
}
m -= psa[e];
long long x = pow4[e] + m;
long long y = pow4[e] * 2;
for (int i = 0; i < e; i++) {
int b = (x >> (2 * i)) % 4;
if (b == 1) {
y |= 2LL << (2 * i);
} else if (b == 2) {
y |= 3LL << (2 * i);
} else if (b == 3) {
y |= 1LL << (2 * i);
}
}
if (n % 3 == 1) {
cout << x << '\n';
} else if (n % 3 == 2) {
cout << y << '\n';
} else {
cout << (x ^ y) << '\n';
}
}
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long pow4[31];
long long psa[32];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
pow4[0] = 1;
psa[0] = 0;
psa[1] = 1;
for (int i = 1; i < 31; i++) {
pow4[i] = pow4[i - 1] * 4;
psa[i + 1] = psa[i] + pow4[i];
}
int tt;
cin >> tt;
while (tt--) {
long long n;
cin >> n;
if (n <= 3) {
cout << n << '\n';
continue;
}
n -= 3;
long long m = (n + 2) / 3;
int e = 31;
while (m < psa[e]) {
e--;
}
m -= psa[e];
long long x = pow4[e] + m;
long long y = pow4[e] * 2;
for (int i = 0; i < e; i++) {
int b = (x >> (2 * i)) % 4;
if (b == 1) {
y |= 2LL << (2 * i);
} else if (b == 2) {
y |= 3LL << (2 * i);
} else if (b == 3) {
y |= 1LL << (2 * i);
}
}
if (n % 3 == 1) {
cout << x << '\n';
} else if (n % 3 == 2) {
cout << y << '\n';
} else {
cout << (x ^ y) << '\n';
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long solve(long long val, long long st) {
if (st == 1) return 0;
long long help = st / 4;
if (val < help) return solve(val, st / 4);
if (val < 2 * help) return solve(val - help, st / 4) + 2 * help;
if (val < 3 * help) return solve(val - 2 * help, st / 4) + 3 * help;
return solve(val - 3 * help, st / 4) + help;
}
long long solve_third(long long val, long long st) {
if (st == 1) return 0;
long long help = st / 4;
if (val < help) return solve_third(val, st / 4);
if (val < 2 * help) return solve_third(val - help, st / 4) + 3 * help;
if (val < 3 * help) return solve_third(val - 2 * help, st / 4) + help;
return solve_third(val - 3 * help, st / 4) + 2 * help;
}
long long query(long long n) {
if (n % 3 == 1) {
long long mx = 1;
while (4 * mx <= n) mx = mx * 4;
return mx + (n - mx) / 3;
}
if (n % 3 == 2) {
long long mx = 1;
while (4 * mx <= n) mx = mx * 4;
return 2 * mx + solve((n - mx) / 3, mx);
}
long long mx = 1;
while (4 * mx <= n) mx = mx * 4;
return 3 * mx + solve_third((n - mx) / 3, mx);
}
int main() {
int t;
scanf("%i", &t);
for (int i = 1; i <= t; i++) {
long long n;
scanf("%lld", &n);
printf("%lld\n", query(n));
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long solve(long long val, long long st) {
if (st == 1) return 0;
long long help = st / 4;
if (val < help) return solve(val, st / 4);
if (val < 2 * help) return solve(val - help, st / 4) + 2 * help;
if (val < 3 * help) return solve(val - 2 * help, st / 4) + 3 * help;
return solve(val - 3 * help, st / 4) + help;
}
long long solve_third(long long val, long long st) {
if (st == 1) return 0;
long long help = st / 4;
if (val < help) return solve_third(val, st / 4);
if (val < 2 * help) return solve_third(val - help, st / 4) + 3 * help;
if (val < 3 * help) return solve_third(val - 2 * help, st / 4) + help;
return solve_third(val - 3 * help, st / 4) + 2 * help;
}
long long query(long long n) {
if (n % 3 == 1) {
long long mx = 1;
while (4 * mx <= n) mx = mx * 4;
return mx + (n - mx) / 3;
}
if (n % 3 == 2) {
long long mx = 1;
while (4 * mx <= n) mx = mx * 4;
return 2 * mx + solve((n - mx) / 3, mx);
}
long long mx = 1;
while (4 * mx <= n) mx = mx * 4;
return 3 * mx + solve_third((n - mx) / 3, mx);
}
int main() {
int t;
scanf("%i", &t);
for (int i = 1; i <= t; i++) {
long long n;
scanf("%lld", &n);
printf("%lld\n", query(n));
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int conv[][3] = {{1, 2, 3}, {2, 3, 1}, {3, 1, 2}};
void solve(long long int n) {
int r = n % 3;
if (r != 0) n = (n / 3 + 1) * 3;
if (r == 0)
r = 2;
else
r = r - 1;
int k = 0;
long long int cnt = 3;
while (cnt < n) {
k++;
cnt += 3 * pow(4, k);
}
cnt -= 3 * pow(4, k);
cnt = n - cnt;
int i = 0;
long long int ans = 0;
ans += conv[0][r] << (2 * (k - i));
int j;
for (i = 1; i < k + 1; i++) {
for (j = 0; j < 4; j++) {
cnt -= 3 * pow(4, k - i);
if (cnt <= 0) {
cnt += 3 * pow(4, k - i);
break;
}
}
if (j > 0) ans += conv[j - 1][r] << (2 * (k - i));
}
printf("%lld\n", ans);
}
int main() {
int t;
long long int n;
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
solve(n);
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int conv[][3] = {{1, 2, 3}, {2, 3, 1}, {3, 1, 2}};
void solve(long long int n) {
int r = n % 3;
if (r != 0) n = (n / 3 + 1) * 3;
if (r == 0)
r = 2;
else
r = r - 1;
int k = 0;
long long int cnt = 3;
while (cnt < n) {
k++;
cnt += 3 * pow(4, k);
}
cnt -= 3 * pow(4, k);
cnt = n - cnt;
int i = 0;
long long int ans = 0;
ans += conv[0][r] << (2 * (k - i));
int j;
for (i = 1; i < k + 1; i++) {
for (j = 0; j < 4; j++) {
cnt -= 3 * pow(4, k - i);
if (cnt <= 0) {
cnt += 3 * pow(4, k - i);
break;
}
}
if (j > 0) ans += conv[j - 1][r] << (2 * (k - i));
}
printf("%lld\n", ans);
}
int main() {
int t;
long long int n;
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
solve(n);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int logo(long long int x, long long int b) {
long long int ans = 0;
while (x > 0) {
x /= b;
ans++;
}
return ans;
}
long long int powr(long long int b, long long int x) {
if (x == 0) return 1;
long long int sq = powr(b, x / 2);
if (x & 1) return sq * sq * b;
return sq * sq;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
long long int n;
cin >> n;
long long int gr = (n - 1) / 3;
long long int sp = (n - 1) % 3;
long long int ser = logo(3 * gr + 1, 4) - 1;
long long int disp = gr - (pow(4, ser) - 1) / 3;
long long int a = (1LL << (2 * ser)) + disp;
long long int c = (3LL << (2 * ser));
for (int i = 0; i <= ser; ++i) {
if (disp % 4 == 0) {
} else if (disp % 4 == 1) {
c |= (3LL << (2 * i));
} else if (disp % 4 == 2) {
c |= (1LL << (2 * i));
} else {
c |= (2LL << (2 * i));
}
disp /= 4;
}
long long int b = a ^ c;
if (sp % 3 == 0)
cout << a << endl;
else if (sp % 3 == 1)
cout << b << endl;
else
cout << c << endl;
}
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int logo(long long int x, long long int b) {
long long int ans = 0;
while (x > 0) {
x /= b;
ans++;
}
return ans;
}
long long int powr(long long int b, long long int x) {
if (x == 0) return 1;
long long int sq = powr(b, x / 2);
if (x & 1) return sq * sq * b;
return sq * sq;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
long long int n;
cin >> n;
long long int gr = (n - 1) / 3;
long long int sp = (n - 1) % 3;
long long int ser = logo(3 * gr + 1, 4) - 1;
long long int disp = gr - (pow(4, ser) - 1) / 3;
long long int a = (1LL << (2 * ser)) + disp;
long long int c = (3LL << (2 * ser));
for (int i = 0; i <= ser; ++i) {
if (disp % 4 == 0) {
} else if (disp % 4 == 1) {
c |= (3LL << (2 * i));
} else if (disp % 4 == 2) {
c |= (1LL << (2 * i));
} else {
c |= (2LL << (2 * i));
}
disp /= 4;
}
long long int b = a ^ c;
if (sp % 3 == 0)
cout << a << endl;
else if (sp % 3 == 1)
cout << b << endl;
else
cout << c << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1, c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f == 1 ? x : -x;
}
long long calc(long long L, long long R, long long p) {
if (L + 1 == R) return L;
long long x = (R - L) / 4;
if (p <= x) return calc(L, L + x, p);
if (p <= x * 2) return calc(L + x * 2, L + x * 3, p - x);
if (p <= x * 3) return calc(L + x * 3, R, p - x * 2);
return calc(L + x, L + x * 2, p - x * 3);
}
long long n, L, R, fi, se;
inline void solve() {
n = read();
long long p = (n + 2) / 3;
for (long long i = 0;; i += 2) {
if (p <= (1ll << i)) {
L = 1ll << (i + 1);
R = 3 * (1ll << i);
fi = (1ll << i) + p - 1;
break;
}
p -= 1ll << i;
}
if (n % 3 == 1) {
cout << fi << "\n";
return;
}
se = calc(L, R, p);
if (n % 3 == 2)
cout << se << "\n";
else
cout << (fi ^ se) << "\n";
}
signed main() {
for (long long T = read(); T--;) solve();
return (0 - 0);
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1, c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f == 1 ? x : -x;
}
long long calc(long long L, long long R, long long p) {
if (L + 1 == R) return L;
long long x = (R - L) / 4;
if (p <= x) return calc(L, L + x, p);
if (p <= x * 2) return calc(L + x * 2, L + x * 3, p - x);
if (p <= x * 3) return calc(L + x * 3, R, p - x * 2);
return calc(L + x, L + x * 2, p - x * 3);
}
long long n, L, R, fi, se;
inline void solve() {
n = read();
long long p = (n + 2) / 3;
for (long long i = 0;; i += 2) {
if (p <= (1ll << i)) {
L = 1ll << (i + 1);
R = 3 * (1ll << i);
fi = (1ll << i) + p - 1;
break;
}
p -= 1ll << i;
}
if (n % 3 == 1) {
cout << fi << "\n";
return;
}
se = calc(L, R, p);
if (n % 3 == 2)
cout << se << "\n";
else
cout << (fi ^ se) << "\n";
}
signed main() {
for (long long T = read(); T--;) solve();
return (0 - 0);
}
``` |
#include <bits/stdc++.h>
using namespace std;
int T;
long long n;
long long solve1(long long x) {
long long ans = 0;
if (!x) return 1;
x--;
for (int i = 2;; i += 2) {
if ((1LL << i) > x) {
ans += (1LL << i);
break;
}
x -= (1LL << i);
}
static long long tmp[] = {0, 1, 2, 3};
for (int i = 0; i <= 60; i += 2) ans += tmp[(x >> i) & 3] << i;
return ans;
}
long long solve2(long long x) {
long long ans = 0;
if (!x) return 2;
x--;
for (int i = 2;; i += 2) {
if ((1LL << i) > x) {
ans += (1LL << i) * 2;
break;
}
x -= (1LL << i);
}
static long long tmp[] = {0, 2, 3, 1};
for (int i = 0; i <= 60; i += 2) ans += tmp[(x >> i) & 3] << i;
return ans;
}
long long solve3(long long x) {
long long ans = 0;
if (!x) return 3;
x--;
for (int i = 2;; i += 2) {
if ((1LL << i) > x) {
ans += (1LL << i) * 3;
break;
}
x -= (1LL << i);
}
static long long tmp[] = {0, 3, 1, 2};
for (int i = 0; i <= 60; i += 2) ans += tmp[(x >> i) & 3] << i;
return ans;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%lld", &n);
if (n % 3 == 1)
printf("%lld\n", solve1((n - 1) / 3));
else if (n % 3 == 2)
printf("%lld\n", solve2((n - 1) / 3));
else
printf("%lld\n", solve3((n - 1) / 3));
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int T;
long long n;
long long solve1(long long x) {
long long ans = 0;
if (!x) return 1;
x--;
for (int i = 2;; i += 2) {
if ((1LL << i) > x) {
ans += (1LL << i);
break;
}
x -= (1LL << i);
}
static long long tmp[] = {0, 1, 2, 3};
for (int i = 0; i <= 60; i += 2) ans += tmp[(x >> i) & 3] << i;
return ans;
}
long long solve2(long long x) {
long long ans = 0;
if (!x) return 2;
x--;
for (int i = 2;; i += 2) {
if ((1LL << i) > x) {
ans += (1LL << i) * 2;
break;
}
x -= (1LL << i);
}
static long long tmp[] = {0, 2, 3, 1};
for (int i = 0; i <= 60; i += 2) ans += tmp[(x >> i) & 3] << i;
return ans;
}
long long solve3(long long x) {
long long ans = 0;
if (!x) return 3;
x--;
for (int i = 2;; i += 2) {
if ((1LL << i) > x) {
ans += (1LL << i) * 3;
break;
}
x -= (1LL << i);
}
static long long tmp[] = {0, 3, 1, 2};
for (int i = 0; i <= 60; i += 2) ans += tmp[(x >> i) & 3] << i;
return ans;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%lld", &n);
if (n % 3 == 1)
printf("%lld\n", solve1((n - 1) / 3));
else if (n % 3 == 2)
printf("%lld\n", solve2((n - 1) / 3));
else
printf("%lld\n", solve3((n - 1) / 3));
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long get1(long long u) {
vector<int> res;
while (u) {
res.push_back(u % 2);
u /= 2;
}
res.push_back(0);
for (int i = 0; i < res.size(); i += 2) {
if (res[i] == 1 && res[i + 1] == 0) {
swap(res[i], res[i + 1]);
} else if (res[i] == 0 && res[i + 1] == 1) {
res[i] = res[i + 1] = 1;
} else if (res[i] == 1 && res[i + 1] == 1) {
res[i] = 1;
res[i + 1] = 0;
}
}
reverse(res.begin(), res.end());
long long ans = 0;
for (int u : res) {
ans = ans * 2ll + u;
}
return ans;
}
long long get2(long long u) {
vector<int> res;
while (u) {
res.push_back(u % 2);
u /= 2;
}
res.push_back(0);
for (int i = 0; i < res.size(); i += 2) {
if (res[i] == 1 && res[i + 1] == 0) {
res[i] = res[i + 1] = 1;
} else if (res[i] == 0 && res[i + 1] == 1) {
res[i] = 1;
res[i + 1] = 0;
} else if (res[i] == 1 && res[i + 1] == 1) {
res[i] = 0;
res[i + 1] = 1;
}
}
reverse(res.begin(), res.end());
long long ans = 0;
for (int u : res) {
ans = ans * 2ll + u;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tc;
cin >> tc;
while (tc--) {
long long n;
cin >> n;
n--;
long long id = n % 3;
n /= 3ll;
for (int i = 0; i <= 60; i += 2) {
if (n >= (1ll << i)) {
n -= (1ll << i);
} else {
n += (1ll << i);
break;
}
}
if (id == 0)
cout << n << '\n';
else if (id == 1)
cout << get1(n) << '\n';
else
cout << get2(n) << '\n';
}
return 0;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long get1(long long u) {
vector<int> res;
while (u) {
res.push_back(u % 2);
u /= 2;
}
res.push_back(0);
for (int i = 0; i < res.size(); i += 2) {
if (res[i] == 1 && res[i + 1] == 0) {
swap(res[i], res[i + 1]);
} else if (res[i] == 0 && res[i + 1] == 1) {
res[i] = res[i + 1] = 1;
} else if (res[i] == 1 && res[i + 1] == 1) {
res[i] = 1;
res[i + 1] = 0;
}
}
reverse(res.begin(), res.end());
long long ans = 0;
for (int u : res) {
ans = ans * 2ll + u;
}
return ans;
}
long long get2(long long u) {
vector<int> res;
while (u) {
res.push_back(u % 2);
u /= 2;
}
res.push_back(0);
for (int i = 0; i < res.size(); i += 2) {
if (res[i] == 1 && res[i + 1] == 0) {
res[i] = res[i + 1] = 1;
} else if (res[i] == 0 && res[i + 1] == 1) {
res[i] = 1;
res[i + 1] = 0;
} else if (res[i] == 1 && res[i + 1] == 1) {
res[i] = 0;
res[i + 1] = 1;
}
}
reverse(res.begin(), res.end());
long long ans = 0;
for (int u : res) {
ans = ans * 2ll + u;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tc;
cin >> tc;
while (tc--) {
long long n;
cin >> n;
n--;
long long id = n % 3;
n /= 3ll;
for (int i = 0; i <= 60; i += 2) {
if (n >= (1ll << i)) {
n -= (1ll << i);
} else {
n += (1ll << i);
break;
}
}
if (id == 0)
cout << n << '\n';
else if (id == 1)
cout << get1(n) << '\n';
else
cout << get2(n) << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, INF = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
cin >> t;
for (long long test = 1; test <= t; test++) {
long long n;
cin >> n;
if (n < 4) {
cout << n << "\n";
continue;
}
long long ans = 3, c1 = 0;
while (ans < n) {
ans <<= 2;
c1 += 2;
ans |= 3;
}
ans >>= 2;
ans++;
n -= ans;
long long temp = n / 3;
ans += temp;
n %= 3;
long long c2 = (3LL << c1);
temp = 0;
while (c2) {
long long c = ans & c2;
c >>= c1;
if (c == 0) {
temp <<= 2;
} else {
c--;
c = (c + n) % 3 + 1;
temp <<= 2;
temp |= c;
}
c1 -= 2;
c2 >>= 2;
}
cout << temp << "\n";
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, INF = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
cin >> t;
for (long long test = 1; test <= t; test++) {
long long n;
cin >> n;
if (n < 4) {
cout << n << "\n";
continue;
}
long long ans = 3, c1 = 0;
while (ans < n) {
ans <<= 2;
c1 += 2;
ans |= 3;
}
ans >>= 2;
ans++;
n -= ans;
long long temp = n / 3;
ans += temp;
n %= 3;
long long c2 = (3LL << c1);
temp = 0;
while (c2) {
long long c = ans & c2;
c >>= c1;
if (c == 0) {
temp <<= 2;
} else {
c--;
c = (c + n) % 3 + 1;
temp <<= 2;
temp |= c;
}
c1 -= 2;
c2 >>= 2;
}
cout << temp << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (auto i = begin(v); i != end(v); i++)
os << *i << (i == end(v) - 1 ? "" : " ");
return os;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
for (auto i = begin(v); i != end(v); i++) is >> *i;
return is;
}
long long recur(pair<long long, long long> a, pair<long long, long long> b,
pair<long long, long long> c, long long nth) {
if (a.second == a.first) {
if (nth == 0) return a.first;
if (nth == 1) return b.first;
return c.first;
}
long long step = (a.second - a.first + 1LL) / 4LL;
if (nth < step * 3LL) {
return recur({a.first, a.first + step - 1LL},
{b.first, b.first + step - 1LL},
{c.first, c.first + step - 1LL}, nth);
} else if (nth < step * 6LL) {
return recur({a.first + step, a.first + 2LL * step - 1LL},
{b.first + step * 2LL, b.first + 3LL * step - 1LL},
{c.first + step * 3LL, c.first + 4LL * step - 1LL},
nth - step * 3LL);
} else if (nth < step * 9LL) {
return recur({a.first + step * 2LL, a.first + 3LL * step - 1LL},
{b.first + 3LL * step, b.first + 4LL * step - 1LL},
{c.first + step, c.first + 2LL * step - 1LL},
nth - step * 6LL);
} else {
return recur({a.first + step * 3LL, a.first + 4LL * step - 1LL},
{b.first + step, b.first + 2LL * step - 1LL},
{c.first + step * 2LL, c.first + 3LL * step - 1LL},
nth - step * 9LL);
}
}
long long find_nth(long long n) {
for (long long i = 0; i < 58; i += 2) {
long long start = 1LL << i;
long long end = start * 4LL - 1;
if (n >= start && n <= end) {
return recur({start, start * 2LL - 1LL}, {start * 2LL, start * 3LL - 1LL},
{start * 3LL, end}, n - start);
}
}
throw 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
cout << find_nth(x) << "\n";
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (auto i = begin(v); i != end(v); i++)
os << *i << (i == end(v) - 1 ? "" : " ");
return os;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
for (auto i = begin(v); i != end(v); i++) is >> *i;
return is;
}
long long recur(pair<long long, long long> a, pair<long long, long long> b,
pair<long long, long long> c, long long nth) {
if (a.second == a.first) {
if (nth == 0) return a.first;
if (nth == 1) return b.first;
return c.first;
}
long long step = (a.second - a.first + 1LL) / 4LL;
if (nth < step * 3LL) {
return recur({a.first, a.first + step - 1LL},
{b.first, b.first + step - 1LL},
{c.first, c.first + step - 1LL}, nth);
} else if (nth < step * 6LL) {
return recur({a.first + step, a.first + 2LL * step - 1LL},
{b.first + step * 2LL, b.first + 3LL * step - 1LL},
{c.first + step * 3LL, c.first + 4LL * step - 1LL},
nth - step * 3LL);
} else if (nth < step * 9LL) {
return recur({a.first + step * 2LL, a.first + 3LL * step - 1LL},
{b.first + 3LL * step, b.first + 4LL * step - 1LL},
{c.first + step, c.first + 2LL * step - 1LL},
nth - step * 6LL);
} else {
return recur({a.first + step * 3LL, a.first + 4LL * step - 1LL},
{b.first + step, b.first + 2LL * step - 1LL},
{c.first + step * 2LL, c.first + 3LL * step - 1LL},
nth - step * 9LL);
}
}
long long find_nth(long long n) {
for (long long i = 0; i < 58; i += 2) {
long long start = 1LL << i;
long long end = start * 4LL - 1;
if (n >= start && n <= end) {
return recur({start, start * 2LL - 1LL}, {start * 2LL, start * 3LL - 1LL},
{start * 3LL, end}, n - start);
}
}
throw 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
cout << find_nth(x) << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long d1[] = {0, 2, 3, 1};
long long fn1(long long start, long long pos, long long len) {
long long zzz = len / 4;
if (len == 1) return start;
for (long long i = 0; i < 4; i++) {
long long l = i * zzz;
long long r = l + (zzz - 1);
if (l <= pos && pos <= r) return fn1(start + d1[i] * zzz, pos - l, zzz);
}
return -1;
}
long long d2[] = {0, 3, 1, 2};
long long fn2(long long start, long long pos, long long len) {
long long zzz = len / 4;
if (len == 1) return start;
for (long long i = 0; i < 4; i++) {
long long l = i * zzz;
long long r = l + (zzz - 1);
if (l <= pos && pos <= r) return fn2(start + d2[i] * zzz, pos - l, zzz);
}
return -1;
}
long long solve() {
long long n;
cin >> n;
n--;
long long row = n / 3;
long long col = n % 3;
if (col == 0) {
long long pos = row;
long long temp = 1;
long long start = 1;
while (true) {
if (pos - temp >= 0) {
pos -= temp;
temp *= 4;
start *= 4;
continue;
}
break;
}
cout << start + pos << "\n";
}
if (col == 1) {
long long pos = row;
long long temp = 1;
long long start = 2;
while (true) {
if (pos - temp >= 0) {
pos -= temp;
temp *= 4;
start *= 4;
continue;
}
break;
}
cout << fn1(start, pos, temp) << "\n";
}
if (col == 2) {
long long pos = row;
long long temp = 1;
long long start = 3;
while (true) {
if (pos - temp >= 0) {
pos -= temp;
temp *= 4;
start *= 4;
continue;
}
break;
}
cout << fn2(start, pos, temp) << "\n";
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
long long t = 1;
cin >> t;
while (t-- != 0) {
long long stat = solve();
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long d1[] = {0, 2, 3, 1};
long long fn1(long long start, long long pos, long long len) {
long long zzz = len / 4;
if (len == 1) return start;
for (long long i = 0; i < 4; i++) {
long long l = i * zzz;
long long r = l + (zzz - 1);
if (l <= pos && pos <= r) return fn1(start + d1[i] * zzz, pos - l, zzz);
}
return -1;
}
long long d2[] = {0, 3, 1, 2};
long long fn2(long long start, long long pos, long long len) {
long long zzz = len / 4;
if (len == 1) return start;
for (long long i = 0; i < 4; i++) {
long long l = i * zzz;
long long r = l + (zzz - 1);
if (l <= pos && pos <= r) return fn2(start + d2[i] * zzz, pos - l, zzz);
}
return -1;
}
long long solve() {
long long n;
cin >> n;
n--;
long long row = n / 3;
long long col = n % 3;
if (col == 0) {
long long pos = row;
long long temp = 1;
long long start = 1;
while (true) {
if (pos - temp >= 0) {
pos -= temp;
temp *= 4;
start *= 4;
continue;
}
break;
}
cout << start + pos << "\n";
}
if (col == 1) {
long long pos = row;
long long temp = 1;
long long start = 2;
while (true) {
if (pos - temp >= 0) {
pos -= temp;
temp *= 4;
start *= 4;
continue;
}
break;
}
cout << fn1(start, pos, temp) << "\n";
}
if (col == 2) {
long long pos = row;
long long temp = 1;
long long start = 3;
while (true) {
if (pos - temp >= 0) {
pos -= temp;
temp *= 4;
start *= 4;
continue;
}
break;
}
cout << fn2(start, pos, temp) << "\n";
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
long long t = 1;
cin >> t;
while (t-- != 0) {
long long stat = solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n;
void solve() {
cin >> n;
long long a = (n - 1) / 3, b = n % 3, cnt;
for (cnt = 0; a >= (1LL << 2 * cnt); ++cnt) a -= (1LL << 2 * cnt);
a += (1LL << 2 * cnt);
long long ans[3] = {0, a, 0};
for (int i = 0; a; a >>= 2, ++i) {
if ((a & 3) == 1)
ans[0] += 3LL << 2 * i, ans[2] += 2LL << 2 * i;
else if ((a & 3) == 2)
ans[0] += 1LL << 2 * i, ans[2] += 3LL << 2 * i;
else if ((a & 3) == 3)
ans[0] += 2LL << 2 * i, ans[2] += 1LL << 2 * i;
}
cout << ans[b] << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) solve();
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n;
void solve() {
cin >> n;
long long a = (n - 1) / 3, b = n % 3, cnt;
for (cnt = 0; a >= (1LL << 2 * cnt); ++cnt) a -= (1LL << 2 * cnt);
a += (1LL << 2 * cnt);
long long ans[3] = {0, a, 0};
for (int i = 0; a; a >>= 2, ++i) {
if ((a & 3) == 1)
ans[0] += 3LL << 2 * i, ans[2] += 2LL << 2 * i;
else if ((a & 3) == 2)
ans[0] += 1LL << 2 * i, ans[2] += 3LL << 2 * i;
else if ((a & 3) == 3)
ans[0] += 2LL << 2 * i, ans[2] += 1LL << 2 * i;
}
cout << ans[b] << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) solve();
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (long long i = 0; i < static_cast<long long>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
const long long INF = LONG_LONG_MAX / 10;
const double PI = 3.141592653589793238;
const long long MOD = 998244353;
bool many = true;
struct Solve {
string endl = "\n";
pair<long long, long long> Get(long long bit, long long idx) {
if (bit == 0) {
return pair<long long, long long>{1, 2};
}
long long some = 1ll << (bit - 2);
auto q = Get(bit - 2, idx % some);
q.first ^= (1ll << (bit - 2));
q.second ^= (1ll << (bit - 1));
long long cs = idx / some;
if (cs == 1) {
q.first ^= (1ll << (bit - 2));
q.second ^= (1ll << (bit - 1));
}
if (cs == 2) {
q.first ^= (1ll << (bit - 1));
q.second ^= (1ll << (bit - 1)) ^ (1ll << (bit - 2));
}
if (cs == 3) {
q.first ^= (1ll << (bit - 1)) ^ (1ll << (bit - 2));
q.second ^= (1ll << (bit - 2));
}
q.first ^= 1ll << bit;
q.second ^= 1ll << (bit + 1);
return q;
}
Solve() {
long long n;
cin >> n;
long long idx = (n - 1) / 3;
long long pos = (n - 1) % 3;
for (long long bit = 0;; bit += 2) {
long long here = 1ll << bit;
if (idx < here) {
auto p = Get(bit, idx);
cout << (pos == 0 ? p.first
: (pos == 1 ? p.second : (p.first ^ p.second)))
<< endl;
break;
}
idx -= here;
}
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long CASES = 1;
if (many) cin >> CASES;
for (long long i = 1; i <= CASES; i++) {
Solve me;
}
}
| ### Prompt
Create a solution in CPP for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (long long i = 0; i < static_cast<long long>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
const long long INF = LONG_LONG_MAX / 10;
const double PI = 3.141592653589793238;
const long long MOD = 998244353;
bool many = true;
struct Solve {
string endl = "\n";
pair<long long, long long> Get(long long bit, long long idx) {
if (bit == 0) {
return pair<long long, long long>{1, 2};
}
long long some = 1ll << (bit - 2);
auto q = Get(bit - 2, idx % some);
q.first ^= (1ll << (bit - 2));
q.second ^= (1ll << (bit - 1));
long long cs = idx / some;
if (cs == 1) {
q.first ^= (1ll << (bit - 2));
q.second ^= (1ll << (bit - 1));
}
if (cs == 2) {
q.first ^= (1ll << (bit - 1));
q.second ^= (1ll << (bit - 1)) ^ (1ll << (bit - 2));
}
if (cs == 3) {
q.first ^= (1ll << (bit - 1)) ^ (1ll << (bit - 2));
q.second ^= (1ll << (bit - 2));
}
q.first ^= 1ll << bit;
q.second ^= 1ll << (bit + 1);
return q;
}
Solve() {
long long n;
cin >> n;
long long idx = (n - 1) / 3;
long long pos = (n - 1) % 3;
for (long long bit = 0;; bit += 2) {
long long here = 1ll << bit;
if (idx < here) {
auto p = Get(bit, idx);
cout << (pos == 0 ? p.first
: (pos == 1 ? p.second : (p.first ^ p.second)))
<< endl;
break;
}
idx -= here;
}
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long CASES = 1;
if (many) cin >> CASES;
for (long long i = 1; i <= CASES; i++) {
Solve me;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int conv[][3] = {{1, 2, 3}, {2, 3, 1}, {3, 1, 2}};
void solve(long long int n) {
int r = n % 3;
if (r != 0) n = (n / 3 + 1) * 3;
if (r == 0)
r = 2;
else
r = r - 1;
int k = 0;
long long int cnt = 3;
while (cnt < n) {
k++;
cnt += 3 * pow(4, k);
}
cnt -= 3 * pow(4, k);
cnt = n - cnt;
vector<int> v(k + 1);
v[0] = 1;
int j;
for (int i = 1; i < k + 1; i++) {
for (j = 0; j < 4; j++) {
cnt -= 3 * pow(4, k - i);
if (cnt <= 0) {
cnt += 3 * pow(4, k - i);
break;
}
}
v[i] = j;
}
long long int ans = 0;
for (int i = 0; i < k + 1; i++) {
if (v[i] > 0) {
ans += conv[v[i] - 1][r] << (2 * (k - i));
}
}
printf("%lld\n", ans);
}
int main() {
int t;
long long int n;
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
solve(n);
}
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int conv[][3] = {{1, 2, 3}, {2, 3, 1}, {3, 1, 2}};
void solve(long long int n) {
int r = n % 3;
if (r != 0) n = (n / 3 + 1) * 3;
if (r == 0)
r = 2;
else
r = r - 1;
int k = 0;
long long int cnt = 3;
while (cnt < n) {
k++;
cnt += 3 * pow(4, k);
}
cnt -= 3 * pow(4, k);
cnt = n - cnt;
vector<int> v(k + 1);
v[0] = 1;
int j;
for (int i = 1; i < k + 1; i++) {
for (j = 0; j < 4; j++) {
cnt -= 3 * pow(4, k - i);
if (cnt <= 0) {
cnt += 3 * pow(4, k - i);
break;
}
}
v[i] = j;
}
long long int ans = 0;
for (int i = 0; i < k + 1; i++) {
if (v[i] > 0) {
ans += conv[v[i] - 1][r] << (2 * (k - i));
}
}
printf("%lld\n", ans);
}
int main() {
int t;
long long int n;
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
solve(n);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
const int ff[4] = {0, 2, 3, 1};
int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
long long n;
scanf("%lld", &n);
long long g = (n - 1) / 3 + 1;
int id = (n - 1) % 3 + 1;
int B = 0;
long long N = 1;
while (g > N) {
g -= N;
N *= 4;
B++;
}
g--;
long long x = N + g;
long long y = 1ll << (B * 2 + 1);
for (int i = (0); i <= (B - 1); i++) {
long long t = (x >> (i * 2)) & 3;
long long tt = ff[t];
y += tt << (i * 2);
}
long long z = x ^ y;
if (id == 1) printf("%lld\n", x);
if (id == 2) printf("%lld\n", y);
if (id == 3) printf("%lld\n", z);
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
const int ff[4] = {0, 2, 3, 1};
int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
long long n;
scanf("%lld", &n);
long long g = (n - 1) / 3 + 1;
int id = (n - 1) % 3 + 1;
int B = 0;
long long N = 1;
while (g > N) {
g -= N;
N *= 4;
B++;
}
g--;
long long x = N + g;
long long y = 1ll << (B * 2 + 1);
for (int i = (0); i <= (B - 1); i++) {
long long t = (x >> (i * 2)) & 3;
long long tt = ff[t];
y += tt << (i * 2);
}
long long z = x ^ y;
if (id == 1) printf("%lld\n", x);
if (id == 2) printf("%lld\n", y);
if (id == 3) printf("%lld\n", z);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long pot[30];
int main(void) {
int t;
scanf("%d", &t);
pot[0] = 1;
for (int i = 1; i <= 27; i++) pot[i] = pot[i - 1] * 4LL;
while (t--) {
long long n;
scanf("%lld", &n);
int p;
long long a, b;
for (int i = 0; i <= 27; i++) {
if (pot[i] - 1 >= n) {
p = i - 1;
a = pot[i - 1], b = 2LL * pot[i - 1];
break;
}
}
long long grupo = (n - a);
a += grupo / 3LL;
p *= 2;
for (int i = 0; i < p; i += 2) {
long long b0, b1;
b0 = (a & (1LL << i));
b1 = (a & (1LL << (i + 1)));
if (b0 && !b1)
b += (1LL << (i + 1));
else if (!b0 && b1)
b += (1LL << i), b += (1LL << (i + 1));
else if (b0 && b1)
b += (1LL << i);
}
if (grupo % 3 == 0)
printf("%lld\n", a);
else if (grupo % 3 == 1)
printf("%lld\n", b);
else
printf("%lld\n", a ^ b);
}
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long pot[30];
int main(void) {
int t;
scanf("%d", &t);
pot[0] = 1;
for (int i = 1; i <= 27; i++) pot[i] = pot[i - 1] * 4LL;
while (t--) {
long long n;
scanf("%lld", &n);
int p;
long long a, b;
for (int i = 0; i <= 27; i++) {
if (pot[i] - 1 >= n) {
p = i - 1;
a = pot[i - 1], b = 2LL * pot[i - 1];
break;
}
}
long long grupo = (n - a);
a += grupo / 3LL;
p *= 2;
for (int i = 0; i < p; i += 2) {
long long b0, b1;
b0 = (a & (1LL << i));
b1 = (a & (1LL << (i + 1)));
if (b0 && !b1)
b += (1LL << (i + 1));
else if (!b0 && b1)
b += (1LL << i), b += (1LL << (i + 1));
else if (b0 && b1)
b += (1LL << i);
}
if (grupo % 3 == 0)
printf("%lld\n", a);
else if (grupo % 3 == 1)
printf("%lld\n", b);
else
printf("%lld\n", a ^ b);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <class K>
inline bool cmax(K& a, const K& b) {
return (a < b) ? a = b, 1 : 0;
}
template <class K>
inline bool cmin(K& a, const K& b) {
return (a > b) ? a = b, 1 : 0;
}
inline long long read() {
register long long s = 0;
register char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) s = (s << 1) + (s << 3) + c - '0', c = getchar();
return s;
}
int T, c;
long long n, f[31], A, B, m, x, base, res;
int main() {
register int i;
T = read(), f[0] = 1;
for (i = 1; i < 30; i++) f[i] = f[i - 1] * 4;
while (T--) {
n = read();
c = lower_bound(f, f + 30, n + 1) - f - 1;
n -= f[c] - 1, m = (n - 1) / 3, A = f[c] + m, base = 1;
B = 0, res = A;
while (A) {
switch (A % 4) {
case 1:
B += 2 * base;
break;
case 2:
B += 3 * base;
break;
case 3:
B += base;
break;
}
base = base * 4, A /= 4;
}
A = res;
if (n % 3 == 1)
printf("%lld\n", A);
else if (n % 3 == 2)
printf("%lld\n", B);
else
printf("%lld\n", A ^ B);
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class K>
inline bool cmax(K& a, const K& b) {
return (a < b) ? a = b, 1 : 0;
}
template <class K>
inline bool cmin(K& a, const K& b) {
return (a > b) ? a = b, 1 : 0;
}
inline long long read() {
register long long s = 0;
register char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) s = (s << 1) + (s << 3) + c - '0', c = getchar();
return s;
}
int T, c;
long long n, f[31], A, B, m, x, base, res;
int main() {
register int i;
T = read(), f[0] = 1;
for (i = 1; i < 30; i++) f[i] = f[i - 1] * 4;
while (T--) {
n = read();
c = lower_bound(f, f + 30, n + 1) - f - 1;
n -= f[c] - 1, m = (n - 1) / 3, A = f[c] + m, base = 1;
B = 0, res = A;
while (A) {
switch (A % 4) {
case 1:
B += 2 * base;
break;
case 2:
B += 3 * base;
break;
case 3:
B += base;
break;
}
base = base * 4, A /= 4;
}
A = res;
if (n % 3 == 1)
printf("%lld\n", A);
else if (n % 3 == 2)
printf("%lld\n", B);
else
printf("%lld\n", A ^ B);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1010;
int used[maxN];
void brut() {
vector<int> res;
int idx = 0;
while (idx < 100) {
++idx;
int found = false;
for (int i = 1; i < maxN && !found; ++i) {
for (int j = i; j < maxN && !found; ++j) {
for (int k = j; k < maxN && !found; ++k) {
if (!used[i] && !used[j] && !used[k]) {
if ((i ^ j ^ k) == 0) {
res.push_back(i);
res.push_back(j);
res.push_back(k);
used[i] = used[j] = used[k] = 1;
cout << i << " " << j << " " << k << endl;
found = true;
}
}
}
}
}
}
}
long long f[16] = {0, 1, 2, 3, 4, 8, 12, 5, 10, 15, 6, 11, 13, 7, 9, 14};
vector<long long> genrow(long long bucket, long long row) {
if (bucket == 1) {
return {1, 2, 3};
}
long long sz = bucket / 4;
auto res = genrow(bucket / 4, row % sz);
long long order[4] = {0, 2, 3, 1};
long long a = bucket + row;
long long b = 2 * bucket + (res[1] - 2 * sz) + order[row / sz] * sz;
return {a, b, a ^ b};
}
vector<long long> genrow(long long row) {
if (row == 1) {
return {1, 2, 3};
}
long long total = 1;
long long cur_size = 1;
while (total < row) {
cur_size *= 4;
total += cur_size;
}
long long pre_size = total - cur_size;
return genrow(cur_size, row - pre_size - 1);
}
void solve() {
long long n;
cin >> n;
long long row = (n + 2) / 3;
auto res = genrow(row);
if (n % 3 == 1) {
cout << res[0] << endl;
} else if (n % 3 == 2) {
cout << res[1] << endl;
} else {
cout << res[2] << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
for (int tt = 0; tt < t; ++tt) {
solve();
}
return 0;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1010;
int used[maxN];
void brut() {
vector<int> res;
int idx = 0;
while (idx < 100) {
++idx;
int found = false;
for (int i = 1; i < maxN && !found; ++i) {
for (int j = i; j < maxN && !found; ++j) {
for (int k = j; k < maxN && !found; ++k) {
if (!used[i] && !used[j] && !used[k]) {
if ((i ^ j ^ k) == 0) {
res.push_back(i);
res.push_back(j);
res.push_back(k);
used[i] = used[j] = used[k] = 1;
cout << i << " " << j << " " << k << endl;
found = true;
}
}
}
}
}
}
}
long long f[16] = {0, 1, 2, 3, 4, 8, 12, 5, 10, 15, 6, 11, 13, 7, 9, 14};
vector<long long> genrow(long long bucket, long long row) {
if (bucket == 1) {
return {1, 2, 3};
}
long long sz = bucket / 4;
auto res = genrow(bucket / 4, row % sz);
long long order[4] = {0, 2, 3, 1};
long long a = bucket + row;
long long b = 2 * bucket + (res[1] - 2 * sz) + order[row / sz] * sz;
return {a, b, a ^ b};
}
vector<long long> genrow(long long row) {
if (row == 1) {
return {1, 2, 3};
}
long long total = 1;
long long cur_size = 1;
while (total < row) {
cur_size *= 4;
total += cur_size;
}
long long pre_size = total - cur_size;
return genrow(cur_size, row - pre_size - 1);
}
void solve() {
long long n;
cin >> n;
long long row = (n + 2) / 3;
auto res = genrow(row);
if (n % 3 == 1) {
cout << res[0] << endl;
} else if (n % 3 == 2) {
cout << res[1] << endl;
} else {
cout << res[2] << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
for (int tt = 0; tt < t; ++tt) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n;
vector<long long> b = {0, 2, 3, 1};
pair<long long, long long> dfs(long long v) {
if (v == 0) return {4, 8};
if (v == 1) return {5, 10};
if (v == 2) return {6, 11};
if (v == 3) return {7, 9};
pair<long long, long long> p = dfs(v / 4 - 1);
p.first <<= 2LL;
p.second <<= 2LL;
p.first |= v % 4;
p.second |= b[v % 4];
return p;
}
void solve() {
if (n < 3) {
cout << n + 1 << '\n';
return;
}
long long g = n / 3 - 1;
pair<long long, long long> p = dfs(g);
vector<long long> ans = {p.first, p.second, (p.first ^ p.second)};
cout << ans[n % 3] << '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t;
cin >> t;
for (; t--;) {
cin >> n;
--n;
solve();
}
}
| ### Prompt
Construct a cpp code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n;
vector<long long> b = {0, 2, 3, 1};
pair<long long, long long> dfs(long long v) {
if (v == 0) return {4, 8};
if (v == 1) return {5, 10};
if (v == 2) return {6, 11};
if (v == 3) return {7, 9};
pair<long long, long long> p = dfs(v / 4 - 1);
p.first <<= 2LL;
p.second <<= 2LL;
p.first |= v % 4;
p.second |= b[v % 4];
return p;
}
void solve() {
if (n < 3) {
cout << n + 1 << '\n';
return;
}
long long g = n / 3 - 1;
pair<long long, long long> p = dfs(g);
vector<long long> ans = {p.first, p.second, (p.first ^ p.second)};
cout << ans[n % 3] << '\n';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t;
cin >> t;
for (; t--;) {
cin >> n;
--n;
solve();
}
}
``` |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt")
using namespace std;
const long long MOD = 1000000007;
const long long MOD2 = 1000000009;
const long long BASE = 257;
const long long INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const long long MX = 100010;
const double EPS = 1e-9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
for (long long i = 0; i < t; i++) {
long long n;
cin >> n;
long long gr = (n - 1) / 3;
long long ind = (n - 1) % 3;
long long cur = 0;
long long add = 0;
while (cur <= gr - (1ll << add)) {
cur += (1ll << add);
add += 2;
}
long long fs = gr - cur + (1ll << add);
if (ind == 0) {
cout << fs << '\n';
} else {
long long cnt = gr - cur;
long long se = 1ll << (add + 1);
long long curpw = 0;
while (cnt) {
long long md = cnt % 4;
long long td = 0;
if (md == 1)
td = 2;
else if (md == 2)
td = 3;
else if (md == 3)
td = 1;
se += td << curpw;
curpw += 2;
cnt /= 4;
}
if (ind == 1) {
cout << se << '\n';
} else {
cout << (se ^ fs) << '\n';
}
}
}
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt")
using namespace std;
const long long MOD = 1000000007;
const long long MOD2 = 1000000009;
const long long BASE = 257;
const long long INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const long long MX = 100010;
const double EPS = 1e-9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
for (long long i = 0; i < t; i++) {
long long n;
cin >> n;
long long gr = (n - 1) / 3;
long long ind = (n - 1) % 3;
long long cur = 0;
long long add = 0;
while (cur <= gr - (1ll << add)) {
cur += (1ll << add);
add += 2;
}
long long fs = gr - cur + (1ll << add);
if (ind == 0) {
cout << fs << '\n';
} else {
long long cnt = gr - cur;
long long se = 1ll << (add + 1);
long long curpw = 0;
while (cnt) {
long long md = cnt % 4;
long long td = 0;
if (md == 1)
td = 2;
else if (md == 2)
td = 3;
else if (md == 3)
td = 1;
se += td << curpw;
curpw += 2;
cnt /= 4;
}
if (ind == 1) {
cout << se << '\n';
} else {
cout << (se ^ fs) << '\n';
}
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 201001;
long long int get(long long int len, long long int pos) {
if (len == 1) return 1;
if (pos < len / 4) {
return get(len / 4, pos);
}
if (pos >= (3 * len / 4)) {
return (len / 4) + get(len / 4, pos - (3 * len / 4));
}
if (pos >= len / 4 && pos < len / 2) {
return len / 2 + get(len / 4, pos - len / 4);
}
if (pos >= len / 2 && pos < (3 * len / 4)) {
return 3 * (len / 4) + get(len / 4, pos - len / 2);
}
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int cn = (n - 1) / 3;
cn++;
long long int f1 = 0, s1 = 0;
long long int tek = 1;
long long int tekp = 1;
long long int num = 1;
long long int ans = 0;
while (num <= cn) {
if (num + tekp > cn) {
ans = tek + (cn - num);
break;
}
num += tekp;
tekp *= 4;
tek = tekp;
}
f1 = ans;
if (n % 3 == 1) {
cout << ans << "\n";
continue;
}
tek = 2;
tekp = 1;
num = 1;
ans = 0;
while (num <= cn) {
if (num + tekp > cn) {
ans = tek + get(tekp, cn - num);
break;
}
num += tekp;
tekp *= 4;
tek *= 4;
}
s1 = ans - 1;
if (n % 3 == 2) {
cout << ans - 1 << "\n";
continue;
}
if (n % 3 == 0) {
cout << (f1 ^ s1) << "\n";
}
}
}
| ### Prompt
Please create a solution in Cpp to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 201001;
long long int get(long long int len, long long int pos) {
if (len == 1) return 1;
if (pos < len / 4) {
return get(len / 4, pos);
}
if (pos >= (3 * len / 4)) {
return (len / 4) + get(len / 4, pos - (3 * len / 4));
}
if (pos >= len / 4 && pos < len / 2) {
return len / 2 + get(len / 4, pos - len / 4);
}
if (pos >= len / 2 && pos < (3 * len / 4)) {
return 3 * (len / 4) + get(len / 4, pos - len / 2);
}
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int cn = (n - 1) / 3;
cn++;
long long int f1 = 0, s1 = 0;
long long int tek = 1;
long long int tekp = 1;
long long int num = 1;
long long int ans = 0;
while (num <= cn) {
if (num + tekp > cn) {
ans = tek + (cn - num);
break;
}
num += tekp;
tekp *= 4;
tek = tekp;
}
f1 = ans;
if (n % 3 == 1) {
cout << ans << "\n";
continue;
}
tek = 2;
tekp = 1;
num = 1;
ans = 0;
while (num <= cn) {
if (num + tekp > cn) {
ans = tek + get(tekp, cn - num);
break;
}
num += tekp;
tekp *= 4;
tek *= 4;
}
s1 = ans - 1;
if (n % 3 == 2) {
cout << ans - 1 << "\n";
continue;
}
if (n % 3 == 0) {
cout << (f1 ^ s1) << "\n";
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL mat[3][4] = {
{0, 1, 2, 3},
{0, 2, 3, 1},
{0, 3, 1, 2},
};
LL recalc(LL n, int typ) {
LL ans = 0;
for (int i = 0; i <= 31; i++) {
if ((n & (1ll << i + i)) || (n & (1ll << i + i + 1))) {
ans |= (mat[typ][(n >> i + i) & 3] << i + i);
}
}
return ans;
}
LL calc(LL n, int typ) {
LL m = 1;
int len = 2;
while (n > m) {
n -= m;
len += 2;
m <<= 2;
}
LL ans = recalc(n - 1, typ);
if (typ != 1) {
ans |= 1ll << len - 2;
}
if (typ != 0) {
ans |= 1ll << len - 1;
}
return ans;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
LL n;
scanf("%I64d", &n);
LL f = (n + 2) / 3;
int typ = (n - 1) % 3;
printf("%I64d\n", calc(f, typ));
}
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL mat[3][4] = {
{0, 1, 2, 3},
{0, 2, 3, 1},
{0, 3, 1, 2},
};
LL recalc(LL n, int typ) {
LL ans = 0;
for (int i = 0; i <= 31; i++) {
if ((n & (1ll << i + i)) || (n & (1ll << i + i + 1))) {
ans |= (mat[typ][(n >> i + i) & 3] << i + i);
}
}
return ans;
}
LL calc(LL n, int typ) {
LL m = 1;
int len = 2;
while (n > m) {
n -= m;
len += 2;
m <<= 2;
}
LL ans = recalc(n - 1, typ);
if (typ != 1) {
ans |= 1ll << len - 2;
}
if (typ != 0) {
ans |= 1ll << len - 1;
}
return ans;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
LL n;
scanf("%I64d", &n);
LL f = (n + 2) / 3;
int typ = (n - 1) % 3;
printf("%I64d\n", calc(f, typ));
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int v = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = v * 10 + c - '0';
c = getchar();
}
return v * f;
}
int H;
void mian(int _ = -1) {
long long n;
scanf("%lld", &n);
if (n <= 3) {
printf("%lld\n", n);
return;
}
long long pos = (n - 1) / 3 + 1;
H = n - pos * 3 + 3;
int w = 0;
while (1) {
if (pos <= (1ll << w)) {
break;
}
pos -= 1ll << w;
w += 2;
}
long long st = 1ll << w;
if (H == 1) {
printf("%lld\n", st + pos - 1);
return;
}
long long A = 0;
pos--;
for (int i = w - 2; i >= 0; i -= 2) {
long long CW = 1ll << i;
long long NW = 1ll << (i + 2);
long long tmp = pos % NW / CW;
if (tmp == 0)
;
else if (tmp == 1)
A |= 2ll << i;
else if (tmp == 2)
A |= 3ll << i;
else
A |= 1ll << i;
}
A += 2ll << w;
if (H == 2) {
printf("%lld\n", A);
return;
}
if (H == 3) {
printf("%lld\n", A ^ (st + pos));
return;
}
}
int main() {
int t;
scanf("%d", &t);
for (int i = 1; i <= t; i++) {
mian();
}
}
| ### Prompt
Please create a solution in CPP to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int v = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = v * 10 + c - '0';
c = getchar();
}
return v * f;
}
int H;
void mian(int _ = -1) {
long long n;
scanf("%lld", &n);
if (n <= 3) {
printf("%lld\n", n);
return;
}
long long pos = (n - 1) / 3 + 1;
H = n - pos * 3 + 3;
int w = 0;
while (1) {
if (pos <= (1ll << w)) {
break;
}
pos -= 1ll << w;
w += 2;
}
long long st = 1ll << w;
if (H == 1) {
printf("%lld\n", st + pos - 1);
return;
}
long long A = 0;
pos--;
for (int i = w - 2; i >= 0; i -= 2) {
long long CW = 1ll << i;
long long NW = 1ll << (i + 2);
long long tmp = pos % NW / CW;
if (tmp == 0)
;
else if (tmp == 1)
A |= 2ll << i;
else if (tmp == 2)
A |= 3ll << i;
else
A |= 1ll << i;
}
A += 2ll << w;
if (H == 2) {
printf("%lld\n", A);
return;
}
if (H == 3) {
printf("%lld\n", A ^ (st + pos));
return;
}
}
int main() {
int t;
scanf("%d", &t);
for (int i = 1; i <= t; i++) {
mian();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long next_val[] = {0, 2, 3, 1};
vector<long long> v;
long long find_se(long long base_value, long long pos, long long width) {
if (width == 0) {
return base_value;
}
return find_se(base_value + width * next_val[pos / width], pos % width,
width / 4);
}
int main() {
long long t;
long long n;
cin >> t;
while (t--) {
cin >> n;
long long base = (n - 1) / 3;
long long type = (n - 1) % 3;
long long i = 0;
long long cummulative = 0;
while (base >= (1ll << (2 * i)) + cummulative) {
cummulative += 1ll << (2 * i);
i++;
}
long long relative_base = base - cummulative;
long long base_value = 1ll << (2 * i);
long long fe = relative_base + base_value;
long long se = 0;
if (type == 0) {
cout << fe;
} else {
se = find_se(base_value << 1ll, relative_base, base_value / 4);
if (type == 1) {
cout << se;
} else {
cout << (fe ^ se);
}
}
cout << "\n";
}
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long next_val[] = {0, 2, 3, 1};
vector<long long> v;
long long find_se(long long base_value, long long pos, long long width) {
if (width == 0) {
return base_value;
}
return find_se(base_value + width * next_val[pos / width], pos % width,
width / 4);
}
int main() {
long long t;
long long n;
cin >> t;
while (t--) {
cin >> n;
long long base = (n - 1) / 3;
long long type = (n - 1) % 3;
long long i = 0;
long long cummulative = 0;
while (base >= (1ll << (2 * i)) + cummulative) {
cummulative += 1ll << (2 * i);
i++;
}
long long relative_base = base - cummulative;
long long base_value = 1ll << (2 * i);
long long fe = relative_base + base_value;
long long se = 0;
if (type == 0) {
cout << fe;
} else {
se = find_se(base_value << 1ll, relative_base, base_value / 4);
if (type == 1) {
cout << se;
} else {
cout << (fe ^ se);
}
}
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
int bit = -1;
for (int i = 0; i < 30; i++) {
long long cnt = 3ll * (1ll << (i + i));
if (cnt < n) {
n -= cnt;
} else {
bit = i;
break;
}
}
assert(~bit);
long long base = (1ll << (bit + bit));
long long ans[] = {base, base * 2, base * 3};
int pat[] = {1, 2, 3};
for (int i = bit - 1; i >= 0; i--) {
for (int j = 0; j < 4; j++) {
long long cnt = (3ll << (i + i));
if (cnt < n) {
n -= cnt;
} else {
if (j == 0) {
break;
} else {
int k = j - 1;
int c = 0;
do {
ans[c] += (1ll * pat[k] * (1ll << (i + i)));
k++;
c++;
k %= 3;
} while (c < 3);
}
break;
}
}
}
cout << ans[n - 1] << "\n";
}
}
| ### Prompt
Create a solution in CPP for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
int bit = -1;
for (int i = 0; i < 30; i++) {
long long cnt = 3ll * (1ll << (i + i));
if (cnt < n) {
n -= cnt;
} else {
bit = i;
break;
}
}
assert(~bit);
long long base = (1ll << (bit + bit));
long long ans[] = {base, base * 2, base * 3};
int pat[] = {1, 2, 3};
for (int i = bit - 1; i >= 0; i--) {
for (int j = 0; j < 4; j++) {
long long cnt = (3ll << (i + i));
if (cnt < n) {
n -= cnt;
} else {
if (j == 0) {
break;
} else {
int k = j - 1;
int c = 0;
do {
ans[c] += (1ll * pat[k] * (1ll << (i + i)));
k++;
c++;
k %= 3;
} while (c < 3);
}
break;
}
}
}
cout << ans[n - 1] << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
const int N = 3e3 + 10;
int flag[N];
int a[] = {
0, 1, 2, 3, 4, 8, 12, 5, 10, 15, 6, 11, 13, 7, 9, 14,
};
int offset[][3] = {
16, 32, 48, 17, 34, 51, 18, 35, 49, 19, 33, 50, 20, 40, 60, 21,
42, 63, 22, 43, 61, 23, 41, 62, 24, 44, 52, 25, 46, 55, 26, 47,
53, 27, 45, 54, 28, 36, 56, 29, 38, 59, 30, 39, 57, 31, 37, 58,
};
void output(int b) {
string s;
while (b) {
s.push_back(b % 2 + '0');
b /= 2;
}
reverse(s.begin(), s.end());
printf(s.c_str());
putchar(' ');
}
long long check(long long cnt) {
int d[4] = {0, 3, 1, 2};
long long res = 0;
long long o = 1;
while (cnt) {
res += d[cnt % 4] * o;
o *= 4;
cnt /= 4;
}
return res;
}
int main() {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 3; j++) {
offset[i][j] -= 16 * (j + 1);
}
}
int ncase;
scanf("%d", &ncase);
while (ncase--) {
long long n;
scanf("%lld", &n);
if (n <= 15)
printf("%d\n", a[n]);
else {
n -= 16;
long long cur = 16;
int cnt = 2;
while (n >= cur * 3) {
cnt++;
n -= cur * 3;
cur *= 4;
}
long long key = cur;
int oo = n % 48;
long long ans1 = cur + n / 3;
long long ans3 = check(n / 3) + cur * 3;
long long ans2 = ans1 ^ ans3;
long long ans[] = {ans1, ans2, ans3};
printf("%lld\n", ans[n % 3]);
}
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
const int N = 3e3 + 10;
int flag[N];
int a[] = {
0, 1, 2, 3, 4, 8, 12, 5, 10, 15, 6, 11, 13, 7, 9, 14,
};
int offset[][3] = {
16, 32, 48, 17, 34, 51, 18, 35, 49, 19, 33, 50, 20, 40, 60, 21,
42, 63, 22, 43, 61, 23, 41, 62, 24, 44, 52, 25, 46, 55, 26, 47,
53, 27, 45, 54, 28, 36, 56, 29, 38, 59, 30, 39, 57, 31, 37, 58,
};
void output(int b) {
string s;
while (b) {
s.push_back(b % 2 + '0');
b /= 2;
}
reverse(s.begin(), s.end());
printf(s.c_str());
putchar(' ');
}
long long check(long long cnt) {
int d[4] = {0, 3, 1, 2};
long long res = 0;
long long o = 1;
while (cnt) {
res += d[cnt % 4] * o;
o *= 4;
cnt /= 4;
}
return res;
}
int main() {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 3; j++) {
offset[i][j] -= 16 * (j + 1);
}
}
int ncase;
scanf("%d", &ncase);
while (ncase--) {
long long n;
scanf("%lld", &n);
if (n <= 15)
printf("%d\n", a[n]);
else {
n -= 16;
long long cur = 16;
int cnt = 2;
while (n >= cur * 3) {
cnt++;
n -= cur * 3;
cur *= 4;
}
long long key = cur;
int oo = n % 48;
long long ans1 = cur + n / 3;
long long ans3 = check(n / 3) + cur * 3;
long long ans2 = ans1 ^ ans3;
long long ans[] = {ans1, ans2, ans3};
printf("%lld\n", ans[n % 3]);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
cout << fixed << setprecision(9), 0);
Int fast(Int N) {
Int ans = 0, t;
for (t = 0; t < 32; t++)
if (N <= (1LL << (2 + 2 * t)) - 1) break;
const int A[3] = {3, 1, 2};
N -= (1LL << (2 * t));
Int a = N % 3;
ans += (a + 1) << (2 * t);
Int d = 1;
N /= 3;
for (int i = 0; i < t; i++) {
Int b = N % 4;
if (b != 0) {
ans += A[(a + b) % 3] * d;
}
N /= 4;
d *= 4;
}
return ans;
}
int main() {
int T;
cin >> T;
while (T--) {
Int N;
cin >> N;
cout << fast(N) << '\n';
}
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
cout << fixed << setprecision(9), 0);
Int fast(Int N) {
Int ans = 0, t;
for (t = 0; t < 32; t++)
if (N <= (1LL << (2 + 2 * t)) - 1) break;
const int A[3] = {3, 1, 2};
N -= (1LL << (2 * t));
Int a = N % 3;
ans += (a + 1) << (2 * t);
Int d = 1;
N /= 3;
for (int i = 0; i < t; i++) {
Int b = N % 4;
if (b != 0) {
ans += A[(a + b) % 3] * d;
}
N /= 4;
d *= 4;
}
return ans;
}
int main() {
int T;
cin >> T;
while (T--) {
Int N;
cin >> N;
cout << fast(N) << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const vector<int> mods = {998244353, 1000000007, 1000000009};
const long long inf = 3e18;
const double pi = acos(-1.0);
const double eps = 0.0000001;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class... K>
using umap = unordered_map<K...>;
template <class... K>
using uset = unordered_set<K...>;
long long t, n;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.precision(13);
cin >> t;
for (long long _t = (0); _t < (t); ++_t) {
cin >> n;
n--;
long long c = n / 3;
long long s = 0;
long long p4 = 1;
for (long long i = (0); i < (inf); ++i) {
if (s + p4 > c) {
c -= s;
long long l = p4 + c;
if (n % 3 == 0) {
cout << l << "\n";
} else {
long long up = n % 3 == 2;
long long a = (p4 << 1);
if (up) a ^= p4;
long long cur = 1;
while (cur < p4) {
if (c % 4 == 1) {
a ^= (cur << 1);
if (up) a ^= cur;
}
if (c % 4 == 2) {
a ^= cur;
if (!up) a ^= (cur << 1);
}
if (c % 4 == 3) {
if (up) a ^= cur << 1;
if (!up) a ^= cur;
}
c >>= 2;
cur <<= 2;
}
cout << a << "\n";
}
goto outer;
}
s += p4;
p4 *= 4;
}
outer:;
}
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const vector<int> mods = {998244353, 1000000007, 1000000009};
const long long inf = 3e18;
const double pi = acos(-1.0);
const double eps = 0.0000001;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class... K>
using umap = unordered_map<K...>;
template <class... K>
using uset = unordered_set<K...>;
long long t, n;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.precision(13);
cin >> t;
for (long long _t = (0); _t < (t); ++_t) {
cin >> n;
n--;
long long c = n / 3;
long long s = 0;
long long p4 = 1;
for (long long i = (0); i < (inf); ++i) {
if (s + p4 > c) {
c -= s;
long long l = p4 + c;
if (n % 3 == 0) {
cout << l << "\n";
} else {
long long up = n % 3 == 2;
long long a = (p4 << 1);
if (up) a ^= p4;
long long cur = 1;
while (cur < p4) {
if (c % 4 == 1) {
a ^= (cur << 1);
if (up) a ^= cur;
}
if (c % 4 == 2) {
a ^= cur;
if (!up) a ^= (cur << 1);
}
if (c % 4 == 3) {
if (up) a ^= cur << 1;
if (!up) a ^= cur;
}
c >>= 2;
cur <<= 2;
}
cout << a << "\n";
}
goto outer;
}
s += p4;
p4 *= 4;
}
outer:;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
class InputReader {
public:
InputReader() {}
InputReader(const char *file_name) {
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
inline InputReader &operator>>(int &n) {
while (buffer[cursor] < '0' || buffer[cursor] > '9') {
advance();
}
n = 0;
while ('0' <= buffer[cursor] && buffer[cursor] <= '9') {
n = n * 10 + buffer[cursor] - '0';
advance();
}
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor;
char buffer[SIZE];
inline void advance() {
++cursor;
if (cursor == SIZE) {
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
};
const int MAXVAL = 5000;
int v[1 + 3 * MAXVAL];
void binary(int x, int bits) {
for (int i = 0; i < bits; i++)
if (x & (1 << i))
cout << "1";
else
cout << "0";
cout << " ";
}
void output(int a, int b, int c, int bits) {
cout << a << " " << b << " " << c << " ";
binary(a, bits);
binary(b, bits);
binary(c, bits);
cout << "\n";
}
long long get(long long n) {
int r = n % 3;
if (n == 1) return 1;
if (n == 2) return 2;
if (n == 3) return 3;
int power = 1;
while ((1LL << (2 * power)) - 1 < n) power++;
n = (n - (1LL << (2 * (power - 1)))) / 3;
long long a = 1LL << (2 * power - 2), b = 1LL << (2 * power - 1);
for (int i = 0; i < power - 1; i++) {
if (((n >> (2 * i)) & 3) == 0) {
}
if (((n >> (2 * i)) & 3) == 1) {
a |= 1LL << (2 * i);
b |= 1LL << (2 * i + 1);
}
if (((n >> (2 * i)) & 3) == 2) {
a |= 1LL << (2 * i + 1);
b |= 1LL << (2 * i);
b |= 1LL << (2 * i + 1);
}
if (((n >> (2 * i)) & 3) == 3) {
a |= 1LL << (2 * i);
a |= 1LL << (2 * i + 1);
b |= 1LL << (2 * i);
}
}
if (r == 1) return a;
if (r == 2) return b;
if (r == 0) return a ^ b;
}
int main() {
int tests;
cin >> tests;
for (int test = 1; test <= tests; test++) {
long long n;
cin >> n;
cout << get(n) << "\n";
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
class InputReader {
public:
InputReader() {}
InputReader(const char *file_name) {
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
inline InputReader &operator>>(int &n) {
while (buffer[cursor] < '0' || buffer[cursor] > '9') {
advance();
}
n = 0;
while ('0' <= buffer[cursor] && buffer[cursor] <= '9') {
n = n * 10 + buffer[cursor] - '0';
advance();
}
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor;
char buffer[SIZE];
inline void advance() {
++cursor;
if (cursor == SIZE) {
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
};
const int MAXVAL = 5000;
int v[1 + 3 * MAXVAL];
void binary(int x, int bits) {
for (int i = 0; i < bits; i++)
if (x & (1 << i))
cout << "1";
else
cout << "0";
cout << " ";
}
void output(int a, int b, int c, int bits) {
cout << a << " " << b << " " << c << " ";
binary(a, bits);
binary(b, bits);
binary(c, bits);
cout << "\n";
}
long long get(long long n) {
int r = n % 3;
if (n == 1) return 1;
if (n == 2) return 2;
if (n == 3) return 3;
int power = 1;
while ((1LL << (2 * power)) - 1 < n) power++;
n = (n - (1LL << (2 * (power - 1)))) / 3;
long long a = 1LL << (2 * power - 2), b = 1LL << (2 * power - 1);
for (int i = 0; i < power - 1; i++) {
if (((n >> (2 * i)) & 3) == 0) {
}
if (((n >> (2 * i)) & 3) == 1) {
a |= 1LL << (2 * i);
b |= 1LL << (2 * i + 1);
}
if (((n >> (2 * i)) & 3) == 2) {
a |= 1LL << (2 * i + 1);
b |= 1LL << (2 * i);
b |= 1LL << (2 * i + 1);
}
if (((n >> (2 * i)) & 3) == 3) {
a |= 1LL << (2 * i);
a |= 1LL << (2 * i + 1);
b |= 1LL << (2 * i);
}
}
if (r == 1) return a;
if (r == 2) return b;
if (r == 0) return a ^ b;
}
int main() {
int tests;
cin >> tests;
for (int test = 1; test <= tests; test++) {
long long n;
cin >> n;
cout << get(n) << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int T;
long long n;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%lld", &n);
long long p = (n - 1) / 3, s = 1, all = 0;
while (p >= s) p -= s, s <<= 2;
if (n % 3 == 1)
printf("%lld\n", s + p);
else if (n % 3 == 2) {
long long ans = s << 1, c = 1;
while (p) {
if (p % 4 == 1)
ans += c * 2;
else if (p % 4 == 2)
ans += c * 3;
else if (p % 4 == 3)
ans += c;
p >>= 2, c <<= 2;
}
printf("%lld\n", ans);
} else {
long long ans = s * 3, c = 1;
while (p) {
if (p % 4 == 1)
ans += c * 3;
else if (p % 4 == 2)
ans += c * 1;
else if (p % 4 == 3)
ans += c * 2;
p >>= 2, c <<= 2;
}
printf("%lld\n", ans);
}
}
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int T;
long long n;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%lld", &n);
long long p = (n - 1) / 3, s = 1, all = 0;
while (p >= s) p -= s, s <<= 2;
if (n % 3 == 1)
printf("%lld\n", s + p);
else if (n % 3 == 2) {
long long ans = s << 1, c = 1;
while (p) {
if (p % 4 == 1)
ans += c * 2;
else if (p % 4 == 2)
ans += c * 3;
else if (p % 4 == 3)
ans += c;
p >>= 2, c <<= 2;
}
printf("%lld\n", ans);
} else {
long long ans = s * 3, c = 1;
while (p) {
if (p % 4 == 1)
ans += c * 3;
else if (p % 4 == 2)
ans += c * 1;
else if (p % 4 == 3)
ans += c * 2;
p >>= 2, c <<= 2;
}
printf("%lld\n", ans);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
vector<long long> csize;
const int transmute[4] = {0, 2, 3, 1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
long long n;
long long x = 1;
long long acc = 0;
for (int i = 0; i < 29; i++) {
csize.push_back(acc + x);
acc += x;
x *= 4;
}
cin >> t;
while (t--) {
cin >> n;
if (n <= 3) {
cout << n << endl;
continue;
}
long long pnum = (n - 1) / 3 + 1;
long long pindex = n - (pnum - 1) * 3;
long long cluster = 0;
for (int i = 0; i < csize.size(); i++) {
if (csize[i] >= pnum) {
cluster = i;
break;
}
}
long long cindex = (pnum - csize[cluster - 1] - 1);
long long a = (((long long)1) << (2 * cluster)) + cindex;
vector<long long> t4;
while (cindex) {
t4.push_back(cindex % 4);
cindex /= 4;
}
long long b = (((long long)1) << (2 * cluster + 1));
long long mult = 1;
for (int i = 0; i < t4.size(); i++) {
t4[i] = transmute[t4[i]];
b += t4[i] * mult;
mult *= 4;
}
long long c = a ^ b;
if (pindex == 1) {
cout << a << endl;
} else if (pindex == 2) {
cout << b << endl;
} else if (pindex == 3) {
cout << c << endl;
}
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long> csize;
const int transmute[4] = {0, 2, 3, 1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
long long n;
long long x = 1;
long long acc = 0;
for (int i = 0; i < 29; i++) {
csize.push_back(acc + x);
acc += x;
x *= 4;
}
cin >> t;
while (t--) {
cin >> n;
if (n <= 3) {
cout << n << endl;
continue;
}
long long pnum = (n - 1) / 3 + 1;
long long pindex = n - (pnum - 1) * 3;
long long cluster = 0;
for (int i = 0; i < csize.size(); i++) {
if (csize[i] >= pnum) {
cluster = i;
break;
}
}
long long cindex = (pnum - csize[cluster - 1] - 1);
long long a = (((long long)1) << (2 * cluster)) + cindex;
vector<long long> t4;
while (cindex) {
t4.push_back(cindex % 4);
cindex /= 4;
}
long long b = (((long long)1) << (2 * cluster + 1));
long long mult = 1;
for (int i = 0; i < t4.size(); i++) {
t4[i] = transmute[t4[i]];
b += t4[i] * mult;
mult *= 4;
}
long long c = a ^ b;
if (pindex == 1) {
cout << a << endl;
} else if (pindex == 2) {
cout << b << endl;
} else if (pindex == 3) {
cout << c << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long a[100010];
long long pow(long long x, long long y, long long k) {
long long res = 1;
while (y) {
if (y & 1) res *= x;
res = res % k;
if (x > 1e9) break;
x *= x;
x = x % k;
y = y >> 1;
}
return res;
}
long long c[200];
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long n;
scanf("%lld", &n);
long long o = 0, res = 1, ans = 0;
long long now = 0;
while (((pow(4, o + 1, 1e18) - 1) / 3) * 3 < n) {
o++;
}
o--;
long long m;
m = n - ((pow(4, o + 1, 1e18) - 1) / 3) * 3;
long long p = (m - 1) / 3, pmod = m % 3;
if (pmod == 0) pmod = 3;
ans += (pmod << 2 * (o + 1));
long long flag = p % 4;
o = 0;
while (p > 0) {
c[o] = p % 4;
o++;
p = p / 4;
}
for (long long i = 0; i < o; i++) {
if (c[i] != 0) {
c[i] = (long long)(c[i] + pmod - 1) % 3;
if (c[i] == 0) c[i] = 3;
}
ans += ((long long)c[i] << (long long)(2 * i));
}
printf("%lld\n", ans);
}
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long a[100010];
long long pow(long long x, long long y, long long k) {
long long res = 1;
while (y) {
if (y & 1) res *= x;
res = res % k;
if (x > 1e9) break;
x *= x;
x = x % k;
y = y >> 1;
}
return res;
}
long long c[200];
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long n;
scanf("%lld", &n);
long long o = 0, res = 1, ans = 0;
long long now = 0;
while (((pow(4, o + 1, 1e18) - 1) / 3) * 3 < n) {
o++;
}
o--;
long long m;
m = n - ((pow(4, o + 1, 1e18) - 1) / 3) * 3;
long long p = (m - 1) / 3, pmod = m % 3;
if (pmod == 0) pmod = 3;
ans += (pmod << 2 * (o + 1));
long long flag = p % 4;
o = 0;
while (p > 0) {
c[o] = p % 4;
o++;
p = p / 4;
}
for (long long i = 0; i < o; i++) {
if (c[i] != 0) {
c[i] = (long long)(c[i] + pmod - 1) % 3;
if (c[i] == 0) c[i] = 3;
}
ans += ((long long)c[i] << (long long)(2 * i));
}
printf("%lld\n", ans);
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long f = 1, ans = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
ans = ans * 10 + c - '0';
c = getchar();
}
return f * ans;
}
pair<long long, long long> F(long long p) {
long long cnt = 0;
for (long long i = 0;; i += 2) {
cnt += (1ll << i);
if (cnt >= p) {
return make_pair(i, (1ll << i) + p - (cnt - (1ll << i) + 1));
}
}
}
long long T(long long p) {
long long cnt = 0;
for (long long i = 0;; i += 2) {
cnt += (1ll << i);
if (cnt >= p) {
return p - (cnt - (1ll << i));
}
}
}
long long Get1(long long p) {
pair<long long, long long> G = F(p);
return G.second;
}
long long Solve(long long x, long long y) {
if (!x) return 2;
long long len = 1ll << (x - 2);
if (y <= len)
return Solve(x - 2, y) + (1ll << (x + 1)) - (1ll << (x - 1));
else if (y <= 2 * len)
return Solve(x - 2, y - len) + (1ll << (x + 1)) - (1ll << (x - 1)) +
(1ll << (x - 1));
else if (y <= 3 * len)
return Solve(x - 2, y - 2 * len) + (1ll << (x + 1)) - (1ll << (x - 1)) +
(1ll << (x - 2)) * 3;
else if (y <= 4 * len)
return Solve(x - 2, y - 3 * len) + (1ll << (x + 1)) - (1ll << (x - 1)) +
(1ll << (x - 2));
return 0;
}
long long Get2(long long p) {
pair<long long, long long> G = F(p);
return Solve(G.first, T(p));
}
signed main() {
long long cas = read();
while (cas--) {
long long x = read();
if (x % 3 == 1) {
printf("%lld\n", Get1((x + 2) / 3));
continue;
} else if (x % 3 == 2) {
printf("%lld\n", Get2((x + 1) / 3));
} else {
printf("%lld\n", Get1(x / 3) ^ Get2(x / 3));
}
}
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long f = 1, ans = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
ans = ans * 10 + c - '0';
c = getchar();
}
return f * ans;
}
pair<long long, long long> F(long long p) {
long long cnt = 0;
for (long long i = 0;; i += 2) {
cnt += (1ll << i);
if (cnt >= p) {
return make_pair(i, (1ll << i) + p - (cnt - (1ll << i) + 1));
}
}
}
long long T(long long p) {
long long cnt = 0;
for (long long i = 0;; i += 2) {
cnt += (1ll << i);
if (cnt >= p) {
return p - (cnt - (1ll << i));
}
}
}
long long Get1(long long p) {
pair<long long, long long> G = F(p);
return G.second;
}
long long Solve(long long x, long long y) {
if (!x) return 2;
long long len = 1ll << (x - 2);
if (y <= len)
return Solve(x - 2, y) + (1ll << (x + 1)) - (1ll << (x - 1));
else if (y <= 2 * len)
return Solve(x - 2, y - len) + (1ll << (x + 1)) - (1ll << (x - 1)) +
(1ll << (x - 1));
else if (y <= 3 * len)
return Solve(x - 2, y - 2 * len) + (1ll << (x + 1)) - (1ll << (x - 1)) +
(1ll << (x - 2)) * 3;
else if (y <= 4 * len)
return Solve(x - 2, y - 3 * len) + (1ll << (x + 1)) - (1ll << (x - 1)) +
(1ll << (x - 2));
return 0;
}
long long Get2(long long p) {
pair<long long, long long> G = F(p);
return Solve(G.first, T(p));
}
signed main() {
long long cas = read();
while (cas--) {
long long x = read();
if (x % 3 == 1) {
printf("%lld\n", Get1((x + 2) / 3));
continue;
} else if (x % 3 == 2) {
printf("%lld\n", Get2((x + 1) / 3));
} else {
printf("%lld\n", Get1(x / 3) ^ Get2(x / 3));
}
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char c = getchar();
long long x = 0;
bool f = 0;
for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
return x;
}
long long get(long long x, long long y, long long o) {
if (x == 1) return 1;
long long t = x / 4;
if (o == 2) {
if (y <= t) return get(t, y, o);
if (y <= 2 * t) return get(t, y - t, o) + 2 * t;
if (y <= 3 * t) return get(t, y - t * 2, o) + 3 * t;
return get(t, y - 3 * t, o) + t;
} else {
if (y <= t) return get(t, y, o);
if (y <= 2 * t) return get(t, y - t, o) + 3 * t;
if (y <= 3 * t) return get(t, y - t * 2, o) + t;
return get(t, y - t * 3, o) + t * 2;
}
}
inline long long work() {
long long n = read(), now = 0, c = 0, op = n % 3;
n = (n - 1) / 3 + 1;
while (now + (1ll << c) < n) now += (1ll << c), c += 2;
long long res = now * 3;
if (op == 1) return res + n - now;
if (op == 2) return res + (1ll << c) + get(1LL << c, n - now, 2);
return res + (1ll << c) * 2 + get(1LL << c, n - now, 3);
}
signed main() {
long long T = read();
while (T--) printf("%lld\n", work());
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char c = getchar();
long long x = 0;
bool f = 0;
for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
return x;
}
long long get(long long x, long long y, long long o) {
if (x == 1) return 1;
long long t = x / 4;
if (o == 2) {
if (y <= t) return get(t, y, o);
if (y <= 2 * t) return get(t, y - t, o) + 2 * t;
if (y <= 3 * t) return get(t, y - t * 2, o) + 3 * t;
return get(t, y - 3 * t, o) + t;
} else {
if (y <= t) return get(t, y, o);
if (y <= 2 * t) return get(t, y - t, o) + 3 * t;
if (y <= 3 * t) return get(t, y - t * 2, o) + t;
return get(t, y - t * 3, o) + t * 2;
}
}
inline long long work() {
long long n = read(), now = 0, c = 0, op = n % 3;
n = (n - 1) / 3 + 1;
while (now + (1ll << c) < n) now += (1ll << c), c += 2;
long long res = now * 3;
if (op == 1) return res + n - now;
if (op == 2) return res + (1ll << c) + get(1LL << c, n - now, 2);
return res + (1ll << c) * 2 + get(1LL << c, n - now, 3);
}
signed main() {
long long T = read();
while (T--) printf("%lld\n", work());
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const double TL = 1.5 * CLOCKS_PER_SEC;
mt19937 rng(
(unsigned int)chrono::steady_clock::now().time_since_epoch().count());
vector<long long> get_row(long long r) {
long long p = 1, s = 0, l = 0;
for (;; p *= 4) {
if (s + p >= r) {
break;
}
l += 2;
s += p;
}
long long d = r - s - 1;
long long a = d + p;
long long b = 2LL << l;
for (int i = 0; i < l; i += 2) {
long long x = (d % (1LL << (i + 2))) / (1LL << i);
long long y = (d % (1LL << (i + 2))) / (1LL << i);
if (x == 2 || x == 3) {
b |= (1LL << i);
}
if (y == 1 || y == 2) {
b |= (1LL << (i + 1));
}
}
long long c = a ^ b;
return {a, b, c};
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long n;
cin >> n;
n -= 1;
long long x = n / 3 + 1, y = n % 3;
vector<long long> ans = get_row(x);
cout << ans[y] << endl;
}
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double TL = 1.5 * CLOCKS_PER_SEC;
mt19937 rng(
(unsigned int)chrono::steady_clock::now().time_since_epoch().count());
vector<long long> get_row(long long r) {
long long p = 1, s = 0, l = 0;
for (;; p *= 4) {
if (s + p >= r) {
break;
}
l += 2;
s += p;
}
long long d = r - s - 1;
long long a = d + p;
long long b = 2LL << l;
for (int i = 0; i < l; i += 2) {
long long x = (d % (1LL << (i + 2))) / (1LL << i);
long long y = (d % (1LL << (i + 2))) / (1LL << i);
if (x == 2 || x == 3) {
b |= (1LL << i);
}
if (y == 1 || y == 2) {
b |= (1LL << (i + 1));
}
}
long long c = a ^ b;
return {a, b, c};
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
long long n;
cin >> n;
n -= 1;
long long x = n / 3 + 1, y = n % 3;
vector<long long> ans = get_row(x);
cout << ans[y] << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
long long period[3][4] = {{0, 3, 1, 2}, {}, {0, 2, 3, 1}};
while (t--) {
long long n;
scanf("%lld", &n);
int p2 = 0;
long long t = 1;
while ((t << 2) <= n) t <<= 2, p2 += 2;
long long row = (n - t) / 3;
if (n % 3 == 1)
printf("%lld\n", t + row);
else {
long long ans;
if (n % 3 == 2)
ans = t << 1;
else
ans = t << 1 | t;
while (p2 > 0) {
p2 -= 2;
ans |= period[n % 3][(row >> p2) & 3] << p2;
}
printf("%lld\n", ans);
}
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
long long period[3][4] = {{0, 3, 1, 2}, {}, {0, 2, 3, 1}};
while (t--) {
long long n;
scanf("%lld", &n);
int p2 = 0;
long long t = 1;
while ((t << 2) <= n) t <<= 2, p2 += 2;
long long row = (n - t) / 3;
if (n % 3 == 1)
printf("%lld\n", t + row);
else {
long long ans;
if (n % 3 == 2)
ans = t << 1;
else
ans = t << 1 | t;
while (p2 > 0) {
p2 -= 2;
ans |= period[n % 3][(row >> p2) & 3] << p2;
}
printf("%lld\n", ans);
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int t;
long long n, ans[5];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
if (n <= 3) {
cout << n << "\n\n";
continue;
}
long long res = n / 3;
if (n % 3 == 0) res--;
long long now = 1, base = 1, cnt = 0;
while (now <= res) {
cnt++;
base *= 4;
now += base;
}
now -= base;
res -= now;
now = 1;
ans[1] = ans[2] = ans[3] = 0;
for (int i = 1; i <= cnt; i++) {
long long j = res % 4;
if (j == 1) {
ans[1] += now * 1;
ans[2] += now * 2;
ans[3] += now * 3;
}
if (j == 2) {
ans[1] += now * 2;
ans[2] += now * 3;
ans[3] += now * 1;
}
if (j == 3) {
ans[1] += now * 3;
ans[2] += now * 1;
ans[3] += now * 2;
}
now *= 4;
res /= 4;
}
long long hah;
if (n % 3 == 0) {
hah = 3;
} else {
hah = n % 3;
}
cout << hah * now + ans[hah] << "\n";
cout << "\n";
}
}
| ### Prompt
Generate a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int t;
long long n, ans[5];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
if (n <= 3) {
cout << n << "\n\n";
continue;
}
long long res = n / 3;
if (n % 3 == 0) res--;
long long now = 1, base = 1, cnt = 0;
while (now <= res) {
cnt++;
base *= 4;
now += base;
}
now -= base;
res -= now;
now = 1;
ans[1] = ans[2] = ans[3] = 0;
for (int i = 1; i <= cnt; i++) {
long long j = res % 4;
if (j == 1) {
ans[1] += now * 1;
ans[2] += now * 2;
ans[3] += now * 3;
}
if (j == 2) {
ans[1] += now * 2;
ans[2] += now * 3;
ans[3] += now * 1;
}
if (j == 3) {
ans[1] += now * 3;
ans[2] += now * 1;
ans[3] += now * 2;
}
now *= 4;
res /= 4;
}
long long hah;
if (n % 3 == 0) {
hah = 3;
} else {
hah = n % 3;
}
cout << hah * now + ans[hah] << "\n";
cout << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int test;
long long n;
long long cal(long long a) {
long long ans = 0;
for (int i = 0; i < 61; i += 2) {
long long cyc = 1LL << i;
if (a < cyc - 1) {
continue;
}
vector<long long> d = {1, 2, 3};
long long x = a - cyc + 1;
if (x < cyc * 3) {
ans |= d[x % 3] << i;
continue;
}
x -= cyc * 3;
if (x < cyc * 3) {
continue;
}
x -= cyc * 3;
x %= cyc * 3 * 4;
if (x >= cyc * 3 * 3) {
continue;
}
while (x >= cyc * 3) {
x -= cyc * 3;
rotate(d.begin(), d.begin() + 1, d.end());
}
if (x <= cyc * 3) {
ans |= d[x % 3] << i;
continue;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> test;
while (test--) {
cin >> n;
cout << cal(n - 1) << '\n';
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int test;
long long n;
long long cal(long long a) {
long long ans = 0;
for (int i = 0; i < 61; i += 2) {
long long cyc = 1LL << i;
if (a < cyc - 1) {
continue;
}
vector<long long> d = {1, 2, 3};
long long x = a - cyc + 1;
if (x < cyc * 3) {
ans |= d[x % 3] << i;
continue;
}
x -= cyc * 3;
if (x < cyc * 3) {
continue;
}
x -= cyc * 3;
x %= cyc * 3 * 4;
if (x >= cyc * 3 * 3) {
continue;
}
while (x >= cyc * 3) {
x -= cyc * 3;
rotate(d.begin(), d.begin() + 1, d.end());
}
if (x <= cyc * 3) {
ans |= d[x % 3] << i;
continue;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> test;
while (test--) {
cin >> n;
cout << cal(n - 1) << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long p[100007] = {0, 1, 2, 3, 4, 8, 12, 5, 10, 15, 6, 11, 13, 7, 9, 14};
long long cha[17] = {0, 1, 2, 3};
long long n, L;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
cin >> n;
--n;
long long cha = n % 3;
n /= 3;
L = 0;
long long temp = 0;
while (n >= (1ll << L)) {
temp = (1ll << L);
L += 2;
n -= temp;
}
for (long long i = 0; i < L; i += 2) {
long long z = (n >> i);
long long y = z & 3;
if (y > 0) {
long long zz = y << i;
y = (cha + y) % 3;
n ^= zz;
if (y == 0) y = 3;
zz = y << i;
n ^= zz;
}
}
if (cha == 1)
n |= 1ll << (L + 1);
else if (cha == 2 || cha == 0) {
temp = (1ll << L);
n |= temp;
if (cha != 0) {
long long tt = (1ll << (L + 1));
n |= tt;
}
}
cout << n << "\n";
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long p[100007] = {0, 1, 2, 3, 4, 8, 12, 5, 10, 15, 6, 11, 13, 7, 9, 14};
long long cha[17] = {0, 1, 2, 3};
long long n, L;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
cin >> n;
--n;
long long cha = n % 3;
n /= 3;
L = 0;
long long temp = 0;
while (n >= (1ll << L)) {
temp = (1ll << L);
L += 2;
n -= temp;
}
for (long long i = 0; i < L; i += 2) {
long long z = (n >> i);
long long y = z & 3;
if (y > 0) {
long long zz = y << i;
y = (cha + y) % 3;
n ^= zz;
if (y == 0) y = 3;
zz = y << i;
n ^= zz;
}
}
if (cha == 1)
n |= 1ll << (L + 1);
else if (cha == 2 || cha == 0) {
temp = (1ll << L);
n |= temp;
if (cha != 0) {
long long tt = (1ll << (L + 1));
n |= tt;
}
}
cout << n << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long x, y, z, w;
void init() {
x = 0;
y = 0;
z = 0;
w = 0;
}
long long sol(long long bs, long long i, long long z) {
if (z == 0) return bs;
long long t = (1ll << (z - 2));
long long t2 = (i - 1) / t;
if (t2 == 0) return sol(bs, i, z - 2);
if (t2 == 1) return sol(bs + t * 2, (i - 1) % t + 1, z - 2);
if (t2 == 2) return sol(bs + t * 2 + t, (i - 1) % t + 1, z - 2);
if (t2 == 3) return sol(bs + t, (i - 1) % t + 1, z - 2);
}
long long sol1(long long n) {
init();
x = (n - 1) / 3 + 1;
for (long long i = 0;; i += 2) {
y += (1ll << i);
if (y >= x) {
return (1ll << (i + 1)) - 1 - (y - x);
}
}
}
long long sol2(long long n) {
init();
x = (n - 2) / 3 + 1;
for (long long i = 0;; i += 2) {
y += (1ll << i);
if (y >= x) {
z = (1ll << i) - (y - x);
return sol((1ll << (i + 1)), z, i);
}
}
}
int main() {
int T;
cin >> T;
while (T--) {
scanf("%lld", &n);
x = 0;
y = 0;
z = 0;
w = 0;
if (n % 3 == 1) {
printf("%lld\n", sol1(n));
} else if (n % 3 == 2) {
printf("%lld\n", sol2(n));
} else {
printf("%lld\n", (sol1(n - 2) ^ sol2(n - 1)));
}
}
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n;
long long x, y, z, w;
void init() {
x = 0;
y = 0;
z = 0;
w = 0;
}
long long sol(long long bs, long long i, long long z) {
if (z == 0) return bs;
long long t = (1ll << (z - 2));
long long t2 = (i - 1) / t;
if (t2 == 0) return sol(bs, i, z - 2);
if (t2 == 1) return sol(bs + t * 2, (i - 1) % t + 1, z - 2);
if (t2 == 2) return sol(bs + t * 2 + t, (i - 1) % t + 1, z - 2);
if (t2 == 3) return sol(bs + t, (i - 1) % t + 1, z - 2);
}
long long sol1(long long n) {
init();
x = (n - 1) / 3 + 1;
for (long long i = 0;; i += 2) {
y += (1ll << i);
if (y >= x) {
return (1ll << (i + 1)) - 1 - (y - x);
}
}
}
long long sol2(long long n) {
init();
x = (n - 2) / 3 + 1;
for (long long i = 0;; i += 2) {
y += (1ll << i);
if (y >= x) {
z = (1ll << i) - (y - x);
return sol((1ll << (i + 1)), z, i);
}
}
}
int main() {
int T;
cin >> T;
while (T--) {
scanf("%lld", &n);
x = 0;
y = 0;
z = 0;
w = 0;
if (n % 3 == 1) {
printf("%lld\n", sol1(n));
} else if (n % 3 == 2) {
printf("%lld\n", sol2(n));
} else {
printf("%lld\n", (sol1(n - 2) ^ sol2(n - 1)));
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool sort_func(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
if (a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
long long f(long long a) {
if (a == 0) return 0;
if (a == 1) return 2;
if (a == 2) return 3;
if (a == 3) return 1;
while (true) {
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long t;
cin >> t;
for (long long idx = 0; idx < t; idx++) {
long long n;
cin >> n;
long long nm = n - (n - 1) % 3;
long long pc = 0, p4 = 1;
while (n >= p4 * 4) {
pc++;
p4 *= 4;
}
long long m = (nm - p4) / 3 + p4;
if (n % 3 == 1)
cout << m << endl;
else {
long long m2 = 0;
long long p = 1;
for (long long i = 0; i < pc; i++) {
m2 += f((m / p) % 4) * p;
p *= 4;
}
m2 += 2 * p4;
if (n % 3 == 2)
cout << m2 << endl;
else
cout << (m ^ m2) << endl;
}
}
long long _;
cin >> _;
}
| ### Prompt
Generate a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool sort_func(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
if (a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
long long f(long long a) {
if (a == 0) return 0;
if (a == 1) return 2;
if (a == 2) return 3;
if (a == 3) return 1;
while (true) {
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long t;
cin >> t;
for (long long idx = 0; idx < t; idx++) {
long long n;
cin >> n;
long long nm = n - (n - 1) % 3;
long long pc = 0, p4 = 1;
while (n >= p4 * 4) {
pc++;
p4 *= 4;
}
long long m = (nm - p4) / 3 + p4;
if (n % 3 == 1)
cout << m << endl;
else {
long long m2 = 0;
long long p = 1;
for (long long i = 0; i < pc; i++) {
m2 += f((m / p) % 4) * p;
p *= 4;
}
m2 += 2 * p4;
if (n % 3 == 2)
cout << m2 << endl;
else
cout << (m ^ m2) << endl;
}
}
long long _;
cin >> _;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long INF(0x3f3f3f3f3f3f3f3fll);
const long long inf(0x3f3f3f3f);
template <typename T>
void read(T &res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
for (res = ch - 48; isdigit(ch = getchar());
res = (res << 1) + (res << 3) + ch - 48)
;
flag && (res = -res);
}
template <typename T>
void Out(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) Out(x / 10);
putchar(x % 10 + '0');
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long pow_mod(long long x, long long n, long long mod) {
long long res = 1;
while (n) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
long long fact_pow(long long n, long long p) {
long long res = 0;
while (n) {
n /= p;
res += n;
}
return res;
}
long long mult(long long a, long long b, long long p) {
a %= p;
b %= p;
long long r = 0, v = a;
while (b) {
if (b & 1) {
r += v;
if (r > p) r -= p;
}
v <<= 1;
if (v > p) v -= p;
b >>= 1;
}
return r;
}
long long quick_pow(long long a, long long b, long long p) {
long long r = 1, v = a % p;
while (b) {
if (b & 1) r = mult(r, v, p);
v = mult(v, v, p);
b >>= 1;
}
return r;
}
bool CH(long long a, long long n, long long x, long long t) {
long long r = quick_pow(a, x, n);
long long z = r;
for (long long i = 1; i <= t; i++) {
r = mult(r, r, n);
if (r == 1 && z != 1 && z != n - 1) return true;
z = r;
}
return r != 1;
}
bool Miller_Rabin(long long n) {
if (n < 2) return false;
if (n == 2) return true;
if (!(n & 1)) return false;
long long x = n - 1, t = 0;
while (!(x & 1)) {
x >>= 1;
t++;
}
srand(time(NULL));
long long o = 8;
for (long long i = 0; i < o; i++) {
long long a = rand() % (n - 1) + 1;
if (CH(a, n, x, t)) return false;
}
return true;
}
void exgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1, y = 0;
return;
}
exgcd(b, a % b, x, y);
long long t = x;
x = y, y = t - (a / b) * y;
}
long long inv(long long a, long long b) {
long long x, y;
return exgcd(a, b, x, y), (x % b + b) % b;
}
long long crt(long long x, long long p, long long mod) {
return inv(p / mod, mod) * (p / mod) * x;
}
long long fac(long long x, long long a, long long b) {
if (!x) return 1;
long long ans = 1;
for (long long i = 1; i <= b; i++)
if (i % a) ans *= i, ans %= b;
ans = pow_mod(ans, x / b, b);
for (long long i = 1; i <= x % b; i++)
if (i % a) ans *= i, ans %= b;
return ans * fac(x / a, a, b) % b;
}
long long C(long long n, long long m, long long a, long long b) {
long long N = fac(n, a, b), M = fac(m, a, b), Z = fac(n - m, a, b), sum = 0,
i;
for (i = n; i; i = i / a) sum += i / a;
for (i = m; i; i = i / a) sum -= i / a;
for (i = n - m; i; i = i / a) sum -= i / a;
return N * pow_mod(a, sum, b) % b * inv(M, b) % b * inv(Z, b) % b;
}
long long exlucas(long long n, long long m, long long p) {
long long t = p, ans = 0, i;
for (i = 2; i * i <= p; i++) {
long long k = 1;
while (t % i == 0) {
k *= i, t /= i;
}
ans += crt(C(n, m, i, k), p, k), ans %= p;
}
if (t > 1) ans += crt(C(n, m, t, t), p, t), ans %= p;
return ans % p;
}
long long prime[100000], minprime[100000];
void euler(long long n) {
long long c = 0, i, j;
for (i = 2; i <= n; i++) {
if (!minprime[i]) prime[++c] = i, minprime[i] = i;
for (j = 1; j <= c && i * prime[j] <= n; j++) {
minprime[i * prime[j]] = prime[j];
if (i % prime[j] == 0) break;
}
}
}
struct Tree {
long long l, r, sum, lazy, maxn, minn;
} tree[100000];
inline void push_up(long long rt) {
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
tree[rt].maxn = max(tree[rt << 1].maxn, tree[rt << 1 | 1].maxn);
tree[rt].minn = min(tree[rt << 1].minn, tree[rt << 1 | 1].minn);
}
inline void push_down(long long rt, long long length) {
if (tree[rt].lazy) {
tree[rt << 1].lazy += tree[rt].lazy;
tree[rt << 1 | 1].lazy += tree[rt].lazy;
tree[rt << 1].sum += (length - (length >> 1)) * tree[rt].lazy;
tree[rt << 1 | 1].sum += (length >> 1) * tree[rt].lazy;
tree[rt << 1].minn += tree[rt].lazy;
tree[rt << 1 | 1].minn += tree[rt].lazy;
tree[rt << 1].maxn += tree[rt].lazy;
tree[rt << 1 | 1].maxn += tree[rt].lazy;
tree[rt].lazy = 0;
}
}
inline void build(long long l, long long r, long long rt, long long *aa) {
tree[rt].lazy = 0;
tree[rt].l = l;
tree[rt].r = r;
if (l == r) {
tree[rt].sum = aa[l];
tree[rt].minn = tree[rt].sum;
tree[rt].maxn = tree[rt].sum;
return;
}
long long mid = (l + r) >> 1;
build(l, mid, rt << 1, aa);
build(mid + 1, r, rt << 1 | 1, aa);
push_up(rt);
}
inline void update_range(long long L, long long R, long long key,
long long rt) {
if (tree[rt].r < L || tree[rt].l > R) return;
if (L <= tree[rt].l && R >= tree[rt].r) {
tree[rt].sum += (tree[rt].r - tree[rt].l + 1) * key;
tree[rt].minn += key;
tree[rt].maxn += key;
tree[rt].lazy += key;
return;
}
push_down(rt, tree[rt].r - tree[rt].l + 1);
long long mid = (tree[rt].r + tree[rt].l) >> 1;
if (L <= mid) update_range(L, R, key, rt << 1);
if (R > mid) update_range(L, R, key, rt << 1 | 1);
push_up(rt);
}
inline long long query_range(long long L, long long R, long long rt) {
if (L <= tree[rt].l && R >= tree[rt].r) {
return tree[rt].sum;
}
push_down(rt, tree[rt].r - tree[rt].l + 1);
long long mid = (tree[rt].r + tree[rt].l) >> 1;
long long ans = 0;
if (L <= mid) ans += query_range(L, R, rt << 1);
if (R > mid) ans += query_range(L, R, rt << 1 | 1);
return ans;
}
inline long long query_min(long long L, long long R, long long rt) {
if (L <= tree[rt].l && R >= tree[rt].r) {
return tree[rt].minn;
}
push_down(rt, tree[rt].r - tree[rt].l + 1);
long long mid = (tree[rt].r + tree[rt].l) >> 1;
long long ans = (0x3f3f3f3f3f3f3f3fll);
if (L <= mid) ans = min(ans, query_min(L, R, rt << 1));
if (R > mid) ans = min(ans, query_min(L, R, rt << 1 | 1));
return ans;
}
inline long long query_max(long long L, long long R, long long rt) {
if (L <= tree[rt].l && R >= tree[rt].r) {
return tree[rt].maxn;
}
push_down(rt, tree[rt].r - tree[rt].l + 1);
long long mid = (tree[rt].r + tree[rt].l) >> 1;
long long ans = -(0x3f3f3f3f3f3f3f3fll);
if (L <= mid) ans = max(ans, query_max(L, R, rt << 1));
if (R > mid) ans = max(ans, query_max(L, R, rt << 1 | 1));
return ans;
}
const long long N = 3e5 + 10;
long long n, a[N], L;
void init() {
n /= 3;
L = 0;
while ((1ll << L) <= n) {
n -= 1ll << L;
L += 2;
}
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
cin >> n;
n--;
long long now = n % 3;
init();
for (long long i = 0; i < L; i += 2) {
long long y = n >> i & 3;
if (y > 0) {
n ^= y << i;
y = (y + now) % 3;
if (!y) y = 3;
n ^= y << i;
}
}
if (!now)
n |= 1ll << L;
else if (now == 1)
n |= 1ll << (L + 1);
else {
n |= 1ll << L;
n |= 1ll << (L + 1);
}
cout << n << '\n';
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF(0x3f3f3f3f3f3f3f3fll);
const long long inf(0x3f3f3f3f);
template <typename T>
void read(T &res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
for (res = ch - 48; isdigit(ch = getchar());
res = (res << 1) + (res << 3) + ch - 48)
;
flag && (res = -res);
}
template <typename T>
void Out(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) Out(x / 10);
putchar(x % 10 + '0');
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long pow_mod(long long x, long long n, long long mod) {
long long res = 1;
while (n) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
long long fact_pow(long long n, long long p) {
long long res = 0;
while (n) {
n /= p;
res += n;
}
return res;
}
long long mult(long long a, long long b, long long p) {
a %= p;
b %= p;
long long r = 0, v = a;
while (b) {
if (b & 1) {
r += v;
if (r > p) r -= p;
}
v <<= 1;
if (v > p) v -= p;
b >>= 1;
}
return r;
}
long long quick_pow(long long a, long long b, long long p) {
long long r = 1, v = a % p;
while (b) {
if (b & 1) r = mult(r, v, p);
v = mult(v, v, p);
b >>= 1;
}
return r;
}
bool CH(long long a, long long n, long long x, long long t) {
long long r = quick_pow(a, x, n);
long long z = r;
for (long long i = 1; i <= t; i++) {
r = mult(r, r, n);
if (r == 1 && z != 1 && z != n - 1) return true;
z = r;
}
return r != 1;
}
bool Miller_Rabin(long long n) {
if (n < 2) return false;
if (n == 2) return true;
if (!(n & 1)) return false;
long long x = n - 1, t = 0;
while (!(x & 1)) {
x >>= 1;
t++;
}
srand(time(NULL));
long long o = 8;
for (long long i = 0; i < o; i++) {
long long a = rand() % (n - 1) + 1;
if (CH(a, n, x, t)) return false;
}
return true;
}
void exgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1, y = 0;
return;
}
exgcd(b, a % b, x, y);
long long t = x;
x = y, y = t - (a / b) * y;
}
long long inv(long long a, long long b) {
long long x, y;
return exgcd(a, b, x, y), (x % b + b) % b;
}
long long crt(long long x, long long p, long long mod) {
return inv(p / mod, mod) * (p / mod) * x;
}
long long fac(long long x, long long a, long long b) {
if (!x) return 1;
long long ans = 1;
for (long long i = 1; i <= b; i++)
if (i % a) ans *= i, ans %= b;
ans = pow_mod(ans, x / b, b);
for (long long i = 1; i <= x % b; i++)
if (i % a) ans *= i, ans %= b;
return ans * fac(x / a, a, b) % b;
}
long long C(long long n, long long m, long long a, long long b) {
long long N = fac(n, a, b), M = fac(m, a, b), Z = fac(n - m, a, b), sum = 0,
i;
for (i = n; i; i = i / a) sum += i / a;
for (i = m; i; i = i / a) sum -= i / a;
for (i = n - m; i; i = i / a) sum -= i / a;
return N * pow_mod(a, sum, b) % b * inv(M, b) % b * inv(Z, b) % b;
}
long long exlucas(long long n, long long m, long long p) {
long long t = p, ans = 0, i;
for (i = 2; i * i <= p; i++) {
long long k = 1;
while (t % i == 0) {
k *= i, t /= i;
}
ans += crt(C(n, m, i, k), p, k), ans %= p;
}
if (t > 1) ans += crt(C(n, m, t, t), p, t), ans %= p;
return ans % p;
}
long long prime[100000], minprime[100000];
void euler(long long n) {
long long c = 0, i, j;
for (i = 2; i <= n; i++) {
if (!minprime[i]) prime[++c] = i, minprime[i] = i;
for (j = 1; j <= c && i * prime[j] <= n; j++) {
minprime[i * prime[j]] = prime[j];
if (i % prime[j] == 0) break;
}
}
}
struct Tree {
long long l, r, sum, lazy, maxn, minn;
} tree[100000];
inline void push_up(long long rt) {
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
tree[rt].maxn = max(tree[rt << 1].maxn, tree[rt << 1 | 1].maxn);
tree[rt].minn = min(tree[rt << 1].minn, tree[rt << 1 | 1].minn);
}
inline void push_down(long long rt, long long length) {
if (tree[rt].lazy) {
tree[rt << 1].lazy += tree[rt].lazy;
tree[rt << 1 | 1].lazy += tree[rt].lazy;
tree[rt << 1].sum += (length - (length >> 1)) * tree[rt].lazy;
tree[rt << 1 | 1].sum += (length >> 1) * tree[rt].lazy;
tree[rt << 1].minn += tree[rt].lazy;
tree[rt << 1 | 1].minn += tree[rt].lazy;
tree[rt << 1].maxn += tree[rt].lazy;
tree[rt << 1 | 1].maxn += tree[rt].lazy;
tree[rt].lazy = 0;
}
}
inline void build(long long l, long long r, long long rt, long long *aa) {
tree[rt].lazy = 0;
tree[rt].l = l;
tree[rt].r = r;
if (l == r) {
tree[rt].sum = aa[l];
tree[rt].minn = tree[rt].sum;
tree[rt].maxn = tree[rt].sum;
return;
}
long long mid = (l + r) >> 1;
build(l, mid, rt << 1, aa);
build(mid + 1, r, rt << 1 | 1, aa);
push_up(rt);
}
inline void update_range(long long L, long long R, long long key,
long long rt) {
if (tree[rt].r < L || tree[rt].l > R) return;
if (L <= tree[rt].l && R >= tree[rt].r) {
tree[rt].sum += (tree[rt].r - tree[rt].l + 1) * key;
tree[rt].minn += key;
tree[rt].maxn += key;
tree[rt].lazy += key;
return;
}
push_down(rt, tree[rt].r - tree[rt].l + 1);
long long mid = (tree[rt].r + tree[rt].l) >> 1;
if (L <= mid) update_range(L, R, key, rt << 1);
if (R > mid) update_range(L, R, key, rt << 1 | 1);
push_up(rt);
}
inline long long query_range(long long L, long long R, long long rt) {
if (L <= tree[rt].l && R >= tree[rt].r) {
return tree[rt].sum;
}
push_down(rt, tree[rt].r - tree[rt].l + 1);
long long mid = (tree[rt].r + tree[rt].l) >> 1;
long long ans = 0;
if (L <= mid) ans += query_range(L, R, rt << 1);
if (R > mid) ans += query_range(L, R, rt << 1 | 1);
return ans;
}
inline long long query_min(long long L, long long R, long long rt) {
if (L <= tree[rt].l && R >= tree[rt].r) {
return tree[rt].minn;
}
push_down(rt, tree[rt].r - tree[rt].l + 1);
long long mid = (tree[rt].r + tree[rt].l) >> 1;
long long ans = (0x3f3f3f3f3f3f3f3fll);
if (L <= mid) ans = min(ans, query_min(L, R, rt << 1));
if (R > mid) ans = min(ans, query_min(L, R, rt << 1 | 1));
return ans;
}
inline long long query_max(long long L, long long R, long long rt) {
if (L <= tree[rt].l && R >= tree[rt].r) {
return tree[rt].maxn;
}
push_down(rt, tree[rt].r - tree[rt].l + 1);
long long mid = (tree[rt].r + tree[rt].l) >> 1;
long long ans = -(0x3f3f3f3f3f3f3f3fll);
if (L <= mid) ans = max(ans, query_max(L, R, rt << 1));
if (R > mid) ans = max(ans, query_max(L, R, rt << 1 | 1));
return ans;
}
const long long N = 3e5 + 10;
long long n, a[N], L;
void init() {
n /= 3;
L = 0;
while ((1ll << L) <= n) {
n -= 1ll << L;
L += 2;
}
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
cin >> n;
n--;
long long now = n % 3;
init();
for (long long i = 0; i < L; i += 2) {
long long y = n >> i & 3;
if (y > 0) {
n ^= y << i;
y = (y + now) % 3;
if (!y) y = 3;
n ^= y << i;
}
}
if (!now)
n |= 1ll << L;
else if (now == 1)
n |= 1ll << (L + 1);
else {
n |= 1ll << L;
n |= 1ll << (L + 1);
}
cout << n << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long int T, n, res, ex, res2, con;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> T;
for (int t = 0; t < T; t++) {
cin >> n;
n--;
res = n % 3;
ex = res;
res++;
n /= 3;
res2 = 0;
con = 0;
while (n != 0) {
n--;
res = (res << 2);
if (n % 4 != 0) {
res2 += ((((n % 4) + ex - 1) % 3) + 1) << 2 * con;
}
n /= 4;
con++;
}
cout << res + res2 << "\n";
}
}
| ### Prompt
Please formulate a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int T, n, res, ex, res2, con;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> T;
for (int t = 0; t < T; t++) {
cin >> n;
n--;
res = n % 3;
ex = res;
res++;
n /= 3;
res2 = 0;
con = 0;
while (n != 0) {
n--;
res = (res << 2);
if (n % 4 != 0) {
res2 += ((((n % 4) + ex - 1) % 3) + 1) << 2 * con;
}
n /= 4;
con++;
}
cout << res + res2 << "\n";
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1000;
long long used[N], a[N];
signed main() {
srand(123);
long long cur = 1, cnt = 0;
while (cur < 256) {
while (used[cur]) cur++;
for (long long i = cur + 1; i < N; i++)
if ((cur ^ i) < N && !used[i] && !used[cur ^ i]) {
cnt++;
a[cnt] = cur;
cnt++;
a[cnt] = i;
cnt++;
a[cnt] = (cur ^ i);
used[cur] = used[i] = used[cur ^ i] = 1;
break;
}
}
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long nn = n;
if (n <= 3) {
cout << n << "\n";
continue;
}
long long x = 1, lg = 0;
while (x * 4 <= n) x *= 4, lg++;
n -= (x - 1);
n--;
long long id = n / 3;
long long f[3];
f[0] = id + x;
f[1] = 2;
while (lg > 0) {
long long t = (1ll << (2 * lg - 2));
if (id / t == 0) f[1] <<= 2;
if (id / t == 1) f[1] <<= 2, f[1] |= 2;
if (id / t == 2) f[1] <<= 2, f[1] |= 3;
if (id / t == 3) f[1] <<= 2, f[1] |= 1;
id %= t;
lg--;
}
f[2] = (f[0] ^ f[1]);
cout << f[n % 3] << "\n";
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 1000;
long long used[N], a[N];
signed main() {
srand(123);
long long cur = 1, cnt = 0;
while (cur < 256) {
while (used[cur]) cur++;
for (long long i = cur + 1; i < N; i++)
if ((cur ^ i) < N && !used[i] && !used[cur ^ i]) {
cnt++;
a[cnt] = cur;
cnt++;
a[cnt] = i;
cnt++;
a[cnt] = (cur ^ i);
used[cur] = used[i] = used[cur ^ i] = 1;
break;
}
}
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long nn = n;
if (n <= 3) {
cout << n << "\n";
continue;
}
long long x = 1, lg = 0;
while (x * 4 <= n) x *= 4, lg++;
n -= (x - 1);
n--;
long long id = n / 3;
long long f[3];
f[0] = id + x;
f[1] = 2;
while (lg > 0) {
long long t = (1ll << (2 * lg - 2));
if (id / t == 0) f[1] <<= 2;
if (id / t == 1) f[1] <<= 2, f[1] |= 2;
if (id / t == 2) f[1] <<= 2, f[1] |= 3;
if (id / t == 3) f[1] <<= 2, f[1] |= 1;
id %= t;
lg--;
}
f[2] = (f[0] ^ f[1]);
cout << f[n % 3] << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int t;
long long n;
long long f(long long n) {
long long x = 0, y = 0, z = 0;
long long cnt = 0;
long long g = (n - 1) / 3 + 1;
long long r = (n - 1) % 3;
long long p4 = 0;
long long sum = 0;
while ((1LL << (p4 * 2)) < g) {
g -= (1LL << (p4 * 2));
p4++;
}
g--;
x += (1LL << (p4 * 2));
y += x * 2;
z += x * 3;
for (long long b = p4 - 1; b >= 0; b--) {
long long d = (g >> (b * 2)) % 4;
if (d == 1) {
x += (1LL << (b * 2));
y += (1LL << (b * 2 + 1));
z += (1LL << (b * 2)) + (1LL << (b * 2 + 1));
} else if (d == 2) {
z += (1LL << (b * 2));
x += (1LL << (b * 2 + 1));
y += (1LL << (b * 2)) + (1LL << (b * 2 + 1));
} else if (d == 3) {
y += (1LL << (b * 2));
z += (1LL << (b * 2 + 1));
x += (1LL << (b * 2)) + (1LL << (b * 2 + 1));
}
}
if (r == 0) return x;
if (r == 1) return y;
if (r == 2) return z;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
cout << f(n) << "\n";
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int t;
long long n;
long long f(long long n) {
long long x = 0, y = 0, z = 0;
long long cnt = 0;
long long g = (n - 1) / 3 + 1;
long long r = (n - 1) % 3;
long long p4 = 0;
long long sum = 0;
while ((1LL << (p4 * 2)) < g) {
g -= (1LL << (p4 * 2));
p4++;
}
g--;
x += (1LL << (p4 * 2));
y += x * 2;
z += x * 3;
for (long long b = p4 - 1; b >= 0; b--) {
long long d = (g >> (b * 2)) % 4;
if (d == 1) {
x += (1LL << (b * 2));
y += (1LL << (b * 2 + 1));
z += (1LL << (b * 2)) + (1LL << (b * 2 + 1));
} else if (d == 2) {
z += (1LL << (b * 2));
x += (1LL << (b * 2 + 1));
y += (1LL << (b * 2)) + (1LL << (b * 2 + 1));
} else if (d == 3) {
y += (1LL << (b * 2));
z += (1LL << (b * 2 + 1));
x += (1LL << (b * 2)) + (1LL << (b * 2 + 1));
}
}
if (r == 0) return x;
if (r == 1) return y;
if (r == 2) return z;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
cout << f(n) << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> conv[4] = {{0, 0}, {2, 3}, {3, 1}, {1, 2}};
vector<long long int> get(long long int k) {
long long int s = 1;
while (k >= s) {
k -= s;
s *= 4;
}
long long int a = s + k, b = 2 * s, c = 3 * s;
for (int i = 0; (1LL << (2 * i)) < s; i++) {
long long int d = (a >> (2 * i)) & 3;
long long int bd, cd;
tie(bd, cd) = conv[d];
b |= bd << (2 * i);
c |= cd << (2 * i);
}
return {a, b, c};
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
long long int k;
cin >> k;
k--;
cout << get(k / 3)[k % 3] << endl;
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
pair<int, int> conv[4] = {{0, 0}, {2, 3}, {3, 1}, {1, 2}};
vector<long long int> get(long long int k) {
long long int s = 1;
while (k >= s) {
k -= s;
s *= 4;
}
long long int a = s + k, b = 2 * s, c = 3 * s;
for (int i = 0; (1LL << (2 * i)) < s; i++) {
long long int d = (a >> (2 * i)) & 3;
long long int bd, cd;
tie(bd, cd) = conv[d];
b |= bd << (2 * i);
c |= cd << (2 * i);
}
return {a, b, c};
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
long long int k;
cin >> k;
k--;
cout << get(k / 3)[k % 3] << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long parse(string s) {
long long k = 1, ret = 0;
for (int i = 0; i < s.size(); i++)
ret += k * (long long)(s[s.size() - i - 1] - '0'), k *= 4;
return ret;
}
string change(string s) {
string ret = s;
for (int i = 0; i < ret.size(); i++) {
if (ret[i] == '1')
ret[i] = '2';
else if (ret[i] == '2')
ret[i] = '3';
else if (ret[i] == '3')
ret[i] = '1';
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long idx = (n + 2) / 3, m = n % 3;
long long k = 0, sum = 0;
while (sum < idx) sum += (1ll << (2 * k)), k++;
k--;
long long buf = (1ll << (2 * k)), c = sum - buf + 1;
string ans = "1";
for (int i = k - 1; i >= 0; i--) {
long long div = (1ll << (2 * i));
long long d = (idx - c) / div;
ans += (char)('0' + d);
c += div * d;
}
long long f = parse(ans);
long long s = parse(change(ans));
if (m == 1)
cout << f << '\n';
else if (m == 2)
cout << s << '\n';
else
cout << (f ^ s) << '\n';
}
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long parse(string s) {
long long k = 1, ret = 0;
for (int i = 0; i < s.size(); i++)
ret += k * (long long)(s[s.size() - i - 1] - '0'), k *= 4;
return ret;
}
string change(string s) {
string ret = s;
for (int i = 0; i < ret.size(); i++) {
if (ret[i] == '1')
ret[i] = '2';
else if (ret[i] == '2')
ret[i] = '3';
else if (ret[i] == '3')
ret[i] = '1';
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long idx = (n + 2) / 3, m = n % 3;
long long k = 0, sum = 0;
while (sum < idx) sum += (1ll << (2 * k)), k++;
k--;
long long buf = (1ll << (2 * k)), c = sum - buf + 1;
string ans = "1";
for (int i = k - 1; i >= 0; i--) {
long long div = (1ll << (2 * i));
long long d = (idx - c) / div;
ans += (char)('0' + d);
c += div * d;
}
long long f = parse(ans);
long long s = parse(change(ans));
if (m == 1)
cout << f << '\n';
else if (m == 2)
cout << s << '\n';
else
cout << (f ^ s) << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long T, n;
long long getFirst(long long n) {
long long cur = 4;
while (n > cur) {
n -= cur;
cur *= 4;
}
return cur + n - 1;
}
long long getSecond(long long n) {
long long cur = 4;
while (n > cur) {
n -= cur;
cur *= 4;
}
n--;
vector<long long> d;
while (n) {
d.push_back(n % 4);
n /= 4;
}
long long f[4] = {0, 2, 3, 1};
reverse(d.begin(), d.end());
for (auto i : d) n = n * 4ll + f[i];
return 2ll * cur + n;
}
long long getThird(long long n) { return getFirst(n) ^ getSecond(n); }
int init() {
scanf("%lld", &n);
if (n <= 3) return printf("%lld\n", n), 0;
if (n % 3 == 1) return printf("%lld\n", getFirst(n / 3));
if (n % 3 == 2) return printf("%lld\n", getSecond(n / 3));
return printf("%lld\n", getThird(n / 3 - 1));
}
int main() {
scanf("%lld", &T);
while (T--) init();
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long T, n;
long long getFirst(long long n) {
long long cur = 4;
while (n > cur) {
n -= cur;
cur *= 4;
}
return cur + n - 1;
}
long long getSecond(long long n) {
long long cur = 4;
while (n > cur) {
n -= cur;
cur *= 4;
}
n--;
vector<long long> d;
while (n) {
d.push_back(n % 4);
n /= 4;
}
long long f[4] = {0, 2, 3, 1};
reverse(d.begin(), d.end());
for (auto i : d) n = n * 4ll + f[i];
return 2ll * cur + n;
}
long long getThird(long long n) { return getFirst(n) ^ getSecond(n); }
int init() {
scanf("%lld", &n);
if (n <= 3) return printf("%lld\n", n), 0;
if (n % 3 == 1) return printf("%lld\n", getFirst(n / 3));
if (n % 3 == 2) return printf("%lld\n", getSecond(n / 3));
return printf("%lld\n", getThird(n / 3 - 1));
}
int main() {
scanf("%lld", &T);
while (T--) init();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int inf = 1e9;
const int mo = 998244353;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long now = 0, base = 1;
long long m = (n - 1) / 3 + 1;
while (now + base < m) {
now += base, base *= 4;
}
long long a = base, b = base * 2;
long long num[4] = {0, 2, 3, 1};
long long pos = m - now - 1;
a += pos;
for (auto i = (1); i <= (31); ++i) {
b += (num[(pos >> (2 * (i - 1))) % 4] << (2 * i - 2));
}
long long ans;
if (n % 3 == 1)
ans = a;
else if (n % 3 == 2)
ans = b;
else
ans = a ^ b;
cout << ans << endl;
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int inf = 1e9;
const int mo = 998244353;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long now = 0, base = 1;
long long m = (n - 1) / 3 + 1;
while (now + base < m) {
now += base, base *= 4;
}
long long a = base, b = base * 2;
long long num[4] = {0, 2, 3, 1};
long long pos = m - now - 1;
a += pos;
for (auto i = (1); i <= (31); ++i) {
b += (num[(pos >> (2 * (i - 1))) % 4] << (2 * i - 2));
}
long long ans;
if (n % 3 == 1)
ans = a;
else if (n % 3 == 2)
ans = b;
else
ans = a ^ b;
cout << ans << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
long long T;
long long n, nn, a[4] = {0, 2, 3, 1}, b[4] = {0, 3, 1, 2};
int main() {
scanf("%d", &T);
while (T--) {
scanf("%lld", &n);
long long x = 1;
while (n > x * 3) n -= x * 3, x <<= 2;
if (n % 3 == 1)
printf("%lld\n", x + n / 3);
else if (n % 3 == 2) {
n /= 3;
long long ans = x << 1;
for (long long i = x >> 2; i; i >>= 2) {
long long dat = n / i;
ans |= a[dat] * i;
n %= i;
}
cout << ans << endl;
} else {
n--;
n /= 3;
long long ans = x << 1 | x;
for (long long i = x >> 2; i; i >>= 2) {
long long dat = n / i;
ans |= b[dat] * i;
n %= i;
}
cout << ans << endl;
}
}
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
long long T;
long long n, nn, a[4] = {0, 2, 3, 1}, b[4] = {0, 3, 1, 2};
int main() {
scanf("%d", &T);
while (T--) {
scanf("%lld", &n);
long long x = 1;
while (n > x * 3) n -= x * 3, x <<= 2;
if (n % 3 == 1)
printf("%lld\n", x + n / 3);
else if (n % 3 == 2) {
n /= 3;
long long ans = x << 1;
for (long long i = x >> 2; i; i >>= 2) {
long long dat = n / i;
ans |= a[dat] * i;
n %= i;
}
cout << ans << endl;
} else {
n--;
n /= 3;
long long ans = x << 1 | x;
for (long long i = x >> 2; i; i >>= 2) {
long long dat = n / i;
ans |= b[dat] * i;
n %= i;
}
cout << ans << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long arr[4] = {1, 2, 3};
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long nn = (n + 2) / 3;
long long xx = 1, c = 0;
while (nn > xx) nn -= xx, xx *= 4, c++;
long long ad = ((n + 2) % 3 + 1) * xx;
long long z = nn - 1;
long long pos = 1, cc = 1;
for (long long i = 0; i < c; i++) {
if ((z) % (cc * 4)) ad += pos * arr[((z) % (cc * 4) + (n + 1 % 3)) % 3];
pos <<= 2;
z /= 4;
}
cout << ad << endl;
}
return 0;
}
| ### Prompt
Generate a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long arr[4] = {1, 2, 3};
int32_t main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long nn = (n + 2) / 3;
long long xx = 1, c = 0;
while (nn > xx) nn -= xx, xx *= 4, c++;
long long ad = ((n + 2) % 3 + 1) * xx;
long long z = nn - 1;
long long pos = 1, cc = 1;
for (long long i = 0; i < c; i++) {
if ((z) % (cc * 4)) ad += pos * arr[((z) % (cc * 4) + (n + 1 % 3)) % 3];
pos <<= 2;
z /= 4;
}
cout << ad << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chkmin(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, T2 y) {
if (x < y) x = y;
}
using ll = long long;
using ld = long double;
const string FILENAME = "input";
void solve() {
ll n;
cin >> n;
n--;
ll id = n / 3;
ll pos;
int it = 0;
for (ll i = 1;; i *= 4) {
it++;
if (i > id) {
pos = i;
break;
}
id -= i;
}
ll a = pos + id;
ll b = pos * 2;
ll g[4] = {0, 2, 3, 1};
for (int i = 0; i < it - 1; i++) {
pos /= 4;
ll val = id / pos;
b += pos * g[val];
id %= pos;
}
if (n % 3 == 0) {
cout << a;
} else if (n % 3 == 1) {
cout << b;
} else {
cout << (a ^ b);
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chkmin(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, T2 y) {
if (x < y) x = y;
}
using ll = long long;
using ld = long double;
const string FILENAME = "input";
void solve() {
ll n;
cin >> n;
n--;
ll id = n / 3;
ll pos;
int it = 0;
for (ll i = 1;; i *= 4) {
it++;
if (i > id) {
pos = i;
break;
}
id -= i;
}
ll a = pos + id;
ll b = pos * 2;
ll g[4] = {0, 2, 3, 1};
for (int i = 0; i < it - 1; i++) {
pos /= 4;
ll val = id / pos;
b += pos * g[val];
id %= pos;
}
if (n % 3 == 0) {
cout << a;
} else if (n % 3 == 1) {
cout << b;
} else {
cout << (a ^ b);
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
unsigned long long next(unsigned long long n) {
unsigned long long mask = 3;
int k = 0;
unsigned long long ret = 0;
while (mask <= n || (mask & n) != 0) {
unsigned long long cur_ret = n & mask;
if (cur_ret != 0) {
cur_ret += (1LL << k);
cur_ret &= mask;
if (cur_ret == 0) cur_ret = (1LL << k);
}
ret |= cur_ret;
mask <<= 2;
k += 2;
}
return ret;
}
unsigned long long get_num_1(unsigned long long n) {
unsigned long long base = 1;
while ((base << 2) <= n) {
base <<= 2;
}
n -= (base - 1);
return base + n / 3;
}
unsigned long long get_num_2(unsigned long long n) {
unsigned long long base = get_num_1(n - 1);
return next(base);
}
unsigned long long get_num_0(unsigned long long n) {
unsigned long long base = get_num_2(n - 1);
return next(base);
}
unsigned long long get_num(unsigned long long n) {
switch (n % 3) {
case 0:
return get_num_0(n);
case 1:
return get_num_1(n);
case 2:
return get_num_2(n);
default:
return 0;
}
}
int main() {
int num;
cin >> num;
for (int i = 0; i < num; ++i) {
unsigned long long n;
cin >> n;
unsigned long long num1 = get_num(n);
cout << num1 << endl;
}
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
unsigned long long next(unsigned long long n) {
unsigned long long mask = 3;
int k = 0;
unsigned long long ret = 0;
while (mask <= n || (mask & n) != 0) {
unsigned long long cur_ret = n & mask;
if (cur_ret != 0) {
cur_ret += (1LL << k);
cur_ret &= mask;
if (cur_ret == 0) cur_ret = (1LL << k);
}
ret |= cur_ret;
mask <<= 2;
k += 2;
}
return ret;
}
unsigned long long get_num_1(unsigned long long n) {
unsigned long long base = 1;
while ((base << 2) <= n) {
base <<= 2;
}
n -= (base - 1);
return base + n / 3;
}
unsigned long long get_num_2(unsigned long long n) {
unsigned long long base = get_num_1(n - 1);
return next(base);
}
unsigned long long get_num_0(unsigned long long n) {
unsigned long long base = get_num_2(n - 1);
return next(base);
}
unsigned long long get_num(unsigned long long n) {
switch (n % 3) {
case 0:
return get_num_0(n);
case 1:
return get_num_1(n);
case 2:
return get_num_2(n);
default:
return 0;
}
}
int main() {
int num;
cin >> num;
for (int i = 0; i < num; ++i) {
unsigned long long n;
cin >> n;
unsigned long long num1 = get_num(n);
cout << num1 << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
int main() {
int t, p;
scanf("%d", &t);
for (p = 1; p <= t; p++) {
long long n;
scanf("%lld", &n);
if (n <= 3) {
printf("%lld\n", n);
continue;
}
long long m = n;
long long base = 1;
while (m > 0) {
m = m / 4;
base = base * 4;
}
base = base / 4;
if (n % 3 == 1) {
printf("%lld\n", base + (n - base) / 3);
continue;
}
long long fi = base + (n - base) / 3;
long long se = base * 2;
m = (n - base) / 3;
long long cur = 1;
while (m > 0) {
if (m % 4 == 1) se += 2 * cur;
if (m % 4 == 2) se += 3 * cur;
if (m % 4 == 3) se += cur;
m = m / 4;
cur = cur * 4;
}
if (n % 3 == 2)
printf("%lld\n", se);
else
printf("%lld\n", fi ^ se);
}
return 0;
}
| ### Prompt
In Cpp, your task is to solve the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
int main() {
int t, p;
scanf("%d", &t);
for (p = 1; p <= t; p++) {
long long n;
scanf("%lld", &n);
if (n <= 3) {
printf("%lld\n", n);
continue;
}
long long m = n;
long long base = 1;
while (m > 0) {
m = m / 4;
base = base * 4;
}
base = base / 4;
if (n % 3 == 1) {
printf("%lld\n", base + (n - base) / 3);
continue;
}
long long fi = base + (n - base) / 3;
long long se = base * 2;
m = (n - base) / 3;
long long cur = 1;
while (m > 0) {
if (m % 4 == 1) se += 2 * cur;
if (m % 4 == 2) se += 3 * cur;
if (m % 4 == 3) se += cur;
m = m / 4;
cur = cur * 4;
}
if (n % 3 == 2)
printf("%lld\n", se);
else
printf("%lld\n", fi ^ se);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long MOD = (1e9) + 7;
const long long N = (1e6) + 5;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tc = 63;
cin >> tc;
for (long long ti = 1; ti <= tc; ti++) {
long long n;
cin >> n;
long long s = 1LL, e = 3LL;
while (1) {
if (n >= s && n <= e) break;
s *= 4LL;
e++;
e *= 4LL;
e--;
}
n -= s;
long long r = (n) / 3LL;
long long siz = s;
long long bs = 2LL * s, cs = 3LL * s, rem;
while (siz > 1LL) {
r = n / 3LL;
siz /= 4LL;
long long bno = r / siz;
if (!bno) {
s = s;
bs = bs;
cs = cs;
} else if (bno == 1) {
s = s + siz;
bs = bs + (2LL * siz);
cs = cs + (3LL * siz);
} else if (bno == 2) {
s = s + (2LL * siz);
bs = bs + (3LL * siz);
cs = cs + (siz);
} else {
s = s + (3LL * siz);
bs = bs + (siz);
cs = cs + (2LL * siz);
}
n -= (bno * siz * 3);
}
if (n % 3 == 0)
cout << s;
else if (n % 3 == 1)
cout << bs;
else
cout << cs;
cout << endl;
}
return 0;
}
| ### Prompt
Generate a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long MOD = (1e9) + 7;
const long long N = (1e6) + 5;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tc = 63;
cin >> tc;
for (long long ti = 1; ti <= tc; ti++) {
long long n;
cin >> n;
long long s = 1LL, e = 3LL;
while (1) {
if (n >= s && n <= e) break;
s *= 4LL;
e++;
e *= 4LL;
e--;
}
n -= s;
long long r = (n) / 3LL;
long long siz = s;
long long bs = 2LL * s, cs = 3LL * s, rem;
while (siz > 1LL) {
r = n / 3LL;
siz /= 4LL;
long long bno = r / siz;
if (!bno) {
s = s;
bs = bs;
cs = cs;
} else if (bno == 1) {
s = s + siz;
bs = bs + (2LL * siz);
cs = cs + (3LL * siz);
} else if (bno == 2) {
s = s + (2LL * siz);
bs = bs + (3LL * siz);
cs = cs + (siz);
} else {
s = s + (3LL * siz);
bs = bs + (siz);
cs = cs + (2LL * siz);
}
n -= (bno * siz * 3);
}
if (n % 3 == 0)
cout << s;
else if (n % 3 == 1)
cout << bs;
else
cout << cs;
cout << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
T abs(T x) {
return x < 0 ? -x : x;
}
template <typename T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <typename T>
bool chmin(T& x, const T& y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T& x, const T& y) {
if (x < y) {
x = y;
return true;
}
return false;
}
string to_bin(int x) {
string ret;
while (x) {
ret = ret + (char)((x % 2) + '0');
x /= 2;
}
return ret;
}
int main(int, char**) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long m = (n - 1) / 3;
int k = 0;
long long p = 1;
while (m >= p) {
m -= p;
k += 1;
p *= 4;
}
vector<long long> a;
for (int i = 0; i < k; ++i) {
a.push_back(m % 4);
m /= 4;
}
long long A, B;
A = 1LL << (2 * k);
B = 1LL << (2 * k + 1);
vector<long long> b = {0, 2, 3, 1};
for (int i = 0; i < k; ++i) {
A ^= a[i] << (2 * i);
B ^= b[a[i]] << (2 * i);
}
long long C = A ^ B;
long long ans;
if (n % 3 == 1) {
ans = A;
} else if (n % 3 == 2) {
ans = B;
} else {
ans = C;
}
cout << ans << "\n";
}
cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
<< endl;
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
T abs(T x) {
return x < 0 ? -x : x;
}
template <typename T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <typename T>
bool chmin(T& x, const T& y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T& x, const T& y) {
if (x < y) {
x = y;
return true;
}
return false;
}
string to_bin(int x) {
string ret;
while (x) {
ret = ret + (char)((x % 2) + '0');
x /= 2;
}
return ret;
}
int main(int, char**) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long m = (n - 1) / 3;
int k = 0;
long long p = 1;
while (m >= p) {
m -= p;
k += 1;
p *= 4;
}
vector<long long> a;
for (int i = 0; i < k; ++i) {
a.push_back(m % 4);
m /= 4;
}
long long A, B;
A = 1LL << (2 * k);
B = 1LL << (2 * k + 1);
vector<long long> b = {0, 2, 3, 1};
for (int i = 0; i < k; ++i) {
A ^= a[i] << (2 * i);
B ^= b[a[i]] << (2 * i);
}
long long C = A ^ B;
long long ans;
if (n % 3 == 1) {
ans = A;
} else if (n % 3 == 2) {
ans = B;
} else {
ans = C;
}
cout << ans << "\n";
}
cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
<< endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int64_t trans[3][4] = {
{0, 1, 2, 3},
{0, 2, 3, 1},
{0, 3, 1, 2},
};
int64_t solve(int64_t N) {
N--;
int64_t tripNum = N / 3;
int numDig = 0;
while (tripNum >= (int64_t(1) << (2 * numDig))) {
tripNum -= (int64_t(1) << (2 * numDig));
numDig++;
}
int64_t v = (int64_t(1) << (2 * numDig)) + tripNum;
int64_t ans = 0;
for (int d = 0; d <= numDig; d++) {
ans |= trans[N % 3][(v >> (2 * d)) & 3] << (2 * d);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int T;
cin >> T;
while (T--) {
int64_t N;
cin >> N;
int64_t ans = solve(N);
cout << ans << '\n';
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int64_t trans[3][4] = {
{0, 1, 2, 3},
{0, 2, 3, 1},
{0, 3, 1, 2},
};
int64_t solve(int64_t N) {
N--;
int64_t tripNum = N / 3;
int numDig = 0;
while (tripNum >= (int64_t(1) << (2 * numDig))) {
tripNum -= (int64_t(1) << (2 * numDig));
numDig++;
}
int64_t v = (int64_t(1) << (2 * numDig)) + tripNum;
int64_t ans = 0;
for (int d = 0; d <= numDig; d++) {
ans |= trans[N % 3][(v >> (2 * d)) & 3] << (2 * d);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int T;
cin >> T;
while (T--) {
int64_t N;
cin >> N;
int64_t ans = solve(N);
cout << ans << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int t;
long long n, sum, x, multi, ans;
vector<pair<int, int> > v;
int main() {
int i, k;
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
sum = 0;
for (i = 0;; i++) {
sum += 3 * (1LL << (i * 2));
if (sum >= n) break;
}
x = 3 * (1LL << (i * 2));
sum -= x;
n -= sum;
v.clear();
if (n % 3 == 1)
v.emplace_back(0, 1);
else if (n % 3 == 2)
v.emplace_back(1, 0);
else
v.emplace_back(1, 1);
k = i;
for (i = 1; i <= k; i++) {
if (n <= x / 4)
v.emplace_back(0, 0);
else if (n <= x / 4 * 2) {
if (n % 3 == 1)
v.emplace_back(0, 1);
else if (n % 3 == 2)
v.emplace_back(1, 0);
else
v.emplace_back(1, 1);
n -= x / 4;
} else if (n <= x / 4 * 3) {
if (n % 3 == 1)
v.emplace_back(1, 0);
else if (n % 3 == 2)
v.emplace_back(1, 1);
else
v.emplace_back(0, 1);
n -= x / 2;
} else {
if (n % 3 == 1)
v.emplace_back(1, 1);
else if (n % 3 == 2)
v.emplace_back(0, 1);
else
v.emplace_back(1, 0);
n -= x / 4 * 3;
}
x /= 4;
}
multi = 1;
ans = 0;
for (i = v.size() - 1; i >= 0; i--) {
ans += multi * (2 * v[i].first + v[i].second);
multi *= 4;
}
printf("%lld\n", ans);
}
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int t;
long long n, sum, x, multi, ans;
vector<pair<int, int> > v;
int main() {
int i, k;
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
sum = 0;
for (i = 0;; i++) {
sum += 3 * (1LL << (i * 2));
if (sum >= n) break;
}
x = 3 * (1LL << (i * 2));
sum -= x;
n -= sum;
v.clear();
if (n % 3 == 1)
v.emplace_back(0, 1);
else if (n % 3 == 2)
v.emplace_back(1, 0);
else
v.emplace_back(1, 1);
k = i;
for (i = 1; i <= k; i++) {
if (n <= x / 4)
v.emplace_back(0, 0);
else if (n <= x / 4 * 2) {
if (n % 3 == 1)
v.emplace_back(0, 1);
else if (n % 3 == 2)
v.emplace_back(1, 0);
else
v.emplace_back(1, 1);
n -= x / 4;
} else if (n <= x / 4 * 3) {
if (n % 3 == 1)
v.emplace_back(1, 0);
else if (n % 3 == 2)
v.emplace_back(1, 1);
else
v.emplace_back(0, 1);
n -= x / 2;
} else {
if (n % 3 == 1)
v.emplace_back(1, 1);
else if (n % 3 == 2)
v.emplace_back(0, 1);
else
v.emplace_back(1, 0);
n -= x / 4 * 3;
}
x /= 4;
}
multi = 1;
ans = 0;
for (i = v.size() - 1; i >= 0; i--) {
ans += multi * (2 * v[i].first + v[i].second);
multi *= 4;
}
printf("%lld\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 100;
const int mod = (int)1e9 + 7;
long long ans[5];
void solve() {
long long n;
scanf("%lld", &n);
if (n <= 3) return (void)printf("%lld\n", n);
long long N = (n - 1) / 3 + 1, b = 1;
for (; b < N; b *= 4) N -= b;
ans[1] = N + b - 1, ans[2] = b * 2;
long long sz = b;
while (sz > 1) {
sz /= 4;
if ((N - 1) / sz == 1)
N -= sz, ans[2] += sz * 2;
else if ((N - 1) / sz == 2)
N -= sz * 2, ans[2] += sz * 3;
else if ((N - 1) / sz == 3)
N -= sz * 3, ans[2] += sz;
}
ans[0] = ans[1] ^ ans[2];
printf("%lld\n", ans[n % 3]);
}
int main() {
int T;
cin >> T;
while (T--) solve();
}
| ### Prompt
Create a solution in CPP for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 100;
const int mod = (int)1e9 + 7;
long long ans[5];
void solve() {
long long n;
scanf("%lld", &n);
if (n <= 3) return (void)printf("%lld\n", n);
long long N = (n - 1) / 3 + 1, b = 1;
for (; b < N; b *= 4) N -= b;
ans[1] = N + b - 1, ans[2] = b * 2;
long long sz = b;
while (sz > 1) {
sz /= 4;
if ((N - 1) / sz == 1)
N -= sz, ans[2] += sz * 2;
else if ((N - 1) / sz == 2)
N -= sz * 2, ans[2] += sz * 3;
else if ((N - 1) / sz == 3)
N -= sz * 3, ans[2] += sz;
}
ans[0] = ans[1] ^ ans[2];
printf("%lld\n", ans[n % 3]);
}
int main() {
int T;
cin >> T;
while (T--) solve();
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long f(long long p, long long n, long long k) {
if (k == 0) return p + n;
if (k == 1) {
for (int i = 0; i < 60; i += 2) {
if ((n & (1ll << i)) != 0 and (n & (1ll << (i + 1))) != 0) {
n -= (1ll << (i + 1));
} else if ((n & (1ll << i)) != 0 and (n & (1ll << (i + 1))) == 0) {
n += (1ll << (i + 1));
n -= (1ll << i);
} else if ((n & (1ll << i)) == 0 and (n & (1ll << (i + 1))) != 0) {
n += (1ll << i);
}
}
return 2 * p + n;
}
for (int i = 0; i < 60; i += 2) {
if ((n & (1ll << i)) != 0 and (n & (1ll << (i + 1))) != 0) {
n -= (1ll << i);
} else if ((n & (1ll << i)) != 0 and (n & (1ll << (i + 1))) == 0) {
n += (1ll << (i + 1));
} else if ((n & (1ll << i)) == 0 and (n & (1ll << (i + 1))) != 0) {
n += (1ll << i);
n -= (1ll << (i + 1));
}
}
return 3 * p + n;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t, n, p, k, sum, i = 1;
cin >> t;
while (t--) {
cin >> n;
n--;
p = 1;
k = n % 3;
n = n / 3;
sum = 0;
while (sum + p <= n) {
sum += p;
p = p * 4;
}
cout << f(p, n - sum, k) << "\n";
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long f(long long p, long long n, long long k) {
if (k == 0) return p + n;
if (k == 1) {
for (int i = 0; i < 60; i += 2) {
if ((n & (1ll << i)) != 0 and (n & (1ll << (i + 1))) != 0) {
n -= (1ll << (i + 1));
} else if ((n & (1ll << i)) != 0 and (n & (1ll << (i + 1))) == 0) {
n += (1ll << (i + 1));
n -= (1ll << i);
} else if ((n & (1ll << i)) == 0 and (n & (1ll << (i + 1))) != 0) {
n += (1ll << i);
}
}
return 2 * p + n;
}
for (int i = 0; i < 60; i += 2) {
if ((n & (1ll << i)) != 0 and (n & (1ll << (i + 1))) != 0) {
n -= (1ll << i);
} else if ((n & (1ll << i)) != 0 and (n & (1ll << (i + 1))) == 0) {
n += (1ll << (i + 1));
} else if ((n & (1ll << i)) == 0 and (n & (1ll << (i + 1))) != 0) {
n += (1ll << i);
n -= (1ll << (i + 1));
}
}
return 3 * p + n;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t, n, p, k, sum, i = 1;
cin >> t;
while (t--) {
cin >> n;
n--;
p = 1;
k = n % 3;
n = n / 3;
sum = 0;
while (sum + p <= n) {
sum += p;
p = p * 4;
}
cout << f(p, n - sum, k) << "\n";
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1, c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f == 1 ? x : -x;
}
long long calc(long long L, long long R, long long p) {
if (L + 1 == R) return L;
long long x = (R - L) / 4;
if (p <= x) return calc(L, L + x, p);
if (p <= x * 2) return calc(L + x * 2, L + x * 3, p - x);
if (p <= x * 3) return calc(L + x * 3, R, p - x * 2);
return calc(L + x, L + x * 2, p - x * 3);
}
long long n, L, R, fi, se;
inline void solve() {
n = read();
long long p = (n + 2) / 3;
for (long long i = 0;; i += 2) {
if (p <= (1ll << i)) {
L = 1ll << (i + 1);
R = 3 * (1ll << i);
fi = (1ll << i) + p - 1;
break;
}
p -= 1ll << i;
}
if (n % 3 == 1) {
cout << fi << "\n";
return;
}
se = calc(L, R, p);
if (n % 3 == 2)
cout << se << "\n";
else
cout << (fi ^ se) << "\n";
}
signed main() {
for (long long T = read(); T--;) solve();
return (0 - 0);
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
Consider the infinite sequence s of positive integers, created by repeating the following steps:
1. Find the lexicographically smallest triple of positive integers (a, b, c) such that
* a β b β c = 0, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
* a, b, c are not in s.
Here triple of integers (a_1, b_1, c_1) is considered to be lexicographically smaller than triple (a_2, b_2, c_2) if sequence [a_1, b_1, c_1] is lexicographically smaller than sequence [a_2, b_2, c_2].
2. Append a, b, c to s in this order.
3. Go back to the first step.
You have integer n. Find the n-th element of s.
You have to answer t independent test cases.
A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Input
The first line contains a single integer t (1 β€ t β€ 10^5) β the number of test cases.
Each of the next t lines contains a single integer n (1β€ n β€ 10^{16}) β the position of the element you want to know.
Output
In each of the t lines, output the answer to the corresponding test case.
Example
Input
9
1
2
3
4
5
6
7
8
9
Output
1
2
3
4
8
12
5
10
15
Note
The first elements of s are 1, 2, 3, 4, 8, 12, 5, 10, 15, ...
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1, c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f == 1 ? x : -x;
}
long long calc(long long L, long long R, long long p) {
if (L + 1 == R) return L;
long long x = (R - L) / 4;
if (p <= x) return calc(L, L + x, p);
if (p <= x * 2) return calc(L + x * 2, L + x * 3, p - x);
if (p <= x * 3) return calc(L + x * 3, R, p - x * 2);
return calc(L + x, L + x * 2, p - x * 3);
}
long long n, L, R, fi, se;
inline void solve() {
n = read();
long long p = (n + 2) / 3;
for (long long i = 0;; i += 2) {
if (p <= (1ll << i)) {
L = 1ll << (i + 1);
R = 3 * (1ll << i);
fi = (1ll << i) + p - 1;
break;
}
p -= 1ll << i;
}
if (n % 3 == 1) {
cout << fi << "\n";
return;
}
se = calc(L, R, p);
if (n % 3 == 2)
cout << se << "\n";
else
cout << (fi ^ se) << "\n";
}
signed main() {
for (long long T = read(); T--;) solve();
return (0 - 0);
}
``` |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, min;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n, greater<int>());
min = a[0] - a[1];
for (int i = 0; i < n - 1; i++)
if (a[i] - a[i + 1] < min) min = a[i] - a[i + 1];
cout << min << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| ### Prompt
Create a solution in CPP for the following problem:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, min;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n, greater<int>());
min = a[0] - a[1];
for (int i = 0; i < n - 1; i++)
if (a[i] - a[i + 1] < min) min = a[i] - a[i + 1];
cout << min << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int u = 0; u < t; u++) {
int a[50], n, r;
bool g = false;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
r = abs(a[0] - a[1]);
for (int i = 1; i < n - 1; i++) {
if (r > (a[i + 1] - a[i])) r = a[i + 1] - a[i];
if (r == 0) {
cout << 0 << endl;
g = true;
break;
}
}
if (g == false) cout << r << endl;
}
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int u = 0; u < t; u++) {
int a[50], n, r;
bool g = false;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
r = abs(a[0] - a[1]);
for (int i = 1; i < n - 1; i++) {
if (r > (a[i + 1] - a[i])) r = a[i + 1] - a[i];
if (r == 0) {
cout << 0 << endl;
g = true;
break;
}
}
if (g == false) cout << r << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e3 + 10;
const int inf = 0x3f3f3f3f;
int t, n, ans;
int dp[M];
int main() {
scanf("%d", &t);
while (t--) {
ans = inf;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &dp[i]);
sort(dp, dp + n);
for (int i = 1; i < n; i++) ans = min(ans, dp[i] - dp[i - 1]);
printf("%d\n", ans);
}
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int M = 1e3 + 10;
const int inf = 0x3f3f3f3f;
int t, n, ans;
int dp[M];
int main() {
scanf("%d", &t);
while (t--) {
ans = inf;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &dp[i]);
sort(dp, dp + n);
for (int i = 1; i < n; i++) ans = min(ans, dp[i] - dp[i - 1]);
printf("%d\n", ans);
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int test;
cin >> test;
while (test--) {
int n;
cin >> n;
multiset<int> s;
s.clear();
int m = 20000, mx, x;
for (int i = 0; i < n; i++) {
cin >> x;
s.insert(x);
}
x = *s.begin();
while (!s.empty()) {
s.erase(s.begin());
if (!s.empty())
mx = *s.begin();
else
break;
m = min(abs(mx - x), m);
x = mx;
}
cout << m << '\n';
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int test;
cin >> test;
while (test--) {
int n;
cin >> n;
multiset<int> s;
s.clear();
int m = 20000, mx, x;
for (int i = 0; i < n; i++) {
cin >> x;
s.insert(x);
}
x = *s.begin();
while (!s.empty()) {
s.erase(s.begin());
if (!s.empty())
mx = *s.begin();
else
break;
m = min(abs(mx - x), m);
x = mx;
}
cout << m << '\n';
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, i, flag;
cin >> t;
while (t--) {
int x = 10000;
flag = 0;
cin >> n;
int s[n];
for (i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n);
for (i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
cout << "0" << endl;
flag = 1;
break;
}
}
if (flag == 0) {
int a[n - 1];
for (i = 0; i < n - 1; i++) {
a[i] = s[i + 1] - s[i];
}
for (i = 0; i < n - 1; i++) {
if (a[i] < x) {
x = a[i];
}
}
cout << x << endl;
}
}
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, i, flag;
cin >> t;
while (t--) {
int x = 10000;
flag = 0;
cin >> n;
int s[n];
for (i = 0; i < n; i++) {
cin >> s[i];
}
sort(s, s + n);
for (i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
cout << "0" << endl;
flag = 1;
break;
}
}
if (flag == 0) {
int a[n - 1];
for (i = 0; i < n - 1; i++) {
a[i] = s[i + 1] - s[i];
}
for (i = 0; i < n - 1; i++) {
if (a[i] < x) {
x = a[i];
}
}
cout << x << endl;
}
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
sort(arr, arr + n);
int diff = arr[1] - arr[0];
for (int i = 1; i < n - 1; ++i) {
if (arr[i + 1] - arr[i] < diff) {
diff = arr[i + 1] - arr[i];
}
}
cout << diff << endl;
}
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
sort(arr, arr + n);
int diff = arr[1] - arr[0];
for (int i = 1; i < n - 1; ++i) {
if (arr[i + 1] - arr[i] < diff) {
diff = arr[i + 1] - arr[i];
}
}
cout << diff << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n, a, i, j;
cin >> t;
long long k[t];
for (i = 0; i < t; i++) {
cin >> n;
int x[n];
for (j = 0; j < n; j++) {
cin >> x[j];
}
sort(x, x + n);
k[i] = x[1] - x[0];
for (j = 2; j < n; j++) {
a = x[j] - x[j - 1];
if (a < k[i]) k[i] = a;
}
}
for (i = 0; i < t; i++) {
cout << k[i] << endl;
}
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n, a, i, j;
cin >> t;
long long k[t];
for (i = 0; i < t; i++) {
cin >> n;
int x[n];
for (j = 0; j < n; j++) {
cin >> x[j];
}
sort(x, x + n);
k[i] = x[1] - x[0];
for (j = 2; j < n; j++) {
a = x[j] - x[j - 1];
if (a < k[i]) k[i] = a;
}
}
for (i = 0; i < t; i++) {
cout << k[i] << endl;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long inf = LLONG_MAX;
long double pi = 3.1415926536;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int tt = 1;
cin >> tt;
while (tt--) {
long long n, x = 0, m = 0, y = 0, k = inf;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (long long i = 1; i < n; i++) k = min(k, a[i] - a[i - 1]);
cout << k << '\n';
}
}
| ### Prompt
Please create a solution in Cpp to the following problem:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long inf = LLONG_MAX;
long double pi = 3.1415926536;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int tt = 1;
cin >> tt;
while (tt--) {
long long n, x = 0, m = 0, y = 0, k = inf;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (long long i = 1; i < n; i++) k = min(k, a[i] - a[i - 1]);
cout << k << '\n';
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
int t, n[maxn], s[maxn], difference[maxn];
int main() {
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n[i];
difference[i] = 10000;
for (int j = 0; j < n[i]; j++) {
cin >> s[j];
}
sort(s, s + n[i]);
for (int j = 1; j < n[i]; j++) {
difference[i] = min(difference[i], (s[j] - s[j - 1]));
}
}
for (int i = 0; i < t; i++) {
cout << difference[i] << endl;
}
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
int t, n[maxn], s[maxn], difference[maxn];
int main() {
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n[i];
difference[i] = 10000;
for (int j = 0; j < n[i]; j++) {
cin >> s[j];
}
sort(s, s + n[i]);
for (int j = 1; j < n[i]; j++) {
difference[i] = min(difference[i], (s[j] - s[j - 1]));
}
}
for (int i = 0; i < t; i++) {
cout << difference[i] << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n, temp, c = 0, k = INT_MAX;
vector<int> v;
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> temp;
v.push_back(temp);
}
sort(v.begin(), v.end());
for (int i = 0; i < n - 1; i++) {
if (v[i + 1] - v[i] < k)
k = v[i + 1] - v[i];
else if (v[i] == v[i + 1]) {
c = 1;
break;
}
}
if (c)
cout << "0"
<< "\n";
else
cout << k << "\n";
v.clear();
c = 0;
k = INT_MAX;
}
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n, temp, c = 0, k = INT_MAX;
vector<int> v;
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> temp;
v.push_back(temp);
}
sort(v.begin(), v.end());
for (int i = 0; i < n - 1; i++) {
if (v[i + 1] - v[i] < k)
k = v[i + 1] - v[i];
else if (v[i] == v[i + 1]) {
c = 1;
break;
}
}
if (c)
cout << "0"
<< "\n";
else
cout << k << "\n";
v.clear();
c = 0;
k = INT_MAX;
}
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
bool func(vector<long long int> s1, vector<long long int> s2) {
return s1[1] < s2[1];
}
string sub(string s1, long long int i, long long int l) {
string s2 = "";
for (long long int j = i; j <= l; j++) {
s2 = s2 + s1[j];
}
return s2;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int max3(long long int a, long long int b, long long int c) {
return max(max(a, b), c);
}
long long int nCr(long long int n, long long int r) {
long long int x = n - r, y = 0, z = 1;
while (y < r) {
z = (z * (x + 1)) / (y + 1);
x++;
y++;
}
return z;
}
long long int gcd(long long int A, long long int B) {
if (B != 0) {
return gcd(B, A % B);
} else {
return A;
}
}
void prime_sieve(long long int *p, long long int n) {
for (long long int i = 3; i <= n; i += 2) {
p[i] = 1;
}
for (long long int i = 3; i <= n; i += 2) {
if (p[i] == 1) {
for (long long int j = i * i; j <= n; j = i + j) {
p[j] = 0;
}
}
}
p[2] = 1;
p[0] = p[1] = 0;
}
long long powers(long long int a, long long int b) {
long long int ans = 1;
while (b > 0) {
if (b & 1) {
ans = ans * a;
}
a = a * a;
b >>= 1;
}
return ans;
}
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, min = INT_MAX;
cin >> n;
long long int arr[n];
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
for (long long int i = 0; i < n - 1; i++) {
if (arr[i + 1] - arr[i] < min) {
min = arr[i + 1] - arr[i];
}
}
cout << min << endl;
}
}
| ### Prompt
In cpp, your task is to solve the following problem:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool func(vector<long long int> s1, vector<long long int> s2) {
return s1[1] < s2[1];
}
string sub(string s1, long long int i, long long int l) {
string s2 = "";
for (long long int j = i; j <= l; j++) {
s2 = s2 + s1[j];
}
return s2;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int max3(long long int a, long long int b, long long int c) {
return max(max(a, b), c);
}
long long int nCr(long long int n, long long int r) {
long long int x = n - r, y = 0, z = 1;
while (y < r) {
z = (z * (x + 1)) / (y + 1);
x++;
y++;
}
return z;
}
long long int gcd(long long int A, long long int B) {
if (B != 0) {
return gcd(B, A % B);
} else {
return A;
}
}
void prime_sieve(long long int *p, long long int n) {
for (long long int i = 3; i <= n; i += 2) {
p[i] = 1;
}
for (long long int i = 3; i <= n; i += 2) {
if (p[i] == 1) {
for (long long int j = i * i; j <= n; j = i + j) {
p[j] = 0;
}
}
}
p[2] = 1;
p[0] = p[1] = 0;
}
long long powers(long long int a, long long int b) {
long long int ans = 1;
while (b > 0) {
if (b & 1) {
ans = ans * a;
}
a = a * a;
b >>= 1;
}
return ans;
}
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, min = INT_MAX;
cin >> n;
long long int arr[n];
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
for (long long int i = 0; i < n - 1; i++) {
if (arr[i + 1] - arr[i] < min) {
min = arr[i + 1] - arr[i];
}
}
cout << min << endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename X>
inline X sqr(const X& a) {
return a * a;
}
int nxt() {
int x;
cin >> x;
return x;
}
void solve() {
int n = nxt();
vector<int> a(n);
generate(a.begin(), a.end(), nxt);
sort(a.begin(), a.end());
int ans = a[1] - a[0];
for (int i = 0; i < int(n - 1); ++i) {
ans = min(ans, a[i + 1] - a[i]);
}
cout << ans << '\n';
}
int main() {
int t = nxt();
while (t--) {
solve();
}
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete β the athlete number i has the strength s_i.
You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.
You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A) - min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.
For example, if n=5 and the strength of the athletes is s=[3, 1, 2, 6, 4], then one of the possible split into teams is:
* first team: A = [1, 2, 4],
* second team: B = [3, 6].
In this case, the value |max(A) - min(B)| will be equal to |4-3|=1. This example illustrates one of the ways of optimal split into two teams.
Print the minimum value |max(A) - min(B)|.
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases in the input. Then t test cases follow.
Each test case consists of two lines.
The first line contains positive integer n (2 β€ n β€ 50) β number of athletes.
The second line contains n positive integers s_1, s_2, β¦, s_n (1 β€ s_i β€ 1000), where s_i β is the strength of the i-th athlete. Please note that s values may not be distinct.
Output
For each test case print one integer β the minimum value of |max(A) - min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.
Example
Input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
Output
1
0
2
999
50
Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2, 1], B=[3, 2, 4, 3], so the answer is |2-2|=0.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename X>
inline X sqr(const X& a) {
return a * a;
}
int nxt() {
int x;
cin >> x;
return x;
}
void solve() {
int n = nxt();
vector<int> a(n);
generate(a.begin(), a.end(), nxt);
sort(a.begin(), a.end());
int ans = a[1] - a[0];
for (int i = 0; i < int(n - 1); ++i) {
ans = min(ans, a[i + 1] - a[i]);
}
cout << ans << '\n';
}
int main() {
int t = nxt();
while (t--) {
solve();
}
return 0;
}
``` |
Subsets and Splits