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

/**
 *  “Experience is the name everyone gives to their mistakes.” – Oscar Wilde
 *
 *  author  :   Prodip Datta
 *  handle  :   prodipdatta7
 *  created :   Sunday 12-July, 2020  07:32:15 PM
 **/

// #include <bits/stdc++.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <iterator>
#include <map>
#include <new>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int>>
#define debug(x) cerr << #x << " = " << x << '\\n';
#define rep(i, b, e)                                                           \\
  for (__typeof(e) i = (b); i != (e + 1) - 2 * ((b) > (e));                    \\
       i += 1 - 2 * ((b) > (e)))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mem(a) memset(a, 0, sizeof a)
#define memn(a) memset(a, -1, sizeof a)
#define fread freopen("input.txt", "r", stdin)
#define fwrite freopen("output.txt", "w", stdout)
#define TN typename
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

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

/*
Note :  There is a problem with erase() function in ordered_multiset (for
less_equal<int> tag). lower_bound() works as upper_bound() and vice-versa. Be
careful to use. i) find_by_order(k) : kth smallest element counting from 0 . ii)
order_of_key(k) : number of elements strictly smaller than k.
*/

/*###############-> Input Section <-###############*/

template <TN T> inline void Int(T &a) {
  bool minus = false;
  a = 0;
  char ch = getchar();
  while (true) {
    if (ch == '-' or (ch >= '0' && ch <= '9'))
      break;
    ch = getchar();
  }
  if (ch == '-')
    minus = true;
  else
    a = ch - '0';
  while (true) {
    ch = getchar();
    if (ch < '0' || ch > '9')
      break;
    a = a * 10 + (ch - '0');
  }
  if (minus)
    a *= -1;
}
template <TN T, TN T1> inline void Int(T &a, T1 &b) { Int(a), Int(b); }
template <TN T, TN T1, TN T2> inline void Int(T &a, T1 &b, T2 &c) {
  Int(a, b), Int(c);
}
template <TN T, TN T1, TN T2, TN T3>
inline void Int(T &a, T1 &b, T2 &c, T3 &d) {
  Int(a, b), Int(c, d);
}
template <TN T, TN T1, TN T2, TN T3, TN T4>
inline void Int(T &a, T1 &b, T2 &c, T3 &d, T4 &e) {
  Int(a, b), Int(c, d, e);
}

/*###############-> Debugger <-###############*/

#ifndef ONLINE_JUDGE
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) { cout << endl; }
template <TN T, TN... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ' ';
  err(++it, args...);
}
#else
#define error(args...)
#endif

template <class ValueType>
bool unstable_remove(typename std::vector<ValueType> &container,
                     typename std::vector<ValueType>::iterator it) {
  auto lastEl = container.end() - 1;
  if (it != lastEl) {
    *it = std::move(*lastEl);
  }
  container.pop_back();
}

/******************************************
#Rows are numbered from top to bottom.
#Columns are numbered from left to right.
Moves : L, U, R, D, LU, RU, RD, LD.
******************************************/
int dx[8] = {0, -1, 0, 1, -1, -1, 1, 1};
int dy[8] = {-1, 0, 1, 0, -1, 1, 1, -1};

/*###############-> Constraints <-###############*/

const int N = (int)2e5 + 5;
const int maxN = (int)1e6 + 6;
const ll Mod = (ll)1e9 + 7;
const int inf = (int)2e9;
const ll Inf = (ll)1e18;
const int mod = (int)1e9 + 7;
const double EPS = (double)1e-9;

inline int add(int a, int b, int mod) {
  a += b;
  return a >= mod ? a - mod : a;
}
inline int sub(int a, int b, int mod) {
  a -= b;
  return a < 0 ? a + mod : a;
}
inline int mul(int a, int b, int mod) { return (ll)a * b % mod; }

/*...... ! Code starts from here ! ......*/

template <typename T, typename T1> inline T modpow(T b, T1 p, T1 mod) {
  T res = 1;
  if (b >= mod)
    b %= mod;
  while (p > 0) {
    if (p & 1)
      res = mul(res, b, mod);
    b = mul(b, b, mod);
    p >>= 1;
  }
  return res;
}

int get(int n) {
  if (n == 0)
    return 0;
  return 1 + get(n % __builtin_popcount(n));
}

int main() {
#ifndef ONLINE_JUDGE
  clock_t tStart = clock();
  // freopen("input.txt", "r", stdin) ;
  // freopen("output.txt", "w", stdout) ;
#endif

  int test = 1, tc = 0;
  // Int(test) ;
  while (test--) {
    int n, t = 0;
    Int(n);
    string s;
    cin >> s;
    int less = 0, more = 0;
    for (int i = 0; i < n; ++i) {
      t += s[i] == '1';
    }
    // error(t)
    for (int i = 0; i < n; ++i) {
      if (s[i] == '0')
        continue;
      if (t > 1)
        less = add(less, modpow(2, n - i - 1, t - 1), t - 1);
      more = add(more, modpow(2, n - i - 1, t + 1), t + 1);
    }
    for (int i = 0; i < n; ++i) {
      if (s[i] == '0') {
        int cur = add(more, modpow(2, n - i - 1, t + 1), t + 1);
        int res = 1 + get(cur);
        cout << res << '\\n';
      } else {
        if (t == 1)
          cout << 0 << '\\n';
        else {
          int cur = sub(less, modpow(2, n - i - 1, t - 1), t - 1);
          int res = 1 + get(cur);
          cout << res << '\\n';
        }
      }
    }
  }

#ifndef ONLINE_JUDGE
  fprintf(stderr, "\\nRuntime: %.10fs\\n",
          (double)(clock() - tStart) / CLOCKS_PER_SEC);
#endif
  return 0;
}

