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



Sample input:
7 5
3 1 4 1 5 9 2


Sample output:
3

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n, m = LI()
    x = LI()
    d = defaultdict(int)
    l = [defaultdict(int) for _ in range(m)]
    for a in x: d[a % m] += 1; l[a % m][a] += 1
    ans = 0
    for a in range(m // 2 + 1):
        if a == (a-m) % m:
            ans += d[a] // 2
        else:
            tmp = min(d[a], d[m - a])
            ans += tmp
            if d[a] < d[m - a]:
                tmp = d[m - a] - d[a]
                for key, value in l[m - a].items():
                    if tmp < 2:
                        break
                    if value >> 1:
                        t = min(tmp, value >> 1)
                        ans += t
                        tmp -= t
            else:
                tmp = d[a] - d[m - a]
                for key, value in l[a].items():
                    if tmp < 2:
                        break
                    if value >> 1:
                        t = min(tmp, value >> 1)
                        ans += t
                        tmp -= t

    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n, m = LI()
    x = LI()
    d = defaultdict(int)
    l = [defaultdict(int) for _ in range(m)]
    for a in x: d[a % m] += 1; l[a % m][a] += 1
    ans = 0
    for a in range(m // 2 + 1):
        if a == (m - a) % m:
            ans += d[a] // 2
        else:
            tmp = min(d[a], d[m - a])
            ans += tmp
            if d[a] < d[m - a]:
                tmp = d[m-a] - d[a]
                for key, value in l[m - a].items():
                    if tmp < 2:
                        break
                    if value >> 1:
                        t = min(tmp, value >> 1)
                        ans += t
                        tmp -= t
            else:
                tmp = d[a] - d[m - a]
                for key, value in l[a].items():
                    if tmp < 2:
                        break
                    if value >> 1:
                        t = min(tmp, value >> 1)
                        ans += t
                        tmp -= t

    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int, input().split())
a = list(map(int, input().split()))
amax = max(a)
c = [0]*(amax+1)
b = [a[i]%m for i in range(n)]
rem = [0]*m
for i in range(n):
    rem[b[i]] += 1
    c[a[i]] += 1
ans = 0
for i in range(m//2+1):
    if i == 0 or i*2 == m:
        ans += rem[i]//2
        rem[i] -= rem[i]//2*2
        continue
    less = min(rem[i],rem[m-i])
    ans += less
    rem[i] -= less
    rem[m-i] -= less
for j in range(amax//m+1):
    for i in range(m):
        if m*j+i<=amax:
            if rem[i] >= c[m*j+i]:
                ans += c[m*j+i]//2
                rem[i] -= c[m*j+i]//2
            else:
                ans += rem[i]//2
                rem[i] -= rem[i]//2
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int, input().split())
a = list(map(int, input().split()))
c = [0]*100001
b = [a[i]%m for i in range(n)]
rem = [0]*m
for i in range(n):
    rem[b[i]] += 1
    c[a[i]] += 1
ans = 0
for i in range(m//2+1):
    if i == 0 or i*2 == m:
        ans += rem[i]//2
        rem[i] -= rem[i]//2*2
        continue
    less = min(rem[i],rem[m-i])
    ans += less
    rem[i] -= less
    rem[m-i] -= less
for j in range(n//m+1):
    for i in range(m):
        if m*j+i<n:
            if rem[i] >= c[m*j+i]:
                ans += c[m*j+i]//2
                rem[i] -= c[m*j+i]//2
            else:
                ans += rem[i]//2
                rem[i] -= rem[i]//2
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int, input().split())
a = list(map(int, input().split()))
c = [0]*100001
b = [a[i]%m for i in range(n)]
rem = [0]*m
for i in range(n):
    rem[b[i]] += 1
    c[a[i]] += 1
ans = 0
for i in range(m//2+1):
    if i == 0 or i*2 == m:
        ans += rem[i]//2
        rem[i] -= rem[i]//2*2
        continue
    less = min(rem[i],rem[m-i])
    ans += less
    rem[i] -= less
    rem[m-i] -= less
for i in range(m):
    if rem[i] > 1:
        for j in range(n//m+1):
            if m*j+i<n:
                ans += c[m*j+i]//2
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m = LI()
    x = LI()
    d = [[0]*2 for i in range(m)]
    for i in x:
        d[i%m][0] += 1
    x.sort()
    for i in range(max(x)+1):
        k = bisect.bisect_right(x,i)-bisect.bisect_left(x,i)
        if k > 1:
            p = i%m
            s = k >> 1
            d[p][0] -= 2*s
            d[p][1] += 2*s
    ans = 0
    for i in range(1,((m+1)>>1)):
        a,b = d[i]
        c,d_ = d[m-i]
        if a > c:
            a,c = c,a
            b,d_ = d_,b
        ans += a
        c -= a
        if b < c:
            ans += b
            ans += d_>>1
        else:
            ans += c
            d_ -= (b-c)
            if d_ > 0:
                ans += d_>>1
    ans += sum(d[0])>>1
    if not m&1:
        k = m >> 1
        ans += sum(d[k])>>1
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m = LI()
    x = LI()
    d = [[0]*2 for i in range(m)]
    for i in x:
        d[i%m][0] += 1
    x.sort()
    for i in range(max(x)+1):
        k = bisect.bisect_right(x,i)-bisect.bisect_left(x,i)
        if k > 1:
            p = i%m
            s = k >> 1
            d[p][0] -= 2*s
            d[p][1] += 2*s
    ans = 0
    for i in range(1,((m+1)>>1)):
        a,b = d[i]
        c,d_ = d[m-i]
        if a > c:
            a,c = c,a
            b,d_ = d_,b
        ans += a
        c -= a
        ans += min(b,c)
        ans += d_>>1
    ans += sum(d[0])>>1
    if not m&1:
        k = m >> 1
        ans += sum(d[k])>>1
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
def examA():
    H, W = LI()
    S = [LSI() for _ in range(H)]
    Alpha = [chr(ord('A') + i) for i in range(26)]
    for i in range(H):
        for j in range(W):
            if S[i][j]=="snuke":
                ans = str(Alpha[j]) + str(i+1)
    print(ans)
    return

def examB():
    N = I()
    ans = []
    cur = int((N*2-1)**0.5)+1
    ansC = (cur+1)*cur//2
    neg = ansC-N
    if neg>cur:
        cur -=1
        ansC = (cur + 1) * cur // 2
        neg = ansC - N
    for i in range(1,cur+1):
        if i!=neg:
            ans.append(i)
    for v in ans:
        print(v)
#    print(cur,neg)
    return

def bfs(n,e,fordfs):
    #点の数、スタートの点、有向グラフ
    que = deque()
    que.append(e)
    len = [10**9]*n
    len[e] = 0
    while que:
        now = que.popleft()
        nowlen = len[now]
        for ne in fordfs[now]:
            if len[ne] == 10**9:
                len[ne] = nowlen+1
                que.append(ne)
    return len
def examC():
    N, M = LI()
    V = [[]for _ in range(N+M)]
    for i in range(N):
        L = LI()
        for k in range(1,L[0]+1):
            V[i].append(L[k]+N-1)
            V[L[k]+N-1].append(i)
    length = bfs(N+M,0,V)
    ans = "YES"
    for i in length[:N]:
        if i==10**9:
            ans = "NO"
    print(ans)
    return

def examD():
    N, M = LI()
    X = LI(); maxX = max(X)+1
    dx = defaultdict(int)
    dm = [set()for _ in range(M)]
    d = defaultdict(int)
    for x in X:
        dx[x] +=1
        d[x%M] +=1
        dm[x%M].add(x)
    ans = 0
#    print(d); print(dx); print(dm)
    ans +=d[0]//2
    d[0] = 0
    for i in range(1,(1+M)//2):
        cur = min(d[i],d[M-i])
        ans +=cur
        d[i] -=cur; d[M-i] -=cur
#    print(ans); print(d)
    for i in d.keys():
        for j in dm[i]:
            cur = min(dx[j]//2,d[i]//2)
            d[i] -= cur * 2
            ans += cur
            if d[i]<=1:
                break
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examD()

Submission status: Wrong Answer

Solution code:
def examA():
    H, W = LI()
    S = [LSI() for _ in range(H)]
    Alpha = [chr(ord('A') + i) for i in range(26)]
    for i in range(H):
        for j in range(W):
            if S[i][j]=="snuke":
                ans = str(Alpha[j]) + str(i+1)
    print(ans)
    return

def examB():
    N = I()
    ans = []
    cur = int((N*2-1)**0.5)+1
    ansC = (cur+1)*cur//2
    neg = ansC-N
    if neg>cur:
        cur -=1
        ansC = (cur + 1) * cur // 2
        neg = ansC - N
    for i in range(1,cur+1):
        if i!=neg:
            ans.append(i)
    for v in ans:
        print(v)
#    print(cur,neg)
    return

def bfs(n,e,fordfs):
    #点の数、スタートの点、有向グラフ
    que = deque()
    que.append(e)
    len = [10**9]*n
    len[e] = 0
    while que:
        now = que.popleft()
        nowlen = len[now]
        for ne in fordfs[now]:
            if len[ne] == 10**9:
                len[ne] = nowlen+1
                que.append(ne)
    return len
def examC():
    N, M = LI()
    V = [[]for _ in range(N+M)]
    for i in range(N):
        L = LI()
        for k in range(1,L[0]+1):
            V[i].append(L[k]+N-1)
            V[L[k]+N-1].append(i)
    length = bfs(N+M,0,V)
    ans = "YES"
    for i in length[:N]:
        if i==10**9:
            ans = "NO"
    print(ans)
    return

def examD():
    N, M = LI()
    X = LI(); maxX = max(X)+1
    dx = defaultdict(int)
    dm = [set()for _ in range(M)]
    d = defaultdict(int)
    for x in X:
        dx[x] +=1
        d[x%M] +=1
        dm[x%M].add(x)
    ans = 0
#    print(d); print(dx); print(dm)
    ans +=d[0]//2
    for i in range(1,(1+M)//2):
        cur = min(d[i],d[M-i])
        ans +=cur
        d[i] -=cur; d[M-i] -=cur
#    print(ans); print(d)
    for i in d.keys():
        for j in dm[i]:
            cur = min(dx[j]//2,d[i]//2)
            d[i] -= cur * 2
            ans += cur
            if d[i]<=1:
                break
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examD()

Submission status: Wrong Answer

Solution code:
from collections import Counter as c
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=c(a)
d=[0]*m
e=[0]*m
for i in b.keys():
    t=b[i]
    d[i%m]+=t
    e[i%m]+=t//2
z=0
for i in range(1,m//2):
    x,y=d[i],d[-i]
    if x>y:
        z+=d[-i]+min(e[i],(d[i]-d[-i])//2)
    else:
        z+=d[i]+min(e[-i],(d[-i]-d[i])//2)
if m%2==0:
    z+=d[m//2]//2
z+=d[0]//2
print(z)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, X):
    ans = 0
    C = Counter(X)
    # X = list(set(X))
    X.sort()


    D = defaultdict(list)
    for x in X:
        D[x%M].append(x)
    ans = 0
    for i in range(1, M//2+1):
        j = M - i
        if i == j:
            continue
        if len(D[i]) == len(D[j]):
            ans += len(D[i])
        else:
            l = D[i]
            s = D[j]
            if len(l) < len(s):
                l, s = s, l
            C = list(Counter(l).values())
            h = []
            for c in C:
                if c % 2 == 1:
                    h.append(1)
                    c -= 1
                if c != 0:
                    h.append(c)
            heapq.heapify(h)
            s = len(s)
            while h and s > 0:
                c = heapq.heappop(h)
                if c == 1:
                    ans +=1
                    s -= 1
                else:
                    if s >= c:
                        ans += s
                        s -= c
                    else:
                        ans += s
                        heapq.heappush(h, c-s)
                        s = 0
            while h:
                c = heapq.heappop(h)
                if c == 1:
                    pass
                else:
                    ans += c // 2
    ans += len(D[0]) // 2
    if M % 2 == 0:
        ans += len(D[M//2]) // 2
    return ans


def main():
    N, M = read_int_n()
    X = read_int_n()
    print(slv(N, M, X))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter

N, M = map(int, input().split())
X = list(map(int, input().split()))

d = defaultdict(int)
for x in X:
    d[x % M] += 1

ans = d[0] // 2
lst = [0] * M
for m in range(1, M):
    ans += min(d[m], d[M - m])
    lst[m] = d[m] - min(d[m], d[M - m])

ans //= 2
# print (ans)
# print ('lst', lst)
# print ('d', d)

C = Counter(X)
for c in C:
    tmp = min(C[c], lst[c % M]) // 2
    lst[c % M] -= 2 * tmp
    ans += tmp

print (ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from collections import Counter,defaultdict
def resolve():
    n,m=map(int,input().split())
    D=defaultdict(list)
    for a in map(int,input().split()):
        D[a%m].append(a)

    ans=len(D[0])//2
    if(m%2==0): ans+=len(D[m//2])//2

    for i in range(1,(m-1)//2+1):
        A=D[i]; B=D[m-i]
        if(len(A)<len(B)): A,B=B,A # len(A)>=len(B)
        n=len(B)
        ans+=n
        for key,val in Counter(A).most_common()[::-1]:
            if(n>=val): n-=val
            else:
                val-=n
                n=0
                ans+=val//2
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

from collections import defaultdict

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	#受け取り、処理
	N, M = getlist()
	X = getlist()
	D = defaultdict(int)
	for i in X:
		D[i] += 1
	L = [[] for i in range(M)]
	for i in D:
		L[i % M].append(D[i])
	#計算
	ans = 0
	for i in range(1, M):
		a1 = 0
		a2 = 0
		for j in L[i]:
			a1 += j
			a2 += int(j // 2) * 2
		b1 = 0
		b2 = 0
		for j in L[M - i]:
			b1 += j
			b2 += int(j // 2) * 2
		if a1 - a2 > b1:
			ans += b1 + a2 // 2
		elif b1 - b2 > a1:
			ans += a1 + b2 // 2
		else:
			ans += (a1 + b1) // 2

	#細かい処理、偶奇
	if M % 2 == 0:
		ans += sum(L[M // 2 - 1]) // 2
	ans += sum(L[0])
	#答え
	print(ans // 2)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

from collections import defaultdict

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	#受け取り、処理
	N, M = getlist()
	X = getlist()
	D = defaultdict(int)
	for i in X:
		D[i] += 1
	L = [[] for i in range(M)]
	for i in D:
		L[i % M].append(D[i])
	#計算
	ans = 0
	for i in range(1, M):
		a1 = 0
		a2 = 0
		for j in L[i]:
			a1 += j
			a2 += int(j // 2) * 2
		b1 = 0
		b2 = 0
		for j in L[M - i]:
			b1 += j
			b2 += int(j // 2) * 2
		if a1 - a2 > b1:
			ans += b1 + a2 // 2
		elif b1 - b2 > a1:
			ans += a1 + b2 // 2
		else:
			ans += (a1 + b1) // 2

	#細かい処理、偶奇
	if M % 2 == 0:
		ans += sum(L[M // 2 - 1])
	ans += sum(L[0])
	#答え
	print(ans // 2)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

from collections import defaultdict

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	#受け取り、処理
	N, M = getlist()
	X = getlist()
	D = defaultdict(int)
	for i in X:
		D[i] += 1
	L = [[] for i in range(M)]
	for i in D:
		L[i % M].append(D[i])
	#計算
	ans = 0
	for i in range(1, M):
		a1 = 0
		a2 = 0
		for j in L[i]:
			a1 += j
			a2 += int(j // 2) * 2
		b1 = 0
		b2 = 0
		for j in L[M - i]:
			b1 += j
			b2 += int(j // 2) * 2
		if a1 - a2 > b1:
			ans += b1 + a2 // 2
		elif b1 - b2 > a1:
			ans += a1 + b2 // 2
		else:
			ans += (a1 + b1) // 2

	#細かい処理、偶奇
	if M % 2 == 0:
		ans += sum(L[M // 2 - 1])
	ans += sum(L[0]) // 2
	#答え
	print(ans // 2)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
n,m,*x=map(int,open(0).read().split())
a,b=[0]*m,[0]*m
s=set()
for i in x:
  j=i%m
  if i in s:
    s-={i}
    b[j]+=1
  else:
    s|={i}
  a[j]+=1
c=a[0]//2+(m%2<1and a[m//2]//2)
for i in range(1,m//2+m%2):
  (i,_),(j,k)=sorted((a[j],b[j])for j in(i,m-i))
  c+=i+min(j-i,k)
print(c)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter

N, M = map(int, input().split())
X = list(map(int, input().split()))


def main():
    P = [[] for _ in range(M)]

    for x in X:
        P[x%M].append(x)

    ans = 0
    checked = [False]*M
    for m in range(M):
        if checked[m]:
            continue
        if m == 0:
            ans += len(P[m])//2
        else:
            L1, L2 = len(P[m]), len(P[M-m])
            if L1 > L2:
                ans += L2
                C1 = Counter(P[m])
                n = 0
                for num, c1 in C1.items():
                    if c1 % 2 != 0:
                        n += 1
                if L2 >= n:
                    ans += (L1-L2)//2
                else:
                    ans += (L1-n)//2
            else:
                ans += L1
                C2 = Counter(P[M-m])
                n = 0
                for num, c2 in C2.items():
                    if c2 % 2 != 0:
                        n += 1
                if L1 >= n:
                    ans += (L2-L1)//2
                else:
                    ans += (L2-n)//2
            checked[m] = True
            checked[M-m] = True

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter

N, M = map(int, input().split())
X = list(map(int, input().split()))


def main():
    P = [[] for _ in range(M)]

    for x in X:
        P[x%M].append(x)

    ans = 0
    checked = [False]*M
    for m in range(M):
        if checked[m]:
            continue
        if m == 0:
            ans += len(P[m])//2
        else:
            L1, L2 = len(P[m]), len(P[M-m])
            if L1 > L2:
                ans += L2
                C1 = Counter(P[m])
                n = 0
                for num, c1 in C1.items():
                    if c1 % 2 != 0:
                        n += 1
                if L2 >= n:
                    ans += (L1-L2)//2
                else:
                    ans += (L1-n)//2
            else:
                ans += L1
                C2 = Counter(P[M-m])
                n = 0
                for num, c2 in C2.items():
                    if c2 % 2 != 0:
                        n += 1
                if L1 >= n:
                    ans += (L2-L1)//2
                else:
                    ans += (L1-n)//2
            checked[m] = True
            checked[M-m] = True

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter

N, M = map(int, input().split())
X = list(map(int, input().split()))


def main():
    P = [[] for _ in range(M)]

    for x in X:
        P[x%M].append(x)

    ans = 0
    checked = [False]*M
    for m in range(M):
        if checked[m]:
            continue
        if m == 0:
            C1 = Counter(P[m])
            for num, c in C1.items():
                ans += c//2
        else:
            L1, L2 = len(P[m]), len(P[M-m])
            if L1 > L2:
                ans += L2
                C1 = Counter(P[m])
                n = 0
                for num, c1 in C1.items():
                    if c1 % 2 != 0:
                        n += 1
                if L2 >= n:
                    ans += (L1-L2)//2
                else:
                    ans += (L1-n)//2
            else:
                ans += L1
                C2 = Counter(P[M-m])
                n = 0
                for num, c2 in C2.items():
                    if c2 % 2 != 0:
                        n += 1
                if L1 >= n:
                    ans += (L2-L1)//2
                else:
                    ans += (L1-n)//2
            checked[m] = True
            checked[M-m] = True

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int, input().split())
x = list(map(int, input().split()))
c = Counter(x)
#c = Counter(map(int, input().split()))
r = [0]*m
s = [0]*m
for key, value in c.items():
    i = key%m
    r[i] += value
    s[i] += value//2
# 0
ans = r[0]//2
for i in range(1, m):
    j = m-i
    if i > j:
        break
    elif i == j:
        ans += r[0]//2
    else:
        #if r[i] < r[j]:
        #    i,j = j,i
        #ans += r[j]
        #ans += min((r[i]-r[j])//2, s[i])
        x = min(r[i], r[j])
        y = min((r[i] - x)//2, s[i]) + min((r[j] - x)//2, s[j])
        ans += x+y
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int, input().split())
c = Counter(map(int, input().split()))
r = [0]*m
s = [0]*m
for key, value in c.items():
    i = key%m
    r[i] += value
    s[i] += value//2
# 0
ans = r[0]//2
for i in range(1, m):
    j = m-i
    if i > j:
        break
    elif i == j:
        ans += r[0]//2
    else:
        #if r[i] < r[j]:
        #    i,j = j,i
        #ans += r[j]
        #ans += min((r[i]-r[j])//2, s[i])
        x = min(r[i], r[j])
        y = min((r[i] - x)//2, s[i]) + min((r[j] - x)//2, s[j])
        ans += x+y
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int, input().split())
c = Counter(map(int, input().split()))
r = [0]*m
s = [0]*m
for key, value in c.items():
    i = key%m
    r[i] += value
    s[i] += value//2
# 0
ans = r[0]//2
for i in range(1, m):
    j = m-i
    if i > j:
        break
    elif i == j:
        ans += r[0]//2
    else:
        if r[i] < r[j]:
            i,j = j,i
        ans += r[j]
        ans += min((r[i]-r[j])//2, s[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int, input().split())
c = Counter(map(int, input().split()))
r = [0]*m
s = [0]*m
for key, value in c.items():
    i = key%m
    r[i] += value
    s[i] += value//2*2
# 0
ans = r[0]//2
for i in range(1, m):
    j = m-i
    if i > j:
        break
    elif i == j:
        ans += r[0]//2
    else:
        if r[i] < r[j]:
            i,j = j,i
        ans += r[j]
        ans += min(r[i]-r[j], s[i])//2
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int, input().split())
c = Counter(map(int, input().split()))
r = [0]*m
s = [0]*m
for key, value in c.items():
    i = key%m
    r[i] += value
    s[i] += value//2
# 0
ans = r[0]//2
for i in range(1, m//2+1):
    j = m-i
    if i == j:
        ans += r[0]//2
    else:
        if r[i] < r[j]:
            i,j = j,i
        ans += r[j]
        ans += min(r[i]-r[j], s[i])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as dd
N, M = map(int, input().split())
c = list(map(int, input().split()))
c.sort()
m = dd(int)
d = dd(int)
for n in c:
  d[n] += 1
  m[n % M] += 1
res = 0
for i in range(M):
  temp = min(m[i], m[(M - i) % M])
  if i == (M - i) % M:
    temp = m[i] // 2
  m[i] -= temp
  m[M - i] -= temp
  res += temp
for i in range(N):
  n = c[i]
  if m[n % M] > 1:
    k = m[n % M] // 2
    if d[n] > 1:
      temp = min(d[n] // 2, k)
      res += temp
      d[n] -= temp * 2
      m[n % M] -= temp * 2
print(res)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N, M = map(int, input().split())
X = list(map(int, input().split()))

modGrp = [[] for _ in range(M)]

for x in X:
    modGrp[x % M].append(x)

ans = len(modGrp[0]) // 2
modGrp[0] = []

if M % 2 == 0:
    ans += len(modGrp[M // 2]) // 2
    modGrp[M // 2] = []

for m in range(1, M // 2 + 1):
    S = modGrp[m]
    T = modGrp[M - m]

    if len(S) < len(T):
        S, T = T, S

    cnt = Counter(S)
    dis = 0
    for c in cnt.values():
        while c >= 2 and len(S) - dis * 2 > len(T):
            dis += 1
            ans += 1

    ans += min(len(T), len(S) - dis * 2)
    modGrp[m] = []
    modGrp[M - m] = []

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N, M = map(int, input().split())
X = list(map(int, input().split()))

modGrp = [[] for _ in range(M)]

for x in X:
    modGrp[x % M].append(x)

ans = len(modGrp[0]) // 2
if M % 2 == 0:
    ans += len(modGrp[M // 2]) // 2
    modGrp[M // 2] = []

for m in range(1, M // 2 + 1):
    S = modGrp[m]
    T = modGrp[M - m]

    if len(S) < len(T):
        S, T = T, S

    cnt = Counter(S)
    dis = 0
    for c in cnt.values():
        if len(S) - dis * 2 <= len(T):
            break
        dis += c // 2
        ans += c // 2

    ans += min(len(T), len(S) - dis * 2)
    modGrp[m] = []
    modGrp[M - m] = []

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N, M = map(int, input().split())
X = list(map(int, input().split()))

X.sort()

modGrp = [[] for _ in range(M)]

for x in X:
    modGrp[x % M].append(x)

ans = len(modGrp[0]) // 2
if M % 2 == 0:
    ans += len(modGrp[M // 2]) // 2
    modGrp[M // 2] = []

for m in range(1, M // 2 + 1):
    S = modGrp[m]
    T = modGrp[M - m]

    if len(S) < len(T):
        S, T = T, S

    cnt = Counter(S)
    dis = 0
    for c in cnt.values():
        if len(S) - dis * 2 <= len(T):
            break
        dis += c // 2
        ans += c // 2

    ans += min(len(T), len(S) - dis * 2)

print(ans)