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

#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
#define op1(x) cout << "Scenario #" << x << ": "
#define endl "\\n"
using namespace std;
ll mod = 1000000007;
const ll INF = 1e18;
const int N = 20;
map<ll, ll> fr;

ll dp[200001];

ll foo(ll n) {
  if (n == 0)
    return dp[0] = 0;

  if (dp[n] != -1)
    return dp[n];

  ll res = 0;
  ll d = bits(n);
  res = 1 + foo(n % d);

  return dp[n] = res;
}

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % mod;
  p = (p % mod * p % mod) % mod;

  return (y % 2 == 0) ? p % mod : (x % mod * p % mod) % mod;
}

void solve(int t) {
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= 200000; i++)
    ll x = foo(i);

  // for (int i = 1; i <= 10; i++) cout << dp[i] << endl;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cn = 0;
  ll a = 0, b = 0, c = 0;
  for (int i = 0; i < n; i++) {
    ll d = (s[i] - '0');
    cn += d;
  }

  if (cn == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << endl;
    }
    return;
  }

  mod = cn + 1;
  // cout << mod << endl;
  for (int i = 0; i < n; i++) {
    ll d = (s[i] - '0');
    a += d * power(2, (n - i - 1)) % mod;
    a %= mod;
  }

  if (cn > 1) {
    mod = cn - 1;
    for (int i = 0; i < n; i++) {
      ll d = (s[i] - '0');
      c += d * power(2, (n - i - 1)) % mod;
      c %= mod;
    }
  }

  // cout << a << " " << b << " " << c << endl;

  for (int i = 0; i < n; i++) {
    ll d = (s[i] - '0');
    if (d == 0) {
      mod = cn + 1;
      cout << dp[(a + power(2, (n - i - 1))) % (mod)] + 1;
    } else {
      if (cn == 1) {
        cout << 0 << endl;
        continue;
      }
      mod = cn - 1;
      cout << dp[(c - power(2, (n - i - 1)) % (cn - 1) + mod) % (mod)] + 1;
    }
    cout << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
#define op1(x) cout << "Scenario #" << x << ": "
#define endl "\\n"
using namespace std;
ll mod = 1000000007;
const ll INF = 1e18;
const int N = 20;
map<ll, ll> fr;

ll dp[200001];

ll foo(ll n) {
  if (n == 0)
    return dp[0] = 0;

  if (dp[n] != -1)
    return dp[n];

  ll res = 0;
  ll d = bits(n);
  res = 1 + foo(n % d);

  return dp[n] = res;
}

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % mod;
  p = (p % mod * p % mod) % mod;

  return (y % 2 == 0) ? p % mod : (x % mod * p % mod) % mod;
}

void solve(int t) {
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i <= 200000; i++)
    ll x = foo(i);

  // for (int i = 1; i <= 10; i++) cout << dp[i] << endl;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cn = 0;
  ll a = 0, b = 0, c = 0;
  for (int i = 0; i < n; i++) {
    ll d = (s[i] - '0');
    cn += d;
  }

  mod = cn + 1;
  // cout << mod << endl;
  for (int i = 0; i < n; i++) {
    ll d = (s[i] - '0');
    a += d * power(2, (n - i - 1)) % mod;
    a %= mod;
  }

  mod = cn - 1;
  for (int i = 0; i < n; i++) {
    ll d = (s[i] - '0');
    c += d * power(2, (n - i - 1)) % mod;
    c %= mod;
  }

  // cout << a << " " << b << " " << c << endl;

  for (int i = 0; i < n; i++) {
    ll d = (s[i] - '0');
    if (d == 0) {
      mod = cn + 1;
      cout << dp[(a + power(2, (n - i - 1))) % (mod)] + 1;
    } else {
      mod = cn - 1;
      cout << dp[(c - power(2, (n - i - 1)) % (cn - 1) + mod) % (mod)] + 1;
    }
    cout << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}