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

#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int)v.size()
#define pr(v)                                                                  \\
  For(i, 0, sz(v)) { cout << v[i] << " "; }                                    \\
  cout << endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (ll i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9 + 7;
const int INF = 1LL << 60;

const int N = 2e5 + 5;

// it's swapnil07 ;)

#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int begtime = clock();
#define end_routine()                                                          \\
  cout << "\\n\\nTime elapsed: " << (clock() - begtime) * 1000 / CLOCKS_PER_SEC  \\
       << " ms\\n\\n";
#else
#define endl '\\n'
#define trace(...)
#define end_routine()
#endif

int n;
string s;

int powmod(int a, int b, int c = MOD) {
  int ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % c;
    }
    a = (a * a) % c;
    b >>= 1;
  }
  return ans;
}

int getmod(int mo) {
  int cur = 1;
  int ans = 0;
  if (mo == 0)
    return 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      ans += cur;
      ans %= mo;
    }
    cur = cur * 2;
    cur %= mo;
  }
  return ans;
}

int play(int x) {
  int v = 1;
  while (x != 0) {
    v++;
    int b = __builtin_popcountl(x);
    x = x % b;
  }
  return v;
}

void solve() {
  cin >> n;
  cin >> s;
  reverse(all(s));
  int sb = 0;
  For(i, 0, sz(s)) {
    if (s[i] == '1')
      sb++;
  }
  if (sb == 0) {
    For(i, 0, n) { cout << 1 << "\\n"; }
    return;
  }
  int m1 = getmod(sb - 1);
  int m2 = getmod(sb + 1);

  vector<int> ans;

  For(i, 0, n) {
    if (s[i] == '1') {
      int b = sb - 1;
      if (b == 0) {
        ans.pb(0);
        continue;
      }
      int x = b - powmod(2, i, b);
      int cur = (m1 + x) % b;
      ans.pb(play(cur));
    } else {
      int b = sb + 1;
      int x = powmod(2, i, b);
      int cur = (m2 + x) % b;
      ans.pb(play(cur));
    }
  }
  reverse(all(ans));
  for (int v : ans) {
    cout << v << "\\n";
  }
}

signed main() {
  fast
#ifdef SWAPNIL07
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int)v.size()
#define pr(v)                                                                  \\
  For(i, 0, sz(v)) { cout << v[i] << " "; }                                    \\
  cout << endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (ll i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9 + 7;
const int INF = 1LL << 60;

const int N = 2e5 + 5;

// it's swapnil07 ;)

#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int begtime = clock();
#define end_routine()                                                          \\
  cout << "\\n\\nTime elapsed: " << (clock() - begtime) * 1000 / CLOCKS_PER_SEC  \\
       << " ms\\n\\n";
#else
#define endl '\\n'
#define trace(...)
#define end_routine()
#endif

int n;
string s;

int powmod(int a, int b, int c = MOD) {
  int ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % c;
    }
    a = (a * a) % c;
    b >>= 1;
  }
  return ans;
}

int getmod(int mo) {
  int cur = 1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      ans += cur;
      ans %= mo;
    }
    cur = cur * 2;
    cur %= mo;
  }
  return ans;
}

int play(int x) {
  int v = 1;
  while (x != 0) {
    v++;
    int b = __builtin_popcountl(x);
    x = x % b;
  }
  return v;
}

void solve() {
  cin >> n;
  cin >> s;
  reverse(all(s));
  int sb = 0;
  For(i, 0, sz(s)) {
    if (s[i] == '1')
      sb++;
  }
  int m1 = getmod(sb - 1);
  int m2 = getmod(sb + 1);

  vector<int> ans;

  For(i, 0, n) {
    if (s[i] == '1') {
      int b = sb - 1;
      int x = b - powmod(2, i, b);
      int cur = (m1 + x) % b;
      ans.pb(play(cur));
    } else {
      int b = sb + 1;
      int x = powmod(2, i, b);
      int cur = (m2 + x) % b;
      ans.pb(play(cur));
    }
  }
  reverse(all(ans));
  for (int v : ans) {
    cout << v << "\\n";
  }
}

signed main() {
  fast
#ifdef SWAPNIL07
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}