A15660 | Lucky Chains
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Let's name a pair of positive integers $(x, y)$ lucky if the greatest common divisor of them is equal to $1$ ( $\gcd(x, y) = 1$ ).
Let's define a chain induced by $(x, y)$ as a sequence of pairs $(x, y)$ , $(x + 1, y + 1)$ , $(x + 2, y + 2)$ , $\dots$ , $(x + k, y + k)$ for some integer $k \ge 0$ . The length of the chain is the number of pairs it consists of, or $(k + 1)$ .
Let's name such chain lucky if all pairs in the chain are lucky.
You are given $n$ pairs $(x_i, y_i)$ . Calculate for each pair the length of the longest lucky chain induced by this pair. Note that if $(x_i, y_i)$ is not lucky itself, the chain will have the length $0$ .
Let's define a chain induced by $(x, y)$ as a sequence of pairs $(x, y)$ , $(x + 1, y + 1)$ , $(x + 2, y + 2)$ , $\dots$ , $(x + k, y + k)$ for some integer $k \ge 0$ . The length of the chain is the number of pairs it consists of, or $(k + 1)$ .
Let's name such chain lucky if all pairs in the chain are lucky.
You are given $n$ pairs $(x_i, y_i)$ . Calculate for each pair the length of the longest lucky chain induced by this pair. Note that if $(x_i, y_i)$ is not lucky itself, the chain will have the length $0$ .
输入格式
The first line contains a single integer $n$ ( $1 \le n \le 10^6$ ) — the number of pairs.
Next $n$ lines contains $n$ pairs — one per line. The $i$ -th line contains two integers $x_i$ and $y_i$ ( $1 \le x_i < y_i \le 10^7$ ) — the corresponding pair.
Next $n$ lines contains $n$ pairs — one per line. The $i$ -th line contains two integers $x_i$ and $y_i$ ( $1 \le x_i < y_i \le 10^7$ ) — the corresponding pair.
输出格式
Print $n$ integers, where the $i$ -th integer is the length of the longest lucky chain induced by $(x_i, y_i)$ or $-1$ if the chain can be infinitely long.
输入输出样例
输入 #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$ , so it's already not lucky, so the length of the lucky chain is $0$ .
In the second test case, $\gcd(13 + 1, 37 + 1) = \gcd(14, 38) = 2$ . So, the lucky chain consists of the single pair $(13, 37)$ .
In the second test case, $\gcd(13 + 1, 37 + 1) = \gcd(14, 38) = 2$ . So, the lucky chain consists of the single pair $(13, 37)$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted