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

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

这题比较考思维,我们首先对所有数取模,这样所有数都在 [0,k1][0,k-1] 之间,那么我们对数 a(a0)a(a\neq0),必须要加上 ka+mk,mZk-a+m*k,m\in Z,且 mm 即为 kak-a 出现的次数,这样只需对每个数更新一下答案即可,注意最后答案要加 1,因为要算上一开始 x=0x=0 的情况,有两个细节:
1.因为最后答案要加一,若原始数组所有数模 kk 都已为 0,此时答案就为 0,所以 ansans 初始值设为 -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);
    }
}
展开阅读全文
©️2020 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值