Coprime Skip Playlist

medium linked-list queue simulation number-theory 1000 ms · 256 MB

Coprime Skip Playlist

A playlist holds n songs numbered 1 to n; song i has a genre code a_i. The player plays the songs in order 1, 2, …, n and then wraps around to song 1 again, forever, always skipping songs that have been deleted.

The player remembers the previous song: the one played right before the current one. Initially nothing is remembered. Every time a song c is played:

  • if a previous song p is remembered and gcd(a_p, a_c) = 1, song c is deleted from the playlist and the memory is cleared, so the next song played has no previous song to compare with;
  • otherwise song c becomes the remembered previous song.

When only one song is left, it is played over and over, so it is compared with itself: it gets deleted exactly when its code is 1.

At some point no deletion can ever happen again (possibly because the playlist is empty). List the deleted songs in the order they were deleted.

Input

The first line contains an integer n (1 ≤ n ≤ 2·10^5).

The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9).

Output

On the first line print k, the number of deleted songs. On the second line print the k indices of the deleted songs in the order they were deleted, separated by spaces (leave the line empty if k = 0).

Notes

In the first example the player plays song 1 (code 2), then song 2 (code 3): the codes are coprime, so song 2 is deleted and the memory is cleared. Song 3 (code 5) is therefore not compared with anything, even though it is coprime with song 1. Then song 4 (code 4) is coprime with song 3 and is deleted. Next song 1 is played with nothing remembered, followed by song 3, which is now coprime with song 1 and gets deleted. Song 1 is then compared with itself (gcd(2, 2) = 2) and survives forever.

In the second example the last remaining song has code 1, so it is deleted when it is compared with itself.

In the third example every pair of neighbours shares a factor, so nothing is deleted.

Example 1

Input

4
2 3 5 4

Output

3
2 4 3

Example 2

Input

3
1 1 1

Output

3
2 1 3

Example 3

Input

3
6 10 15

Output

0

Hints · reveal one at a time if you're stuck

Hint 1

Simulating the player song by song is too slow, because a song can survive many laps. Only a pair of songs that are next to each other can ever trigger a deletion, so which pairs can cause one, and what does a deletion change about the neighbours?

Hint 2

A pair that is not coprime never becomes coprime later, since its two songs stay put. A deletion creates just one new adjacent pair, so you only need to check the coprime candidates, but the order matters: the reset after a deletion decides when each pair is actually compared.

Hint 3

Pending checks happen in lap order, and a song whose neighbour was just deleted only meets its new neighbour one lap later, after every check already waiting. That is exactly a queue, with the circle kept as a linked list.

report an issue Prefer your own editor? sooks prep 13 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.