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 Rust (1.15.1) (judge verdict: Accepted)

use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::iter::FromIterator;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let n = scan.read::<usize>();
    let m = scan.read::<usize>();
    let x = scan.readn::<usize>(n);
    let xcnt = x.iter().cloned().collect::<Counter<_>>();
    let mut xcnt_pair_modm = vec![0;m];
    for (&k, &v) in xcnt.iter() {
        xcnt_pair_modm[k % m] += v / 2;
    }
    let xcnt_modm = x.iter().map(|i| *i % m).collect::<Counter<_>>();
    let mut ret = 0;
    for i in 0..m/2+1 {
        if i == 0 || m / 2 == i && m % 2 == 0 {
            ret += xcnt_modm[i] / 2;
        } else {
            let st = vec![xcnt_modm[i], xcnt_modm[m - i]];
            if st[0] < st[1] {
                let delta = st[1] - st[0];
                ret += st[0] + min(delta / 2, xcnt_pair_modm[m - i]);
            } else {
                let delta = st[0] - st[1];
                ret += st[1] + min(delta / 2, xcnt_pair_modm[i]);
            }
        }
    }
    println!("{}", ret);
}

// region counter

use std::collections::HashMap;
use std::hash::Hash;
use std::ops::*;
use std::cmp::min;

#[derive(Clone, Debug)]
struct Counter<K: Eq + Hash> {
    cnt: HashMap<K, i64>,
    d: i64,
}

#[allow(dead_code)]
impl <K: Eq + Hash> Counter<K> {
    fn new() -> Counter<K> {
        Counter {cnt: HashMap::new(), d: 0}
    }
}

#[allow(dead_code)]
impl <K: Eq + Hash> Deref for Counter<K> {
    type Target = HashMap<K, i64>;
    fn deref(&self) -> &Self::Target {
        &self.cnt
    }
}

#[allow(dead_code)]
impl <K: Eq + Hash> DerefMut for Counter<K> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.cnt
    }
}

#[allow(dead_code)]
impl<K: Eq + Hash> Index<K> for Counter<K> {
    type Output = i64;

    fn index(&self, index: K) -> &Self::Output {
        self.cnt.get(&index).unwrap_or(&self.d)
    }
}

#[allow(dead_code)]
impl<K: Eq + Hash + Clone> IndexMut<K> for Counter<K> {
    fn index_mut(&mut self, index: K) -> &mut i64 {
        if !self.cnt.contains_key(&index) {
            self.cnt.insert(index.clone(), self.d);
        }
        self.cnt.get_mut(&index).unwrap()
    }
}

#[allow(dead_code)]
impl<K: Eq + Hash> std::iter::FromIterator<K> for Counter<K> {
    fn from_iter<T: IntoIterator<Item=K>>(iter: T) -> Self {
        let mut cnt = HashMap::new();
        for i in iter {
            *cnt.entry(i).or_insert(0) += 1;
        }
        Counter {cnt: cnt, d: 0}
    }
}
// endregion

// region template
#[allow(dead_code)]
fn iu(i: i64) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> i64 { i as i64 }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

#[allow(dead_code)]
fn alt<S, T>(v: Vec<S>) -> T where T: FromIterator<S> {
    v.into_iter().collect::<T>()
}

#[allow(dead_code)]
fn dup<S, T>(v: &[S]) -> T where T: FromIterator<S>, S: Clone {
    v.iter().cloned().collect::<T>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion