fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n, k; cin >> n >> k;
  7. vector<int> arr(n+1);
  8. vector<int> p(n+1);
  9. int psum = 0;
  10. for (int i = 1; i <= n; i++){
  11. cin >> arr[i];
  12. p[i] = p[i-1]+arr[i];
  13. }
  14.  
  15. int count = 0;
  16. unordered_map<int, int> freq;
  17. freq[0] = 1;
  18. for(int j = 1; j <= n; j++){
  19. int rhs = ((p[j] % k) - (j % k) + k) % k;
  20. if(freq.find(rhs) != freq.end()){
  21. count += freq[rhs];
  22. }
  23.  
  24. freq[rhs]++;
  25. }
  26.  
  27. cout << count;
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5288KB
stdin
5 4
1 4 3 2 4
stdout
2