cf 1217c
https://codeforc.es/problemset/problem/1217/C
这题明显是$O(nlogn)$的做法,因为二进制的位置还是很有限的,首位连续的0我们可以用前缀和来判断。
1 #include <bits/stdc++.h> 2 using namespace std; 3 int const N = 200000 + 10; 4 int sum[N], t; 5 char s[N]; 6 int main() 7 { 8 scanf("%d", &t); 9 while (t--) 10 { 11 scanf("%s", s + 1); 12 int len = strlen(s + 1); 13 for (int i = 1; i <= len; i++) 14 sum[i] = sum[i - 1] + (s[i] == '1'); 15 int ans = 0; 16 for (int i = 1; i <= len; i++) 17 { 18 int k = 0; 19 for (int j = i; j >= 1; j--) 20 { 21 int p = i - j; 22 if ((1 << p) + k > i) 23 { 24 if (k > p && k <= i && sum[j] - sum[i - k] == 0) 25 ans++; 26 break; 27 } 28 if (s[j] == '1') 29 k += (1 << p); 30 if (p + 1 == k) 31 ans++; 32 } 33 } 34 printf("%d\n", ans); 35 } 36 return 0; 37 }