Traverse a and counter a[i] mod k ; there must be k such samples.
Reserve and memoize over separate partitions k, 2*k, 3*k...etc. with parts less than or equal to k , adding the products of the corresponding counters.
For example, if k were 10 , some of the sections would be 1+2+7 and 1+2+3+4 ; but while memoizing, we only need to calculate how many mod k pairs in the array will produce (1 + 2) .
For example, k = 5, a = {1,4,2,3,5,6} :
counts of a[i] mod k: {1,2,1,1,1} products of distinct partitions of k: 5 => 1 4,1 => 2 3,2 => 1 products of distinct partitions of 2 * k with parts <= k: 5,4,1 => 2 5,3,2 => 1 4,1,3,2 => 2 products of distinct partitions of 3 * k with parts <= k: 5,4,1,3,2 => 2 answer = 11 {1,4} {4,6} {2,3} {5} {1,4,2,3} {1,4,5} {4,6,2,3} {4,6,5} {2,3,5} {1,4,2,3,5} {4,6,2,3,5}