-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathBit Substrings.cpp
86 lines (72 loc) · 1.73 KB
/
Bit Substrings.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef complex<ld> cd;
const int maxN = 2e5+5;
const ld PI = acos(-1);
ll ans[maxN];
char S[maxN];
void fft(vector<cd> &a, bool inv){
int N = (int) a.size();
for(int i = 1, j = 0; i < N; i++){
int bit = N>>1;
for(; j&bit; bit >>= 1)
j ^= bit;
j ^= bit;
if(i < j)
swap(a[i], a[j]);
}
for(int len = 2; len <= N; len <<= 1){
ld theta = 2*PI / len * (inv ? -1 : 1);
cd wlen(cos(theta), sin(theta));
for(int i = 0; i < N; i += len){
cd w(1);
for(int j = 0; j < len / 2; j++){
cd u = a[i+j], v = a[i+j+len/2] * w;
a[i+j] = u + v;
a[i+j+len/2] = u - v;
w *= wlen;
}
}
}
if(inv)
for(cd &z : a)
z /= N;
}
int next_two_pow(int x){
return 1<<(32 - __builtin_clz(x));
}
void solve(int l, int r){
if(l+1 == r){
ans[(int) (S[l] - '0')]++;
return;
}
int m = (l+r)/2;
int sz = next_two_pow(r-l);
vector<cd> A(sz), B(sz);
for(int i = m-1, cnt = 0; i >= l; i--){
if(S[i] == '1') cnt++;
A[cnt] += 1;
}
for(int i = m, cnt = 0; i < r; i++){
if(S[i] == '1') cnt++;
B[cnt] += 1;
}
fft(A, false);
fft(B, false);
for(int i = 0; i < sz; i++)
A[i] *= B[i];
fft(A, true);
for(int i = 0; i < sz && i < maxN; i++)
ans[i] += llround(A[i].real());
solve(l, m);
solve(m, r);
}
int main(){
scanf(" %s", S);
int N = (int) strlen(S);
solve(0, N);
for(int i = 0; i <= N; i++)
printf("%lld%c", ans[i], (" \n")[i==N]);
}