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 Scala (2.11.7) (judge verdict: Accepted)


import java.util.Scanner

import scala.collection.mutable

/**
 * Created by sonetsuyoshi on 2015/12/16.
 */
object Main extends App {
  val sc = new Scanner(System.in)
  val N = sc.nextInt
  val M = sc.nextInt

  val valCountMap = mutable.Map[Int, Int]().withDefaultValue(0)
  val modValMap = Array.fill(M)(mutable.Set[Int]())
  val modCountMap = Array.fill(M)(0)

  for(i <- 0 until N) {
    val n = sc.nextInt
    val m = n % M
    valCountMap.+=((n, valCountMap(n) + 1))
    modValMap(m).+=(n)
    modCountMap(m) += 1
  }

  var count = modCountMap(0) / 2
  for(i <- 1 to ((M - 1) / 2)) {
    val j = M - i
    val (sId, lId) = if(modCountMap(i) < modCountMap(j)) {
      (i, j)
    } else {
      (j, i)
    }

    val countDif = modCountMap(lId) - modCountMap(sId)
    val maxPairNum = countDif / 2

    val pairNum = modValMap(lId).foldLeft(0){case(s, x) => s + valCountMap(x) / 2}

    count += modCountMap(sId) + math.min(maxPairNum, pairNum)
  }

  if(M % 2 == 0) count += modCountMap(M / 2) / 2

  println(count)
}