fork download
  1. for _ in range(int(input())):
  2. n = int(input())
  3. a = input()
  4. d = {0 : 1}
  5. res, s = 0, 0
  6.  
  7. for i in range(n):
  8. s += int(a[i])
  9. x = s - i - 1
  10. if x not in d:
  11. d[x] = 0
  12. d[x] += 1
  13. res += d[x] - 1
  14.  
  15. print(res)# your code goes here
Success #stdin #stdout 0.1s 14104KB
stdin
3
3
120
5
11011
6
600005
stdout
3
6
1