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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static System.Math;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Runtime.CompilerServices;
using Library;

namespace Program
{
    public static class CODEFESTIVAL_2016_FINALD
    {
        static public int numberOfRandomCases = 0;
        static public void MakeTestCase(List<string> _input, List<string> _output, ref Func<string[], bool> _outputChecker)
        {
        }
        static public void Solve()
        {
            var N = NN;
            var M = NN;
            var XList = NNList(N);
            var processPair = new List<Tuple<List<int>, List<int>>>();
            var oddPair = new List<List<int>>();
            var valToIdx = new LIB_Dict<long, List<int>>(_ => new List<int>());
            for (var i = 0; i < N; i++)
            {
                valToIdx[XList[i] % M].Add(i);
            }
            for (var i = 1; i < M / 2; i++)
            {
                processPair.Add(Tuple.Create(valToIdx[i], valToIdx[M - i]));
            }
            oddPair.Add(valToIdx[0]);
            if (M % 2 == 0) oddPair.Add(valToIdx[M / 2]);
            var cnt = 0L;
            foreach (var item in oddPair)
            {
                cnt += item.Count / 2;
            }
            foreach (var item in processPair)
            {
                var a = item.Item1.Select(e => XList[e]).ToArray();
                var b = item.Item2.Select(e => XList[e]).ToArray();
                var aKi = a.Count(e => e % 2 == 1);
                var bKi = b.Count(e => e % 2 == 1);
                if (aKi > bKi)
                {
                    var t = aKi; aKi = bKi; bKi = t;
                    var t2 = a; a = b; b = t2;
                }
                var add = bKi - aKi;
                foreach (var itema in LIB_Common.RunLength(a.OrderByRand().OrderBy(e => e)))
                {
                    var tmp = itema.Item1;
                    if (itema.Item1 % 2 == 1)
                    {
                        cnt++;
                        tmp--;
                    }
                    if (add != 0)
                    {
                        var tmp2 = Min(add, tmp);
                        add -= tmp2;
                        tmp -= (int)tmp2;
                        cnt += tmp2;
                    }
                    cnt += tmp / 2;
                }
                foreach (var itemb in LIB_Common.RunLength(b.OrderByRand().OrderBy(e => e)))
                {
                    var tmp = itemb.Item1;
                    cnt += tmp / 2;
                }
            }
            Console.WriteLine(cnt);
        }
        static class Console_
        {
            static Queue<string> param = new Queue<string>();
            public static string NextString() { if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item); return param.Dequeue(); }
        }
        class Printer : StreamWriter
        {
            public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
            public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { base.AutoFlush = false; }
            public Printer(Stream stream, Encoding encoding) : base(stream, encoding) { base.AutoFlush = false; }
        }
        static public void Main(string[] args) { if (args.Length == 0) { Console.SetOut(new Printer(Console.OpenStandardOutput())); } var t = new Thread(Solve, 134217728); t.Start(); t.Join(); Console.Out.Flush(); }
        static long NN => long.Parse(Console_.NextString());
        static double ND => double.Parse(Console_.NextString());
        static string NS => Console_.NextString();
        static long[] NNList(long N) => Repeat(0, N).Select(_ => NN).ToArray();
        static double[] NDList(long N) => Repeat(0, N).Select(_ => ND).ToArray();
        static string[] NSList(long N) => Repeat(0, N).Select(_ => NS).ToArray();
        static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => xorshift);
        static long Count<T>(this IEnumerable<T> x, Func<T, bool> pred) => Enumerable.Count(x, pred);
        static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n);
        static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c);
        static uint xorshift { get { _xsi.MoveNext(); return _xsi.Current; } }
        static IEnumerator<uint> _xsi = _xsc();
        static IEnumerator<uint> _xsc() { uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff); while (true) { var t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); yield return w; } }
    }
}
namespace Library {
    class LIB_Common
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public List<Tuple<int, T>> RunLength<T>(IEnumerable<T> l)
        {
            T before = default(T);
            var cnt = 0;
            var ret = new List<Tuple<int, T>>();
            foreach (var item in l)
            {
                if (!before.Equals(item))
                {
                    if (cnt != 0)
                    {
                        ret.Add(Tuple.Create(cnt, before));
                        cnt = 0;
                    }
                }
                before = item;
                ++cnt;
            }
            if (cnt != 0) ret.Add(Tuple.Create(cnt, before));
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public List<T> LCS<T>(T[] s, T[] t) where T : IEquatable<T>
        {
            int sl = s.Length, tl = t.Length;
            var dp = new int[sl + 1, tl + 1];
            for (var i = 0; i < sl; i++)
            {
                for (var j = 0; j < tl; j++)
                {
                    dp[i + 1, j + 1] = s[i].Equals(t[j]) ? dp[i, j] + 1 : Max(dp[i + 1, j], dp[i, j + 1]);
                }
            }
            {
                var r = new List<T>();
                int i = sl, j = tl;
                while (i > 0 && j > 0)
                {
                    if (s[--i].Equals(t[--j])) r.Add(s[i]);
                    else if (dp[i, j + 1] > dp[i + 1, j]) ++j;
                    else ++i;
                }
                r.Reverse();
                return r;
            }
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static public long LIS<T>(T[] array, bool strict)
        {
            var l = new List<T>();
            foreach (var e in array)
            {
                var i = l.BinarySearch(e);
                if (i < 0) i = ~i;
                else if (!strict) ++i;
                if (i == l.Count) l.Add(e);
                else l[i] = e;
            }
            return l.Count;
        }
    }
    class LIB_Dict<K, V> : Dictionary<K, V>
    {
        Func<K, V> d;
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_Dict(Func<K, V> _d) { d = _d; }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public LIB_Dict() : this(_ => default(V)) { }
        new public V this[K i]
        {
            get
            {
                V v;
                return TryGetValue(i, out v) ? v : base[i] = d(i);
            }
            set
            {
                base[i] = value;
            }
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override int GetHashCode()
        {
            var ret = 0;
            for (var e = GetEnumerator(); e.MoveNext();)
            {
                ret ^= e.Current.Key.GetHashCode();
                ret ^= e.Current.Value.GetHashCode();
            }
            return ret;
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override bool Equals(object obj)
        {
            var o = (LIB_Dict<K, V>)obj;
            foreach (var kv in o)
            {
                if (!ContainsKey(kv.Key) || !this[kv.Key].Equals(kv.Value))
                    return false;
            }
            return o.Count == Count;
        }
    }
}