Farmer John's Favorite Operation

시간 제한2초메모리 제한2048 MB

요약
배열과 정수 M이 주어질 때, 모든 a_i가 M으로 나눈 나머지가 x와 같아지도록 만드는 최소 연산 횟수를 구한다.
난이도

보통10점 중 7점

유형
정렬, 누적 합, 수학, 이분 탐색
정답자
아직 제출이 없습니다

문제

It is another cold and boring day on Farmer John's farm. To pass the time, Farmer John has invented a fun leisure activity involving performing operations on an integer array.

Farmer John has an array aa of NN (1≤N≤2⋅1051 \leq N \leq 2 \cdot 10^5) non-negative integers and an integer MM (1≤M≤1091 \leq M \leq 10^9). Then, FJ will ask Bessie for an integer xx. In one operation, FJ can pick an index ii and subtract or add 11 to a_ia\_i. FJ's boredom value is the minimum number of operations he must perform so that a_i−xa\_i-x is divisible by MM for all 1≤i≤N1 \leq i \leq N.

Among all possible xx, output FJ's minimum possible boredom value.

입력

The first line contains TT (1≤T≤101 \leq T \leq 10), the number of independent test cases to solve.

The first line of each test case contains NN and MM.

The second line of each test case contains a_1,a_2,...,a_Na\_1, a\_2, ..., a\_N (0≤a_i≤1090 \leq a\_i \leq 10^9).

It is guaranteed that the sum of NN over all test cases does not exceed 5⋅1055 \cdot 10^5.

출력

For each test case, output an integer on a new line containing FJ's minimum possible boredom value among all possible values of xx.

예제1

  1. 예제 1

    입력
    2
    5 9
    15 12 18 3 8
    3 69
    1 988244353 998244853
    
    예상 출력
    10
    21