A14150 | Playlist
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Arkady has a playlist that initially consists of $n$ songs, numerated from $1$ to $n$ in the order they appear in the playlist. Arkady starts listening to the songs in the playlist one by one, starting from song $1$ . The playlist is cycled, i. e. after listening to the last song, Arkady will continue listening from the beginning.
Each song has a genre $a_i$ , which is a positive integer. Let Arkady finish listening to a song with genre $y$ , and the genre of the next-to-last listened song be $x$ . If $\operatorname{gcd}(x, y) = 1$ , he deletes the last listened song (with genre $y$ ) from the playlist. After that he continues listening normally, skipping the deleted songs, and forgetting about songs he listened to before. In other words, after he deletes a song, he can't delete the next song immediately.
Here $\operatorname{gcd}(x, y)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$ .
For example, if the initial songs' genres were $[5, 9, 2, 10, 15]$ , then the playlist is converted as follows: \[5, 9, 2, 10, 15\] $\to$ \[5, 9, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] (because $\operatorname{gcd}(5, 9) = 1$ ) $\to$ \[5, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] $\to$ \[5, 10, 15\] (because $\operatorname{gcd}(5, 2) = 1$ ) $\to$ \[5, 10, 15\] $\to$ \[5, 10, 15\] $\to$ ... The bold numbers represent the two last played songs. Note that after a song is deleted, Arkady forgets that he listened to that and the previous songs.
Given the initial playlist, please determine which songs are eventually deleted and the order these songs are deleted.
Each song has a genre $a_i$ , which is a positive integer. Let Arkady finish listening to a song with genre $y$ , and the genre of the next-to-last listened song be $x$ . If $\operatorname{gcd}(x, y) = 1$ , he deletes the last listened song (with genre $y$ ) from the playlist. After that he continues listening normally, skipping the deleted songs, and forgetting about songs he listened to before. In other words, after he deletes a song, he can't delete the next song immediately.
Here $\operatorname{gcd}(x, y)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$ .
For example, if the initial songs' genres were $[5, 9, 2, 10, 15]$ , then the playlist is converted as follows: \[5, 9, 2, 10, 15\] $\to$ \[5, 9, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] (because $\operatorname{gcd}(5, 9) = 1$ ) $\to$ \[5, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] $\to$ \[5, 2, 10, 15\] $\to$ \[5, 10, 15\] (because $\operatorname{gcd}(5, 2) = 1$ ) $\to$ \[5, 10, 15\] $\to$ \[5, 10, 15\] $\to$ ... The bold numbers represent the two last played songs. Note that after a song is deleted, Arkady forgets that he listened to that and the previous songs.
Given the initial playlist, please determine which songs are eventually deleted and the order these songs are deleted.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \le t \le 10\,000$ ). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the number of songs.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le 10^9$ ) — the genres of the songs.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the number of songs.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le 10^9$ ) — the genres of the songs.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ .
输出格式
For each test case, print a single line. First, print a single integer $k$ — the number of deleted songs. After that print $k$ distinct integers: deleted songs in the order of their deletion.
输入输出样例
输入 #1
5 5 5 9 2 10 15 6 1 2 4 2 4 2 2 1 2 1 1 1 2
输出 #1
2 2 3 2 2 1 2 2 1 1 1 0
Explanation of the first test case is given in the statement.
In the second test case, the playlist is converted as follows: \[1, 2, 4, 2, 4, 2\] $\to$ \[1, 2, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] (because $\operatorname{gcd}(1, 2) = 1$ ) $\to$ \[1, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] $\to$ \[4, 2, 4, 2\] (because $\operatorname{gcd}(2, 1) = 1$ ) $\to$ \[4, 2, 4, 2\] $\to$ ...
In the third test case, the playlist is converted as follows: \[1, 2\] $\to$ \[1, 2\] $\to$ \[1\] (because $\operatorname{gcd}(1, 2) = 1$ ) $\to$ \[1\] $\to$ \[1\] (Arkady listened to the same song twice in a row) $\to$ \[\] (because $\operatorname{gcd}(1, 1) = 1$ ).
The fourth test case is same as the third after deletion of the second song.
In the fifth test case, the same song is listened to over and over again, but since $\operatorname{gcd}(2, 2) \ne 1$ , it is not deleted.
In the second test case, the playlist is converted as follows: \[1, 2, 4, 2, 4, 2\] $\to$ \[1, 2, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] (because $\operatorname{gcd}(1, 2) = 1$ ) $\to$ \[1, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] $\to$ \[1, 4, 2, 4, 2\] $\to$ \[4, 2, 4, 2\] (because $\operatorname{gcd}(2, 1) = 1$ ) $\to$ \[4, 2, 4, 2\] $\to$ ...
In the third test case, the playlist is converted as follows: \[1, 2\] $\to$ \[1, 2\] $\to$ \[1\] (because $\operatorname{gcd}(1, 2) = 1$ ) $\to$ \[1\] $\to$ \[1\] (Arkady listened to the same song twice in a row) $\to$ \[\] (because $\operatorname{gcd}(1, 1) = 1$ ).
The fourth test case is same as the third after deletion of the second song.
In the fifth test case, the same song is listened to over and over again, but since $\operatorname{gcd}(2, 2) \ne 1$ , it is not deleted.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted