Difficulty:1039
Expand

Learn the building blocks of programming languages
Take our free programming courses and learn to solve problems like these.
Start Learning

Small GCD Sort

There are NN players, numbered from 11 to NN.

Player ii's score is defined to be gcd(i,N)\gcd(i, N).
Here, gcd(i,N)\gcd(i, N) refers to the largest positive integer that divides both ii and NN.

Your task is to find an ordering of the players, from left to right, that satisfies the following conditions:

  • Between two players with different scores, the one with a higher score must appear somewhere to the left of the one with a lower score.
  • Between two players with the same score, the player with a smaller number must appear somewhere to the left of the one with a lower score.

It can be proved that these rules uniquely define an ordering of the NN people.
Your task is to find this ordering.

Input Format

  • The first line contains a single integer TT — the number of test cases.
  • Each of the next TT lines contains a single integer NN — the number of players.

Output Format

For each test case, print NN space-separated integers on a new line — the player numbers in the required preference order.

Constraints

  • 1T1001 \leq T \leq 100
  • 1N1001 \leq N \leq 100

Sample 1:

Input
Output
5
1
2
3
4
5
1
2 1
3 1 2
4 2 1 3
5 1 2 3 4

Explanation:

Test case 11: There is only one player.

Test case 22: There are two players.
Player 11 has a score of gcd(1,2)=1\gcd(1, 2) = 1, while player 22 has a score of gcd(2,2)=2\gcd(2, 2) = 2.
Player 22 has a higher score, and so appears to the left of player 11, making the only possible order [2,1][2, 1].

Test case 44: There are four players. It can be verified that players 11 and 33 have a score of 11, player 22 has a score of 22, and player 44 has a score of 44.
So,

  • Player 44 has the highest score, and so must be placed first.
  • Player 22 has the second highest score, and so must be placed second.
  • Players 11 and 33 have the same score.
    Among them, 11 has the smaller number, and so must be placed ahead of 33.

Thus, the final order is [4,2,1,3][4, 2, 1, 3].


More Info
Time limit1 secs
Memory limit1.5 GB
Source Limit50000 Bytes

Author(s)
Tester(s)
Editorialist(s)
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
}
 
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Visualize Code