fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int t; cin >> t;
  6. while (t--) {
  7. int n; string str;
  8. cin >> n >> str;
  9.  
  10. long long ans = 0, prefix = 0;
  11. unordered_map<long long, long long> freq;
  12. freq[0] = 1;
  13.  
  14. for (int i = 0; i < n; i++) {
  15. prefix += (str[i] - '0');
  16. long long key = prefix - (i + 1);
  17. ans += freq[key];
  18. freq[key]++;
  19. }
  20.  
  21. cout << ans << endl;
  22. }
  23.  
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0.01s 5324KB
stdin
3
3
120
5
11011
6
600005
stdout
3
6
1