-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF.cpp
52 lines (51 loc) · 1.07 KB
/
F.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "bits/stdc++.h"
using namespace std;
int p[200010];
int nxt[200010];
const int magic = 107;
int main(int argc, char const *argv[])
{
ios_base :: sync_with_stdio (false);
cin.tie(0);
string s;
cin >> s;
int n = s.size();
s = "$" + s;
for(int i = 1; i <= n; i++) {
p[i] = p[i - 1] + (s[i] == '1');
}
nxt[n + 1] = n + 1;
for(int i = n; i >= 1; i--) {
nxt[i] = nxt[i + 1];
if(s[i] == '1') nxt[i] = i;
}
long long ans = 0;
for(int k = 1; k <= magic; k++) {
map <int, int> mp;
mp[0] += 1;
for(int i = 1; i <= n; i++) {
ans += mp[i - k * p[i]];
mp[i - k * p[i]] += 1;
}
}
for(int i = 1; i <= n; i++) {
int cnt = 0;
int cur = i;
int tmp;
while(cnt * magic <= n) {
cur = nxt[cur];
if(cur > n) break;
cnt += 1;
tmp = nxt[cur + 1] - 1;
int start = max(magic, ((cur - i + 1) / cnt));
int end = (tmp - i + 1) / cnt;
ans += max(0, end - start);
if((cur - i + 1) % cnt == 0 && (cur - i + 1) / cnt > magic) {
++ans;
}
cur += 1;
}
}
cout << ans << endl;
return 0;
}