You are given an array a1,a2,…,an consisting of n positive integers and a positive integer m.
You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want.
Let’s call an array m-divisible if for each two adjacent numbers in the array (two numbers on the positions i and i+1 are called adjacent for each i) their sum is divisible by m. An array of one element is m-divisible.
Find the smallest number of m-divisible arrays that a1,a2,…,an is possible to divide into.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains two integers n, m (1≤n≤105,1≤m≤105).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109).
It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 105.
Output
For each test case print the answer to the problem.
Example Input
4
6 4
2 2 8 6 9 4
10 8
1 1 1 5 2 4 4 8 6 7
1 1
666
2 2
2 4
Output
3
6
1
1
Note
In the first test case we can divide the elements as follows:
[4,8]. It is a 4-divisible array because 4+8 is divisible by 4.
[2,6,2]. It is a 4-divisible array because 2+6 and 6+2 are divisible by 4.
[9]. It is a 4-divisible array because it consists of one element.
题意:一个n,一个m。接下来给你n个数,让你从这n个数中分出若干个数组,保证数组中相邻两个数(i、i+1)的和%m=0。如果数组中只有一个数也认为%m=0。新的数组里面的数的顺序可以改变。
问:分的新的数组的个数最少是多少。
题解
:用余数做。用一个book数组记录余数的个数,我们知道n%m取余,余数为0~m-1。
1、对于余数为0的所有数放到一个数组里面。
2、把剩下的余数从1到m/2(防止重复)开始遍历,将余数为x和余数为m-x进行结合。即x m-x x m-x…这样可以保证相邻两个数的和%m=0。
3、如果book[x]==book[m-x],即:x m-x x m-x这种情况,分为一组。
4、对于 x m-x x 或 x m-x x x 或 x m-x m-x m-x 分成fabs(book[x]-book[m-x])组。
AC代码
:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
int book[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,x;
int num=0;
memset(book,0,sizeof(book));
scanf("%d%d",&n,&m);
int i,j,k;
for(i=1; i<=n; i++)
{
scanf("%d",&x);
book[x%m]++;
}
if(book[0]!=0)
num++;
for(i=1; i<=m/2; i++)
{
if(book[i]&&book[i]==book[m-i])
num++;
else
num+=fabs(book[i]-book[m-i]);
}
printf("%d\n",num);
}
return 0;
}