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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using uint = unsigned int;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll, ll>>>;
#define rep(i, n) for (int i = 0; i < int(n); i++)

using namespace std;
ll MOD7 = 1000000007;
ll MOD9 = 1000000009;
vector<ll> InputVec(ll N) {
  vector<ll> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  return A;
}
void OutputVec(vector<ll> A) {
  for (int i = 0; i < A.size(); ++i) {
    cout << A[i] << ",";
  }
  cout << endl;
}
void OutputVec(vector<bool> A) {
  for (int i = 0; i < A.size(); ++i) {
    cout << A[i] << ",";
  }
  cout << endl;
}
vector<vector<ll>> InputVec2d(ll H, ll W) {
  vector<vector<ll>> A(H);
  for (int yi = 0; yi < H; ++yi) {
    A[yi] = vector<ll>(W);
  }
  for (int yi = 0; yi < H; ++yi) {
    for (int xi = 0; xi < W; ++xi) {
      cin >> A[yi][xi];
    }
  }
  return A;
}
vector<vector<char>> InputVec2dStr(ll H, ll W) {
  vector<vector<char>> A(H);
  for (int yi = 0; yi < H; ++yi) {
    A[yi] = vector<char>(W);
  }
  for (int yi = 0; yi < H; ++yi) {
    string S;
    cin >> S;
    for (int xi = 0; xi < W; ++xi) {
      A[yi][xi] = S[xi];
    }
  }
  return A;
}
void OutputVec2d(vector<vector<ll>> A) {
  for (int yi = 0; yi < A.size(); ++yi) {
    for (int xi = 0; xi < A[yi].size(); ++xi) {
      cout << A[yi][xi] << ",";
    }
    cout << endl;
  }
}
graph InputGraph(ll N, ll M) {
  graph G(N);
  for (int i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  return G;
}
graph InputDGraph(ll N, ll M) {
  graph G(N);
  for (int i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
  }
  return G;
}
wGraph InputWGraph(ll N, ll M, bool isNeg) {
  wGraph G(N);
  for (int i = 0; i < M; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (isNeg) {
      c = -c;
    }
    G[a].push_back(make_pair(b, c));
    G[b].push_back(make_pair(a, c));
  }
  return G;
}
wGraph InputDWGraph(ll N, ll M, bool isNeg) {
  wGraph G(N);
  for (int i = 0; i < M; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (isNeg) {
      c = -c;
    }
    G[a].push_back(make_pair(b, c));
  }
  return G;
}
void OutputGraph(graph G) {
  for (int i = 0; i < G.size(); ++i) {
    cout << i << ":";
    for (int j = 0; j < G[i].size(); ++j) {
      cout << G[i][j] << ",";
    }
    cout << endl;
  }
}

void OutputGraph(wGraph G) {
  for (int i = 0; i < G.size(); ++i) {
    cout << i << ":";
    for (int j = 0; j < G[i].size(); ++j) {
      cout << G[i][j].first << "/" << G[i][j].second << ",";
    }
    cout << endl;
  }
}

void OutputAnswerBool(bool b, bool isUpperCase = false) {
  if (b) {
    if (isUpperCase) {
      cout << "YES" << endl;
    } else {
      cout << "Yes" << endl;
    }
  } else {
    if (isUpperCase) {
      cout << "NO" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

int main() {
  ll N;
  cin >> N;
  string X;
  cin >> X;
  vector<ll> tmp(N);
  vector<ll> res(N, 0);
  ll popcount = 0;
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      popcount++;
    }
  }
  ll popUp = popcount + 1;
  ll popDown = popcount - 1;
  if (popcount == 1) {
    popDown = -1010;
  }
  // cout << popUp <<","<< popDown << endl;
  vector<ll> modDigUp(N);
  vector<ll> modDigDown(N);
  ll tmpModUp = 1;
  ll tmpModDown = 1;
  for (int i = 0; i < N; ++i) {
    tmpModUp %= popUp;
    tmpModDown %= popDown;
    modDigUp[i] = tmpModUp;
    modDigDown[i] = tmpModDown;
    tmpModUp *= 2;
    tmpModDown *= 2;
  }
  // OutputVec(modDigUp);
  // OutputVec(modDigDown);
  ll modUp = 0;
  ll modDown = 0;
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      modUp += modDigUp[N - 1 - i];
      modDown += modDigDown[N - 1 - i];
      modUp %= popUp;
      modDown %= popDown;
    }
  }
  // cout << modUp << "," << modDown << endl;
  for (int i = 0; i < N; ++i) {
    if (X[i] == '0') {
      ll mod = modUp;
      mod += modDigUp[N - 1 - i];
      mod %= popUp;
      tmp[i] = mod;
      res[i]++;
    } else {
      if (popDown == -1010) {
        tmp[i] = 0;
        res[i] = 0;
      } else {
        ll mod = modDown;
        mod -= modDigDown[N - 1 - i];
        mod += popDown;
        mod %= popDown;
        tmp[i] = mod;
        res[i]++;
      }
    }
  }
  // OutputVec(tmp);
  for (int i = 0; i < N; ++i) {
    ll current = tmp[i];
    while (current != 0) {
      ll t = current;
      ll tc = 0;
      while (t != 0) {
        if (t % 2 == 1) {
          tc++;
        }
        t /= 2;
      }
      current = current % tc;
      res[i]++;
    }
  }
  for (int i = 0; i < N; ++i) {
    cout << res[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using ll = long long;
using uint = unsigned int;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll, ll>>>;
#define rep(i, n) for (int i = 0; i < int(n); i++)

using namespace std;
ll MOD7 = 1000000007;
ll MOD9 = 1000000009;
vector<ll> InputVec(ll N) {
  vector<ll> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  return A;
}
void OutputVec(vector<ll> A) {
  for (int i = 0; i < A.size(); ++i) {
    cout << A[i] << ",";
  }
  cout << endl;
}
void OutputVec(vector<bool> A) {
  for (int i = 0; i < A.size(); ++i) {
    cout << A[i] << ",";
  }
  cout << endl;
}
vector<vector<ll>> InputVec2d(ll H, ll W) {
  vector<vector<ll>> A(H);
  for (int yi = 0; yi < H; ++yi) {
    A[yi] = vector<ll>(W);
  }
  for (int yi = 0; yi < H; ++yi) {
    for (int xi = 0; xi < W; ++xi) {
      cin >> A[yi][xi];
    }
  }
  return A;
}
vector<vector<char>> InputVec2dStr(ll H, ll W) {
  vector<vector<char>> A(H);
  for (int yi = 0; yi < H; ++yi) {
    A[yi] = vector<char>(W);
  }
  for (int yi = 0; yi < H; ++yi) {
    string S;
    cin >> S;
    for (int xi = 0; xi < W; ++xi) {
      A[yi][xi] = S[xi];
    }
  }
  return A;
}
void OutputVec2d(vector<vector<ll>> A) {
  for (int yi = 0; yi < A.size(); ++yi) {
    for (int xi = 0; xi < A[yi].size(); ++xi) {
      cout << A[yi][xi] << ",";
    }
    cout << endl;
  }
}
graph InputGraph(ll N, ll M) {
  graph G(N);
  for (int i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  return G;
}
graph InputDGraph(ll N, ll M) {
  graph G(N);
  for (int i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
  }
  return G;
}
wGraph InputWGraph(ll N, ll M, bool isNeg) {
  wGraph G(N);
  for (int i = 0; i < M; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (isNeg) {
      c = -c;
    }
    G[a].push_back(make_pair(b, c));
    G[b].push_back(make_pair(a, c));
  }
  return G;
}
wGraph InputDWGraph(ll N, ll M, bool isNeg) {
  wGraph G(N);
  for (int i = 0; i < M; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (isNeg) {
      c = -c;
    }
    G[a].push_back(make_pair(b, c));
  }
  return G;
}
void OutputGraph(graph G) {
  for (int i = 0; i < G.size(); ++i) {
    cout << i << ":";
    for (int j = 0; j < G[i].size(); ++j) {
      cout << G[i][j] << ",";
    }
    cout << endl;
  }
}

void OutputGraph(wGraph G) {
  for (int i = 0; i < G.size(); ++i) {
    cout << i << ":";
    for (int j = 0; j < G[i].size(); ++j) {
      cout << G[i][j].first << "/" << G[i][j].second << ",";
    }
    cout << endl;
  }
}

void OutputAnswerBool(bool b, bool isUpperCase = false) {
  if (b) {
    if (isUpperCase) {
      cout << "YES" << endl;
    } else {
      cout << "Yes" << endl;
    }
  } else {
    if (isUpperCase) {
      cout << "NO" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

int main() {
  ll N;
  cin >> N;
  string X;
  cin >> X;
  vector<ll> tmp(N);
  vector<ll> res(N, 0);
  ll popcount = 0;
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      popcount++;
    }
  }
  ll popUp = popcount + 1;
  ll popDown = popcount - 1;
  // cout << popUp <<","<< popDown << endl;
  vector<ll> modDigUp(N);
  vector<ll> modDigDown(N);
  ll tmpModUp = 1;
  ll tmpModDown = 1;
  for (int i = 0; i < N; ++i) {
    tmpModUp %= popUp;
    tmpModDown %= popDown;
    modDigUp[i] = tmpModUp;
    modDigDown[i] = tmpModDown;
    tmpModUp *= 2;
    tmpModDown *= 2;
  }
  // OutputVec(modDigUp);
  // OutputVec(modDigDown);
  ll modUp = 0;
  ll modDown = 0;
  for (int i = 0; i < N; ++i) {
    if (X[i] == '1') {
      modUp += modDigUp[N - 1 - i];
      modDown += modDigDown[N - 1 - i];
      modUp %= popUp;
      modDown %= popDown;
    }
  }
  // cout << modUp << "," << modDown << endl;
  for (int i = 0; i < N; ++i) {
    if (X[i] == '0') {
      ll mod = modUp;
      mod += modDigUp[N - 1 - i];
      mod %= popUp;
      tmp[i] = mod;
      res[i]++;
    } else {
      ll mod = modDown;
      mod -= modDigDown[N - 1 - i];
      mod += popDown;
      mod %= popDown;
      tmp[i] = mod;
      res[i]++;
    }
  }
  // OutputVec(tmp);
  for (int i = 0; i < N; ++i) {
    ll current = tmp[i];
    while (current != 0) {
      ll t = current;
      ll tc = 0;
      while (t != 0) {
        if (t % 2 == 1) {
          tc++;
        }
        t /= 2;
      }
      current = current % tc;
      res[i]++;
    }
  }
  for (int i = 0; i < N; ++i) {
    cout << res[i] << endl;
  }
}