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

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

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int N, num = 0;
  cin >> N;
  char X[N];
  for (int i = 0; i < N; i++) {
    cin >> X[i];
    if (X[i] == '1') {
      num++;
    }
  }

  int p[N], m[N];
  p[0] = 1;
  m[0] = 1;
  for (int i = 1; i < N; i++) {
    p[i] = p[i - 1] * 2;
    if (p[i] > (num + 1)) {
      p[i] = p[i] % (num + 1);
    }
    // cout << p[i] << ",";
  }
  // cout << endl;
  if (num > 1) {
    for (int i = 1; i < N; i++) {
      m[i] = m[i - 1] * 2;
      if (m[i] > (num - 1)) {
        m[i] = m[i] % (num - 1);
      }
      // cout << m[i] << ",";
    }
  } else {
    for (int i = 0; i < N; i++) {
      m[i] = 0;
    }
  }
  // cout <<endl;

  int t[2];
  for (int i = 0; i < 2; i++) {
    t[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      t[0] = t[0] + m[N - i - 1];
      t[1] = t[1] + p[N - i - 1];
      if (t[0] > 2 * (num - 1)) {
        t[0] = t[0] - (num - 1);
      }
      if (t[1] > 2 * (num + 1)) {
        t[1] = t[1] - (num + 1);
      }
    }
  }

  // cout << t[0] <<endl;
  // cout << t[1] <<endl;
  // cout << t[2] <<endl;
  int n, count, proc, tmp;
  for (int i = 0; i < N; i++) {
    count = 0;
    if (X[i] == '1') {
      proc = num - 1;
      n = t[0] - m[N - 1 - i];
    } else {
      proc = num + 1;
      n = t[1] + p[N - 1 - i];
    }
    if (proc == 0) {
      n = 0;
    } else if (n == 0) {
      count = 1;
    }
    // cout << proc <<":" << n <<endl;
    while (n != 0) {
      n = n % proc;
      tmp = n;
      proc = 0;
      while (tmp > 0) {
        if (tmp % 2 == 1) {
          proc++;
        }
        tmp = tmp / 2;
      }
      count++;
    }

    cout << count << endl;
  }
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}


Error: WA

Fixed submission:

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

long long int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int N, num = 0;
  cin >> N;
  char X[N];
  for (int i = 0; i < N; i++) {
    cin >> X[i];
    if (X[i] == '1') {
      num++;
    }
  }

  int p[N], m[N];
  p[0] = 1;
  m[0] = 1;
  for (int i = 1; i < N; i++) {
    p[i] = p[i - 1] * 2;
    if (p[i] > (num + 1)) {
      p[i] = p[i] % (num + 1);
    }
    // cout << p[i] << ",";
  }
  // cout << endl;
  if (num > 1) {
    for (int i = 1; i < N; i++) {
      m[i] = m[i - 1] * 2;
      if (m[i] > (num - 1)) {
        m[i] = m[i] % (num - 1);
      }
      // cout << m[i] << ",";
    }
  } else {
    for (int i = 0; i < N; i++) {
      m[i] = 0;
    }
  }
  // cout <<endl;

  int t[2];
  for (int i = 0; i < 2; i++) {
    t[i] = 0;
  }
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      t[0] = t[0] + m[N - i - 1];
      t[1] = t[1] + p[N - i - 1];
      // cout << m[i] << ":" <<d[i] <<":" <<p[i] <<endl;
    }
  }

  // cout << t[0] <<endl;
  // cout << t[1] <<endl;
  // cout << t[2] <<endl;
  int n, count, proc, tmp;
  for (int i = 0; i < N; i++) {
    count = 0;
    if (X[i] == '1') {
      proc = num - 1;
      n = t[0] - m[N - 1 - i];
    } else {
      proc = num + 1;
      n = t[1] + p[N - 1 - i];
    }
    if (proc == 0) {
      n = 0;
    } else if (n == 0) {
      count = 1;
    }
    cout << proc << ":" << n << endl;
    while (n != 0) {
      n = n % proc;
      tmp = n;
      proc = 0;
      while (tmp > 0) {
        if (tmp % 2 == 1) {
          proc++;
        }
        tmp = tmp / 2;
      }
      count++;
    }

    cout << count << endl;
  }
}

long long int Exp(long long int exp_n, long long int exp_e,
                  long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}