Coloring Natural Numbers
Time limit1sMemory limit1024 MB
Assign colors to 1..N so coprime numbers differ, using the minimum possible number of colors, and output the count and a valid coloring.
- Level
Medium7 of 10
- Topics
- Number theory, Math, Greedy, Combinatorics
- Solved
- No attempts yet
Problem
Color the natural numbers from to . Two distinct coprime natural numbers must receive different colors. Write a program that finds a way to color all the numbers using the minimum number of colors.
Input
The first line gives the natural number . ()
Output
On the first line, print the number of colors used, .
On the second line, print numbers separated by spaces. The -th number is the color of the natural number . Colors are integers from to .
Hint
The following are examples of a valid coloring and an invalid coloring for .

The first coloring is a valid coloring. Although and are colored the same, they are not coprime, so the condition of the problem is not violated. One can also prove that at least colors are needed to color the natural numbers up to .
The second coloring is an invalid coloring because the coprime numbers and are colored the same.
The third coloring is an invalid coloring because it does not use the minimum number of colors.