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 (Python):
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m = map(int, input().split())
    X = list(map(int, input().split()))

    MOD = [defaultdict(int) for _ in range(m + 1)]
    cnt = defaultdict(int)
    for x in X:
        MOD[x % m][x] += 1
        cnt[x % m] += 1

    res = cnt[0] // 2
    if m % 2 == 0:
        res += cnt[m // 2] // 2

    for i in range(1, (m + 1) // 2):
        res += min(cnt[i], cnt[m - i])

        if cnt[i] > cnt[m - i]:
            diff = (cnt[i] - cnt[m - i]) // 2
            for v in MOD[i].values():
                if diff == 0:
                    break
                t = min(diff, v // 2)
                diff -= t
                res += t
        elif cnt[i] < cnt[m - i]:
            diff = (cnt[m - i] - cnt[i]) // 2
            for v in MOD[m - i].values():
                if diff == 0:
                    break
                t = min(diff, v // 2)
                diff -= t
                res += t

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,M = LI()
    X = LI()
    c = collections.Counter()
    for k in X:
        c[k] += 1

    ans = 0
    d = collections.Counter()
    ix = sorted(c.items(),key=lambda x:x[1])
    for k,v in ix:
        x = (-k) % M
        if d[x] > 0:
            z = min(d[x],v)
            ans += z
            d[k%M] += v - z
            d[x] -= z
        else:
            d[k%M] += v
    for k,v in ix:
        x = min(v//2,d[k%M]//2)
        if x:
            ans += x
            d[k%M] -= x * 2

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
N,M = map(int,input().split())
X = list(map(int,input().split()))
c = Counter(X)
pair = [0]*M
num = [0]*M
for i in range(1,10**5+1):
    rem = i % M
    pair[rem] += c[i]//2
    num[rem] += c[i]
ans = 0
for i in range(M//2+1):
    j = (M-i) % M
    if i == j:
        ans += num[i]//2
    else:
        if num[i] > num[j]:
            i,j = j,i
        ans += num[i]
        ans += min(pair[j],(num[j]-num[i])//2)
print(ans)

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import Counter
    N, M = map(int, readline().split())
    X = list(map(int, readline().split()))

    c_num = Counter(X)
    c_pair = Counter()
    c_mod = Counter()

    for i in range(1, 10 ** 5 + 1):
        m = i % M
        cnt = c_num[i]
        c_pair[m] += cnt // 2
        c_mod[m] += cnt % 2

    ans = 0
    ans += c_mod[0] // 2
    c_mod[0] -= c_mod[0] // 2

    if M % 2 == 0:
        ans += c_mod[M // 2] // 2
        c_mod[M // 2] -= c_mod[M // 2] // 2

    for i in range(1, (M - 1) // 2 + 1):
        cnt = min(c_mod[i], c_mod[M - i])
        ans += cnt
        c_mod[i] -= cnt
        c_mod[M - i] -= cnt

        if c_mod[i] >= 2:
            c2 = c_mod[i] // 2
            p = c_pair[M - i]
            cnt2 = 2 * min(c2, p)
            ans += cnt2
            c_mod[i] -= cnt2
            c_pair[M - i] -= cnt2 // 2
        if c_mod[M - i] >= 2:
            c2 = c_mod[M - i] // 2
            p = c_pair[i]
            cnt3 = 2 * min(c2, p)
            ans += cnt3
            c_mod[M - i] -= cnt3
            c_pair[i] -= cnt3 // 2

    for i in range(M):
        ans += c_pair[i]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import defaultdict

n, m, *xxx = map(int, sys.stdin.buffer.read().split())
d = defaultdict(lambda: defaultdict(int))

for x in xxx:
    d[x % m][x] += 1

ans = sum(d[0].values()) // 2
if m % 2 == 0:
    ans += sum(d[m // 2].values()) // 2

for a in range(1, (m + 1) // 2):
    b = m - a
    a_pair = 0
    a_alone = 0
    for c in d[a].values():
        k, l = divmod(c, 2)
        a_pair += k
        a_alone += l
    b_pair = 0
    b_alone = 0
    for c in d[b].values():
        k, l = divmod(c, 2)
        b_pair += k
        b_alone += l
    if a_alone > b_alone:
        a_pair, b_pair = b_pair, a_pair
        a_alone, b_alone = b_alone, a_alone
    remaining_alone = b_alone - a_alone
    if a_pair * 2 >= remaining_alone:
        ans += a_pair + b_pair + a_alone + remaining_alone // 2
    else:
        ans += a_pair * 2 + b_pair + a_alone

print(ans)

Solution (Python):
import sys
import collections

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def ns(i_s, i_d, other_s, other_d):
	
	if i_s > other_s:
		i_s, i_d, other_s, other_d = other_s, other_d, i_s, i_d
	
	r = i_s
	other_s -= i_s
	if other_s >= i_d:
		r += i_d
		r += other_d // 2
	else:
		r += other_s
		i_d -= other_s
		r += i_d // 2
		r += other_d // 2
	
	return r

def nss(aa, ab):
	r = (aa+ab) // 2
	
	return r

def main():
	
	n,m = tin()
	
	al=lin()
	dd=[0]*m
	ni=[0]*m
	cc = collections.Counter(al)
	for k in cc:
		
		nv = cc[k]
		dd[k%m] += nv%2
		ni[k%m] += nv-(nv%2)
	
	
	ret = 0
	for i in range(m):
		other = (m-i) % m
		if other < i:
			continue
		
		if i == other:
			ret += nss(dd[i], ni[i])
		else:
			ret += ns(dd[i], ni[i], dd[other], ni[other])
	print(ret)
	
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
def main():
  n,m=map(int,input().split())
  x=list(map(int,input().split()))
  xmod=[y%m for y in x]
  from collections import Counter
  cx=dict(Counter(x))
  cxmod=dict(Counter(xmod))
  ans1,ans2=0,0 
  for k in cxmod.keys():
    if k==0:
      ans1+=cxmod[k]//2
      cxmod[k]-=2*(cxmod[k]//2)
    else:
      if k==m-k:
        ans1+=cxmod[k]//2
        cxmod[k]-=2*(cxmod[k]//2)
      elif m-k in cxmod.keys():
        tmp=min(cxmod[k],cxmod[m-k])
        ans1+=tmp
        cxmod[k]-=tmp
        cxmod[m-k]-=tmp
  for k in cx:
    v=cx[k]
    if v>=2 and cxmod[k%m]>=2:
      tmp=min(v//2,cxmod[k%m]//2)
      ans2+=tmp
      cxmod[k%m]-=tmp*2

  print(ans1+ans2)
if __name__=='__main__':
  main()

Solution (Python):
def main():
  n,m=map(int,input().split())
  x=list(map(int,input().split()))
  xmod=[y%m for y in x]
  from collections import Counter
  cx=dict(Counter(x))
  cxmod=dict(Counter(xmod))
  keys=cxmod.keys()
  ans1,ans2=0,0 
  for k in keys:
    if k==0:
      ans1+=cxmod[k]//2
      cxmod[k]-=2*(cxmod[k]//2)
    else:
      if k==m-k:
        ans1+=cxmod[k]//2
        cxmod[k]-=2*(cxmod[k]//2)
      elif m-k in keys:
        tmp=min(cxmod[k],cxmod[m-k])
        ans1+=tmp
        cxmod[k]-=tmp
        cxmod[m-k]-=tmp
  for k in cx:
    v=cx[k]
    if v>=2 and cxmod[k%m]>=2:
      tmp=min(v//2,cxmod[k%m]//2)
      ans2+=tmp
      cxmod[k%m]-=tmp*2

  print(ans1+ans2)
if __name__=='__main__':
  main()

Solution (Python):
def main():
  n,m=map(int,input().split())
  x=list(map(int,input().split()))
  xmod=[y%m for y in x]
  from collections import Counter
  cx=dict(Counter(x))
  cxmod=dict(Counter(xmod))
  keys=cxmod.keys()
  ans1,ans2=0,0 
  for k in keys:
    if k==0:
      ans1+=cxmod[k]//2
      cxmod[k]-=2*(cxmod[k]//2)
    else:
      if k==m-k:
        ans1+=cxmod[k]//2
        cxmod[k]-=2*(cxmod[k]//2)
      elif m-k in keys:
        tmp=min(cxmod[k],cxmod[m-k])
        ans1+=tmp
        cxmod[k]-=tmp
        cxmod[m-k]-=tmp
  for k in cx:
    v=cx[k]
    if v>=2 and cxmod[k%m]>=2:
      tmp=min(v//2,cxmod[k%m]//2)
      ans2+=tmp
      cxmod[k%m]-=tmp*2

  print(ans1+ans2)
if __name__=='__main__':
  main()

Solution (Python):
def main():
  n,m=map(int,input().split())
  x=list(map(int,input().split()))
  xmod=[y%m for y in x]
  from collections import Counter
  cx=Counter(x)
  cxmod=Counter(xmod)
  keys=cxmod.keys()
  ans1,ans2=0,0 
  for k in keys:
    if k==0:
      ans1+=cxmod[k]//2
      cxmod[k]-=2*(cxmod[k]//2)
    else:
      if k==m-k:
        ans1+=cxmod[k]//2
        cxmod[k]-=2*(cxmod[k]//2)
      elif m-k in keys:
        tmp=min(cxmod[k],cxmod[m-k])
        ans1+=tmp
        cxmod[k]-=tmp
        cxmod[m-k]-=tmp
  for k in cx:
    v=cx[k]
    if v>=2 and cxmod[k%m]>=2:
      tmp=min(v//2,cxmod[k%m]//2)
      ans2+=tmp
      cxmod[k%m]-=tmp*2

  print(ans1+ans2)
if __name__=='__main__':
  main()

Solution (Python):
def main():
  n,m=map(int,input().split())
  x=list(map(int,input().split()))
  
  from collections import Counter
  cx=Counter(x)
  cxmod={}
  for k in cx:
    if k%m in cxmod:
      cxmod[k%m]+=cx[k]
    else:
      cxmod[k%m]=cx[k]
  keys=cxmod.keys()
  ans1,ans2=0,0 
  for k in keys:
    if k==0:
      ans1+=cxmod[k]//2
      cxmod[k]-=2*(cxmod[k]//2)
    else:
      if k==m-k:
        ans1+=cxmod[k]//2
        cxmod[k]-=2*(cxmod[k]//2)
      elif m-k in keys:
        tmp=min(cxmod[k],cxmod[m-k])
        ans1+=tmp
        cxmod[k]-=tmp
        cxmod[m-k]-=tmp
  for k in cx:
    v=cx[k]
    if v>=2 and cxmod[k%m]>=2:
      tmp=min(v//2,cxmod[k%m]//2)
      ans2+=tmp
      cxmod[k%m]-=tmp*2

  print(ans1+ans2)
if __name__=='__main__':
  main()

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

from collections import Counter
cs = [Counter() for _ in range(M+1)]
ca = Counter()
for x in X:
    cs[x%M][x] += 1
    ca[x%M] += 1

ans = ca[0]//2
for i in range(1,(M+1)//2):
    j = M-i
    ans += min(ca[i], ca[j])
    if ca[i] > ca[j]:
        d = (ca[i] - ca[j]) // 2
        for v in cs[i].values():
            t = min(d, v//2)
            d -= t
            ans += t
            if d==0: break
    elif ca[i] < ca[j]:
        d = (ca[j] - ca[i]) // 2
        for v in cs[j].values():
            t = min(d, v//2)
            d -= t
            ans += t
            if d==0: break
if M%2==0:
    ans += ca[M//2]//2
print(ans)

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
a=[]
for i in range(m):
  a.append([])
for i in range(n):
  a[x[i]%m].append(x[i])
for i in range(m):
  a[i].sort()
ans=0
for i in range(1,(m+1)//2):
  if len(a[i])<len(a[m-i]):
    t=m-i
  else:
    t=i
  pairs=0
  r=0
  count=1
  for j in range(len(a[t])):
    if a[t][j]==r:
      count+=1
    else:
      pairs+=count//2
      count=1
      r=a[t][j]
  pairs+=count//2
  ans+=len(a[m-t])
  ans+=min((len(a[t])-len(a[m-t]))//2,pairs)
if m%2==0:
  ans+=len(a[m//2])//2
ans+=len(a[0])//2
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

n,m = readints()
x = readints()
modx = [i%m for i in x]

a = [0]*(10**5+1)
b = [0]*m
for i in range(n):
    a[x[i]]+=1
    b[modx[i]]+=1

ans = b[0]//2
b[0] = 0
y = m//2
if m%2==0:
    ans += b[m//2]//2
    b[m//2] = 0
else:
    y += 1
for i in range(1,y):
    c = min(b[i],b[m-i])
    b[i] -= c
    b[m-i] -= c
    ans += c
for i in range(10**5+1):
    if b[i%m]>1 and a[i]>1:
        c = min(b[i%m],a[i])//2
        ans += c
        b[i%m] -= c*2

print(ans)

Solution (Python):
from collections import defaultdict

N,M = map(int,input().split())
X = sorted(map(int,input().split()))
d = defaultdict(list)
for x in X:
    d[x%M].append(x)

res = 0
for i in range(M//2+1):
    j = (-i)%M

    if i == j:
        res += len(d[i])//2
    else:
        l, r = d[i], d[j]
        if len(l) < len(r):
            l,r = r,l

        t = 0
        last = None
        for v in l:
            if last is None:
                last = v
            elif last == v:
                t += 1
                last = None
            else:
                last = v

        res += len(r) + min((len(l)-len(r))//2, t)

print(res)

Solution (Python):
import sys
from collections import Counter
input = sys.stdin.buffer.readline

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

counter = Counter(Xs)
same_number_pairs = [0] * M 
not_paired_numbers = [0] * M

for value, count in counter.items():
    same_number_pairs[value % M] += count // 2
    not_paired_numbers[value % M] += count % 2

sum_pairs = not_paired_numbers[0] // 2
not_paired_numbers[0] %= 2
if M % 2 == 0:
    sum_pairs += not_paired_numbers[M//2] // 2
    not_paired_numbers[M//2] %= 2
for i in range(1, (M+1)//2):
    pair = min(not_paired_numbers[i], not_paired_numbers[-i])
    sum_pairs += pair
    not_paired_numbers[i] -= pair
    not_paired_numbers[-i] -= pair

for mod, count in enumerate(not_paired_numbers):
    while same_number_pairs[(-mod)%M] >= 1 and count >= 2:
        same_number_pairs[(-mod)%M] -= 1
        count -= 2
        sum_pairs += 2

print(sum(same_number_pairs) + sum_pairs)

Solution (Python):
from collections import Counter

N, M = map(int, input().split())
ct = Counter(map(int, input().split()))

G = [0] * M
pairs_num = [0] * M
for k, v in ct.items():
    G[k % M] += v
    pairs_num[k % M] += v // 2

ans = G[0] // 2
if not M % 2:
    ans += G[M // 2] // 2
for i in range(1, (M + 1) // 2):
    if G[i] >= G[M - i]:
        ans += G[M - i] + min(pairs_num[i], (G[i] - G[M - i]) // 2)
    else:
        ans += G[i] + min(pairs_num[M - i], (G[M - i] - G[i]) // 2)
print(ans)

Solution (Python):
N,M = map(int, input().split())
X = [int(x) for x in input().split()]

num = [0]*(max(X)+1)
R = [0]*M
for x in X:
    num[x] += 1
    R[x%M] += 1

ans = R[0]//2
for i in range(1, M//2+1):
    ans += min(R[i], R[M-i])
if M%2 == 0:
    ans -= R[M//2]-R[M//2]//2

for i in range(len(num)):
    if num[i] < 2 or i%M == 0:
        continue
    d = R[i%M]-R[M-i%M]
    if d < 2:
        continue
    t = min(d, num[i])//2
    ans += t
    R[i%M] -= 2*t

print(ans)

Solution (Python):
N,M=map(int,input().split())
X=list(map(int,input().split()))
X.sort()
L=[[] for i in range(M)]
for i in range(N):
  L[X[i]%M].append(X[i])
for i in range(1,(M+1)//2):
  if len(L[i])<len(L[M-i]):
    L[i],L[M-i]=L[M-i],L[i]
P=0
X=0
B=0
for i in range(1,(M+1)//2):
  X=len(L[i])
  B=True
  for j in range(X-1):
    if B:
      if L[i][j]==L[i][j+1] and X-2>=len(L[M-i]):
        X-=2
        P+=1
        B=False
    else:
      B=True
  P+=min(X,len(L[M-i]))
P+=len(L[0])//2
if M&1==0:
  P+=len(L[M//2])//2
print(P)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = map(int,input().split())
x = list(map(int,input().split()))

from collections import Counter, defaultdict
c = Counter(x)
cc = defaultdict(list)
onum = defaultdict(int)
esum = defaultdict(int)
for k,v in c.items():
    cc[k%m].append(v)
    if v%2:
        onum[k%m] += 1
        esum[k%m] += v-1
    else:
        esum[k%m] += v
    
def sub(l, o):
    return sum(item//2 for item in l) + o//2
def sub2(l1, o1, l2, o2, e1, e2):
    if o1==o2:
        return sum(item//2 for item in l1) + sum(item//2 for item in l2) + o1
    elif o1<o2:
        o1, o2 = o2, o1
        l1, l2 = l2, l1
        e1, e2 = e2, e1
    ans = 0
    ans += o2
    o1 = o1 - o2
    if o1 <= e2:
        ans += o1
        e2 -= o1
        ans += (e1//2) + (e2//2)
    else:
        ans += e2
        ans += (e1//2)
    return ans

if m%2==0:
    ans = 0
    ans += sub(cc[0], onum[0])
    ans += sub(cc[m//2], onum[m//2])
    for k in range(1,m//2):
        ans += sub2(cc[k], onum[k], cc[m-k], onum[m-k], esum[k], esum[m-k])
else:
    ans = 0
    ans += sub(cc[0], onum[0])
    for k in range(1,m//2+1):
        ans += sub2(cc[k], onum[k], cc[m-k], onum[m-k], esum[k],  esum[m-k])
print(ans)

Solution (Python):
from collections import defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,m = inm()
x = inl()
h = defaultdict(int)
for y in x:
    h[y] += 1
od = [0]*m
ev = [0]*m
for y in h:
    od[y%m] += h[y]%2
    ev[y%m] += h[y]-h[y]%2
ans = (ev[0]+od[0])//2
if m%2==0:
    ans += (ev[m//2]+od[m//2])//2
for i in range(1,m//2+m%2):
    a = od[i]
    b = ev[i]
    c = od[m-i]
    d = ev[m-i]
    if a>c+d:
        ans += c+d+b//2
    elif c>a+b:
        ans += a+b+d//2
    else:
        ans += (a+b+c+d)//2
print(ans)

Solution (Python):
from collections import defaultdict

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

d = defaultdict(int)
num = defaultdict(list)
ans = 0
for x in X:
  y = x % M
  num[y].append(x)
  pair = (M - y) % M
  if d[pair] >= 1:
    d[pair] -= 1
    ans += 1
  else:
    d[y] += 1

def count_pair(Z, n):
  res = 0
  dd = defaultdict(int)
  for z in Z:
    if dd[z] >= 1:
      res += 1
      dd[z] -= 1
    else:
      dd[z] += 1
  return min(res, n)

for y in num:
  ans += count_pair(num[y], d[y]//2)

print(ans)

Solution (Python):
from collections import Counter

n,m = map(int, input().split())
x = list(map(int, input().split()))
mod_count = [0] * m
pair = [0] * m
count = Counter(x)

for i in x:
  mod_count[i%m] += 1
for k,v in count.items():
  pair[k%m] += v // 2
ans = mod_count[0] // 2
if m % 2 == 0:
  ans += mod_count[m//2] // 2
for i in range(1,(m+1)//2):
  a1,a2 = mod_count[i], pair[i]
  b1,b2 = mod_count[m-i], pair[m-i]
  if a1 > b1:
    if a1 - a2 * 2 >= b1:
      ans += a2 + b1
    else:
      ans += (a1 + b1) // 2
  else:
    if b1 - b2 * 2 >= a1:
      ans += b2 + a1
    else:
      ans += (a1+b1) // 2
print(ans)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))





lst = [{} for i in range(m)]
lst_num = [0 for i in range(m)]
for i in range(n):
    xi = x[i]
    amari = xi % m
    if xi in lst[amari]:
        lst[amari][xi] += 1
    else:
        lst[amari][xi] = 1
    lst_num[amari] += 1

lst_kisu = [0 for i in range(m)]
lst_gusu = [0 for i in range(m)]
for i in range(m):
    num = 0
    for value in lst[i].values():
        if value % 2 == 1:
            num += 1
    lst_kisu[i] = num
    lst_gusu[i] = lst_num[i] - num

ans = 0
ans += lst_num[0] // 2
for i in range(1, (m-1)//2+(m-1)%2+1):
    amari1 = i
    amari2 = m - i
    if amari1 == amari2:
        ans += lst_num[amari1] // 2
    else:
        if lst_num[amari1] <= lst_num[amari2]:
            ans += lst_num[amari1]
            if lst_num[amari1] <= lst_kisu[amari2]:
                ans += lst_gusu[amari2] // 2
            else:
                ans += (lst_gusu[amari2]-(lst_num[amari1]-lst_kisu[amari2])) // 2
        else:
            ans += lst_num[amari2]
            if lst_num[amari2] <= lst_kisu[amari1]:
                ans += lst_gusu[amari1] // 2
            else:
                ans += (lst_gusu[amari1]-(lst_num[amari2]-lst_kisu[amari1])) // 2
print(ans)

Solution (Python):
n, m = map(int, input().split())
x = list(map(int, input().split()))

if m == 1:
    print(n//2)
    exit()

lst = [{} for i in range(m)]
lst_num = [0 for i in range(m)]
for i in range(n):
    xi = x[i]
    amari = xi % m
    if xi in lst[amari]:
        lst[amari][xi] += 1
    else:
        lst[amari][xi] = 1
    lst_num[amari] += 1

lst_kisu = [0 for i in range(m)]
lst_gusu = [0 for i in range(m)]
for i in range(m):
    num = 0
    for value in lst[i].values():
        if value % 2 == 1:
            num += 1
    lst_kisu[i] = num
    lst_gusu[i] = lst_num[i] - num

ans = 0
ans += lst_num[0] // 2
for i in range(1, (m-1)//2+(m-1)%2+1):
    amari1 = i
    amari2 = m - i
    if amari1 == amari2:
        ans += lst_num[amari1] // 2
    else:
        if lst_num[amari1] <= lst_num[amari2]:
            ans += lst_num[amari1]
            if lst_num[amari1] <= lst_kisu[amari2]:
                ans += lst_gusu[amari2] // 2
            else:
                ans += (lst_gusu[amari2]-(lst_num[amari1]-lst_kisu[amari2])) // 2
        else:
            ans += lst_num[amari2]
            if lst_num[amari2] <= lst_kisu[amari1]:
                ans += lst_gusu[amari1] // 2
            else:
                ans += (lst_gusu[amari1]-(lst_num[amari2]-lst_kisu[amari1])) // 2
print(ans)

Solution (Python):
from collections import Counter

N, M = map(int, input().split())
*X, = map(int, input().split())
*l, = map(lambda x:x%M, X)
c1 = Counter(l)
c2 = Counter(X)
c3 = Counter()

for i, j in c2.items():
    c3[i%M] += j//2

ans = c1[0]//2
for i in range(1, M):
    if i>M-i:break
    elif i==M-i:
        ans += c1[i]//2
    else:
        ans += min(c1[i], c1[M-i])        
        ans += min(abs(c1[i]-c1[M-i])//2, c3[i] if c1[i]>c1[M-i] else c3[M-i])

print(ans)

Solution (Python):
from collections import Counter

N, M = map(int, input().split())
*X, = map(int, input().split())
*l, = map(lambda x:x%M, X)
c1 = Counter(l)
c2 = Counter(X)
c3 = Counter()

for i, j in c2.items():
    c3[i%M] += j//2

ans = c1[0]//2

for i in range(1, M):
    if i>M-i:break
    elif i==M-i:
        ans += c1[i]//2
    else:
        ans += min(c1[i], c1[M-i])        
        if c1[i] > c1[M-i]:
            v2 = c1[i]-c1[M-i]
            ans += min(v2//2, c3[i])
        else:
            v2 = c1[M-i]-c1[i]
            ans += min(v2//2, c3[M-i])

print(ans)

Solution (Python):
from collections import defaultdict
from itertools import groupby
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
alone = defaultdict(int)
pairs = defaultdict(int)
for k, g in groupby(X):
    l = len(list(g))
    alone[k % M] += l % 2
    pairs[k % M] += l // 2
ans = 0
for i in range(M):
    if i == 0:
        ans += alone[i] // 2
        ans += pairs[i]
    else:
        a = alone[i]
        b = alone[M - i]
        c = min(a, b)
        ans += c
        alone[i] -= c
        alone[M - i] -= c

        a = alone[i]
        b = pairs[M - i] * 2
        c = min(a, b)
        ans += c
        alone[i] -= c
        pairs[M - i] -= (c + 1) // 2

        ans += pairs[M - i]
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import *

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

for Xi in X:
    d[Xi%M].append(Xi)

ans = 0

for i in range(1, (M+1)//2):
    S = d[i]
    T = d[M-i]
    
    if len(S)>len(T):
        S, T = T, S
    
    cnt = Counter(T)
    ans += min(sum(v//2 for v in cnt.values()), (len(T)-len(S))//2)+len(S)
    
ans += len(d[0])//2

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

print(ans)

Solution (Python):
n,m=map(int,input().split())
x=list(map(int,input().split()))
a=[[]for _ in range(m)]
for i in x:a[i%m].append(i)
b=[]
from collections import Counter
for i in range(m):
  d=Counter(a[i])
  ans=0
  for j in d:ans+=2*(d[j]//2)
  b.append([len(a[i]),ans])
ans=0
if m%2==0:ans+=len(a[m//2])//2
ans+=len(a[0])//2
for i in range(1,0--m//2):
  sc,sp=b[i]
  tc,tp=b[m-i]
  v=min(sc,tc)
  ans+=v
  ans+=min(sp,sc-v)//2
  ans+=min(tp,tc-v)//2
print(ans)

Solution (Python):
from collections import Counter

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

dupCnt = Counter()
modCnt = Counter()

for x, c in Counter(X).items():
    x %= M
    dupCnt[x] += c // 2
    modCnt[x] += c

ans = 0
for i in range(M):
    j = M - i
    if i > j:
        break
    if i == j or i == 0:
        ans += modCnt[i] // 2
        continue

    mi = min(modCnt[i], modCnt[j])
    ans += mi

    ans += min((modCnt[i] - mi) // 2, dupCnt[i])
    ans += min((modCnt[j] - mi) // 2, dupCnt[j])

print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))





N,M = LI()
X = LI()

d = defaultdict(int)
dmod = defaultdict(int)
pair = defaultdict(int)

for i in range(N):
    d[X[i]] += 1
    m = X[i]%M
    dmod[m] += 1
    if d[X[i]]%2 == 0:
        pair[m] += 1

ans = dmod[0]//2
for i in range(1,(M+1)//2):
    a = dmod[i]
    b = dmod[M-i]
    if a <= b:
        ans += a + min((b-a)//2, pair[M-i])
    else:
        ans += b + min((a-b)//2, pair[i])
if M%2 == 0:
    ans += dmod[M//2]//2

print(ans)

Solution (Python):
from collections import Counter
n, m = map(int, input().split())
X = list(map(int, input().split()))
Odd = [0]*m
C = [0]*m
L = [0]*m
D = Counter(X)
for i, c in D.items():
  C[i%m] += c
  Odd[i%m] += c%2
  L[i%m] += c//2
ans = 0
ans += C[0]//2
for i in range(1, (m+1)//2):
  if C[i] <= C[m-i]:
    ans += C[i]
    if C[i] <= Odd[m-i]:
      ans += L[m-i]
    else:
      ans += L[m-i] + (-(C[i]-Odd[m-i])//2)
  else:
    ans += C[m-i]
    if C[m-i] <= Odd[i]:
      ans += L[i]
    else:
      ans += L[i] + (-(C[m-i]-Odd[i])//2)
if m%2 == 0:
  ans += C[m//2]//2
print(ans)

Solution (Python):
n,m = map(int,input().split())
X = list(map(int,input().split()))
mod = [[0,0] for _ in range(m)]
d = {}
for x in X:
    if x not in d.keys():d[x] = 0
    d[x] += 1
for k,v in d.items():
    mod[k%m][0] += v//2
    mod[k%m][1] += v%2
ans = 0
for i in range(m//2+1):
    if i == 0 or i == m-i:
        ans += mod[i][1]//2 + mod[i][0]
    else:
        if mod[i][1] < mod[m-i][1]:
            ans += min(mod[m-i][1], mod[i][0]*2 + mod[i][1])
            ans += mod[m-i][0]
            ans += max(0,(mod[i][0]*2 + mod[i][1] - mod[m-i][1]))//2
        elif mod[i][1] == mod[m-i][1]:
            ans += mod[i][0] + mod[m-i][0] + mod[i][1]
        else:
            ans += min(mod[i][1], mod[m-i][0]*2 + mod[m-i][1])
            ans += mod[i][0]
            ans += max(0,(mod[m-i][0]*2 + mod[m-i][1] - mod[i][1]))//2
print(ans)

Solution (Python):
import sys

input=sys.stdin.readline

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

dic={}
for i in range(N):
    if not X[i] in dic:
        dic[X[i]]=0
    dic[X[i]]+=1

table=[[0,0] for i in range(0,M)]
for key in dic.keys():
    q=dic[key]//2
    r=dic[key]%2
    table[key%M][0]+=q
    table[key%M][1]+=r

ans=table[0][0]+table[0][1]//2
for i in range(1,M//2+1):
    if 2*i!=M:
        a,b=table[i]
        c,d=table[M-i]
        if b>=d:
            ans+=a+d+c+min((b-d)//2,c)
        else:
            ans+=a+b+c+min((d-b)//2,a)
    else:
        ans+=table[i][0]+table[i][1]//2

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import *

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

for i in range(N):
    d[X[i]%M].append(i)

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

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

for m in range(1, (M+1)//2):
    if len(d[m])>len(d[M-m]):
        S, T = d[m], d[M-m]
    else:
        S, T = d[M-m], d[m]
    
    cnt = Counter([X[i] for i in S])
    ans += min(sum(v//2 for v in cnt.values()), (len(S)-len(T))//2)+len(T)

print(ans)

Solution (Python):
import sys
from collections import defaultdict

read = sys.stdin.read
readline = sys.stdin.readline

N, M, *X = map(int, read().split())
x = defaultdict(lambda: defaultdict(int))
for i in X:
    x[i % M][i] += 1

answer = 0
checked = [False] * M
for i in range(M):
    if checked[i]:
        continue
    checked[i] = True
    if i == 0 or i * 2 == M:
        answer += sum(x[i].values()) // 2
        continue
    if i not in x:
        continue
    if M - i in x:
        checked[M - i] = True
        v1 = list(x[i].values())
        v2 = list(x[M - i].values())
        cnt1 = sum(v1)
        cnt2 = sum(v2)
        if cnt1 == cnt2:
            answer += cnt1
            continue
        if cnt1 < cnt2:
            v1, v2 = v2, v1
            cnt1, cnt2 = cnt2, cnt1

        n = sum(i % 2 for i in v1)
        if n >= cnt2:
            answer += cnt2 + sum(i // 2 for i in v1)
        else:
            answer += cnt2 + (cnt1 - cnt2) // 2
    else:
        answer += sum(i // 2 for i in x[i].values())
print(answer)

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))

import collections
c = collections.Counter(x)

rems = [[0] * 2 for _ in range(m)]

for key, val in c.items():
    rems[key%m][0] += val%2
    rems[key%m][1] += (val//2) * 2

ans = 0
for i in range(1 + m//2):
    if(i*2 == m)|(i==0):
        ans += sum(rems[i])//2
        continue

    j = m-i
    if(sum(rems[i]) < sum(rems[j])):
        pair_mul = sum(rems[i])
        more = j
    else:
        pair_mul = sum(rems[j])
        more = i
    pair_same = (rems[more][1] - max(pair_mul - rems[more][0], 0))//2
    ans += pair_mul + pair_same

print(ans)