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

#include <bits/stdc++.h>

const double pi = 3.141592653589793238462643383279;
using namespace std;
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-10;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

// ここから編集

ll modPow(ll x, ll n, ll mod = MOD) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;

    res %= mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int N;
  string s;
  cin >> N >> s;

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

  if (cnt == 0) {
    ll plus1 = cnt + 1;

    ll s1 = 0;
    for (int i = 0; i < s.size(); i++) {

      // 0 -> 1
      ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
      int ans = 1;
      while (t) {
        t %= __builtin_popcount(t);
        ans++;
      }
      cout << ans << endl;
    }
  } else if (cnt == 1) {
    ll plus1 = cnt + 1, minus1 = cnt - 1;

    ll s1 = 0, s2 = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1') {
        s1 += modPow(2, s.size() - i - 1, plus1);
        s1 %= plus1;
      }
    }

    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1') {
        // 1 -> 0
        if (minus1 == 0) {
          cout << 0 << endl;
          continue;
        }
        ll t = (s2 - modPow(2, s.size() - i - 1, minus1) + minus1) % minus1;
        int ans = 1;
        while (t) {
          t %= __builtin_popcount(t);
          ans++;
        }
        cout << ans << endl;
      } else {
        // 0 -> 1
        ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
        int ans = 1;
        while (t) {
          t %= __builtin_popcount(t);
          ans++;
        }
        cout << ans << endl;
      }
    }
  } else {
    ll plus1 = cnt + 1, minus1 = cnt - 1;

    ll s1 = 0, s2 = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1') {
        s1 += modPow(2, s.size() - i - 1, plus1);
        s1 %= plus1;
        s2 += modPow(2, s.size() - i - 1, minus1);
        s2 %= minus1;
      }
    }

    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1') {
        // 1 -> 0
        ll t = (s2 - modPow(2, s.size() - i - 1, minus1) + minus1) % minus1;
        int ans = 1;
        while (t) {
          t %= __builtin_popcount(t);
          ans++;
        }
        cout << ans << endl;
      } else {
        // 0 -> 1
        ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
        int ans = 1;
        while (t) {
          t %= __builtin_popcount(t);
          ans++;
        }
        cout << ans << endl;
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

const double pi = 3.141592653589793238462643383279;
using namespace std;
// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-10;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

// ここから編集

ll modPow(ll x, ll n, ll mod = MOD) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;

    res %= mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int N;
  string s;
  cin >> N >> s;

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

  if (cnt == 0) {
    ll plus1 = cnt + 1;

    ll s1 = 0;
    for (int i = 0; i < s.size(); i++) {

      // 0 -> 1
      ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
      int ans = 1;
      while (t) {
        t %= __builtin_popcount(t);
        ans++;
      }
      cout << ans << endl;
    }
  } else {
    ll plus1 = cnt + 1, minus1 = cnt - 1;

    ll s1 = 0, s2 = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1') {
        s1 += modPow(2, s.size() - i - 1, plus1);
        s1 %= plus1;
        s2 += modPow(2, s.size() - i - 1, minus1);
        s2 %= minus1;
      }
    }

    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1') {
        // 1 -> 0
        ll t = (s2 - modPow(2, s.size() - i - 1, minus1) + minus1) % minus1;
        int ans = 1;
        while (t) {
          t %= __builtin_popcount(t);
          ans++;
        }
        cout << ans << endl;
      } else {
        // 0 -> 1
        ll t = (s1 + modPow(2, s.size() - i - 1, plus1)) % plus1;
        int ans = 1;
        while (t) {
          t %= __builtin_popcount(t);
          ans++;
        }
        cout << ans << endl;
      }
    }
  }

  return 0;
}