#C7845. Count Pairs Divisible by k
Count Pairs Divisible by k
Count Pairs Divisible by k
You are given an array of n integers and a positive integer k. Your task is to determine the number of distinct pairs (i, j) (with i < j) such that the sum ai + aj is divisible by k. In mathematical terms, you need to count the number of pairs satisfying
\[ (a_i + a_j) \mod k = 0 \]Please note that each pair is considered only once. This problem will test your ability to work with modular arithmetic and counting pairs efficiently.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains two integers n and k, where n is the number of elements in the array and k is the divisor.
- The second line contains n space-separated integers representing the array elements.
outputFormat
The output, written to standard output (stdout), is a single integer which is the count of pairs (i, j) with i < j such that (ai + aj) \mod k = 0.
## sample5 3
1 2 3 4 5
4