CF2231F.Quadratic Jumps

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two integers nn and qq. Consider a graph with nn vertices, where vertices ii and jj are connected by an edge if and only if ∣j−i∣|j - i| is a perfect square∗^{\text{∗}}.

You are given qq pairs of numbers ai,bia_i, b_i. For each of these qq pairs, you need to find the shortest distance between vertices aia_i and bib_i in this graph. It can be proved that the graph is connected, so the distance between aia_i and bib_i is not infinite.

∗^{\text{∗}}An integer xx is a perfect square if there exists an integer yy such that x=y2x = y^2.

给你两个整数 nn 和 qq。考虑一个包含 nn 个顶点的图,其中顶点 ii 与顶点 jj 之间存在一条边,当且仅当 ∣j−i∣|j - i| 是一个完全平方数∗^{\text{∗}}。

你将得到 qq 对数字 ai,bia_i, b_i。对于这 qq 对中的每一对,你需要求出该图中顶点 aia_i 与 bib_i 之间的最短距离。可以证明该图是连通的,因此 aia_i 与 bib_i 之间的距离是有限的。

∗^{\text{∗}} 整数 xx 是一个完全平方数,当且仅当存在整数 yy,使得 x=y2x = y^2。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001 \le t \le 1000). The description of the test cases follows.

The first line of each test case contains two integers nn and qq (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5, 1≤q≤1051 \le q \le 10^5) — the number of vertices in the graph and the number of pairs of vertices for which the distance must be found.

Then the next qq lines describe the pairs of vertices for which the shortest distance must be found. Each pair is described by two numbers a,ba, b (1≤a<b≤n1 \leq a \lt b \leq n) — the numbers of the vertices between which the shortest distance must be found.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5, and the sum of qq over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10001 \le t \le 1000)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 qq(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5,1≤q≤1051 \le q \le 10^5)—— 分别表示图中顶点的数量以及需要查询最短距离的顶点对数量。

接下来的 qq 行描述需要查询最短距离的顶点对。每一对由两个数字 a,ba, b(1≤a<b≤n1 \leq a \lt b \leq n)描述——即需要查询最短距离的两个顶点的编号。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5,且所有测试用例的 qq 之和不超过 10510^5。

输出格式

For each test case, output the shortest distance between each of the qq pairs of vertices.

对于每个测试用例,输出 qq 对顶点之间各自的最短距离。

输入输出样例

  • 输入#1

    2
    5 4
    1 2
    1 3
    1 4
    1 5
    8 3
    3 7
    2 5
    1 7

    输出#1

    1
    2
    2
    1
    1
    2
    3

说明/提示

This is what the graph looks like for the first test case:

  • For the first pair of vertices, the shortest path is 1→21 \rightarrow 2.
  • For the second pair of vertices, the shortest path is 1→2→31 \rightarrow 2 \rightarrow 3.
  • For the third pair of vertices, the shortest path is 1→5→41 \rightarrow 5 \rightarrow 4.
  • For the fourth pair of vertices, the shortest path is 1→51 \rightarrow 5.

This is what the graph looks like for the second test case:

  • For the first pair of vertices, the shortest path is 1→21 \rightarrow 2.
  • For the second pair of vertices, the shortest path is 2→6→52 \rightarrow 6 \rightarrow 5.
  • For the third pair of vertices, the shortest path is 1→2→6→71 \rightarrow 2 \rightarrow 6 \rightarrow 7.

第一个测试用例对应的图如下所示:

  • 对于第一对顶点,最短路径为 1→21 \rightarrow 2。
  • 对于第二对顶点,最短路径为 1→2→31 \rightarrow 2 \rightarrow 3。
  • 对于第三对顶点,最短路径为 1→5→41 \rightarrow 5 \rightarrow 4。
  • 对于第四对顶点,最短路径为 1→51 \rightarrow 5。

第二个测试用例对应的图如下所示:

  • 对于第一对顶点,最短路径为 1→21 \rightarrow 2。
  • 对于第二对顶点,最短路径为 2→6→52 \rightarrow 6 \rightarrow 5。
  • 对于第三对顶点,最短路径为 1→2→6→71 \rightarrow 2 \rightarrow 6 \rightarrow 7。

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

首页