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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;

public class Main {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    void solve() throws IOException {
        int n = ni();
        int m = ni();
        int[] x = nia(n);

        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            if (map.containsKey(x[i])) {
                map.put(x[i], map.get(x[i]) + 1);
            } else {
                map.put(x[i], 1);
            }
        }

        int[][] cnt = new int[2][m];
        for (int t : map.keySet()) {
            cnt[0][t % m] += map.get(t) - map.get(t) % 2;
            cnt[1][t % m] += map.get(t) % 2;
        }

        int ans = (cnt[0][0] + cnt[1][0]) / 2;
        for (int i = 1; i <= m / 2; i++) {
            if (i * 2 == m) {
                ans += cnt[1][i] / 2;
                ans += cnt[0][i] / 2;
            } else {
                int tmp = Math.min(cnt[1][i], cnt[1][m - i]);
                cnt[1][i] -= tmp;
                cnt[1][m - i] -= tmp;
                ans += tmp;

                int tmp2 = Math.min(cnt[1][i], cnt[0][m - i]);
                cnt[1][i] -= tmp2;
                cnt[0][m - i] -= tmp2;
                ans += tmp2;

                int tmp3 = Math.min(cnt[1][m - i], cnt[0][i]);
                cnt[1][m - i] -= tmp3;
                cnt[0][i] -= tmp3;
                ans += tmp3;

                ans += cnt[0][i] / 2;
                ans += cnt[0][m - i] / 2;
            }
        }

        out.println(ans);
    }

    public class Pair implements Comparable<Pair> {
    	int x, y;

    	public Pair(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}

    	public int compareTo(Pair p) {
    		return y - p.y;
    	}
    }

    String ns() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine(), " ");
        }
        return tok.nextToken();
    }

    int ni() throws IOException {
        return Integer.parseInt(ns());
    }

    long nl() throws IOException {
        return Long.parseLong(ns());
    }

    double nd() throws IOException {
        return Double.parseDouble(ns());
    }

    String[] nsa(int n) throws IOException {
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = ns();
        }
        return res;
    }

    int[] nia(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = ni();
        }
        return res;
    }

    long[] nla(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = nl();
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tok = new StringTokenizer("");
        Main main = new Main();
        main.solve();
        out.close();
    }
}