chyam

[백준] 10986번, python - 나머지 합 본문

백준

[백준] 10986번, python - 나머지 합

chyam_eun 2026. 1. 25. 14:53

https://www.acmicpc.net/problem/10986

import sys
input = sys.stdin.readline

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

cnt = [0] * m
prefix = 0

cnt[0] = 0

# (a+b) % m == a % m == b % m 

for x in numbers: # 누적합이 같은것의 개수 증가
    prefix = (prefix + x) % m
    cnt[prefix] += 1 

res = cnt[0] # 나머지가 0인 경우

for c in cnt:
    res += c * (c - 1) // 2 # 같은 나머지가 나온 개수가 C일때 만들 수 있는 쌍의 개수

print(res)