Strange order

4.6

5 votes
Mathematics, Approved, Easy-Medium, Factorization
Problem

Given an integer n. Initially you have permutation p of size n: pi=i. To build new array a from p following steps are done while permutation p is not empty:

  1. Choose maximum existing element in p and define it as x;
  2. Choose all such y in p that gcd(x,y)1;
  3. Add all chosen numbers into array a in decreasing order and remove them from permutation.

Your task is to find a after p became empty.

Note: gcd(a,b) is the greatest common divisor of integers a and b.

Input format

Input contains single integer n (1n2106) — the size of permutation p.

Output format

Print n integers — array a.

Time Limit: 2
Memory Limit: 512
Source Limit:
Explanation

It's needed 4 iterations to create array a:

  1. Add 5
  2. Add 4 and 2
  3. Add 3
  4. Add 1
Editor Image

?