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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cout << elem;
  }
  cerr << endl;
}

int count_bits(unsigned x) {
  return (x == 0) ? 0 : count_bits(x >> 1) + (x & 1);
} // 自然数のビット数を求める.

template <class Type_k, class Type_p>
Type_p mod_pow(long long n, Type_k k, Type_p p) { // n^k(mod p).
  if (k == 0)
    return 1;
  long long res = mod_pow(n * n % p, k / 2, p);
  if (k & 1)
    res = res * n % p;
  return res;
}

// ツイッター参考.
int main() { // 大きくてもn桁なので1回目の操作でn=0~n-1になる!
  int n;
  string s;
  cin >> n >> s;

  vi dp(n); // dp[i]:=f(i) (i:0~n-1).
  dp[0] = 0;
  for (int i = 1; i < n; ++i) {
    int tmp = count_bits(i);
    dp[i] = dp[i % tmp] + 1;
  }

  int num = 0;     // sの'1'の数.
  int sum[2] = {}; // mod num+1とnum-1の場合の合計.
  REP(i, n) {
    if (s[i] == '1')
      num++;
  }
  REP(i, n) {
    if (s[i] == '1') {
      sum[0] = (sum[0] + mod_pow(2LL, n - 1 - i, num + 1)) % (num + 1);
      if (num > 1)
        sum[1] = (sum[1] + mod_pow(2LL, n - 1 - i, num - 1)) % (num - 1);
    }
  }

  REP(i, n) {
    if (s[i] == '0') {
      int tmp = (sum[0] + mod_pow(2LL, n - 1 - i, num + 1)) % (num + 1);
      cout << dp[tmp] + 1 << endl;
    } else {
      if (num == 1)
        cout << 0 << endl; // 0割り防止!
      else {
        int tmp =
            ((num - 1) + sum[1] - mod_pow(2LL, n - 1 - i, num - 1)) % (num - 1);
        cout << dp[tmp] + 1 << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cout << elem;
  }
  cerr << endl;
}

int count_bits(unsigned x) {
  return (x == 0) ? 0 : count_bits(x >> 1) + (x & 1);
} // 自然数のビット数を求める.

template <class Type_k, class Type_p>
Type_p mod_pow(long long n, Type_k k, Type_p p) { // n^k(mod p).
  if (k == 0)
    return 1;
  long long res = mod_pow(n * n % p, k / 2, p);
  if (k & 1)
    res = res * n % p;
  return res;
}

// ツイッター参考.
int main() { // 大きくてもn桁なので1回目の操作でn=0~n-1になる!
  int n;
  string s;
  cin >> n >> s;

  vi dp(n); // dp[i]:=f(i) (i:0~n-1).
  dp[0] = 0;
  for (int i = 1; i < n; ++i) {
    int tmp = count_bits(i);
    dp[i] = dp[i % tmp] + 1;
  }

  int num = 0;     // sの'1'の数.
  int sum[2] = {}; // mod num+1とnum-1の場合の合計.
  REP(i, n) {
    if (s[i] == '1')
      num++;
  }
  REP(i, n) {
    if (s[i] == '1') {
      sum[0] = (sum[0] + mod_pow(2LL, n - 1 - i, num + 1)) % (num + 1);
      sum[1] = (sum[1] + mod_pow(2LL, n - 1 - i, num - 1)) % (num - 1);
    }
  }

  REP(i, n) {
    if (s[i] == '0') {
      int tmp = (sum[0] + mod_pow(2LL, n - 1 - i, num + 1)) % (num + 1);
      cout << dp[tmp] + 1 << endl;
    } else {
      if (num == 1)
        cout << 0 << endl; // 0割り防止!
      else {
        int tmp =
            ((num - 1) + sum[1] - mod_pow(2LL, n - 1 - i, num - 1)) % (num - 1);
        cout << dp[tmp] + 1 << endl;
      }
    }
  }
}