Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <cmath>
- #include <cstdio>
- #include <iomanip>
- #include <fstream>
- #include <cassert>
- #include <cstring>
- #include <unordered_set>
- #include <unordered_map>
- #include <numeric>
- #include <ctime>
- #include <bitset>
- #include <complex>
- #include <random>
- using namespace std;
- #define int long long
- int stupid(string s) {
- int n = (int)s.size();
- vector<int> ps(n + 1);
- for (int i = 0; i < n; i++) {
- ps[i + 1] = ps[i] + s[i] - '0';
- }
- int ans = 0;
- for (int i = 0; i < n; i++) {
- for (int j = i + 1; j <= n; j++) {
- if (ps[i] != ps[j] && (j - i) % (ps[j] - ps[i]) == 0) ans++;
- }
- }
- return ans;
- }
- int solve(string s) {
- const int LIM = 300;
- int ans = 0;
- int n = (int)s.size();
- vector<int> ps(n + 1);
- for (int i = 0; i < n; i++) {
- ps[i + 1] = ps[i] + s[i] - '0';
- }
- vector<int> val(n + 1);
- vector<int> tv(n + 1);
- iota(val.begin(), val.end(), 0);
- for (int i = 1; i < LIM; i++) {
- for (int j = 0; j <= n; j++) {
- val[j] -= ps[j];
- tv[j] = val[j];
- }
- sort(tv.begin(), tv.end());
- for (int j = 0; j <= n; j++) {
- int r = j;
- while (r + 1 <= n && tv[r] == tv[r + 1]) {
- r++;
- }
- int len = r - j + 1;
- ans += (len) * (len - 1) / 2;
- j = r;
- }
- }
- vector<int> pos;
- for (int i = 0; i < n; i++) {
- if (s[i] == '1') {
- pos.push_back(i);
- }
- }
- pos.push_back(n);
- int possz = (int)pos.size() - 1;
- int cur_cnt = 0;
- for (int i = 0; i < n; i++) {
- for (int j = cur_cnt; j < min(possz, cur_cnt + n / LIM + 1); j++) {
- int cnt_1 = j - cur_cnt + 1;
- int cur_lol = i + cnt_1 * LIM;
- {
- int delta = pos[j] - cur_lol + 1;
- if (delta > 0) {
- cur_lol += (cnt_1) * ((delta + cnt_1 - 1) / cnt_1);
- }
- }
- if (cur_lol <= pos[j + 1]){
- int delta = pos[j + 1] - cur_lol;
- ans += (delta / cnt_1) + 1;
- }
- }
- if (s[i] == '1') {
- cur_cnt++;
- }
- }
- return ans;
- }
- void gen(int n) {
- string s;
- for (int i = 0; i < n; i++) {
- s.push_back('0' + rand() % 2);
- }
- if (stupid(s) != solve(s)) {
- cout << "WA " << s << endl;
- exit(0);
- } else {
- cout << "OK" << endl;
- }
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- // int n;
- // cin >> n;
- // while (true) {
- // gen(n);
- // }
- string s;
- cin >> s;
- cout << solve(s) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement