Strange order
Practice
4.6 (5 votes)
Mathematics
Approved
Easy Medium
Factorization
Problem
94% Success 3109 Attempts 30 Points 2s Time Limit 512MB Memory 1024 KB Max Code
Given an integer n. Initially you have permutation p of size n: \(p_i = i\). To build new array a from p following steps are done while permutation p is not empty:
- Choose maximum existing element in p and define it as x;
- Choose all such y in p that \(\gcd(x, y) \neq 1\);
- 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 (\(1 \leq n \leq 2 \cdot 10^6\)) — the size of permutation p.
Output format
Print n integers — array a.
Explanation
It's needed 4 iterations to create array a:
- Add 5
- Add 4 and 2
- Add 3
- Add 1
Code Editor
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
2 votes
Tags:
Integer FactorizationMathImplementationMathematicsSieveBasics of ImplementationNumber Theory
Points:30
3 votes
Tags:
MathematicsEasy-MediumShortest path problemFactorization
Points:30
4 votes
Tags:
Number TheoryPrime FactorizationEasy-MediumMathematicsSieveFactorization
Editorial
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Results
Custom Input
Run your code to see the output