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 Haskell (GHC 7.10.3) (judge verdict: Wrong Answer)

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections     #-}

import           Control.Applicative
import           Control.Monad
import           Control.Monad.Primitive
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Data.Bool
import qualified Data.ByteString.Char8       as B
import qualified Data.ByteString.Unsafe      as B
import           Data.Char
import           Data.Function
import qualified Data.IntMap.Strict          as IM
import qualified Data.IntSet                 as IS
import           Data.List
import qualified Data.Map.Strict             as M
import           Data.Monoid
import           Data.Ord
import           Data.Primitive.MutVar
import qualified Data.Set                    as S
import           Data.Tuple
import qualified Data.Vector                 as V
import qualified Data.Vector.Generic         as G
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed         as U
import qualified Data.Vector.Unboxed.Mutable as UM
import           Debug.Trace
import           Foreign                     hiding (void)
import           Unsafe.Coerce

main :: IO ()
main = do
    [n, m] <- map read.words <$> getLine :: IO [Int]
    xs <- U.unfoldrN n (B.readInt.B.dropWhile isSpace) <$> B.getLine
    print $ solve m xs

solve :: Int -> U.Vector Int -> Int
solve m xs = go 0 $ sort ks
  where
    !im = U.foldl' (\acc x -> IM.insertWith (++) (rem x m) [x] acc) IM.empty xs
    !ks = IM.keys im
    go !acc (k:ks)
      | (k + k == m || k == 0), Just xs<-IM.lookup k im = go (acc + same xs) ks
      | 2 * k > m = acc
      | Just xs <-IM.lookup k im
          , Just ys<-IM.lookup (m-k) im = go (acc + diff xs ys) ks
      | Just xs<-IM.lookup k im = go (acc + go0 0 (sort xs)) ks
    go acc [] = acc
    same xs = length xs `div` 2
    diff :: [Int] -> [Int] -> Int
    diff xs ys
        | lx == ly = lx
        | lx < ly = go0 lx $ drop lx ys'
        | otherwise = go0 ly $ drop ly xs'
      where
        !lx = length xs
        !ly = length ys
        xs' = concat . map snd . sort . concatMap f .group $ sort xs
        ys' = concat . map snd . sort . concatMap f .group $ sort ys
        f :: [Int] -> [(Int, [Int])]
        f xs
            | even l || l == 1 = [(l, xs)]
            | otherwise = [(1, [head xs]), (l-1, tail xs)]
          where
            !l = length xs
    go0 !acc (x:y:xs)
         | x == y = go (acc+1) xs
         | otherwise = go acc (y:xs)
    go0 acc _ = acc