Problem:
Score : 700 points

Problem Statement

Takahashi is playing with N cards.

The i-th card has an integer X_i on it.

Takahashi is trying to create as many pairs of cards as possible satisfying one of the following conditions:

The integers on the two cards are the same.

The sum of the integers on the two cards is a multiple of M.

Find the maximum number of pairs that can be created.

Note that a card cannot be used in more than one pair.

Constraints

2≦N≦10^5

1≦M≦10^5

1≦X_i≦10^5

Input

The input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_N

Output

Print the maximum number of pairs that can be created.

Sample Input 1

7 5
3 1 4 1 5 9 2

Sample Output 1

3

Three pairs (3,2), (1,4) and (1,9) can be created.

It is possible to create pairs (3,2) and (1,1), but the number of pairs is not maximized with this.

Sample Input 2

15 10
1 5 6 10 11 11 11 20 21 25 25 26 99 99 99

Sample Output 2

6

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
int MIN(int a,int b){return a<b?a:b;}
//添字ヒープを使うための関数達!
int R=1,C=1,H[2000010],N[2000010];
//評価関数（いまはMAX）
int hyouka(int a,int b){
  if(C<b)return 1;
  if(C<a||b==0)return 0;
  return N[H[a]]>N[H[b]]?1:0;
}
//挿入関数
void hin(int a){
  int i=C++;
  for(N[H[0]=R]=a;hyouka(0,i/2);i/=2)H[i]=H[i/2];
  H[i]=R++;
}
//取り出す関数
int hout(){
  int rt=H[1],i,j=2,k=H[--C];
  for(i=1;hyouka(i,C);i=j)H[i]=H[j=i*2+1-hyouka(i*2,i*2+1)];
  H[j/2]=k;
  return rt;
}
int n,m;
int main(){
  int i,s,a,b,c[100010]={};
  int p[100010]={};
  scanf("%d %d",&n,&m);
  for(i=0;i<n;i++){
    scanf("%d",&a);
    hin(a);
  }
  b=-1;
  for(i=0;i<n;i++){
    a=N[hout()];
    if(b==a){
      p[a%m]++;
      b=-1;
    }
    else b=a;
    c[a%m]++;
  }
  //for(i=0;i<m;i++)printf("%d %d %d\n",i,c[i],p[i]);
  s=c[0]/2;
  if(m%2==0)s+=c[m/2]/2;
  for(i=1;i*2<m;i++){
    if(c[i]>c[(m-i)%m]){
      //printf("%d %d %d %d\n",c[i],c[m-i],p[i],p[m-i]);
      a=MIN((c[i]-c[m-i])/2,p[i]);
      c[i]-=a*2;
    }
    else{
      a=MIN((c[m-i]-c[i])/2,p[m-i]);
      c[m-i]-=a*2;
    }
    s+=MIN(c[i],c[m-i]);//printf("%d ",s);
    s+=a;//printf("%d\n",s);
  }
  printf("%d\n",s);
  return 0;
}