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:
import sys
input = sys.stdin.readline
from collections import *

N, M = map(int, input().split())
X = list(map(int, input().split()))
cnt = Counter(X)
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
    
    ans += len(S)
    T.sort(key=lambda t: cnt[t])
    cnt2 = Counter(T[len(S):])
    
    for k in cnt2.values():
        ans += k//2

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
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]

print(ans)

Submission status: Wrong Answer

Solution code:
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),c)
        else:
            ans+=a+b+c+min((d-b),a)
    else:
        ans+=table[i][0]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

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

dic={}


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),c)
        else:
            ans+=a+b+c+min((d-b),a)
    else:
        ans+=table[i][0]

print(ans)

Submission status: Wrong Answer

Solution code:
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 X[i] not in dic.keys():
        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),c)
        else:
            ans+=a+b+c+min((d-b),a)
    else:
        ans+=table[i][0]

print(ans)

Submission status: Wrong Answer

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

dic={}
for i in range(N):
    if X[i] not in dic.keys():
        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),c)
        else:
            ans+=a+b+c+min((d-b),a)
    else:
        ans+=table[i][0]

print(ans)

Submission status: Wrong Answer

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

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

ans=0
for x in dic.keys():
    ans+=dic[x]//2
    dic[x]%=2

A=list(dic.keys())
for i in range(0,len(A)):
    A[i]%=M

dic={}
for i in range(0,len(A)):
    if A[i] not in dic.keys():
        dic[A[i]]=0
    dic[A[i]]+=1

count=0
for a in dic.keys():
    if a!=0 and M-a in dic.keys():
        count+=min(dic[M-a],dic[a])
count//=2
if 0 in dic.keys():
    count+=dic[0]//2
print(ans+count)

Submission status: Wrong Answer

Solution code:
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
    else:
        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

print(answer)

Submission status: Wrong Answer

