B. Raspberries

B. Raspberries

You are given an array of integers $a_1, a_2, \ldots, a_n$ and a number $k$ ($2 \leq k \leq 5$). In one operation, you can do the following:

  • Choose an index $1 \leq i \leq n$,
  • Set $a_i = a_i + 1$.

Find the minimum number of operations needed to make the product of all the numbers in the array $a_1 \cdot a_2 \cdot \ldots \cdot a_n$ divisible by $k$.

Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers $n$ and $k$ ($2 \leq n \leq 10^5$, $2 \leq k \leq 5$) — the size of the array $a$ and the number $k$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10$).

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

Output

For each test case, output the minimum number of operations needed to make the product of all the numbers in the array divisible by $k$.

Example

input

15
2 5
7 3
3 3
7 4 1
5 2
9 7 7 3 9
5 5
5 4 1 2 3
7 4
9 5 1 5 9 5 1
3 4
6 3 6
3 4
6 1 5
3 4
1 5 9
4 4
1 4 1 1
3 4
3 5 3
4 5
8 9 9 3
2 5
1 6
2 5
10 10
4 5
1 6 1 1
2 5
7 7

output

2
2
1
0
2
0
1
2
0
1
1
4
0
4
3

Note

In the first test case, we need to choose the index $i = 2$ twice. After that, the array will be $a = [7, 5]$. The product of all the numbers in the array is $35$.

In the fourth test case, the product of the numbers in the array is $120$, which is already divisible by $5$, so no operations are needed.

In the eighth test case, we can perform two operations by choosing $i = 2$ and $i = 3$ in any order. After that, the array will be $a = [1, 6, 10]$. The product of the numbers in the array is $60$.

 

解题思路

  昨天两场掉大分,好似喵。这题卡了我快 $1$ 个小时,心态直接炸了。即便后面还是坚持做出来 $4$ 题排名还是基本垫底。

  $k$ 很小直接分类讨论,当时逻辑很乱都不知道在想什么。

  当 $k = 2, \, 3, \, 5$,那么只要数组中有一个数是 $k$ 的倍数,答案就是 $0$。否则考虑将一个数加成 $k$ 的倍数为止。很明显只对一个数进行操作是最优解,因为我们只需让这些数中有一个是 $k$ 的倍数即可。对于某个数 $x$,若要将其变成 $k$ 的倍数那么最少需要操作 $k - 1 - ((x - 1) \bmod k)$ 次。

  当 $k = 4$,与上面的方法相同,不过还要考虑另外一种情况。因为 $4 = 2^2$,因此如果数组中存在至少两个偶数,那么答案是 $0$(当然只有一个 $8$ 也可以,不过这种情况已经用上面的方法判掉了)。否则我们可以通过一次操作将一个奇数变偶数,因此最多选择两个奇数进行操作。假设数组中有 $c$ 个偶数,那么就要执行 $\max \{ 0, 2-c \}$ 次操作。注意,要特判 $n=1$ 的情况。

  AC 代码如下,时间复杂度为 $O(n)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

void solve() {
    int n, k;
    scanf("%d %d", &n, &k);
    int ret = 1e9, c = 0;
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        ret = min(ret, k - 1 - (x - 1) % k);
        if (~x & 1) c++;
    }
    if (k == 4 && n > 1) ret = min(ret, max(0, 2 - c));
    printf("%d\n", ret);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round #905 (Div. 1, Div. 2, Div. 3) Editorial:https://codeforces.com/blog/entry/121621

posted @ 2023-10-23 19:34  onlyblues  阅读(14)  评论(0编辑  收藏  举报
Web Analytics