C. Yet Another Permutation Problem(Codeforces Round 893 (Div. 2))

Alex got a new game called “GCD permutations” as a birthday present. Each round of this game proceeds as follows:

  • First, Alex chooses a permutation † ^{\dagger} a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an of integers from 1 1 1 to n n n.
  • Then, for each i i i from 1 1 1 to n n n, an integer d i = gcd ⁡ ( a i , a ( i   m o d   n ) + 1 ) d_i = \gcd(a_i, a_{(i \bmod n) + 1}) di=gcd(ai,a(imodn)+1) is calculated.
  • The score of the round is the number of distinct numbers among d 1 , d 2 , … , d n d_1, d_2, \ldots, d_n d1,d2,,dn.

Alex has already played several rounds so he decided to find a permutation a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an such that its score is as large as possible.

Recall that gcd ⁡ ( x , y ) \gcd(x, y) gcd(x,y) denotes the greatest common divisor (GCD) of numbers x x x and y y y, and x   m o d   y x \bmod y xmody denotes the remainder of dividing x x x by y y y.

† ^{\dagger} A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 2 2 appears twice in the array), and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 n=3 n=3 but there is 4 4 4 in the array).

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

Each test case consists of one line containing a single integer n n n ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2n105).

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.

Output

For each test case print n n n distinct integers a 1 , a 2 , … , a n a_{1},a_{2},\ldots,a_{n} a1,a2,,an ( 1 ≤ a i ≤ n 1 \le a_i \le n 1ain) — the permutation with the largest possible score.

If there are several permutations with the maximum possible score, you can print any one of them.

Example

input
4
5
2
7
10
output
1 2 4 3 5
1 2
1 2 3 6 4 5 7
1 2 3 4 8 5 10 6 9 7

Note

In the first test case, Alex wants to find a permutation of integers from 1 1 1 to 5 5 5. For the permutation a = [ 1 , 2 , 4 , 3 , 5 ] a=[1,2,4,3,5] a=[1,2,4,3,5], the array d d d is equal to [ 1 , 2 , 1 , 1 , 1 ] [1,2,1,1,1] [1,2,1,1,1]. It contains 2 2 2 distinct integers. It can be shown that there is no permutation of length 5 5 5 with a higher score.

In the second test case, Alex wants to find a permutation of integers from 1 1 1 to 2 2 2. There are only two such permutations: a = [ 1 , 2 ] a=[1,2] a=[1,2] and a = [ 2 , 1 ] a=[2,1] a=[2,1]. In both cases, the array d d d is equal to [ 1 , 1 ] [1,1] [1,1], so both permutations are correct.

In the third test case, Alex wants to find a permutation of integers from 1 1 1 to 7 7 7. For the permutation a = [ 1 , 2 , 3 , 6 , 4 , 5 , 7 ] a=[1,2,3,6,4,5,7] a=[1,2,3,6,4,5,7], the array d d d is equal to [ 1 , 1 , 3 , 2 , 1 , 1 , 1 ] [1,1,3,2,1,1,1] [1,1,3,2,1,1,1]. It contains 3 3 3 distinct integers so its score is equal to 3 3 3. It can be shown that there is no permutation of integers from 1 1 1 to 7 7 7 with a score higher than 3 3 3.

解题思路

给一个整数n,需要用1到n这n个数字组成一个整数数组a,再用a数组每相邻两个数的最大公约数组成一个新数组b(最后一个与第一个组),使b中的不同数字数量最多。

代码实现

#include
#include

using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n);
        int cur = 0;
        for (int i = 1; i <= n; i += 2) {
            for (int j = i; j <= n; j *= 2) {
                a[cur++] = j;
            }
        }
        for (int i = 0; i<n; ++i) {
            cout << a[i] << " ";
        }
        cout << '\n';
    }
    return 0;
}

你可能感兴趣的:(cf,算法,codeforces)