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


/*
LANG:C++
PROB:

*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iostream>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
typedef unsigned long long ui64;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int mod = 1000000007;
i64 poww(i64 a, i64 b) {
  a %= mod;
  i64 res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;

    a = a * a % mod;
    b /= 2;
  }
  return res;
}
i64 inv(i64 a) { return poww(a, mod - 2); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  // ifstream fin(".in");
  // ofstream fout(".in");

  i64 n;
  string s;
  cin >> n >> s;
  reverse(all(s));
  int cnt = 0;
  forn(i, s.size()) cnt += (s[i] - '0');
  i64 pw = 1, sum = 0, sum1 = 0, sum2 = 0, pw1 = 1, pw2 = 1;
  vvi p(3, vi(n + 1));
  if (cnt == 0)
    pw = 0;
  if (cnt == 1)
    pw2 = 0;
  forn(i, s.size()) {
    if (s[i] == '1') {
      // sum+=pw;
      // sum%=cnt;
      sum1 += pw1;
      sum1 %= (cnt + 1);
      sum2 += pw2;
      if (cnt > 1)
        sum2 %= (cnt - 1);
      else
        sum2 = 0;
    }
    p[0][n - 1 - i] = pw;
    p[1][n - 1 - i] = pw1;
    p[2][n - 1 - i] = pw2;
    // pw=pw*2%cnt;
    pw1 = pw1 * 2 % (cnt + 1);
    if (cnt > 1)
      pw2 = pw2 * 2 % (cnt - 1);
    else
      pw2 = 0;
  }
  vi c(s.size(), sum);

  reverse(all(s));
  forn(i, s.size()) {
    if (s[i] == '0') {
      c[i] = sum1;
      pw = p[1][i];
      c[i] += pw;

      c[i] %= (cnt + 1);
      if (c[i] < 0)
        c[i] += cnt + 1;
    } else {
      c[i] = sum2;
      pw = p[2][i];
      c[i] -= pw;
      if (c[i] < 0)
        c[i] += cnt - 1;
      // c[i]%=cnt;
      // c[i]++;
      if (cnt > 1)
        c[i] %= (cnt - 1);
      else
        c[i] = 0;
    }
  }
  // reverse(all(c));
  forn(i, n) {
    i64 ans = 1;
    if (cnt == 1 && c[i] == 0 && s[i] == '1')
      ans = 0;
    // cout<<c[i]<<" ";
    while (c[i]) {
      c[i] %= __builtin_popcount(c[i]);
      ans++;
    }
    cout << ans << "\\n";
  }

  return 0;
}


Error: 0

Fixed submission:


/*
LANG:C++
PROB:

*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iostream>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
typedef unsigned long long ui64;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int mod = 1000000007;
i64 poww(i64 a, i64 b) {
  a %= mod;
  i64 res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;

    a = a * a % mod;
    b /= 2;
  }
  return res;
}
i64 inv(i64 a) { return poww(a, mod - 2); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  // ifstream fin(".in");
  // ofstream fout(".in");

  i64 n;
  string s;
  cin >> n >> s;
  reverse(all(s));
  int cnt = 0;
  forn(i, s.size()) cnt += (s[i] - '0');
  i64 pw = 1, sum = 0, sum1 = 0, sum2 = 0, pw1 = 1, pw2 = 1;
  vvi p(3, vi(n));
  if (cnt == 0)
    pw = 0;
  if (cnt == 1)
    pw2 = 0;
  forn(i, s.size()) {
    if (s[i] == '1') {
      sum += pw;
      sum %= cnt;
      sum1 += pw1;
      sum1 %= (cnt + 1);
      sum2 += pw2;
      if (cnt > 1)
        sum2 %= (cnt - 1);
      else
        sum2 = 0;
    }
    p[0][n - 1 - i] = pw;
    p[1][n - 1 - i] = pw1;
    p[2][n - 1 - i] = pw2;
    pw = pw * 2 % cnt;
    pw1 = pw1 * 2 % (cnt + 1);
    if (cnt > 1)
      pw2 = pw2 * 2 % (cnt - 1);
    else
      pw2 = 0;
  }
  vi c(s.size(), sum);

  reverse(all(s));
  forn(i, s.size()) {
    if (s[i] == '0') {
      c[i] = sum1;
      pw = p[1][i];
      c[i] += pw;

      c[i] %= (cnt + 1);
      if (c[i] < 0)
        c[i] += cnt + 1;
    } else {
      c[i] = sum2;
      pw = p[2][i];
      c[i] -= pw;
      if (c[i] < 0)
        c[i] += cnt - 1;
      // c[i]%=cnt;
      // c[i]++;
      if (cnt > 1)
        c[i] %= (cnt - 1);
      else
        c[i] = 0;
    }
  }
  // reverse(all(c));
  forn(i, n) {
    i64 ans = 1;
    if (cnt == 1 && c[i] == 0 && s[i] == '1')
      ans = 0;
    // cout<<c[i]<<" ";
    while (c[i]) {
      c[i] %= __builtin_popcount(c[i]);
      ans++;
    }
    cout << ans << "\\n";
  }

  return 0;
}