CF1684G.Euclid Guess

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Let's consider Euclid's algorithm for finding the greatest common divisor, where tt is a list:

function Euclid(a, b):
if a < b:
swap(a, b)

if b == 0:
return a

r = reminder from dividing a by b
if r > 0:
append r to the back of t

return Euclid(b, r)

There is an array pp of pairs of positive integers that are not greater than mm. Initially, the list tt is empty. Then the function is run on each pair in pp. After that the list tt is shuffled and given to you.

You have to find an array pp of any size not greater than 2⋅1042 \cdot 10^4 that produces the given list tt, or tell that no such array exists.

我们来考虑用于求最大公约数的欧几里得算法,其中 tt 是一个列表:

函数 Euclid(a, b):
若 a < b:
交换 a 和 b

若 b == 0:
返回 a

r = a 除以 b 的余数
若 r > 0:
将 r 追加到列表 t 的末尾

返回 Euclid(b, r)

给定一个由若干正整数对构成的数组 pp,每对中的两个数均不超过 mm。初始时列表 tt 为空。随后,对 pp 中的每一对数依次调用上述函数 Euclid。调用全部结束后,列表 tt 被随机打乱并交予你。

你需要构造一个长度不超过 2⋅1042 \cdot 10^4 的数组 pp(其中每个元素为一对正整数),使得按上述过程运行后恰好生成(打乱前的)给定列表 tt;若不存在这样的数组 pp,则需指出无解。

输入格式

The first line contains two integers nn and mm (1≤n≤1031 \le n \le 10^3, 1≤m≤1091 \le m \le 10^9) — the length of the array tt and the constraint for integers in pairs.

The second line contains nn integers t1,t2,…,tnt_1, t_2, \ldots, t_n (1≤ti≤m1 \le t_i \le m) — the elements of the array tt.

第一行包含两个整数 nn 和 mm(1≤n≤1031 \le n \le 10^3,1≤m≤1091 \le m \le 10^9)—— 分别表示数组 tt 的长度以及数对中整数的约束条件。

第二行包含 nn 个整数 t1,t2,…,tnt_1, t_2, \ldots, t_n(1≤ti≤m1 \le t_i \le m)—— 表示数组 tt 的元素。

输出格式

  • If the answer does not exist, output −1-1.

  • If the answer exists, in the first line output kk (1≤k≤2⋅1041 \le k \le 2 \cdot 10^4) — the size of your array pp, i. e. the number of pairs in the answer.

    The ii-th of the next kk lines should contain two integers aia_i and bib_i (1≤ai,bi≤m1 \le a_i, b_i \le m) — the ii-th pair in pp.

If there are multiple valid answers you can output any of them.

  • 如果答案不存在,输出 −1-1。

  • 如果答案存在,在第一行输出 kk(1≤k≤2⋅1041 \le k \le 2 \cdot 10^4)—— 即你构造的数组 pp 的大小,也就是答案中数对的个数。

    接下来的 kk 行中,第 ii 行应包含两个整数 aia_i 和 bib_i(1≤ai,bi≤m1 \le a_i, b_i \le m)—— 即 pp 中的第 ii 个数对。

若存在多个合法答案,输出任意一个即可。

输入输出样例

  • 输入#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 tt for each pair:

  • (19, 11)(19,\, 11): t=[8,3,2,1]t = [8, 3, 2, 1];
  • (15, 9)(15,\, 9): t=[6,3]t = [6, 3];
  • (3, 7)(3,\, 7): t=[1]t = [1].

So in total t=[8,3,2,1,6,3,1]t = [8, 3, 2, 1, 6, 3, 1], which is the same as the input tt (up to a permutation).

In the second test case it is impossible to find such array pp of pairs that all integers are not greater than 1010 and t=[7,1]t = [7, 1]

In the third test case for the pair (15, 8)(15,\, 8) array tt will be [7,1][7, 1].

在第一个样例中,我们考虑每一对数对应的数组 tt:

  • (19, 11)(19,\, 11):t=[8,3,2,1]t = [8, 3, 2, 1];
  • (15, 9)(15,\, 9):t=[6,3]t = [6, 3];
  • (3, 7)(3,\, 7):t=[1]t = [1]。

因此,总的 t=[8,3,2,1,6,3,1]t = [8, 3, 2, 1, 6, 3, 1],这与输入的 tt 相同(允许元素顺序不同)。

在第二个测试用例中,无法找到满足条件的数对数组 pp,使得所有整数均不超过 1010,且 t=[7,1]t = [7, 1]。

在第三个测试用例中,对于数对 (15, 8)(15,\, 8),数组 tt 将为 [7,1][7, 1]。

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

首页