CF2137E.Mexification

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array aa of size nn and an integer kk. You do the following procedure kk times:

  • For each element aia_i, you set aia_i to mex⁡\operatorname{mex}∗^{\text{∗}}(a1,a2,…,ai−1,ai+1,ai+2,…,an)(a_1,a_2,\ldots,a_{i-1},a_{i+1},a_{i+2}, \ldots,a_n). In other words, you set aia_i to the mex⁡\operatorname{mex} of all other elements in the array. This is done for all elements in the array at the same time.

Please find the sum of elements in the array after all kk operations.

∗^{\text{∗}}The minimum excluded (MEX) of a collection of integers d1,d2,…,dkd_1, d_2, \ldots, d_k is defined as the smallest non-negative integer xx which does not occur in the collection dd.

给你一个大小为 nn 的数组 aa 和一个整数 kk。你执行以下操作 kk 次:

  • 对于每个元素 aia_i,将其设置为 mex⁡\operatorname{mex}∗^{\text{∗}}(a1,a2,…,ai−1,ai+1,ai+2,…,an)(a_1,a_2,\ldots,a_{i-1},a_{i+1},a_{i+2}, \ldots,a_n)。换言之,将 aia_i 设置为数组中其余所有元素的 mex⁡\operatorname{mex}。该操作对数组中所有元素同时进行。

请计算经过全部 kk 次操作后,数组中所有元素的和。

∗^{\text{∗}} 一组整数 d1,d2,…,dkd_1, d_2, \ldots, d_k 的最小未出现值(MEX) 定义为未在该集合 dd 中出现的最小非负整数 xx。

输入格式

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 contains two integers nn and kk (2≤n≤2⋅105,1≤k≤1092 \leq n \leq 2\cdot 10^5, 1 \leq k \leq 10^9) – the number of elements in aa and the number of operations done.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (0≤ai≤n0 \leq a_i \leq n).

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(2≤n≤2⋅105, 1≤k≤1092 \leq n \leq 2\cdot 10^5,\ 1 \leq k \leq 10^9)——分别表示数组 aa 的元素个数以及执行的操作次数。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(0≤ai≤n0 \leq a_i \leq n)。

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

输出格式

For each test case, output the sum of elements after all kk operations on a new line.

对于每个测试用例,在一行中输出执行所有 kk 次操作后的元素之和。

输入输出样例

  • 输入#1

    5
    3 3
    0 2 1
    2 4
    0 2
    4 1
    0 0 1 1
    8 7
    6 6 2 4 3 0 1 8
    2 2
    0 0

    输出#1

    3
    1
    8
    25
    0

说明/提示

In the first test case, we performed the operation on the array [0,2,1][0,2,1] three times. Let's compute the result after the first time:

  • The first element becomes MEX⁡(2,1)=0\operatorname{MEX}(2,1)=0
  • The second element becomes MEX⁡(0,1)=2\operatorname{MEX}(0,1)=2
  • The third element becomes MEX⁡(0,2)=1\operatorname{MEX}(0,2)=1

So, after the first operation, [0,2,1][0,2,1] becomes [0,2,1][0,2,1] again. It can be shown that the array will not change no matter how many times we perform the operation, so the final array after three operations is still [0,2,1][0,2,1]. The sum is 0+2+1=30+2+1=3.

In the third test case, the array becomes [2,2,2,2][2,2,2,2].

在第一个测试用例中,我们对数组 [0,2,1][0,2,1] 执行了三次操作。我们来计算第一次操作后的结果:

  • 第一个元素变为 MEX⁡(2,1)=0\operatorname{MEX}(2,1)=0;
  • 第二个元素变为 MEX⁡(0,1)=2\operatorname{MEX}(0,1)=2;
  • 第三个元素变为 MEX⁡(0,2)=1\operatorname{MEX}(0,2)=1。

因此,第一次操作后,[0,2,1][0,2,1] 仍为 [0,2,1][0,2,1]。可以证明,无论执行多少次该操作,数组均不会发生变化,故经过三次操作后的最终数组仍是 [0,2,1][0,2,1]。其元素和为 0+2+1=30+2+1=3。

在第三个测试用例中,数组变为 [2,2,2,2][2,2,2,2]。

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

首页