CF1553G.Common Divisor Graph

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Consider a sequence of distinct integers a1,…,ana_1, \ldots, a_n, 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 11.

There are qq queries, in each query, you want to get from one given node asa_s to another ata_t. In order to achieve that, you can choose an existing value aia_i and create new value an+1=ai⋅(1+ai)a_{n+1} = a_i \cdot (1 + a_i), with edges to all values that are not coprime with an+1a_{n+1}. Also, nn gets increased by 11. 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 ata_t is reachable from asa_s?

Queries are independent. In each query, you start with the initial sequence aa given in the input.

考虑一个由互不相同的整数 a1,…,ana_1, \ldots, a_n 构成的序列,其中每个整数代表图中的一个节点。当且仅当两个数值不互质(即它们拥有大于 11 的公共约数)时,这两个节点之间存在一条边。

共有 qq 个查询。在每次查询中,你需要从给定的起始节点 asa_s 到达目标节点 ata_t。为实现该目标,你可以选择当前序列中某个已存在的值 aia_i,并创建一个新值 an+1=ai⋅(1+ai)a_{n+1} = a_i \cdot (1 + a_i);该新节点与所有与其不互质的已有值之间均连边。同时,序列长度 nn 增加 11。你可以重复执行该操作多次,从而使得序列显著变长,并可能生成极大或重复的数值。问:使得 ata_t 从 asa_s 可达所需的最少新增节点数目是多少?

各查询相互独立。每次查询均从输入中给出的初始序列 aa 开始。

输入格式

The first line contains two integers nn and qq (2≤n≤150 0002 \leq n \leq 150\,000, 1≤q≤300 0001 \leq q \leq 300\,000) — the size of the sequence and the number of queries.

The second line contains nn distinct integers a1,a2,…,ana_1, a_2, \ldots, a_n (2≤ai≤1062 \leq a_i \leq 10^6, ai≠aja_i \neq a_j if i≠ji \ne j).

The jj-th of the following qq lines contains two distinct integers sjs_j and tjt_j (1≤sj,tj≤n1 \leq s_j, t_j \leq n, sj≠tjs_j \neq t_j) — indices of nodes for jj-th query.

第一行包含两个整数 nn 和 qq(2≤n≤150 0002 \leq n \leq 150\,000,1≤q≤300 0001 \leq q \leq 300\,000)—— 分别表示序列的长度和查询次数。

第二行包含 nn 个互不相同的整数 a1,a2,…,ana_1, a_2, \ldots, a_n(2≤ai≤1062 \leq a_i \leq 10^6,当 i≠ji \neq j 时 ai≠aja_i \neq a_j)。

接下来的 qq 行中,第 jj 行包含两个互不相同的整数 sjs_j 和 tjt_j(1≤sj,tj≤n1 \leq s_j, t_j \leq n,sj≠tjs_j \neq t_j)—— 表示第 jj 次查询所涉及的节点下标。

输出格式

Print qq lines. The jj-th line should contain one integer: the minimum number of new nodes you create in order to move from asja_{s_j} to atja_{t_j}.

输出 qq 行。第 jj 行应包含一个整数:从 asja_{s_j} 移动到 atja_{t_j} 所需创建的新节点的最少数量。

输入输出样例

  • 输入#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=62 \cdot 3 = 6 or 10⋅11=11010 \cdot 11 = 110 or 3⋅4=123 \cdot 4 = 12. None of that is needed in the first query because you can already get from a1=2a_1 = 2 to a2=10a_2 = 10.

In the second query, it's optimal to first create 66 or 1212. For example, creating 66 makes it possible to get from a1=2a_1 = 2 to a3=3a_3 = 3 with a path (2,6,3)(2, 6, 3).

In the last query of the second example, we want to get from a3=7a_3 = 7 to a5=25a_5 = 25. One way to achieve that is to first create 6⋅7=426 \cdot 7 = 42 and then create 25⋅26=65025 \cdot 26 = 650. The final graph has seven nodes and it contains a path from a3=7a_3 = 7 to a5=25a_5 = 25.

在第一个例子中,你可以首先创建新值 2⋅3=62 \cdot 3 = 6、10⋅11=11010 \cdot 11 = 110 或 3⋅4=123 \cdot 4 = 12。但在第一个查询中,这些操作均非必需,因为你已经可以直接从 a1=2a_1 = 2 到达 a2=10a_2 = 10。

在第二个查询中,最优策略是先创建 66 或 1212。例如,创建 66 后,即可通过路径 (2,6,3)(2, 6, 3) 从 a1=2a_1 = 2 到达 a3=3a_3 = 3。

在第二个例子的最后一个查询中,我们需要从 a3=7a_3 = 7 到达 a5=25a_5 = 25。一种可行的方法是:先创建 6⋅7=426 \cdot 7 = 42,再创建 25⋅26=65025 \cdot 26 = 650。最终图包含七个节点,且其中存在一条从 a3=7a_3 = 7 到 a5=25a_5 = 25 的路径。

输入解题思路,AI测评打分。不知道怎么写?

首页