PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000361. Những chú nhện thân thiện (Friendly Spiders)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Những chú nhện thân thiện (Friendly Spiders)

Friendly Spiders

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn spiders. Spider ii has aia_i legs. Two distinct spiders i,ji,j can transmit a message directly if and only if gcd⁡(ai,aj)>1\gcd(a_i,a_j)>1. Each direct transmission takes one second.

For indices s,ts,t, find any shortest path from spider ss to spider tt, 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: nn.
  • Line 2: a1,a2,…,ana_1,a_2,\ldots,a_n.
  • Line 3: s,ts,t.

Output

Print -1 if unreachable. Otherwise print the minimum number kk of spiders on the first line and kk indices b1,…,bkb_1,\ldots,b_k on the second line, with b1=sb_1=s, bk=tb_k=t and each adjacent pair sharing a GCD greater than 1.

Subtasks

  • Subtask 1 (20 points): 2<=n<=10;1<=ai<=100;1<=s,t<=n2 <= n <= 10; 1 <= a_i <= 100; 1 <= s,t <= n.
  • Subtask 2 (30 points): 2<=n<=5000;1<=ai<=10000;1<=s,t<=n2 <= n <= 5000; 1 <= a_i <= 10000; 1 <= s,t <= n.
  • 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.