CF1699E.Three Days Grace

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Ibti was thinking about a good title for this problem that would fit the round theme (numerus ternarium). He immediately thought about the third derivative, but that was pretty lame so he decided to include the best band in the world — Three Days Grace.

You are given a multiset AA with initial size nn, whose elements are integers between 11 and mm. In one operation, do the following:

  • select a value xx from the multiset AA, then
  • select two integers pp and qq such that p,q>1p, q \gt 1 and p⋅q=xp \cdot q = x. Insert pp and qq to AA, delete xx from AA.

Note that the size of the multiset AA increases by 11 after each operation.

We define the balance of the multiset AA as max⁡(ai)−min⁡(ai)\max(a_i) - \min(a_i). Find the minimum possible balance after performing any number (possible zero) of operations.

伊布提正在为这道题构思一个契合本轮主题(“三进制数”)的优秀标题。他立刻想到了“三阶导数”,但这未免太过平庸,于是他决定加入世界上最棒的乐队——Three Days Grace。

给定一个初始大小为 nn 的多重集 AA,其中所有元素均为介于 11 与 mm 之间的整数。每次操作执行如下步骤:

  • 从多重集 AA 中选取一个值 xx;
  • 再选取两个整数 pp 和 qq,满足 p,q>1p, q \gt 1 且 p⋅q=xp \cdot q = x;将 pp 和 qq 插入 AA,并从 AA 中删除 xx。

注意:每次操作后,多重集 AA 的大小增加 11。

我们定义多重集 AA 的平衡值为 max⁡(ai)−min⁡(ai)\max(a_i) - \min(a_i)。求在执行任意次数(包括零次)操作后,所能达到的最小可能平衡值。

输入格式

The first line of the input contains a single integer tt (1≤t≤1051 \le t \le 10^5) — the number of test cases.

The second line of each test case contains two integers nn and mm (1≤n≤1061 \le n \le 10^6, 1≤m≤5⋅1061 \le m \le 5 \cdot 10^6) — the initial size of the multiset, and the maximum value of an element.

The third line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤m1 \le a_i \le m) — the elements in the initial multiset.

It is guaranteed that the sum of nn across all test cases does not exceed 10610^6 and the sum of mm across all test cases does not exceed 5⋅1065 \cdot 10^6.

输入的第一行包含一个整数 tt(1≤t≤1051 \le t \le 10^5),表示测试用例的数量。

每个测试用例的第二行包含两个整数 nn 和 mm(1≤n≤1061 \le n \le 10^6,1≤m≤5⋅1061 \le m \le 5 \cdot 10^6),分别表示多重集的初始大小以及元素的最大值。

每个测试用例的第三行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤m1 \le a_i \le m),表示初始多重集中的元素。

保证所有测试用例中 nn 的总和不超过 10610^6,且所有测试用例中 mm 的总和不超过 5⋅1065 \cdot 10^6。

输出格式

For each test case, print a single integer — the minimum possible balance.

对于每个测试用例,输出一个整数——可能的最小余额。

输入输出样例

  • 输入#1

    4
    5 10
    2 4 2 4 2
    3 50
    12 2 3
    2 40
    6 35
    2 5
    1 5

    输出#1

    0
    1
    2
    4

说明/提示

In the first test case, we can apply the operation on each of the 44s with (p,q)=(2,2)(p,q) = (2,2) and make the multiset 2,2,2,2,2,2,2{2,2,2,2,2,2,2} with balance max⁡(2,2,2,2,2,2,2)−min⁡(2,2,2,2,2,2,2)=0\max({2,2,2,2,2,2,2}) - \min({2,2,2,2,2,2,2}) = 0. It is obvious we cannot make this balance less than 00.

In the second test case, we can apply an operation on 1212 with (p,q)=(3,4)(p,q) = (3,4). After this our multiset will be 3,4,2,3{3,4,2,3}. We can make one more operation on 44 with (p,q)=(2,2)(p,q) = (2,2), making the multiset 3,2,2,2,3{3,2,2,2,3} with balance equal to 11.

In the third test case, we can apply an operation on 3535 with (p,q)=(5,7)(p,q) = (5,7). The final multiset is 6,5,7{6,5,7} and has a balance equal to 7−5=27-5 = 2.

In the forth test case, we cannot apply any operation, so the balance is 5−1=45 - 1 = 4.

在第一个测试用例中,我们可以对每个 44 应用操作 (p,q)=(2,2)(p,q) = (2,2),从而得到多重集 2,2,2,2,2,2,2{2,2,2,2,2,2,2},其平衡值为 max⁡(2,2,2,2,2,2,2)−min⁡(2,2,2,2,2,2,2)=0\max({2,2,2,2,2,2,2}) - \min({2,2,2,2,2,2,2}) = 0。显然,我们无法使该平衡值小于 00。

在第二个测试用例中,我们可以对 1212 应用操作 (p,q)=(3,4)(p,q) = (3,4)。操作后,我们的多重集变为 3,4,2,3{3,4,2,3}。接着,我们可再对 44 应用一次操作 (p,q)=(2,2)(p,q) = (2,2),使得多重集变为 3,2,2,2,3{3,2,2,2,3},其平衡值为 11。

在第三个测试用例中,我们可以对 3535 应用操作 (p,q)=(5,7)(p,q) = (5,7)。最终的多重集为 6,5,7{6,5,7},其平衡值为 7−5=27-5 = 2。

在第四个测试用例中,我们无法执行任何操作,因此平衡值为 5−1=45 - 1 = 4。

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

首页