fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. long k = sc.nextLong();
  8.  
  9. long[] arr = new long[n + 1];
  10. long[] prefix = new long[n + 1];
  11.  
  12. for (int i = 1; i <= n; i++) {
  13. arr[i] = sc.nextLong();
  14. prefix[i] = prefix[i - 1] + arr[i];
  15. }
  16.  
  17. Map<Long, Long> freq = new HashMap<>();
  18. freq.put(0L, 1L);
  19.  
  20. int count = 0;
  21. for (int j = 1; j <= n; j++) {
  22. long key = (prefix[j] - j) % k;
  23. if (key < 0) key += k;
  24.  
  25. count += freq.getOrDefault(key, 0L);
  26. freq.put(key, freq.getOrDefault(key, 0L) + 1);
  27. }
  28.  
  29. System.out.println(count);
  30. sc.close();
  31. }
  32. }
  33.  
Success #stdin #stdout 0.18s 54536KB
stdin
4 1 
1 3 2 4 
stdout
10