Solution code:
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
for i in range(M // 2 + 1):
    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:
        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 + (sum(v1) - cnt2) // 2

print(answer)

Submission status: Wrong Answer

Solution code:
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
for i in range(M // 2 + 1):
    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:
        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:
            if (cnt2 - n) % 2 == 1:
                cnt2 -= 1
            answer += cnt2 + sum(i // 2 for i in v1)

print(answer)

Submission status: Wrong Answer

Solution code:
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
for i in range(M // 2 + 1):
    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:
        v1 = list(x[i].values())
        v2 = list(x[M - i].values())
        cnt1 = sum(v1)
        cnt2 = sum(v2)
        if cnt1 == cnt2:
            answer += cnt1
        elif cnt1 > cnt2:
            n = sum(j // 2 for j in v1)
            if cnt2 + 2 * n > cnt1:
                answer += (cnt1 + cnt2) // 2
            else:
                answer += cnt2 + n
        else:
            n = sum(j // 2 for j in v2)
            if cnt1 + 2 * n > cnt2:
                answer += (cnt1 + cnt2) // 2
            else:
                answer += cnt1 + n

print(answer)

Submission status: Wrong Answer

Solution code:
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
for i in range(M // 2 + 1):
    if i == 0 or i * 2 == M:
        answer += sum(x[i].values()) // 2
        continue
    if i in x:
        if M - i in x:
            v1 = list(x[i].values())
            v2 = list(x[M - i].values())
            cnt1 = sum(v1)
            cnt2 = sum(v2)
            if cnt1 == cnt2:
                answer += cnt1
            elif cnt1 > cnt2:
                n = sum(j // 2 for j in v1)
                if cnt2 + 2 * n > cnt1:
                    answer += (cnt1 + cnt2) // 2
                else:
                    answer += cnt2 + n
            else:
                answer += cnt1
                n = sum(j // 2 for j in v2)
                if cnt1 + 2 * n > cnt2:
                    answer += (cnt1 + cnt2) // 2
                else:
                    answer += cnt1 + n

print(answer)

Submission status: Wrong Answer

Solution code:
import collections

n,m=map(int,input().split())
arr=list(map(int,input().split()))
cnt=collections.Counter(arr)
cnt1=collections.defaultdict(int)
cnt2=collections.defaultdict(int)
for key in cnt.keys():
  tmp=key%m
  val=cnt[key]
  if val%2==0:
    cnt2[tmp]+=val
  else:
    cnt1[tmp]+=1
    cnt2[tmp]+=val-1
ans=0
for i in range(m):
  l=i
  r=(m-i)%m
  if l>r:
    break
  elif l==r:
    ans+=(cnt1[l]+cnt2[l])//2
  else:
    ans+=min(cnt1[l],cnt1[r])
    if cnt1[l]==cnt1[r]:
      ans+=min(cnt2[l],cnt2[r])
      if cnt2[l]==cnt2[r]:
        continue
      elif cnt2[l]<cnt2[r]:
        ans+=(cnt2[r]-cnt2[l])//2
      else:
        ans+=(cnt2[l]-cnt2[r])//2
    elif cnt1[l]<cnt1[r]:
      ans+=min(cnt2[l],cnt1[r])
      if cnt2[l]<=cnt1[r]:
        ans+=cnt2[r]//2
      else:
        cnt2[l]-=cnt1[r]
        ans+=min(cnt2[l],cnt2[r])
        if cnt2[l]==cnt2[r]:
          continue
        elif cnt2[l]<cnt2[r]:
          ans+=(cnt2[r]-cnt2[l])//2
        else:
          ans+=(cnt2[l]-cnt2[r])//2
    else:
      ans+=min(cnt1[l],cnt2[r])
      if cnt2[r]<=cnt1[l]:
        ans+=cnt2[l]//2
      else:
        cnt2[r]-=cnt1[l]
        ans+=min(cnt2[l],cnt2[r])
        if cnt2[l]==cnt2[r]:
          continue
        elif cnt2[l]<cnt2[r]:
          ans+=(cnt2[r]-cnt2[l])//2
        else:
          ans+=(cnt2[l]-cnt2[r])//2
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, defaultdict

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

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

answer = 0
for i in range(M // 2 + 1):
    if i == 0 or i * 2 == M:
        answer += sum(x[i].values()) // 2
        continue
    if i in x:
        if M - i in x:
            v1 = list(x[i].values())
            v2 = list(x[M - i].values())
            cnt1 = sum(v1)
            cnt2 = sum(v2)
            if cnt1 == cnt2:
                answer += cnt1
            elif cnt1 > cnt2:
                n = sum(j // 2 for j in v1)
                if cnt2 + 2 * n > cnt1:
                    answer += (cnt1 + cnt2) // 2
                else:
                    answer += cnt2 + n
            else:
                answer += cnt1
                n = sum(j // 2 for j in v2)
                if cnt1 + 2 * n > cnt2:
                    answer += (cnt1 + cnt2) // 2
                else:
                    answer += cnt1 + n

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, defaultdict

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

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

answer = 0
for i in range(M // 2 + 1):
    if i == 0 or i * 2 == M:
        answer += sum(x[i].values()) // 2
        continue
    if i in x:
        if M - i in x:
            v1 = list(x[i].values())
            v2 = list(x[M - i].values())
            cnt1 = sum(v1)
            cnt2 = sum(v2)
            if cnt1 == cnt2:
                answer += cnt1
            elif cnt1 > cnt2:
                answer += cnt2
                for j in v1:
                    if j >= 2:
                        while cnt1 >= cnt2 + 2:
                            answer += 1
                            cnt1 -= 2
            else:
                answer += cnt1
                for j in v2:
                    if j >= 2:
                        while cnt2 >= cnt1 + 2:
                            answer += 1
                            cnt2 -= 2

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, defaultdict

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

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

answer = 0
for i in range(1, M // 2 + 1):
    if i == 0 or i * 2 == M:
        answer += sum(x[i].values()) // 2
    if i in x:
        if M - i in x:
            v1 = list(x[i].values())
            v2 = list(x[M - i].values())
            cnt1 = sum(v1)
            cnt2 = sum(v2)
            if cnt1 == cnt2:
                answer += cnt1
            elif cnt1 > cnt2:
                answer += cnt2
                for j in v1:
                    if j >= 2:
                        while cnt1 >= cnt2 + 2:
                            answer += 1
                            cnt1 -= 2
            else:
                answer += cnt1
                for j in v2:
                    if j >= 2:
                        while cnt2 >= cnt1 + 2:
                            answer += 1
                            cnt2 -= 2

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, defaultdict

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

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

answer = sum(x[0].values()) // 2
if M % 2 == 0:
    answer += sum(x[M // 2].values()) // 2
for i in range(1, M // 2 + 1):
    if i in x:
        if M - i in x:
            v1 = list(x[i].values())
            v2 = list(x[M - i].values())
            cnt1 = sum(v1)
            cnt2 = sum(v2)
            if cnt1 == cnt2:
                answer += cnt1
            elif cnt1 > cnt2:
                answer += cnt2
                for j in v1:
                    if j >= 2:
                        while cnt1 >= cnt2 + 2:
                            answer += 1
                            cnt1 -= 2
            else:
                answer += cnt1
                for j in v2:
                    if j >= 2:
                        while cnt2 >= cnt1 + 2:
                            answer += 1
                            cnt2 -= 2

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, defaultdict

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

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

answer = sum(x[0].values()) // 2
if M % 2 == 0:
    answer += sum(x[M // 2].values()) // 2
for i in range(1, (M - 1) // 2 + 1):
    if i in x:
        if M - i in x:
            v1 = list(x[i].values())
            v2 = list(x[M - i].values())
            cnt1 = sum(v1)
            cnt2 = sum(v2)
            if cnt1 > cnt2:
                answer += cnt2
                for j in v1:
                    if j >= 2:
                        while cnt1 >= cnt2 + 2:
                            answer += 1
                            cnt1 -= 2
            else:
                answer += cnt1
                for j in v2:
                    if j >= 2:
                        while cnt2 >= cnt1 + 2:
                            answer += 1
                            cnt2 -= 2

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, defaultdict

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

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

answer = sum(x[0].values()) // 2
for i in range(1, M // 2 + 1):
    if i == M - i:
        answer += sum(x[i].values()) // 2
        continue
    if i in x:
        if M - i in x:
            v1 = list(x[i].values())
            v2 = list(x[M - i].values())
            cnt1 = sum(v1)
            cnt2 = sum(v2)
            if cnt1 > cnt2:
                answer += cnt2
                for j in v1:
                    if j >= 2:
                        while cnt1 >= cnt2 + 2:
                            answer += 1
                            cnt1 -= 2
            else:
                answer += cnt1
                for j in v2:
                    if j >= 2:
                        while cnt2 >= cnt1 + 2:
                            answer += 1
                            cnt2 -= 2

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    modDict = dict()
    modDictTotal = [0] * M
    numCount = dict()
    X = [int(x) for x in input().split()]
    used = [False] * M
    for x in X:
        modDictTotal[x % M] += 1
        if x % M in modDict: modDict[x % M] |= {x}
        else: modDict[x % M] = {x}
        if x in numCount: numCount[x] += 1
        else: numCount[x] = 1

    pairs = 0
    for key in modDict:
        if not used[key]:
            used[key] = True
            used[(M - key) % M] = True
            op = (M - key) % M
            if op != key:
                if modDictTotal[key] >= modDictTotal[op]:
                    pairs += modDictTotal[op]
                    left = modDictTotal[op]
                    for n in modDict[key]:
                        if numCount[n] % 2 == 1:
                            left -= 1
                            numCount[n] -= 1
                    for n in modDict[key]:
                        if left == 0: pairs += numCount[n] // 2
                        elif left > numCount[n]: 
                            left -= numCount[n]
                            numCount[n] = 0
                        else:
                            numCount[n] -= left
                            left = 0
                            pairs += numCount[n] // 2
                else:
                    pairs += modDictTotal[key]
                    left = modDictTotal[key]
                    for n in modDict[op]:
                        if numCount[n] % 2 == 1:
                            left -= 1
                            numCount[n] -= 1
                    for n in modDict[op]:
                        if left == 0: pairs += numCount[n] // 2
                        elif left > numCount[n]: 
                            left -= numCount[n]
                            numCount[n] = 0
                        else:
                            numCount[n] -= left
                            left = 0
                            pairs += numCount[n] // 2
            else: pairs += modDictTotal[key] // 2
    print(pairs)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    modDict = dict()
    modDictTotal = [0] * M
    numCount = dict()
    X = [int(x) for x in input().split()]
    used = [False] * M
    for x in X:
        modDictTotal[x % M] += 1
        if x % M in modDict: modDict[x % M] |= {x}
        else: modDict[x % M] = {x}
        if x in numCount: numCount[x] += 1
        else: numCount[x] = 1

    pairs = 0
    for key in modDict:
        if not used[key]:
            used[key] = True
            used[(M - key) % M] = True
            op = (M - key) % M
            if op != key:
                if op in modDict: 
                    if modDictTotal[key] > modDictTotal[op]:
                        pairs += modDictTotal[op]
                        left = modDictTotal[op]
                        for n in modDict[key]:
                            if numCount[n] % 2 == 1:
                                left -= 1
                                numCount[n] -= 1
                        for n in modDict[key]:
                            if left == 0: pairs += numCount[n] // 2
                            elif left > numCount[n]: 
                                left -= numCount[n]
                                numCount[n] = 0
                            else:
                                numCount[n] -= left
                                left = 0
                                pairs += numCount[n] // 2
                    else:
                        pairs += modDictTotal[key]
                        left = modDictTotal[key]
                        for n in modDict[op]:
                            if numCount[n] % 2 == 1:
                                left -= 1
                                numCount[n] -= 1
                        for n in modDict[op]:
                            if left == 0: pairs += numCount[n] // 2
                            elif left > numCount[n]: 
                                left -= numCount[n]
                                numCount[n] = 0
                            else:
                                numCount[n] -= left
                                left = 0
                                pairs += numCount[n] // 2
                            
                else: #自身のダブりを数える
                    for n in modDict[key]: pairs += numCount[n] // 2
            elif (M - key) % M == key: pairs += modDictTotal[key] // 2
    print(pairs)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    modDict = dict()
    modDictTotal = [0] * M
    numCount = dict()
    X = [int(x) for x in input().split()]
    used = [False] * M
    for x in X:
        modDictTotal[x % M] += 1
        if x % M in modDict: modDict[x % M] |= {x}
        else: modDict[x % M] = {x}
        if x in numCount: numCount[x] += 1
        else: numCount[x] = 1

    pairs = 0
    for key in modDict:
        if not used[key]:
            used[key] = True
            used[(M - key) % M] = True
            op = (M - key) % M
            if op != key:
                if op in modDict: 
                    if modDictTotal[key] > modDictTotal[op]:
                        pairs += modDictTotal[op]
                        left = modDictTotal[op]
                        for n in modDict[key]:
                            if numCount[n] % 2 == 1:
                                left -= 1
                                numCount[n] -= 1
                        for n in modDict[key]:
                            if left == 0: pairs += numCount[n] // 2
                            elif left > numCount[n]: 
                                left -= numCount[n]
                                numCount[n] = 0
                            else:
                                left = 0
                                numCount[n] -= left
                                pairs += numCount[n] // 2
                    else:
                        pairs += modDictTotal[key]
                        left = modDictTotal[key]
                        for n in modDict[op]:
                            if numCount[n] % 2 == 1:
                                left -= 1
                                numCount[n] -= 1
                        for n in modDict[op]:
                            if left == 0: pairs += numCount[n] // 2
                            elif left > numCount[n]: 
                                left -= numCount[n]
                                numCount[n] = 0
                            else:
                                left = 0
                                numCount[n] -= left
                                pairs += numCount[n] // 2
                            
                else: #自身のダブりを数える
                    for n in modDict[key]: pairs += numCount[n] // 2
            elif (M - key) % M == key: pairs += modDictTotal[key] // 2
    print(pairs)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
A = LIST()

modA = [a%M for a in A]
modC = Counter(modA)

li = [Counter() for i in range(M)]
for a in A:
    li[a%M][a] += 1
for i in range(M):
    li[i] = li[i].most_common()

def sub(x, cnt):
    while li[x] and cnt - li[x][-1][1] >= 0:
        cnt -= li[x][-1][1]
        li[x].pop()
    if li[x]:
        li[x][-1] = (li[x][-1][0], li[x][-1][1] - cnt)

ans = 0
pcnt = modC[0] // 2
sub(0, pcnt*2)
ans += pcnt
if M % 2 == 0:
    pcnt = modC[M//2] // 2
    sub(M//2, pcnt*2)
    ans += pcnt

for i in range(1, ceil(M, 2)):
    j = M - i
    pcnt = min(modC[i], modC[j])
    sub(i, pcnt)
    sub(j, pcnt)
    ans += pcnt

for i in range(M):
    for _, cnt in li[i]:
        ans += cnt // 2
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

R = [0 for _ in range(M)]
D = defaultdict(int)

for i in range(N):
    R[X[i] % M] += 1
    D[X[i]] += 1

P = [0 for _ in range(M)]

for k, v in D.items():
    P[k % M] += v // 2

res = R[0] //2

for i in range(1, M // 2 + 1):
    if i * 2 == M:
        res += R[i] // 2
    else:
        x = min(R[i], R[-i])
        res += x
        res += min(P[i], (R[i] - x + 1) // 2)
        res += min(P[-i], (R[-i] - x + 1) // 2)

print(res)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
from re import split
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
import re

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n, m = LI()
X = LI()
D = defaultdict(lambda:defaultdict(int))
for k in X:
    D[k % m][k] += 1

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

ans += sum(D[0].values()) // 2

for i in range(1, (m + 1) // 2):
    x = D[i].values()
    y = D[m - i].values()
    x_sum = sum(x)
    y_sum = sum(y)
    if x_sum > y_sum:
        x, y = y, x
    ans += x_sum
    ret = 0
    for j in y:
        ret += j // 2 * 2
        if ret > y_sum - x_sum:
            ans += (y_sum - x_sum) // 2
            break
    else:
        ans += ret // 2

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
from re import split
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
from copy import deepcopy
import re

sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n, m = LI()
X = LI()
D = defaultdict(lambda:defaultdict(int))
for k in X:
    D[k % m][k] += 1

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

ans += sum(D[0].values()) // 2
for i in range(1, (m + 1) // 2):
    x = D[i].values()
    y = D[m - i].values()
    x_sum = sum(x)
    y_sum = sum(y)
    ans += x_sum
    if x_sum > y_sum:
        x, y = y, x
    ret = 0
    for j in y:
        ret += j // 2 * 2
        if ret > y_sum - x_sum:
            ans += (y_sum - x_sum) // 2
            break
    else:
        ans += ret // 2

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, M = (int(i) for i in input().split())
    X = [int(i) for i in input().split()]
    Xm = [x % M for x in X]
    from collections import Counter
    """
    c = Counter(X)
    cd = []
    cs = []
    for k, v in c.items():
        if v >= 2:
            cd.extend([k]*v)
        else:
            cs.append(k)
    """
    cm = Counter(Xm)
    ans = 0
    for i in range(1, M//2 + 1):
        cur = min(cm[i], cm[M-i])
        cm[i] -= cur
        cm[M-i] -= cur
        ans += cur
    print(ans)


if __name__ == '__main__':
    main()