#GL0000361. Những chú nhện thân thiện (Friendly Spiders)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Những chú nhện thân thiện (Friendly Spiders)
Friendly Spiders
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are spiders. Spider has legs. Two distinct spiders can transmit a message directly if and only if . Each direct transmission takes one second.
For indices , find any shortest path from spider to spider , or report that no path exists. A path is a sequence of distinct spider indices. Multiple optimal paths may exist: output any one.
Input
- Line 1: .
- Line 2: .
- Line 3: .
Output
Print -1 if unreachable. Otherwise print the minimum number of spiders on the first line and indices on the second line, with , and each adjacent pair sharing a GCD greater than 1.
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): .
- Subtask 3 (50 points): $2 <= n <= 300000; 1 <= a_i <= 300000; 1 <= s,t <= n$.
Special judge: output any shortest valid route; paths are verified by the checker, not compared to a single exact route.
Examples
Example 1
Input
7
2 14 9 6 8 15 11
5 6
Output
3
5 4 6
Explanation
The path 5 -> 4 -> 6 uses gcd(8,6)=2 and gcd(6,15)=3, so it needs two moves. No direct edge exists.
Example 2
Input
7
2 14 9 6 8 15 11
5 7
Output
-1
Explanation
Spider 7 has 11 legs and shares no factor greater than 1 with any other spider.
Example 3
Input
7
2 14 9 6 8 15 11
5 5
Output
1
5
Explanation
The sender and receiver are the same spider, so the route has one vertex.