fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <iomanip>
  9. #include <stack>
  10. #include <unordered_map>
  11.  
  12. using namespace std;
  13. #define all(v) (v.begin()), (v.end())
  14.  
  15. void fast_io()
  16. {
  17. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  18. }
  19.  
  20. int main()
  21. {
  22. fast_io();
  23. int n, k;
  24. cin >> n >> k;
  25. int a[n];
  26. for (int i = 0; i < n; i++){
  27. cin >> a[i];
  28. }
  29.  
  30. int res = 0;
  31. unordered_map<int, int> countMap;
  32. for (int i = 0; i < n; i++){
  33. int counter = 0;
  34. int sum = 0;
  35.  
  36. while (i+k-1 < n && counter < k){
  37. sum += a[i+counter];
  38. counter++;
  39. }
  40. if (!sum) break; // Idea
  41.  
  42. if (!countMap.count(sum) && sum){
  43. countMap[sum] = 1;
  44. //cout << sum << endl;
  45. res++;
  46. }
  47. }
  48. cout << res << endl;
  49. }
Success #stdin #stdout 0.01s 5288KB
stdin
4 1
1 2 3 1
stdout
3