Sort by

recency

|

13 Discussions

|

  • + 0 comments
    import java.util.Scanner;
    
    public class ConsecutiveSubsequences {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int t = scanner.nextInt();
            while (t-- > 0) {
                int[] cnt = new int[100];
                int n = scanner.nextInt();
                int k = scanner.nextInt();
                cnt[0] = 1;
                int sum = 0;
                for (int i = 0; i < n; i++) {
                    int num = scanner.nextInt();
                    sum = (sum + num) % k;
                    cnt[sum]++;
                }
                long ret = 0;
                for (int i = 0; i < k; i++) {
                    ret += (long) cnt[i] * (cnt[i] - 1) / 2;
                }
                System.out.println(ret);
            }
            scanner.close();
        }
    }
    
  • + 0 comments
    for _ in range(int(input())):
        n,k=map(int,input().split())
        list1=list(map(int,input().split()))
        dict1={0:1}
        sum1,res=0,0
        for i in range(len(list1)):
            sum1+=list1[i]
            t=sum1%k
            if t in dict1:
                res+=dict1[t]
                dict1[t]+=1
            else:
                dict1[t]=1
        print(res)
    
  • + 0 comments
    t=int(input())
    for i in range(t):
        n,k=map(int,input().split())
        a=list(map(int,input().split()))
        d={0:1}
        ans=0
        sums=0
        for i in range(n):
            sums+=a[i]
            temp=sums%k
            if temp in d:
                ans+=d[temp]
                d[temp]+=1
            else:
                d[temp]=1
        print(ans)
    
  • + 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)
    }
    
  • + 1 comment

    Simple Python 3 solution -

    from itertools import accumulate
    t = int(input())
    while t:
        s, t = 0, t-1
        n, k = map(int, input().split())  
        prefix = accumulate(map(int, input().split())) # prefix sum oneliner
        rem = [i%k for i in prefix]
        count = [1] + [0] * (k-1)
        for i in rem:   count[i] += 1
        for i in count: s += (i*(i-1))//2
        print(s)