A15124 | Euclid Guess
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Let's consider Euclid's algorithm for finding the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor), where $t$ is a list:
```
<pre class="verbatim"><br></br>function Euclid(a, b):<br></br> if a < b:<br></br> swap(a, b)<br></br><br></br> if b == 0:<br></br> return a<br></br><br></br> r = reminder from dividing a by b<br></br> if r > 0:<br></br> append r to the back of t<br></br><br></br> return Euclid(b, r)<br></br>
```
There is an array $p$ of pairs of positive integers that are not greater than $m$ . Initially, the list $t$ is empty. Then the function is run on each pair in $p$ . After that the list $t$ is shuffled and given to you.
You have to find an array $p$ of any size not greater than $2 \cdot 10^4$ that produces the given list $t$ , or tell that no such array exists.
```
<pre class="verbatim"><br></br>function Euclid(a, b):<br></br> if a < b:<br></br> swap(a, b)<br></br><br></br> if b == 0:<br></br> return a<br></br><br></br> r = reminder from dividing a by b<br></br> if r > 0:<br></br> append r to the back of t<br></br><br></br> return Euclid(b, r)<br></br>
```
There is an array $p$ of pairs of positive integers that are not greater than $m$ . Initially, the list $t$ is empty. Then the function is run on each pair in $p$ . After that the list $t$ is shuffled and given to you.
You have to find an array $p$ of any size not greater than $2 \cdot 10^4$ that produces the given list $t$ , or tell that no such array exists.
输入格式
The first line contains two integers $n$ and $m$ ( $1 \le n \le 10^3$ , $1 \le m \le 10^9$ ) — the length of the array $t$ and the constraint for integers in pairs.
The second line contains $n$ integers $t_1, t_2, \ldots, t_n$ ( $1 \le t_i \le m$ ) — the elements of the array $t$ .
The second line contains $n$ integers $t_1, t_2, \ldots, t_n$ ( $1 \le t_i \le m$ ) — the elements of the array $t$ .
输出格式
- If the answer does not exist, output $-1$ .
- If the answer exists, in the first line output $k$ ( $1 \le k \le 2 \cdot 10^4$ ) — the size of your array $p$ , i. e. the number of pairs in the answer. The $i$ -th of the next $k$ lines should contain two integers $a_i$ and $b_i$ ( $1 \le a_i, b_i \le m$ ) — the $i$ -th pair in $p$ .
If there are multiple valid answers you can output any of them.
- If the answer exists, in the first line output $k$ ( $1 \le k \le 2 \cdot 10^4$ ) — the size of your array $p$ , i. e. the number of pairs in the answer. The $i$ -th of the next $k$ lines should contain two integers $a_i$ and $b_i$ ( $1 \le a_i, b_i \le m$ ) — the $i$ -th pair in $p$ .
If there are multiple valid answers you can output any of them.
输入输出样例
输入 #1
7 20 1 8 1 6 3 2 3
输出 #1
3 19 11 15 9 3 7
输入 #2
2 10 7 1
输出 #2
-1
输入 #3
2 15 1 7
输出 #3
1 15 8
输入 #4
1 1000000000 845063470
输出 #4
-1
In the first sample let's consider the array $t$ for each pair:
- $(19,\, 11)$ : $t = [8, 3, 2, 1]$ ;
- $(15,\, 9)$ : $t = [6, 3]$ ;
- $(3,\, 7)$ : $t = [1]$ .
So in total $t = [8, 3, 2, 1, 6, 3, 1]$ , which is the same as the input $t$ (up to a permutation).
In the second test case it is impossible to find such array $p$ of pairs that all integers are not greater than $10$ and $t = [7, 1]$
In the third test case for the pair $(15,\, 8)$ array $t$ will be $[7, 1]$ .
- $(19,\, 11)$ : $t = [8, 3, 2, 1]$ ;
- $(15,\, 9)$ : $t = [6, 3]$ ;
- $(3,\, 7)$ : $t = [1]$ .
So in total $t = [8, 3, 2, 1, 6, 3, 1]$ , which is the same as the input $t$ (up to a permutation).
In the second test case it is impossible to find such array $p$ of pairs that all integers are not greater than $10$ and $t = [7, 1]$
In the third test case for the pair $(15,\, 8)$ array $t$ will be $[7, 1]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted