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

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
// #define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, MOD;

// moddivとnCk
ll moddiv(ll a, ll b) {
  ll x0 = MOD, x1 = b, x2, n0 = 0LL, n1 = 1LL, n2, t = a % b, m, ans;
  if (t == 0LL)
    return a / b;
  for (int i = 0; i < 900; i++) {
    m = x0 / x1;
    x2 = x0 - x1 * m;
    n2 = (n0 - m * n1) % MOD;
    if (x2 == 1LL) {
      ans = (n2 + MOD) % MOD;
      break;
    }
    x0 = x1;
    x1 = x2;
    n0 = n1;
    n1 = n2;
  }
  return (a + ((t * ans) % MOD) * b - t) / b;
}
// 剰余の割ざん

// aのn乗 mod c
ll expmod(ll a, ll n) {
  ll ans = 1LL, aa = a, beki = n;
  for (int i = 0; i < 64; i++) {
    if (beki % 2 == 1LL)
      ans = ans * aa % MOD;
    aa = aa * aa % MOD;
    beki /= 2LL;
    if (beki == 0LL)
      break;
  }
  return ans;
}

// nCk
vi mulmod(500001); // 0!,1!,2!,3!,4!,,,,
void first_nCk() {
  mulmod[0] = 1LL;
  for (ll i = 1; i < 500001; i++) {
    mulmod[i] = mulmod[i - 1] * i % MOD;
  }
}
ll nCk(ll nn, ll kk) {
  return moddiv(mulmod[nn], mulmod[kk] * mulmod[nn - kk] % MOD);
}

int bitnum(int x) {
  int a = 0;
  for (ll i = 0; i < 32; i++) {
    a += x % 2;
    x /= 2;
  }
  return a;
}

int calc(int in) {
  int b = 0;
  for (ll i = 0; i < 100; i++) {
    if (bitnum(in) == 0)
      break;
    in = in % bitnum(in);
    b++;
  }
  return b;
}

int main() {
  ll ans = 0;
  cin >> n;
  string s;
  cin >> s;
  ll btnum = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      btnum++;
  }

  ll mod1, mod2;
  mod1 = btnum - 1;
  mod2 = btnum + 1;
  if (n == btnum) {
    mod2 = mod1;
  }
  if (btnum == 0) {
    mod1 = mod2;
  }

  ll val1 = 0, val2 = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (mod1 != 0) {
        MOD = mod1;
        val1 = (expmod(2LL, n - i - 1) + val1) % mod1;
      }

      if (mod2 != 0) {
        MOD = mod2;
        val2 = (expmod(2LL, n - i - 1) + val2) % mod2;
      }
    }
  }

  REP(i, n) {
    if (s[i] == '1') {
      if (mod1 == 0) {
        ans = -1;
        cout << (ans + 1) << endl;
        continue;
      }
      MOD = mod1;
      ans = val1 - expmod(2LL, n - i - 1);
      ans = (ans + mod1) % mod1;
      if (ans != 0) {
        ans = calc(ans);
      }

    } else {
      MOD = mod2;
      ans = val2 + expmod(2LL, n - i - 1);
      ans = (ans + mod2) % mod2;
      if (ans != 0) {
        ans = calc(ans);
      }
    }

    cout << (ans + 1) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define ll long long
// #define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>;  // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n, MOD;

// moddivとnCk
ll moddiv(ll a, ll b) {
  ll x0 = MOD, x1 = b, x2, n0 = 0LL, n1 = 1LL, n2, t = a % b, m, ans;
  if (t == 0LL)
    return a / b;
  for (int i = 0; i < 900; i++) {
    m = x0 / x1;
    x2 = x0 - x1 * m;
    n2 = (n0 - m * n1) % MOD;
    if (x2 == 1LL) {
      ans = (n2 + MOD) % MOD;
      break;
    }
    x0 = x1;
    x1 = x2;
    n0 = n1;
    n1 = n2;
  }
  return (a + ((t * ans) % MOD) * b - t) / b;
}
// 剰余の割ざん

// aのn乗 mod c
ll expmod(ll a, ll n) {
  ll ans = 1LL, aa = a, beki = n;
  for (int i = 0; i < 64; i++) {
    if (beki % 2 == 1LL)
      ans = ans * aa % MOD;
    aa = aa * aa % MOD;
    beki /= 2LL;
    if (beki == 0LL)
      break;
  }
  return ans;
}

// nCk
vi mulmod(500001); // 0!,1!,2!,3!,4!,,,,
void first_nCk() {
  mulmod[0] = 1LL;
  for (ll i = 1; i < 500001; i++) {
    mulmod[i] = mulmod[i - 1] * i % MOD;
  }
}
ll nCk(ll nn, ll kk) {
  return moddiv(mulmod[nn], mulmod[kk] * mulmod[nn - kk] % MOD);
}

int bitnum(int x) {
  int a = 0;
  for (ll i = 0; i < 32; i++) {
    a += x % 2;
    x /= 2;
  }
  return a;
}

int calc(int in) {
  int b = 0;
  for (ll i = 0; i < 100; i++) {
    if (bitnum(in) == 0)
      break;
    in = in % bitnum(in);
    b++;
  }
  return b;
}

int main() {
  ll ans = 0;
  cin >> n;
  string s;
  cin >> s;
  ll btnum = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      btnum++;
  }

  ll mod1, mod2;
  mod1 = btnum - 1;
  mod2 = btnum + 1;
  if (n == btnum) {
    mod2 = mod1;
  }
  if (btnum == 0) {
    mod1 = mod2;
  }

  ll val1 = 0, val2 = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      MOD = mod1;
      val1 = (expmod(2LL, n - i - 1) + val1) % mod1;

      MOD = mod2;
      val2 = (expmod(2LL, n - i - 1) + val2) % mod2;
    }
  }

  REP(i, n) {
    if (s[i] == '1') {
      MOD = mod1;
      ans = val1 - expmod(2LL, n - i - 1);
      ans = (ans + mod1) % mod1;
      if (ans != 0) {
        ans = calc(ans);
      }

    } else {
      MOD = mod2;
      ans = val2 + expmod(2LL, n - i - 1);
      ans = (ans + mod2) % mod2;
      if (ans != 0) {
        ans = calc(ans);
      }
    }

    cout << (ans + 1) << endl;
  }

  return 0;
}