# Codeforces Round #653 (Div. 3) D. Zero Remainder Array

You are given an array a consisting of n positive integers.

Initially, you have an integer x=0. During one move, you can do one of the following two operations:

1. Choose exactly one i from 1 to n and increase ai by x (ai:=ai+x), then increase x by 1 (x:=x+1).
2. Just increase x by 1 (x:=x+1).

The first operation can be applied no more than once to each i from 1 to n.

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).

You have to answer t independent test cases.

## Input

The first line of the input contains one integer t (1≤t≤2e4) — the number of test cases. Then t test cases follow.

The first line of the test case contains two integers n and k (1≤n≤2e5;1≤k≤1e9) — the length of a and the required divisior. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤1e9), where ai is the i-th element of a.

It is guaranteed that the sum of n does not exceed 2e5 (∑n≤2e5).

## Output

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.

## Example

### input

5
4 3
1 2 1 3
10 6
8 7 1 8 3 7 5 10 8 9
5 10
20 100 50 20 100500
10 25
24 24 24 24 24 24 24 24 24 24
8 8
1 2 3 4 5 6 7 8


### output

6
18
0
227
8


1.因为最后答案要加一，若原始数组所有数模 $k$ 都已为 0，此时答案就为 0，所以 $ans$ 初始值设为 -1
2.会爆 int
AC代码如下：

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

int main(){
int n,t,k;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
ll x,ans=-1;
map<ll,ll>m;
for(int i=0;i<n;i++){
scanf("%lld",&x);
if(x%k) ans=max(ans,k-x%k+k*m[k-x%k]++);
}
printf("%lld\n",ans+1);
}
}


#### 完整的人工智能学习路径，学习AI的请收藏好

07-10

06-29 104

#### Codeforces Round #653 (Div. 3) A、B、C、D、E1题解 06-29 74

#### Codeforces Round #653 (Div. 3)(A~E)

06-29 94

#### Codeforces Round #653 (Div. 3) D. Zero Remainder Array(思维)

06-29 3957

#### D. Zero Remainder Array(循环,模拟)

06-29 245

#### Codeforces Round #653 (Div. 3)A-B-C-D-E1

10-04 725

#### Given an array of non-negative integers, you are initially positioned at the first index of the arra

07-06 61

#### D. Zero Remainder Array

06-29 117

#### Codeforces Round #653 (Div. 3)部分题解

05-14 170

#### Codeforces Round #640 (Div. 4)题解

08-09 785

#### Codeforces Round #295 (Div. 1) B. Cubes（最大最小堆+拓扑模拟）

04-17 1052

#### Codeforces Round #240 (Div. 1) 前3题 解题报告

10-13 627

#### codeforces Round #272(div2) C解题报告

02-09 161

#### Codeforces Round #175 (Div. 2) A~D 题解

06-19 74

#### Codeforces Round #650 (Div. 3) ABC

11-07 1393

#### Python可以这样学（第一季：Python内功修炼）

02-23 ©️2020 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客 点击重新获取   扫码支付 1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。 余额充值