#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

long long pre[101010] = {0,};

int main() {
    ios_base::sync_with_stdio(false);
   cin.tie(NULL);
  
    int n; 
    cin >> n;
    string s;
    cin >> s; 
    for (int i = 1; i <= n; i++) {
        pre[i] = s[i - 1] - '0' + pre[i-1];
    }
    for (int i = 0; i <= n; i++) {
        pre[i] = pre[i] - i;
    }
    
    long long cnt = 0; 
    long long count = 1; 
    for (int i = 1; i <= n; i++) {
        if (pre[i] == pre[i - 1]) {
            count++;
        } 
        else {
            cnt += count * (count - 1) / 2;
            count = 1;
        }
    }
    cout << cnt;
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: