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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
using P = pair<int, int>;
using lint = long long;
using ll = long long;
const lint inf = 1e18 + 7;
const int MOD = 1000000007;
vector<int> f;
int solve(int i) {
  if (f.size() > i) {
    return f[i];
  }
  int flag = __builtin_popcount(i);
  int res = solve(i % flag) + 1;
  return res;
}
int Smod(string const &s, int m) {
  if (m == 0)
    return 0;
  // stringからmodをとってintで返す
  int res = s[0] - '0';
  int n = s.length();
  for (int i = 1; i < n; ++i) {
    res = res * 2 + s[i] - '0';
    res %= m;
  }
  return res;
}
long long myPow(int x, int n, int rest) {
  int res = 1;
  int cur = x;
  while (n > 0) {
    if (n & 1) {
      res *= cur;
      res %= rest;
    }
    n /= 2;
    cur *= cur;
    cur %= rest;
  }
  return res;
}
signed main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  f.push_back(0);
  rep1(i, n) {
    int buf = solve(i);
    f.push_back(buf);
  }
  /*
  vector<int> hoge(n);
  rep(i,n){
    cin>>hoge[i];
  }*/
  int flag = 0;
  rep(i, n) {
    if (x[i] == '1')
      ++flag;
  }
  int z0 = Smod(x, flag);
  int p1 = Smod(x, flag + 1);
  int m1 = 0;
  if (flag > 0)
    m1 = Smod(x, flag - 1);
  rep(i, n) {
    int ans;
    if (x[i] == '0') {
      int buf = p1;
      buf += myPow(2, n - i - 1, flag + 1);
      buf %= flag + 1;
      ans = f[buf];
    } else if (flag > 1) {
      int buf = m1;
      buf += flag - 1 - myPow(2, n - i - 1, flag - 1);
      buf %= flag - 1;
      ans = f[buf];
    } else {
      ans = -1;
    }
    cout << ans + 1 << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
using P = pair<int, int>;
using lint = long long;
using ll = long long;
const lint inf = 1e18 + 7;
const int MOD = 1000000007;
vector<int> f;
int solve(int i) {
  if (f.size() > i) {
    return f[i];
  }
  int flag = __builtin_popcount(i);
  int res = solve(i % flag) + 1;
  return res;
}
int Smod(string const &s, int m) {
  // stringからmodをとってintで返す
  int res = s[0] - '0';
  int n = s.length();
  for (int i = 1; i < n; ++i) {
    res = res * 2 + s[i] - '0';
    res %= m;
  }
  return res;
}
long long myPow(int x, int n, int rest) {
  int res = 1;
  int cur = x;
  while (n > 0) {
    if (n & 1) {
      res *= cur;
      res %= rest;
    }
    n /= 2;
    cur *= cur;
    cur %= rest;
  }
  return res;
}
signed main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  f.push_back(0);
  rep1(i, n + 10) {
    int buf = solve(i);
    f.push_back(buf);
  }
  /*
  vector<int> hoge(n);
  rep(i,n){
    cin>>hoge[i];
  }*/
  int flag = 0;
  rep(i, n) {
    if (x[i] == '1')
      ++flag;
  }
  int z0 = Smod(x, flag);
  int p1 = Smod(x, flag + 1);
  int m1 = Smod(x, flag - 1);
  rep(i, n) {
    int ans;
    if (x[i] == '0') {
      int buf = p1;
      buf += myPow(2, n - i - 1, flag + 1);
      buf %= flag + 1;
      ans = f[buf];
    } else if (flag > 1) {
      int buf = m1;
      buf += flag - 1 - myPow(2, n - i - 1, flag - 1);
      buf %= flag - 1;
      ans = f[buf];
    } else {
      int buf = 0;
      ans = -1;
    }
    cout << ans + 1 << "\\n";
  }
  return 0;
}