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

// FIRST THINK THEN CODE.

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MOD 998244353
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define rrep(i, a, b) for (ll i = a; i > b; --i)
#define FOR(i, n) for (ll i = 0; i < n; i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout << (x) << endl
#define d2(x, y) cout << (x) << " " << (y) << endl
#define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define d4(a, b, c, d)                                                         \\
  cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define PI 3.1415926535897932384626433832795
#define fix(f, n) fixed << setprecision(n) << f
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

ll newmod(ll a, ll b) { return ((a % b) + b) % b; }

ll powM(ll a, ll b, ll m) {
  if (m <= 1)
    return 0;
  a %= m;
  ll ans = 1LL;
  while (b) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }

  return ans;
}

ll poww(ll a, ll b) {

  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    a = a * a;
    b >>= 1;
  }

  return ans;
}

template <typename T, typename F> void chmax(T &a, F b) {
  if (b > a)
    a = b;
}

template <typename T, typename F> void chmin(T &a, F b) {
  if (b < a)
    a = b;
}

const ll N = 3e5 + 5;

int main() {

  IOS;

  ll n;
  cin >> n;

  ll pp = -1;

  string s;
  cin >> s;

  ll cnt = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++, pp = i;
  }

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

  vpll mm(n);

  vl ans(n, 1);

  if (cnt == 1) {
    ll gf = powM(2, n - pp - 1, 2);
    for (int i = 0; i < n; i++) {
      if (i == pp) {
        cout << 0 << endl;
        continue;
      }
      gf += powM(2, n - i - 1, 2);
      if (gf % 2 == 0)
        cout << 1 << endl;
      else
        cout << 2 << endl;
    }

    return 0;
  }

  vl x(n), y(n);
  // x:cnt+1,y=cnt-1

  ll s1 = 0, s2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      x[i] = powM(2, n - i - 1, cnt + 1);
      s1 += x[i];
      y[i] = powM(2, n - i - 1, cnt - 1);
      s2 += y[i];
    }
  }

  for (int i = 0; i < n; i++) {
    ll d = 0;
    if (s[i] == '1') {
      d = (s2 - y[i]) % (cnt - 1);
      d = (d + cnt - 1) % (cnt - 1);
    } else {
      d = (s1 + powM(2, n - i - 1, cnt + 1)) % (cnt + 1);
    }
    // cout<<d<<endl;
    mm[i].ff = d;
    ll t = 0;
    while (d) {
      if (d & 1)
        t++;
      d >>= 1;
    }
    mm[i].ss = t;
  }

  ll flag = 0;

  while (!flag) {

    ll cc = 0;

    // cout<<"hi"<<" "<<mm[0].ff<<endl;

    for (int i = 0; i < n; i++) {
      if (mm[i].ff == 0) {
        cc++;
        continue;
      }
      //  if(i==n-1){
      //  cout<<"hi"<<" "<<mm[i].ss<<endl;
      //}
      ans[i]++;
      ll f = mm[i].ff;
      ll temp = 0;
      ll cn = 0;
      ll pos = 0;
      while (f) {
        if (f & 1) {
          // cn++;
          temp += powM(2, pos, mm[i].ss);
          temp %= (mm[i].ss);
        }
        f >>= 1;
        pos++;
      }
      mm[i].ff = temp;
      while (temp) {
        if (temp & 1)
          cn++;
        temp >>= 1;
      }
      // if(i==n-1)cout<<"blah"<<" "<<endl;

      mm[i].ss = cn;
    }
    if (cc == n) {
      flag = 1;
      // break;
    }
  }

  for (auto p : ans) {
    cout << p << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

// FIRST THINK THEN CODE.

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MOD 998244353
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define rrep(i, a, b) for (ll i = a; i > b; --i)
#define FOR(i, n) for (ll i = 0; i < n; i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout << (x) << endl
#define d2(x, y) cout << (x) << " " << (y) << endl
#define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define d4(a, b, c, d)                                                         \\
  cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define PI 3.1415926535897932384626433832795
#define fix(f, n) fixed << setprecision(n) << f
#define all(x) x.begin(), x.end()
#define endl "\\n"
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

ll newmod(ll a, ll b) { return ((a % b) + b) % b; }

ll powM(ll a, ll b, ll m) {
  if (m == 1)
    return 0;
  a %= m;
  ll ans = 1LL;
  while (b) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }

  return ans;
}

ll poww(ll a, ll b) {

  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a;
    a = a * a;
    b >>= 1;
  }

  return ans;
}

template <typename T, typename F> void chmax(T &a, F b) {
  if (b > a)
    a = b;
}

template <typename T, typename F> void chmin(T &a, F b) {
  if (b < a)
    a = b;
}

const ll N = 3e5 + 5;

int main() {

  IOS;

  ll n;
  cin >> n;

  string s;
  cin >> s;

  ll cnt = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }

  vpll mm(n);

  vl ans(n, 1);

  vl x(n), y(n);
  // x:cnt+1,y=cnt-1

  ll s1 = 0, s2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      x[i] = powM(2, n - i - 1, cnt + 1);
      s1 += x[i];
      y[i] = powM(2, n - i - 1, cnt - 1);
      s2 += y[i];
    }
  }

  for (int i = 0; i < n; i++) {
    ll d = 0;
    if (s[i] == '1') {
      d = (s2 - y[i]) % (cnt - 1);
    } else {
      d = (s1 + powM(2, n - i - 1, cnt + 1)) % (cnt + 1);
    }
    // cout<<d<<endl;
    mm[i].ff = d;
    ll t = 0;
    while (d) {
      if (d & 1)
        t++;
      d >>= 1;
    }
    mm[i].ss = t;
  }

  ll flag = 0;

  ll amount = 10;

  while (amount--) {

    ll cc = 0;

    // cout<<"hi"<<" "<<mm[0].ff<<endl;

    for (int i = 0; i < n; i++) {
      if (mm[i].ff == 0) {
        cc++;
        continue;
      }
      //  if(i==n-1){
      //  cout<<"hi"<<" "<<mm[i].ss<<endl;
      //}
      ans[i]++;
      ll f = mm[i].ff;
      ll temp = 0;
      ll cn = 0;
      ll pos = 0;
      while (f) {
        if (f & 1) {
          // cn++;
          temp += powM(2, pos, mm[i].ss);
        }
        f >>= 1;
        pos++;
      }
      mm[i].ff = temp;
      while (temp) {
        if (temp & 1)
          cn++;
        temp >>= 1;
      }
      // if(i==n-1)cout<<"blah"<<" "<<endl;

      mm[i].ss = cn;
    }
    if (cc == n)
      break;
  }

  for (auto p : ans) {
    cout << p << endl;
  }

  return 0;
}