1374D.md (2298B)
1 Source: https://codeforces.com/problemset/problem/1374/D 2 3 D. Zero Remainder Array 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given an array $a$ consisting of $n$ positive integers. 22 23 Initially, you have an integer $x = 0$. During one move, you can do one of the following two operations: 24 25 1. Choose exactly one $i$ from $1$ to $n$ and increase $a_i$ by $x$ ($a_i := a_i + x$), then increase $x$ by $1$ ($x := x + 1$). 26 2. Just increase $x$ by $1$ ($x := x + 1$). 27 28 The first operation can be applied no more than once to each $i$ from $1$ to $n$. 29 30 Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by $k$ (the value $k$ is given). 31 32 You have to answer $t$ independent test cases. 33 34 Input 35 36 The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow. 37 38 The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9$) — the length of $a$ and the required divisior. The second line of the test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the $i$-th element of $a$. 39 40 It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). 41 42 Output 43 44 For each test case, print the answer — the minimum number of moves required to obtain such an array that each its element is divisible by $k$. 45 46 Example 47 48 Input 49 50 ``` 51 5 52 4 3 53 1 2 1 3 54 10 6 55 8 7 1 8 3 7 5 10 8 9 56 5 10 57 20 100 50 20 100500 58 10 25 59 24 24 24 24 24 24 24 24 24 24 60 8 8 61 1 2 3 4 5 6 7 8 62 ``` 63 64 Output 65 66 ``` 67 6 68 18 69 0 70 227 71 8 72 ``` 73 74 Note 75 76 Consider the first test case of the example: 77 78 1. $x=0$, $a = [1, 2, 1, 3]$. Just increase $x$; 79 2. $x=1$, $a = [1, 2, 1, 3]$. Add $x$ to the second element and increase $x$; 80 3. $x=2$, $a = [1, 3, 1, 3]$. Add $x$ to the third element and increase $x$; 81 4. $x=3$, $a = [1, 3, 3, 3]$. Add $x$ to the fourth element and increase $x$; 82 5. $x=4$, $a = [1, 3, 3, 6]$. Just increase $x$; 83 6. $x=5$, $a = [1, 3, 3, 6]$. Add $x$ to the first element and increase $x$; 84 7. $x=6$, $a = [6, 3, 3, 6]$. We obtained the required array. 85 86 Note that you can't add $x$ to the same element more than once.