CF1766D.Lucky Chains

普及/提高-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Let's name a pair of positive integers (x,y)(x, y) lucky if the greatest common divisor of them is equal to 11 (gcd⁡(x,y)=1\gcd(x, y) = 1).

Let's define a chain induced by (x,y)(x, y) as a sequence of pairs (x,y)(x, y), (x+1,y+1)(x + 1, y + 1), (x+2,y+2)(x + 2, y + 2), …\dots, (x+k,y+k)(x + k, y + k) for some integer k≥0k \ge 0. The length of the chain is the number of pairs it consists of, or (k+1)(k + 1).

Let's name such chain lucky if all pairs in the chain are lucky.

You are given nn pairs (xi,yi)(x_i, y_i). Calculate for each pair the length of the longest lucky chain induced by this pair. Note that if (xi,yi)(x_i, y_i) is not lucky itself, the chain will have the length 00.

我们称一对正整数 (x,y)(x, y) 为“幸运的”,当且仅当它们的最大公约数为 11(即 gcd⁡(x,y)=1\gcd(x, y) = 1)。

我们定义由 (x,y)(x, y) 诱导出的一条链为如下形式的数对序列:(x,y)(x, y),(x+1,y+1)(x + 1, y + 1),(x+2,y+2)(x + 2, y + 2),…\dots,(x+k,y+k)(x + k, y + k),其中 k≥0k \ge 0 为某个整数。该链的长度为其所含数对的个数,即 (k+1)(k + 1)。

若一条链中所有数对均为幸运的,则称该链为“幸运链”。

现给你 nn 对数 (xi,yi)(x_i, y_i)。对每一对 (xi,yi)(x_i, y_i),请计算由其诱导出的最长幸运链的长度。注意:若 (xi,yi)(x_i, y_i) 本身不是幸运的,则对应链的长度为 00。

输入格式

The first line contains a single integer nn (1≤n≤1061 \le n \le 10^6) — the number of pairs.

Next nn lines contains nn pairs — one per line. The ii-th line contains two integers xix_i and yiy_i (1≤xi<yi≤1071 \le x_i \lt y_i \le 10^7) — the corresponding pair.

第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6)—— 表示数对的个数。

接下来的 nn 行包含 nn 个数对,每行一个数对。第 ii 行包含两个整数 xix_i 和 yiy_i(1≤xi<yi≤1071 \le x_i \lt y_i \le 10^7)—— 对应的数对。

输出格式

Print nn integers, where the ii-th integer is the length of the longest lucky chain induced by (xi,yi)(x_i, y_i) or −1-1 if the chain can be infinitely long.

输出 nn 个整数,其中第 ii 个整数为由 (xi,yi)(x_i, y_i) 诱导的最长幸运链的长度;若该链可无限长,则输出 −1-1。

输入输出样例

  • 输入#1

    4
    5 15
    13 37
    8 9
    10009 20000

    输出#1

    0
    1
    -1
    79

说明/提示

In the first test case, gcd⁡(5,15)=5>1\gcd(5, 15) = 5 \gt 1, so it's already not lucky, so the length of the lucky chain is 00.

In the second test case, gcd⁡(13+1,37+1)=gcd⁡(14,38)=2\gcd(13 + 1, 37 + 1) = \gcd(14, 38) = 2. So, the lucky chain consists of the single pair (13,37)(13, 37).

在第一个测试用例中,gcd⁡(5,15)=5>1\gcd(5, 15) = 5 \gt 1,因此它已经不是幸运的,故幸运链的长度为 00。

在第二个测试用例中,gcd⁡(13+1,37+1)=gcd⁡(14,38)=2\gcd(13 + 1, 37 + 1) = \gcd(14, 38) = 2。因此,幸运链仅包含单个数对 (13,37)(13, 37)。

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

首页