Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define lli long long int
#define pb(a) push_back(a)
#define forr(a, n) for (lli i = a; i < n; i++)
#define forrr(j, a, n) for (lli j = a; j < n; j++)
#define all(x) x.begin(), x.end()
#define pi2 1.57079632679489661923
using namespace std;
void print(lli a[], lli n) {
  forr(0, n) { cout << a[i] << " "; }
  cout << "\\n";
}
lli power(lli x) {
  lli u = 0;
  while (x % 2 == 0) {
    u++;
    x = x / 2;
  }
  return u;
}

lli powM(lli x, lli y, lli m) {
  if (m == 1) {
    return 0;
  }
  lli ans = 1, r = 1;
  x %= m;
  while (r > 0 && r <= y) {
    if (r & y) {
      ans *= x;
      ans %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return ans;
}
int powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
lli gcd(lli a, lli b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
/*
vector <lli> adj[100001];
lli vis[100001]={0};
void dfs(lli a){
    vis[a]=1;
    forr(0,adj[a].size()){
    if(vis[adj[a][i]]){continue;}
    else{
        dfs(adj[a][i]);
    }
    }
}
lli level[100001];
void bfs(lli a){
queue <lli> q;
vis[a]=1;
level[a]=0;
q.push(a);
while(!q.empty()){
    lli p=q.front();
    q.pop();
    forr(0,adj[p].size()){
    if(vis[adj[p][i]]==0){
        level[adj[p][i]]=level[p]+1;
        q.push(adj[p][i]);
        vis[adj[p][i]]=1;
    }
    }

}

}

vector <pair<lli,lli> > dj[100001];
lli dist[100001];
vector<lli> ans[100001];
void dijkstra(lli a){
dist[a]=0;
ans[a].pb(a);
multiset<pair<lli,lli> > s;
s.insert({0,a});
while(s.size()!=0){
    lli x=(*s.begin()).second;
    s.erase(s.begin());
    if(vis[x]){continue;}
    vis[x]=1;
    forr(0,dj[x].size()){
    lli e=dj[x][i].first;
    lli we=dj[x][i].second;
    if(dist[x]+we<dist[e]){
        dist[e]=dist[x]+we;
        ans[e].clear();
        ans[e].pb(x);
        s.insert({dist[e],e});
    }
    }
}

}
*/
unsigned int b(unsigned int n) {
  unsigned int count = 0;
  while (n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("outpu.txt","w",stdout);
  int t1 = 1;
  // cin>>t1;
  lli M = 1e9 + 7;
  while (t1--) {
    lli n;
    cin >> n;
    string s;
    cin >> s;
    lli u = 0;
    forr(0, n) {
      if (s[i] == '1') {
        u++;
      }
    }

    lli x = 0, y = 0;
    if (u != 1) {
      forr(0, n) {
        x = (x + ((s[i] - '0') * powM(2, n - 1 - i, u - 1))) % (u - 1);
        // cout<<x<<" ";
      }
    }
    forr(0, n) {
      y = (y + (powM(2, n - 1 - i, u + 1) * (s[i] - '0'))) % (u + 1);
    }
    // cout<<3%(-2)<<" ";
    forr(0, n) {
      if (s[i] == '0') {
        lli a = y;
        a = (a + (powM(2, n - 1 - i, u + 1))) % (u + 1);
        lli ans = 1;
        while (a > 0) {
          a = a % b(a);
          ans++;
        }
        cout << ans << "\\n";
      } else {
        if (u == 1) {
          cout << "0\\n";
        } else {
          lli a = x;
          if (a - (powM(2, n - 1 - i, u - 1)) < 0) {
            a = ((u - 1) - (((powM(2, n - 1 - i, u - 1)) - a) % (u - 1))) %
                (u - 1);
          } else {
            a = (a - (powM(2, n - 1 - i, u - 1))) % (u - 1);
          }
          lli ans = 1;
          while (a > 0) {
            a = a % b(a);
            ans++;
          }
          cout << ans << "\\n";
        }
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define lli long long int
#define pb(a) push_back(a)
#define forr(a, n) for (lli i = a; i < n; i++)
#define forrr(j, a, n) for (lli j = a; j < n; j++)
#define all(x) x.begin(), x.end()
#define pi2 1.57079632679489661923
using namespace std;
void print(lli a[], lli n) {
  forr(0, n) { cout << a[i] << " "; }
  cout << "\\n";
}
lli power(lli x) {
  lli u = 0;
  while (x % 2 == 0) {
    u++;
    x = x / 2;
  }
  return u;
}

lli powM(lli x, lli y, lli m) {
  if (m == 1) {
    return 0;
  }
  lli ans = 1, r = 1;
  x %= m;
  while (r > 0 && r <= y) {
    if (r & y) {
      ans *= x;
      ans %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return ans;
}
int powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
lli gcd(lli a, lli b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
bool isPrime(int n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
/*
vector <lli> adj[100001];
lli vis[100001]={0};
void dfs(lli a){
    vis[a]=1;
    forr(0,adj[a].size()){
    if(vis[adj[a][i]]){continue;}
    else{
        dfs(adj[a][i]);
    }
    }
}
lli level[100001];
void bfs(lli a){
queue <lli> q;
vis[a]=1;
level[a]=0;
q.push(a);
while(!q.empty()){
    lli p=q.front();
    q.pop();
    forr(0,adj[p].size()){
    if(vis[adj[p][i]]==0){
        level[adj[p][i]]=level[p]+1;
        q.push(adj[p][i]);
        vis[adj[p][i]]=1;
    }
    }

}

}

vector <pair<lli,lli> > dj[100001];
lli dist[100001];
vector<lli> ans[100001];
void dijkstra(lli a){
dist[a]=0;
ans[a].pb(a);
multiset<pair<lli,lli> > s;
s.insert({0,a});
while(s.size()!=0){
    lli x=(*s.begin()).second;
    s.erase(s.begin());
    if(vis[x]){continue;}
    vis[x]=1;
    forr(0,dj[x].size()){
    lli e=dj[x][i].first;
    lli we=dj[x][i].second;
    if(dist[x]+we<dist[e]){
        dist[e]=dist[x]+we;
        ans[e].clear();
        ans[e].pb(x);
        s.insert({dist[e],e});
    }
    }
}

}
*/
unsigned int b(unsigned int n) {
  unsigned int count = 0;
  while (n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("outpu.txt","w",stdout);
  int t1 = 1;
  // cin>>t1;
  lli M = 1e9 + 7;
  while (t1--) {
    lli n;
    cin >> n;
    string s;
    cin >> s;
    lli u = 0;
    forr(0, n) {
      if (s[i] == '1') {
        u++;
      }
    }
    if (u == 0) {
      forr(0, n) { cout << "0\\n"; }
      return 0;
    }
    lli x = 0, y = 0;
    forr(0, n) {
      x = (x + ((s[i] - '0') * powM(2, n - 1 - i, u - 1))) % (u - 1);
      // cout<<x<<" ";
    }
    forr(0, n) {
      y = (y + (powM(2, n - 1 - i, u + 1) * (s[i] - '0'))) % (u + 1);
    }
    // cout<<3%(-2)<<" ";
    forr(0, n) {
      if (s[i] == '0') {
        lli a = y;
        a = (a + (powM(2, n - 1 - i, u + 1))) % (u + 1);
        lli ans = 1;
        while (a > 0) {
          a = a % b(a);
          ans++;
        }
        cout << ans << "\\n";
      } else {
        if (u == 1) {
          cout << "0\\n";
        } else {
          lli a = x;
          if (a - (powM(2, n - 1 - i, u - 1)) < 0) {
            a = (u - 1) - (((powM(2, n - 1 - i, u - 1)) - a) % (u - 1));
          } else {
            a = (a - (powM(2, n - 1 - i, u - 1))) % (u - 1);
          }
          lli ans = 1;
          while (a > 0) {
            a = a % b(a);
            ans++;
          }
          cout << ans << "\\n";
        }
      }
    }
  }

  return 0;
}