前缀和(prefix sum/cumulative sum)的应用.
还用了一个知识点:
a≡b(mod d) 则 a-b被d整除.
即:a与b对d同余,则a-b被d整除.
class Solution(object): def subarraysDivByK(self, A, K): P = [0] for x in A: P.append((P[-1] + x) % K) count = collections.Counter(P) return sum(v*(v-1)/2 for v in count.values())