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

#include <bits/stdc++.h>
using namespace std;

long long pop(long long num) {
  long long cnt = 0;
  while (num > 0) {
    if (num % 2 != 0) {
      cnt++;
    }
    num /= 2;
  }
  return cnt;
}

int main(void) {

  long long digit;
  cin >> digit;

  string s;
  cin >> s;

  long long cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == '1') {
      cnt++;
    }
  }

  vector<long long> op(cnt + 2), pow2m(s.size()), pow2p(s.size());
  long long summ = 0, sump = 0;
  for (int i = 0; i < cnt + 2; i++) {
    if (i == 0) {
      op.at(i) = 0;
    } else {
      op.at(i) = op.at(i % pop(i)) + 1;
    }
    // cout << i << " " << op.at(i) << endl;
  }

  for (int i = s.size() - 1; i >= 0; i--) {
    if (i == s.size() - 1) {
      if (cnt - 1 != 0) {
        pow2m.at(i) = 1 % (cnt - 1);
      }
      pow2p.at(i) = 1 % (cnt + 1);
    } else {
      if (cnt - 1 != 0) {
        pow2m.at(i) = pow2m.at(i + 1) * 2 % (cnt - 1);
      }
      pow2p.at(i) = pow2p.at(i + 1) * 2 % (cnt + 1);
    }
    if (s.at(i) == '1') {
      if (cnt - 1 != 0) {
        summ = (summ + pow2m.at(i)) % (cnt - 1);
      }
      sump = (sump + pow2p.at(i)) % (cnt + 1);
    }
  }

  // cout << cnt - 1 << " " << cnt + 1 << endl;
  // cout << summ << " " << sump << endl;

  //*
  long long treat;
  for (int i = 0; i < s.size(); i++) {
    // printf("%2d ", i);
    if (s.at(i) == '1') {
      if (cnt - 1 == 0) {
        cout << 0 << endl;
      } else {
        treat = (summ - pow2m.at(i)) % (cnt - 1);
        while (treat < 0) {
          treat = (treat + (cnt - 1)) % (cnt - 1);
        }
        // cout << treat % ( cnt - 1 ) << endl;
        cout << 1 + op.at(treat % (cnt - 1)) << endl;
      }
    } else {
      treat = (sump + pow2p.at(i)) % (cnt + 1);
      // cout << treat % ( cnt + 1 ) << endl;
      cout << 1 + op.at(treat % (cnt + 1)) << endl;
    }
  }
  //*/

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long pop(long long num) {
  long long cnt = 0;
  while (num > 0) {
    if (num % 2 != 0) {
      cnt++;
    }
    num /= 2;
  }
  return cnt;
}

int main(void) {

  long long digit;
  cin >> digit;

  string s;
  cin >> s;

  long long cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == '1') {
      cnt++;
    }
  }

  vector<long long> op(cnt + 2), pow2m(s.size()), pow2p(s.size());
  long long summ = 0, sump = 0;
  for (int i = 0; i < cnt + 2; i++) {
    if (i == 0) {
      op.at(i) = 0;
    } else {
      op.at(i) = op.at(i % pop(i)) + 1;
    }
    // cout << i << " " << op.at(i) << endl;
  }

  for (int i = s.size() - 1; i >= 0; i--) {
    if (i == s.size() - 1) {
      pow2m.at(i) = 1 % (cnt - 1);
      pow2p.at(i) = 1 % (cnt + 1);
    } else {
      pow2m.at(i) = pow2m.at(i + 1) * 2 % (cnt - 1);
      pow2p.at(i) = pow2p.at(i + 1) * 2 % (cnt + 1);
    }
    if (s.at(i) == '1') {
      summ = (summ + pow2m.at(i)) % (cnt - 1);
      sump = (sump + pow2p.at(i)) % (cnt + 1);
    }
  }

  // cout << cnt - 1 << " " << cnt + 1 << endl;
  // cout << summ << " " << sump << endl;

  //*
  long long treat;
  for (int i = 0; i < s.size(); i++) {
    // printf("%2d ", i);
    if (s.at(i) == '1') {
      if (cnt - 1 == 0) {
        cout << 0 << endl;
      } else {
        treat = (summ - pow2m.at(i)) % (cnt - 1);
        while (treat < 0) {
          treat = (treat + (cnt - 1)) & (cnt - 1);
        }
        // cout << treat % ( cnt - 1 ) << endl;
        cout << 1 + op.at(treat % (cnt - 1)) << endl;
      }
    } else {
      treat = (sump + pow2p.at(i)) % (cnt + 1);
      // cout << treat % ( cnt + 1 ) << endl;
      cout << 1 + op.at(treat % (cnt + 1)) << endl;
    }
  }
  //*/

  return 0;
}