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


// Problem : D - Anything Goes to Zero
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// look at my code
// my code is amazing
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll long long
#define int long long
#define mod 1000000007 // 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define f(i, n) for (ll i = 0; i < n; i++)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define nl "\\n"
#define trace(x) cerr << #x << ": " << x << " " << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define cout1(a) cout << a << nl
#define cout2(a, b) cout << a << " " << b << nl
#define cout3(a, b, c) cout << a << " " << b << " " << c << nl
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << nl
#define vcout(v, i) cout << v[i].fi << " " << v[i].se << nl
#define x first
#define y second
#define vc vector
#define pb push_back
#define ar array
#define all(a) (a).begin(), (a).end()
#define ms(v, n, x) fill(v, v + n, x);
#define init(c, a) memset(c, a, sizeof(c))
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define sll set<ll>
#define vll vector<ll>
#define vpll vector<pll>
#define maxi(a, b, c) max(a, max(b, c))
#define maxii(a, b, c, d) max(max(a, b), max(c, d))
#define mini(a, b, c) min(a, min(b, c))
#define md(a, b) ((a % mod) * (b % mod) + mod) % mod
#define inf 9e18
#define cases                                                                  \\
  ll T;                                                                        \\
  cin >> T;                                                                    \\
  while (T--)
#define BLOCK 500
// const double PI = 3.141592653589793238460;
template <typename T> bool mmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T> bool mmin(T &m, const T q) {
  if (m > q) {
    m = q;
    return true;
  } else
    return false;
}
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
// void __print(long long x) {cerr << x;}
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

std::mt19937
    rng((int)std::chrono::steady_clock::now().time_since_epoch().count());

const int N = (1 << 18);
ll dp[N];

void pre() {
  dp[0] = 0;
  for (ll i = 1; i < N; i++) {
    ll p = __builtin_popcount(i);
    dp[i] = 1 + dp[i % p];
  }
}

int32_t main() {

  fast pre();
  cout << fixed << setprecision(12);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll pc = 0;
  f(i, n) {
    if (s[i] == '1')
      pc++;
  }

  ll m = pc - 1;
  ll p = pc + 1;

  vll ptm(n), ptp(n);
  if (m > 0)
    ptm[0] = 1 % m;
  ptp[0] = 1 % p;

  fore(i, 1, n - 1) {
    if (m > 0)
      ptm[i] = (ptm[i - 1] * 2) % m;
    ptp[i] = (ptp[i - 1] * 2) % p;
  }

  ll res1 = 0, res2 = 0;
  ll i = 0, j = n - 1;
  while (i < n) {

    // If current bit is 1
    if (s[j] == '1') {

      // Add the current power of 2
      res1 += (ptm[i]);
      if (m > 0)
        res1 %= m;

      res2 += (ptp[i]);
      res2 %= p;
    }
    i++;
    j--;
  }

  // debug(res1,res2,p,m);

  f(i, n) {

    ll t = n - i - 1;

    // debug(i,t);
    if (s[i] == '1') {
      if (m == 0) {
        cout << 0 << nl;
      } else {
        ll ans = (res1 - ptm[t] + m) % m;
        cout << 1 + dp[ans] << nl;
      }
    } else {

      ll ans = (res2 + ptp[t] + p) % p;
      cout << 1 + dp[ans] << nl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:


// Problem : D - Anything Goes to Zero
// Contest : AtCoder - AIsing Programming Contest 2020
// URL : https://atcoder.jp/contests/aising2020/tasks/aising2020_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// look at my code
// my code is amazing
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll long long
#define int long long
#define mod 1000000007 // 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define f(i, n) for (ll i = 0; i < n; i++)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define nl "\\n"
#define trace(x) cerr << #x << ": " << x << " " << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define cout1(a) cout << a << nl
#define cout2(a, b) cout << a << " " << b << nl
#define cout3(a, b, c) cout << a << " " << b << " " << c << nl
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << nl
#define vcout(v, i) cout << v[i].fi << " " << v[i].se << nl
#define x first
#define y second
#define vc vector
#define pb push_back
#define ar array
#define all(a) (a).begin(), (a).end()
#define ms(v, n, x) fill(v, v + n, x);
#define init(c, a) memset(c, a, sizeof(c))
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define sll set<ll>
#define vll vector<ll>
#define vpll vector<pll>
#define maxi(a, b, c) max(a, max(b, c))
#define maxii(a, b, c, d) max(max(a, b), max(c, d))
#define mini(a, b, c) min(a, min(b, c))
#define md(a, b) ((a % mod) * (b % mod) + mod) % mod
#define inf 9e18
#define cases                                                                  \\
  ll T;                                                                        \\
  cin >> T;                                                                    \\
  while (T--)
#define BLOCK 500
// const double PI = 3.141592653589793238460;
template <typename T> bool mmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T> bool mmin(T &m, const T q) {
  if (m > q) {
    m = q;
    return true;
  } else
    return false;
}
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
// void __print(long long x) {cerr << x;}
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

std::mt19937
    rng((int)std::chrono::steady_clock::now().time_since_epoch().count());

const int N = (1 << 18);
ll dp[N];

void pre() {
  dp[0] = 0;
  for (ll i = 1; i < N; i++) {
    ll p = __builtin_popcount(i);
    dp[i] = 1 + dp[i % p];
  }
}

int32_t main() {

  fast pre();
  cout << fixed << setprecision(12);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll pc = 0;
  f(i, n) {
    if (s[i] == '1')
      pc++;
  }

  ll m = pc - 1;
  ll p = pc + 1;

  vll ptm(n), ptp(n);
  if (m)
    ptm[0] = 1 % m;
  ptp[0] = 1 % p;

  fore(i, 1, n - 1) {
    ptm[i] = (ptm[i - 1] * 2) % m;
    ptp[i] = (ptp[i - 1] * 2) % p;
  }

  ll res1 = 0, res2 = 0;
  ll i = 0, j = n - 1;
  while (i < n) {

    // If current bit is 1
    if (s[j] == '1') {

      // Add the current power of 2
      res1 += (ptm[i]);
      res1 %= m;

      res2 += (ptp[i]);
      res2 %= p;
    }
    i++;
    j--;
  }

  // debug(res1,res2,p,m);

  f(i, n) {

    ll t = n - i - 1;

    // debug(i,t);
    if (s[i] == '1') {
      if (m == 0) {
        cout << 0 << nl;
      } else {
        ll ans = (res1 - ptm[t] + m) % m;
        cout << 1 + dp[ans] << nl;
      }
    } else {

      ll ans = (res2 + ptp[t] + p) % p;
      cout << 1 + dp[ans] << nl;
    }
  }

  return 0;
}