fork download
  1. import java.util.*;
  2.  
  3. class Ideone
  4. {
  5. public static void main (String[] args) throws java.lang.Exception
  6. {
  7. Scanner sc = new Scanner(System.in);
  8.  
  9. int n = sc.nextInt();
  10. int k = sc.nextInt();
  11.  
  12. int[] nums = new int[n];
  13. for(int i = 0; i < n; i++){
  14. nums[i] = sc.nextInt();
  15. }
  16.  
  17. int[] p = new int[n+1];
  18. for(int i = 1; i <= n; i++){
  19. p[i] = p[i-1] + nums[i-1];
  20. }
  21.  
  22. HashMap<Integer, Integer> mp = new HashMap<>();
  23.  
  24. mp.put(-k, 1);
  25.  
  26. int count = 0;
  27.  
  28. for(int j = 1; j <= n; j++){
  29. int rhs = p[j] - k*j - k;
  30.  
  31. count += mp.getOrDefault(rhs, 0);
  32.  
  33. int lhs = p[j] - k*(j + 1);
  34. mp.put(lhs, mp.getOrDefault(lhs, 0) + 1);
  35. }
  36.  
  37. System.out.println(count);
  38. }
  39. }
  40.  
Success #stdin #stdout 0.14s 54448KB
stdin
5 2
1 3 2 4 2
stdout
4