Bitwise Triangles

시간 제한1초메모리 제한1024 MB

요약
1부터 n까지의 정수로 이루어진 삼중항 중 임의의 두 수의 비트 AND가 0이 아닌 것들을 최대한 많이, 서로 겹치지 않게 골라 출력한다.
난이도

보통10점 중 7점

유형
그리디, 비트 연산, 수학, 구현
정답자
아직 제출이 없습니다

문제

We define a bitwise triangle to be a triplet of distinct integers (a,,b,,c)(a,\\,b,\\,c) with a&b≠0a\\\&b\neq0, a&c≠0a\\\&c\neq0, and b&c≠0b\\\&c\neq0, where \\& indicates the bitwise AND operator.

You are given an integer nn. We define a triangle packing to be a set of pairwise disjoint bitwise triangles consisting of integers from 11 to nn inclusive. Find a maximal triangle packing (one with the maximum number of bitwise triangles).

입력

The first line of the input contains a single integer tt (1≤t≤1041 \le t \le 10^4) --- the number of test cases. The description of the test cases follows.

Each test case consists of a single line containing one integer nn (1≤n≤2⋅1051 \le n \le 2\cdot 10^5) --- the maximum allowed number in any of the triangles.

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

출력

The first line of output for each test case should contain a single integer kk (0≤k≤n0 \le k \le n) --- the size of the maximal triangle packing.

The next kk lines of output should each contain three distinct integers aa, bb, and cc (1≤a,b,c≤n1 \le a, b, c \le n), representing one triangle of the maximal triangle packing.

If there are multiple solutions, print any.

힌트

In the first test case, we have n=4n=4. It can be shown that we cannot create any bitwise triangles from the set 1,2,3,4\\{1, 2, 3, 4\\}, so the size of the maximal packing is 00.

In the second test case, we have n=5n=5. The given triangle packing is valid since each of the three numbers in the one bitwise triangle are odd, and therefore the bitwise AND of any two of them is nonzero. It can be shown that this packing is maximal for n=5n=5.

예제1

  1. 예제 1

    입력
    6
    4
    5
    6
    10
    18
    14
    
    예상 출력
    0
    1
    1 3 5
    1
    1 3 5
    3
    1 3 5
    8 9 10
    2 6 7
    6
    1 5 15
    6 7 13
    2 3 10
    4 12 14
    8 9 11
    16 17 18
    4
    1 5 11
    8 9 10
    2 3 6
    4 7 12