• + 0 comments

    Golang submission:

    package main
    
    import (
    	"bufio"
    	"fmt"
    	"os"
    	"strconv"
    	"strings"
    )
    
    func main() {
    	rd := bufio.NewReader(os.Stdin)
    	t, _ := rd.ReadString('\n')
    	T, _ := strconv.Atoi(strings.TrimRight(t, "\n"))
    
    	for i := 0; i < T; i++ {
    		nk, _ := rd.ReadString('\n')
    		nkarr := strings.Fields(nk)
    		n, _ := strconv.Atoi(nkarr[0])
    		k, _ := strconv.Atoi(nkarr[1])
    
    		a, _ := rd.ReadString('\n')
    		aArr := strings.Fields(a)
    
    		nums := []int{}
    		for x := 0; x < n; x++ {
    			y, _ := strconv.Atoi(aArr[x])
    			nums = append(nums, y)
    		}
    
    		findSubseq(n, k, nums)
    	}
    
    }
    
    func findSubseq(n int, k int, nums []int) {
    	modCount := make([]int, k)
    	modCount[0] = 1
    
    	sum := 0
    	for x := 0; x < n; x++ {
    		sum += nums[x]
    		sum %= k
    		modCount[sum] += 1
    		// fmt.Printf("num = %d, sum = %d\n", nums[x], sum)
    	}
    	// fmt.Printf("Mod counts = %d\n", modCount)
    
    	res := 0
    	for _, x := range modCount {
    		res += x * (x - 1) / 2
    	}
    
    	// fmt.Printf("Result: %d\n", res)
    	fmt.Println(res)
    }