CF1553G.Common Divisor Graph
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Consider a sequence of distinct integers a1,…,an , each representing one node of a graph. There is an edge between two nodes if the two values are not coprime, i. e. they have a common divisor greater than 1 .
There are q queries, in each query, you want to get from one given node as to another at . In order to achieve that, you can choose an existing value ai and create new value an+1=ai⋅(1+ai) , with edges to all values that are not coprime with an+1 . Also, n gets increased by 1 . You can repeat that operation multiple times, possibly making the sequence much longer and getting huge or repeated values. What's the minimum possible number of newly created nodes so that at is reachable from as ?
Queries are independent. In each query, you start with the initial sequence a given in the input.
输入格式
The first line contains two integers n and q ( 2≤n≤150000 , 1≤q≤300000 ) — the size of the sequence and the number of queries.
The second line contains n distinct integers a1,a2,…,an ( 2≤ai≤106 , ai=aj if i=j ).
The j -th of the following q lines contains two distinct integers sj and tj ( 1≤sj,tj≤n , sj=tj ) — indices of nodes for j -th query.
输出格式
Print q lines. The j -th line should contain one integer: the minimum number of new nodes you create in order to move from asj to atj .
输入输出样例
输入#1
3 3 2 10 3 1 2 1 3 2 3
输出#1
0 1 1
输入#2
5 12 3 8 7 6 25 1 2 1 3 1 4 1 5 2 1 2 3 2 4 2 5 3 1 3 2 3 4 3 5
输出#2
0 1 0 1 0 1 0 1 1 1 1 2
说明/提示
In the first example, you can first create new value 2⋅3=6 or 10⋅11=110 or 3⋅4=12 . None of that is needed in the first query because you can already get from a1=2 to a2=10 .
In the second query, it's optimal to first create 6 or 12 . For example, creating 6 makes it possible to get from a1=2 to a3=3 with a path (2,6,3) .
In the last query of the second example, we want to get from a3=7 to a5=25 . One way to achieve that is to first create 6⋅7=42 and then create 25⋅26=650 . The final graph has seven nodes and it contains a path from a3=7 to a5=25 .