#P1876. Toggle the Lights
Toggle the Lights
Toggle the Lights
Initially, all the lights are off. Then, person \(1\) comes and turns on every light (i.e. all lights whose indices are multiples of \(1\)). Next, person \(2\) comes and turns off every light whose index is a multiple of \(2\). For each person \(i\) from \(3\) to \(N\), they toggle the state of every light whose index is a multiple of \(i\) (if it is off, it becomes on; if it is on, it becomes off). After all \(N\) persons have performed their operations, your task is to determine which lights are on.
The operations can be summarized as follows:
- For \(i = 1\): Set every light to on.
- For \(i = 2\): Set every light with index divisible by \(2\) to off.
- For \(i \ge 3\): For every light with index divisible by \(i\), toggle its state.
Given an integer \(N\), representing both the number of lights (numbered from \(1\) to \(N\)) and the number of persons, output the indices of the lights that are on after all operations, in increasing order, separated by a space.
inputFormat
The input consists of a single integer \(N\) (\(1 \le N \le 10^4\)) representing the number of lights and persons.
outputFormat
Output the indices of all lights that remain on after \(N\) rounds, separated by a space. If only one light is on, output its index.
sample
5
1 4