CF BUDDY
← Problems·

1991D · Prime XOR Coloring

1900 · bitmasks, constructive algorithms, graphs

Problem: You are given an undirected graph with nn vertices, numbered from 11 to nn. There is an edge between vertices uu and vv if and only if uvu \oplus v is a prime number, where \oplus denotes the bitwise XOR operator.

Color all vertices of the graph using the minimum number of colors, such that no two vertices directly connected by an edge have the same color.

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t5001 \le t \le 500). The description of test cases follows.

The only line contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of vertices in the graph.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output two lines.

The first line should contain a single integer kk (1kn1 \le k \le n) — the minimum number of colors required.

The second line should contain nn integers c1,c2,,cnc_1, c_2, \ldots, c_n (1cik1 \le c_i \le k) — the color of each vertex.

If there are multiple solutions, output any of them.

Note: In the first test case, the minimum number of colors is 11, because there is only one vertex.

In the second test case, the minimum number of colors is 22, because there is an edge connecting 11 and 22 (12=31 \oplus 2 = 3, which is a prime number).

In the third test case, the minimum number of colors is still 22, because 22 and 33 can be colored the same since there is no edge between 22 and 33 (23=12 \oplus 3 = 1, which is not a prime number).

In the fourth test case, it can be shown that the minimum number of colors is 33.

In the fifth test case, it can be shown that the minimum number of colors is 33.

In the sixth test case, it can be shown that the minimum number of colors is 44.

Sample Cases

Case 1

Input

6
1
2
3
4
5
6

Output

1
1
2
1 2
2
1 2 2
3
1 2 2 3
3
1 2 2 3 3
4
1 2 2 3 3 4

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback