Sort by

recency

|

15 Discussions

|

  • + 0 comments

    Java 8 solution:

    import java.io.*;
    import java.util.*;
    import java.util.Scanner;
    
    public class ConsecutiveSubsequences {
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            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

    from sys import stderr from itertools import accumulate from operator import xor

    MAXNUM = 1 << 16

    def main(): n = int(input()) a = [int(input()) for _ in range(n)] c = [0] * MAXNUM for elt in accumulate(a, xor): c[elt] += 1 c[0] += 1 conv = xorfft(i * i for i in xorfft(c)) conv[0] = 2 * MAXNUM * sum(i * (i-1) // 2 for i in c) ans = max((v , -i) for i, v in enumerate(conv)) print(-ans[1], ans[0] // (2 * MAXNUM))

    def xorfft(a): a = list(a) la = len(a) assert la & (la-1) == 0 k = 1 while k < la: for i in range(0, la, 2*k): for j in range(i, i+k): x, y = a[j], a[j+k] a[j], a[j+k] = x + y, x - y k <<= 1 return a

    main()

  • + 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)