Error: 0

Runtime: 0.0000770000s


Fixed submission:

/**
 *  “Experience is the name everyone gives to their mistakes.” – Oscar Wilde
 *
 *  author  :   Prodip Datta
 *  handle  :   prodipdatta7
 *  created :   Sunday 12-July, 2020  07:32:15 PM
 **/

// #include <bits/stdc++.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <iterator>
#include <map>
#include <new>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int>>
#define debug(x) cerr << #x << " = " << x << '\\n';
#define rep(i, b, e)                                                           \\
  for (__typeof(e) i = (b); i != (e + 1) - 2 * ((b) > (e));                    \\
       i += 1 - 2 * ((b) > (e)))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mem(a) memset(a, 0, sizeof a)
#define memn(a) memset(a, -1, sizeof a)
#define fread freopen("input.txt", "r", stdin)
#define fwrite freopen("output.txt", "w", stdout)
#define TN typename
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

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

/*
Note :  There is a problem with erase() function in ordered_multiset (for
less_equal<int> tag). lower_bound() works as upper_bound() and vice-versa. Be
careful to use. i) find_by_order(k) : kth smallest element counting from 0 . ii)
order_of_key(k) : number of elements strictly smaller than k.
*/

/*###############-> Input Section <-###############*/

template <TN T> inline void Int(T &a) {
  bool minus = false;
  a = 0;
  char ch = getchar();
  while (true) {
    if (ch == '-' or (ch >= '0' && ch <= '9'))
      break;
    ch = getchar();
  }
  if (ch == '-')
    minus = true;
  else
    a = ch - '0';
  while (true) {
    ch = getchar();
    if (ch < '0' || ch > '9')
      break;
    a = a * 10 + (ch - '0');
  }
  if (minus)
    a *= -1;
}
template <TN T, TN T1> inline void Int(T &a, T1 &b) { Int(a), Int(b); }
template <TN T, TN T1, TN T2> inline void Int(T &a, T1 &b, T2 &c) {
  Int(a, b), Int(c);
}
template <TN T, TN T1, TN T2, TN T3>
inline void Int(T &a, T1 &b, T2 &c, T3 &d) {
  Int(a, b), Int(c, d);
}
template <TN T, TN T1, TN T2, TN T3, TN T4>
inline void Int(T &a, T1 &b, T2 &c, T3 &d, T4 &e) {
  Int(a, b), Int(c, d, e);
}

/*###############-> Debugger <-###############*/

#ifndef ONLINE_JUDGE
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) { cout << endl; }
template <TN T, TN... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ' ';
  err(++it, args...);
}
#else
#define error(args...)
#endif

template <class ValueType>
bool unstable_remove(typename std::vector<ValueType> &container,
                     typename std::vector<ValueType>::iterator it) {
  auto lastEl = container.end() - 1;
  if (it != lastEl) {
    *it = std::move(*lastEl);
  }
  container.pop_back();
}

/******************************************
#Rows are numbered from top to bottom.
#Columns are numbered from left to right.
Moves : L, U, R, D, LU, RU, RD, LD.
******************************************/
int dx[8] = {0, -1, 0, 1, -1, -1, 1, 1};
int dy[8] = {-1, 0, 1, 0, -1, 1, 1, -1};

/*###############-> Constraints <-###############*/

const int N = (int)2e5 + 5;
const int maxN = (int)1e6 + 6;
const ll Mod = (ll)1e9 + 7;
const int inf = (int)2e9;
const ll Inf = (ll)1e18;
const int mod = (int)1e9 + 7;
const double EPS = (double)1e-9;

inline int add(int a, int b, int mod) {
  a += b;
  return a >= mod ? a - mod : a;
}
inline int sub(int a, int b, int mod) {
  a -= b;
  return a < 0 ? a + mod : a;
}
inline int mul(int a, int b, int mod) { return (ll)a * b % mod; }

/*...... ! Code starts from here ! ......*/

template <typename T, typename T1> inline T modpow(T b, T1 p, T1 mod) {
  T res = 1;
  if (b >= mod)
    b %= mod;
  while (p > 0) {
    if (p & 1)
      res = mul(res, b, mod);
    b = mul(b, b, mod);
    p >>= 1;
  }
  return res;
}

int get(int n) {
  if (n == 0)
    return 0;
  return 1 + get(n % __builtin_popcount(n));
}

int main() {
#ifndef ONLINE_JUDGE
  clock_t tStart = clock();
  // freopen("input.txt", "r", stdin) ;
  // freopen("output.txt", "w", stdout) ;
#endif

  int test = 1, tc = 0;
  // Int(test) ;
  while (test--) {
    int n, t = 0;
    Int(n);
    string s;
    cin >> s;
    int less = 0, more = 0;
    for (int i = 0; i < n; ++i) {
      t += s[i] == '1';
    }
    for (int i = 0; i < n; ++i) {
      if (s[i] == '0')
        continue;
      if (t)
        less = add(less, modpow(2, n - i - 1, t - 1), t - 1);
      more = add(more, modpow(2, n - i - 1, t + 1), t + 1);
    }
    for (int i = 0; i < n; ++i) {
      if (s[i] == '0') {
        int cur = add(more, modpow(2, n - i - 1, t + 1), t + 1);
        int res = 1 + get(cur);
        cout << res << '\\n';
      } else {
        int cur = sub(less, modpow(2, n - i - 1, t - 1), t - 1);
        int res = 1 + get(cur);
        cout << res << '\\n';
      }
    }
  }

#ifndef ONLINE_JUDGE
  fprintf(stderr, "\\nRuntime: %.10fs\\n",
          (double)(clock() - tStart) / CLOCKS_PER_SEC);
#endif
  return 0;
}