Problem S5: The Filter
2023 · S5 · Original problem · DMOJProblem 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:



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 and creating three intervals of equal length.
The four marked points are labelled , , , and .
The filter covers one of the intervals: between and (2nd interval).
The second filter has eight points marked between and creating nine intervals of equal length.
The ten marked points are labelled , , , , , , , , , and .
The filter covers three of the intervals: between and (2nd interval), between and (5th interval), and between and (eighth interval).
The third filter has twenty-six points marked between and 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 -th filter can be defined as follows:
-
Consider the number line from 0 to 1.
-
Split this number line into equal-sized pieces. There are points and intervals.
-
The -th filter consists of the 2nd interval, 5th interval, 8th interval, and in general, the th interval. The points are not part of the -th filter.
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 , Alice would like to know which fractions are in the Cantor set.
Input Specification
The first line contains the integer .
The following table shows how the available 15 marks are distributed.
| Marks | Bounds on | Additional Constraints |
|---|---|---|
| 3 marks | is a power of | |
| 4 marks | None | |
| 8 marks | None |
Output Specification
Output all integers where and is in the Cantor set.
Output the answers in increasing order. The number of answers will not exceed .
Sample Input
12
Output for Sample Input
0
1
3
4
8
9
11
12Explanation of Output for Sample Input
Here is a diagram of the fractions and the first 4 filters. In reality, there are infinitely many filters.

Hide/Reveal Description of Diagram for S5 Explanation
Thirteen equidistant points, , , , , , , , , , , , , and , 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.
- The first filter covers one interval: between and .
- The second filter covers three intervals that are each one-third the length of the interval covered by the first filter. They are as follows:
- the first interval starts at a point one-third of the way from to and ends at a point two-thirds of the way from to ,
- the second interval starts at a point one-third of the way from to and ends at a point two-thirds of the way from to , and
- the third interval starts at a point one-third of the way from to and ends at a point two-thirds of the way from to .
- The third filter covers nine intervals that are each one-third the length of the intervals covered by the second filter.
- The fourth filter covers twenty-seven intervals that are each one-third the length of the intervals covered by the third filter.
, , and are not in the Cantor set because they were covered by the filter.
Furthermore, and are not in the Cantor set because they were covered by the filter.
It can be shown that the remaining fractions will pass through all filters.