CCCGuide

Problem S5: The Filter

2023 · S5 · Original problem · DMOJ

Problem Description

Alice, the mathematician, likes to study real numbers that are between 0 and 1. Her favourite tool is the filter.

A filter covers part of the number line. When a number reaches a filter, two events can happen. If a number is not covered by the filter, the number will pass through. If a number is covered, the number will be removed.

Alice has infinitely many filters. Her first 3 filters look like this:

A description of the diagram follows.

Hide/Reveal Description of Diagram for S5

All three filters are on a number line from 0 to 1.

The first filter has two additional points marked between 00 and 11 creating three intervals of equal length.
The four marked points are labelled 03\frac{0}{3}, 13\frac{1}{3}, 23\frac{2}{3}, and 33\frac{3}{3}.
The filter covers one of the intervals: between 13\frac{1}{3} and 23\frac{2}{3} (2nd interval).

The second filter has eight points marked between 00 and 11 creating nine intervals of equal length.
The ten marked points are labelled 09\frac{0}{9}, 19\frac{1}{9}, 29\frac{2}{9}, 39\frac{3}{9}, 49\frac{4}{9}, 59\frac{5}{9}, 69\frac{6}{9}, 79\frac{7}{9}, 89\frac{8}{9}, and 99\frac{9}{9}.
The filter covers three of the intervals: between 19\frac{1}{9} and 29\frac{2}{9} (2nd interval), between 49\frac{4}{9} and 59\frac{5}{9} (5th interval), and between 79\frac{7}{9} and 89\frac{8}{9} (eighth interval).

The third filter has twenty-six points marked between 00 and 11 creating twenty-seven intervals of equal length.
The twenty-eight marked points are not labelled.
The filter covers nine of the intervals: 2nd, 5th, 8th, 11th, 14th, 17th, 20th, 23rd, and 26th intervals.


In general, the kk-th filter can be defined as follows:

Alice has instructions for constructing the Cantor set. Start with the number line from 0 to 1. Apply all filters on the number line, and remove the numbers that are covered. The remaining numbers form the Cantor set.

Alice wants to research the Cantor set, and she came to you for help. Given an integer NN, Alice would like to know which fractions xN\frac{x}{N} are in the Cantor set.

Input Specification

The first line contains the integer NN.

The following table shows how the available 15 marks are distributed.

MarksBounds on NNAdditional Constraints
3 marks3≤N≤3183 \le N \le 3^{18}NN is a power of 33
4 marks2≤N≤1052 \le N \le 10^5None
8 marks2≤N≤1092 \le N \le 10^9None

Output Specification

Output all integers xx where 0≤x≤N0 \le x \le N and xN\frac{x}{N} is in the Cantor set.

Output the answers in increasing order. The number of answers will not exceed 10610^6.

Sample Input

12

Output for Sample Input

0
1
3
4
8
9
11
12

Explanation of Output for Sample Input

Here is a diagram of the fractions and the first 4 filters. In reality, there are infinitely many filters.

A description of the diagram follows.

Hide/Reveal Description of Diagram for S5 Explanation

Thirteen equidistant points, 012\frac{0}{12}, 112\frac{1}{12}, 212\frac{2}{12}, 312\frac{3}{12}, 412\frac{4}{12}, 512\frac{5}{12}, 612\frac{6}{12}, 712\frac{7}{12}, 812\frac{8}{12}, 912\frac{9}{12}, 1012\frac{10}{12}, 1212\frac{12}{12}, and 1112\frac{11}{12}, are in a line at the top of the digram. A dashed vertical line is drawn down from each of these of these points. The filters are placed along four different horizontal lines, passing across these lines.


512\dfrac{5}{12}, 612\dfrac{6}{12}, and 712\dfrac{7}{12} are not in the Cantor set because they were covered by the 1st1^\text{st} filter.

Furthermore, 212\dfrac{2}{12} and 1012\dfrac{10}{12} are not in the Cantor set because they were covered by the 2nd2^\text{nd} filter.

It can be shown that the remaining fractions will pass through all filters.