Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a, n, t)                                                            \\
  for (int ii = 0; ii < n; ii++)                                               \\
  scanf("%" #t "", &(a)[ii])
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, a, n) for (int i = n - 1; i >= a; i--)
#define validl(i, a, b) ((i >= a) && (i <= b)) ? 1 : 0
#define validmat(x, y, a, b, c, d) (validl(x, a, b) && validl(y, a, b)) ? 1 : 0
#define validmn(x, y, m, n) validmat(x, y, 0, m, 0, n)
#define valid(i, n) validl(i, 0, n)
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define mk make_pair
#define pb push_back
#define pa(a, n, t)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  printf("%" #t "", a[i])
#define p2 pair<int, int>
#define p3 pair<p2, int>
#define fi first
#define se second
#define sd(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld", a)
#define pd(a) printf("%d", a)
#define sf(a) scanf("%lf", &a)
#define pf(a) printf("%lf", a)
#define nline printf("\\n")
#define ss(a) scanf("%s", a)
#define ps(a) printf("%s", a)
#define sc(a) scanf("%c", &a)
#define pc(a) printf("%c", &a)
#define mf(m, f) m.find(f) != m.end()
#define pp3(m) cout << m.fi.fi << " " << m.fi.se << " " << m.se << " "
#define pp2(m) cout << m.fi << " " << m.se << " "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
    rep(j,adj[i].size()){
        if(p!=adj[i][j])
            dfs(adj[i][j],i);
    }
}*/
int div(char *res, char *s1, int divisor) {
  int tempval = 0, currpos = 0, currres = 0;
  while (tempval < divisor && s1[currpos] != '\\0') {
    tempval = tempval * 2 + s1[currpos++] - 48;
  }
  while (s1[currpos] != '\\0') {
    res[currres++] = tempval / divisor + 48;
    tempval %= divisor;
    tempval = tempval * 2 + s1[currpos++] - 48;
  }
  res[currres++] = tempval / divisor + 48;
  res[currres] = '\\0';
  return tempval % divisor;
}
int main() {
  int t, n;
  cin >> n;
  int arr[200001] = {0};
  arr[1] = 1;
  reps(i, 2, 200001) { arr[i] = arr[i % __builtin_popcount(i)] + 1; }
  lli brr[200001][2] = {0};
  string s;
  cin >> s;
  int c = 0;
  // int l=strlen(s);
  rep(i, s.length()) {
    if (s[i] == '1')
      c++;
  }
  lli l1 = 1, l2 = 1;
  lli fans[2] = {0};
  rep(i, s.length()) {
    brr[i][0] = l1;
    brr[i][1] = l2;
    if (s[s.length() - 1 - i] == '1') {
      fans[0] = (fans[0] + l1) % (c + 1);
      if (c - 1)
        fans[1] = (fans[1] + l2) % (c - 1);
    }
    l1 = (l1 * 2) % (c + 1);
    if (c - 1)
      l2 = (l2 * 2) % (c - 1);
  }
  int cc = c, val;

  // strcpy(str,s);
  rep(i, s.length()) {
    cc = c;
    if (s[i] == '1') {
      cc--;
      if (cc == 0) {
        cout << "0\\n";
        continue;
      }
      val = (fans[1] - brr[s.length() - 1 - i][1] + cc) % cc;
      // cout<<"vw"<<val<<" "<<fans[1]<<" "<<brr[s.length()-1-i][1]<<"
      // "<<cc<<endl;
      if (val)
        cout << 1 + arr[val] << "\\n";
      else
        cout << "1\\n";
    } else {
      cc++;
      val = (fans[0] + brr[s.length() - 1 - i][0]) % cc;
      // cout<<"vq"<<val<<" "<<fans[0]<<" "<<brr[s.length()-1-i][0]<<"
      // "<<cc<<endl;
      if (val)
        cout << 1 + arr[val] << "\\n";
      else
        cout << "1\\n";
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a, n, t)                                                            \\
  for (int ii = 0; ii < n; ii++)                                               \\
  scanf("%" #t "", &(a)[ii])
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, a, n) for (int i = a; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rreps(i, a, n) for (int i = n - 1; i >= a; i--)
#define validl(i, a, b) ((i >= a) && (i <= b)) ? 1 : 0
#define validmat(x, y, a, b, c, d) (validl(x, a, b) && validl(y, a, b)) ? 1 : 0
#define validmn(x, y, m, n) validmat(x, y, 0, m, 0, n)
#define valid(i, n) validl(i, 0, n)
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define mk make_pair
#define pb push_back
#define pa(a, n, t)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  printf("%" #t "", a[i])
#define p2 pair<int, int>
#define p3 pair<p2, int>
#define fi first
#define se second
#define sd(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pl(a) printf("%lld", a)
#define pd(a) printf("%d", a)
#define sf(a) scanf("%lf", &a)
#define pf(a) printf("%lf", a)
#define nline printf("\\n")
#define ss(a) scanf("%s", a)
#define ps(a) printf("%s", a)
#define sc(a) scanf("%c", &a)
#define pc(a) printf("%c", &a)
#define mf(m, f) m.find(f) != m.end()
#define pp3(m) cout << m.fi.fi << " " << m.fi.se << " " << m.se << " "
#define pp2(m) cout << m.fi << " " << m.se << " "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
    rep(j,adj[i].size()){
        if(p!=adj[i][j])
            dfs(adj[i][j],i);
    }
}*/
int div(char *res, char *s1, int divisor) {
  int tempval = 0, currpos = 0, currres = 0;
  while (tempval < divisor && s1[currpos] != '\\0') {
    tempval = tempval * 2 + s1[currpos++] - 48;
  }
  while (s1[currpos] != '\\0') {
    res[currres++] = tempval / divisor + 48;
    tempval %= divisor;
    tempval = tempval * 2 + s1[currpos++] - 48;
  }
  res[currres++] = tempval / divisor + 48;
  res[currres] = '\\0';
  return tempval % divisor;
}
int main() {
  int t, n;
  cin >> n;
  int arr[200001] = {0};
  arr[1] = 1;
  reps(i, 2, 200001) { arr[i] = arr[i % __builtin_popcount(i)] + 1; }
  char s[200002];
  char str[200002];
  char res[200002];
  // string s;
  cin >> s;
  int c = 0;
  int l = strlen(s);
  rep(i, l) {
    if (s[i] == '1')
      c++;
  }
  int cc = c, val;
  strcpy(str, s);
  rep(i, l) {
    cc = c;
    if (s[i] == '1')
      cc--;
    else
      cc++;

    if (s[i] == '1')
      str[i] = '0';
    else
      str[i] = '1';
    if (cc) {
      val = div(res, str, cc);
      cout << 1 + arr[val] << "\\n";
    } else {
      val = 0;
      cout << 0 << "\\n";
    }
    str[i] = s[i];
  }
  return 0;
}