CF1909D.Split Plus K

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

eliteLAQ - Desert Ruins

⠀

There are nn positive integers a1,a2,…,ana_1, a_2, \dots, a_n on a blackboard. You are also given a positive integer kk. You can perform the following operation some (possibly 00) times:

  • choose a number xx on the blackboard;
  • erase one occurrence of xx;
  • write two positive integers yy, zz such that y+z=x+ky+z = x+k on the blackboard.

Is it possible to make all the numbers on the blackboard equal? If yes, what is the minimum number of operations you need?

eliteLAQ - 沙漠遗迹

⠀

黑板上有 nn 个正整数 a1,a2,…,ana_1, a_2, \dots, a_n。同时给定一个正整数 kk。你可以执行以下操作若干次(可以为 00 次):

  • 选择黑板上的一个数 xx;
  • 擦除该数 xx 的一次出现;
  • 在黑板上写下两个正整数 yy、zz,满足 y+z=x+ky+z = x+k。

是否可能使黑板上所有数字都相等?如果可以,最少需要多少次操作?

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn, kk (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, 1≤k≤10121 \leq k \leq 10^{12}) — the number of integers initially on the blackboard and the constant kk.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤10121 \le a_i \le 10^{12}) — the initial state of the blackboard.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn、kk(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5,1≤k≤10121 \leq k \leq 10^{12})——分别表示黑板上初始的整数个数和常数 kk。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤10121 \le a_i \le 10^{12})——表示黑板的初始状态。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single line containing an integer: the minimum number of operations you need to make all the numbers on the blackboard equal, or −1-1 if it is impossible.

对于每个测试用例,输出一行包含一个整数:使黑板上所有数字相等所需的最少操作次数;如果不可能实现,则输出 −1-1。

输入输出样例

  • 输入#1

    9
    2 1
    3 4
    2 3
    7 11
    3 10
    100 40 100
    2 1
    1 2
    2 2
    1 2
    1 327869541
    327869541
    5 26250314066
    439986238782 581370817372 409476934981 287439719777 737637983182
    5 616753575719
    321037808624 222034505841 214063039282 441536506916 464097941819
    5 431813672576
    393004301966 405902283416 900951084746 672201172466 518769038906

    输出#1

    3
    1
    4
    -1
    -1
    0
    3119
    28999960732
    -1

说明/提示

In the first test case, k=1k = 1. You can make all the numbers on the blackboard equal to 22 with the following operations:

  • Erase x=4x = 4 and write (y,z)=(2,3)(y, z) = (2, 3). Note that y+z=x+ky+z=x+k. The blackboard now contains the multiset 3,2,3{3, 2, 3}.
  • Erase x=3x = 3 and write (y,z)=(2,2)(y, z) = (2, 2). Note that y+z=x+ky+z=x+k. The blackboard now contains 2,2,2,3{2, 2, 2, 3}.
  • Erase x=3x = 3 and write (y,z)=(2,2)(y, z) = (2, 2). Note that y+z=x+ky+z=x+k. The blackboard now contains 2,2,2,2,2{2, 2, 2, 2, 2}.

This makes all the numbers equal in 33 operations. It can be shown that you cannot make all the numbers equal in less than 33 operations.

In the second test case, k=3k = 3. You can make all the numbers on the blackboard equal to 77 with the following operation:

  • Erase x=11x = 11 and write (y,z)=(7,7)(y, z) = (7, 7). Note that y+z=x+ky+z=x+k. The blackboard now contains 7,7,7{7, 7, 7}.

In the third test case, k=10k = 10. You can make all the numbers on the blackboard equal to 4040 with the following operations:

  • Erase x=100x = 100 and write (y,z)=(70,40)(y, z) = (70, 40). Note that y+z=x+ky+z=x+k. The blackboard now contains 70,40,40,100{70, 40, 40, 100}.
  • Erase x=70x = 70 and write (y,z)=(40,40)(y, z) = (40, 40). Note that y+z=x+ky+z=x+k. The blackboard now contains 40,40,40,40,100{40, 40, 40, 40, 100}.
  • Erase x=100x = 100 and write (y,z)=(40,70)(y, z) = (40, 70). Note that y+z=x+ky+z=x+k. The blackboard now contains 40,40,40,40,40,70{40, 40, 40, 40, 40, 70}.
  • Erase x=70x = 70 and write (y,z)=(40,40)(y, z) = (40, 40). Note that y+z=x+ky+z=x+k. The blackboard now contains 40,40,40,40,40,40,40{40, 40, 40, 40, 40, 40, 40}.

In the fourth and in the fifth test case, you can show that it is impossible to make all the numbers on the blackboard equal.

在第一个测试用例中,k=1k = 1。你可以通过以下操作使黑板上的所有数字都变为 22:

  • 擦除 x=4x = 4,写入 (y,z)=(2,3)(y, z) = (2, 3)。注意 y+z=x+ky+z = x + k。此时黑板上的多重集为 {3,2,3}\{3, 2, 3\}。
  • 擦除 x=3x = 3,写入 (y,z)=(2,2)(y, z) = (2, 2)。注意 y+z=x+ky+z = x + k。此时黑板上的多重集为 {2,2,2,3}\{2, 2, 2, 3\}。
  • 擦除 x=3x = 3,写入 (y,z)=(2,2)(y, z) = (2, 2)。注意 y+z=x+ky+z = x + k。此时黑板上的多重集为 {2,2,2,2,2}\{2, 2, 2, 2, 2\}。

这共需 33 次操作使得所有数字相等。可以证明,无法用少于 33 次操作使所有数字相等。

在第二个测试用例中,k=3k = 3。你可以通过以下操作使黑板上的所有数字都变为 77:

  • 擦除 x=11x = 11,写入 (y,z)=(7,7)(y, z) = (7, 7)。注意 y+z=x+ky+z = x + k。此时黑板上的多重集为 {7,7,7}\{7, 7, 7\}。

在第三个测试用例中,k=10k = 10。你可以通过以下操作使黑板上的所有数字都变为 4040:

  • 擦除 x=100x = 100,写入 (y,z)=(70,40)(y, z) = (70, 40)。注意 y+z=x+ky+z = x + k。此时黑板上的多重集为 {70,40,40,100}\{70, 40, 40, 100\}。
  • 擦除 x=70x = 70,写入 (y,z)=(40,40)(y, z) = (40, 40)。注意 y+z=x+ky+z = x + k。此时黑板上的多重集为 {40,40,40,40,100}\{40, 40, 40, 40, 100\}。
  • 擦除 x=100x = 100,写入 (y,z)=(40,70)(y, z) = (40, 70)。注意 y+z=x+ky+z = x + k。此时黑板上的多重集为 {40,40,40,40,40,70}\{40, 40, 40, 40, 40, 70\}。
  • 擦除 x=70x = 70,写入 (y,z)=(40,40)(y, z) = (40, 40)。注意 y+z=x+ky+z = x + k。此时黑板上的多重集为 {40,40,40,40,40,40,40}\{40, 40, 40, 40, 40, 40, 40\}。

在第四个和第五个测试用例中,可以证明:无法使黑板上的所有数字相等。

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

首页