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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
ll binpow(ll a, ll b, ll mod) {
  // if(mod<=1) return 0;
  ll res = 1;
  while (b > 0) {
    if (b % 2) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
ll getans(string s, ll p) {
  int n = s.length();
  ll pop = p;
  ll ans = 0;

  if (pop > 1) {

    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        ans = (ans + binpow((ll)2, (ll)(n - 1 - i), pop)) % pop;
      }
    }
  }
  return ans;
}
int solve(int ans) {
  //     int n=s.length();
  //     ll pop=p;
  //   //  for(auto it : s) if(it=='1') pop++;
  //     ll ans=0;
  //     int cnt=0;
  //     if(pop>0){
  //     for(int i=0;i<n;i++)
  //     {
  //         if(s[i]=='1')
  //         {
  //             ans=(ans+binpow((ll)2,(ll)(n-1-i),pop))%pop;

  //         }
  //     }
  //     }
  //     if(pop>0)
  //     cnt++;
  int cnt = 0;
  cnt++;
  while (ans > 0) {
    int z = (__builtin_popcount(ans));
    ans = ans % z;
    cnt++;
  }
  return cnt;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  // int val=0;
  cin >> n;
  cin >> s;
  ll p = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      p++;
    // val+=(1<<(n-1-i))*(s[i]-'0');
  }
  // if(p<=1)
  // {
  //      cout<<"1"<<endl;
  //      return 0;
  // }
  // cout<<val<<endl;
  ll ans, ans1 = 0, ans2 = 0;
  // ans=getans(s,p);
  if (p > 1)
    ans1 = getans(s, p - 1);
  ans2 = getans(s, p + 1);
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      // int x=val-(1<<(n-1-i));

      ll val = 0;
      if (p > 1) {
        val = ((ans1 + p - 1) - binpow((ll)2, (ll)n - 1 - i, p - 1)) % (p - 1);
        cout << solve(val) << endl;
      } else if (p - 1 == 0)
        cout << "0" << endl;

      // cout<<solve(t,p-1)<<endl;
    } else {
      // int x=val+(1<<(n-1-i));
      ll val = 0;

      val = (ans2 + binpow((ll)2, (ll)n - 1 - i, p + 1)) % (p + 1);

      cout << solve(val) << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
ll binpow(ll a, ll b, ll mod) {
  if (mod <= 1)
    return 0;
  ll res = 1;
  while (b > 0) {
    if (b % 2) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
ll getans(string s, ll p) {
  int n = s.length();
  ll pop = p;
  ll ans = 0;

  if (pop > 1) {

    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        ans = (ans + binpow((ll)2, (ll)(n - 1 - i), pop)) % pop;
      }
    }
  }
  return ans;
}
int solve(int ans) {
  //     int n=s.length();
  //     ll pop=p;
  //   //  for(auto it : s) if(it=='1') pop++;
  //     ll ans=0;
  //     int cnt=0;
  //     if(pop>0){
  //     for(int i=0;i<n;i++)
  //     {
  //         if(s[i]=='1')
  //         {
  //             ans=(ans+binpow((ll)2,(ll)(n-1-i),pop))%pop;

  //         }
  //     }
  //     }
  //     if(pop>0)
  //     cnt++;
  int cnt = 0;
  cnt++;
  while (ans > 0) {
    int z = (__builtin_popcount(ans));
    ans = ans % z;
    cnt++;
  }
  return cnt;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  // int val=0;
  cin >> n;
  cin >> s;
  ll p = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      p++;
    // val+=(1<<(n-1-i))*(s[i]-'0');
  }
  if (p == 0) {
    cout << "1" << endl;
    return 0;
  }
  // cout<<val<<endl;
  ll ans, ans1 = 0, ans2 = 0;
  // ans=getans(s,p);
  if (p > 1)
    ans1 = getans(s, p - 1);
  ans2 = getans(s, p + 1);
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      // int x=val-(1<<(n-1-i));

      ll val = 0;
      val = ((ans1 + p - 1) - binpow((ll)2, (ll)n - 1 - i, p - 1)) % (p - 1);

      cout << solve(val) << endl;

      // cout<<solve(t,p-1)<<endl;
    } else {
      // int x=val+(1<<(n-1-i));
      ll val = 0;

      val = (ans2 + binpow((ll)2, (ll)n - 1 - i, p + 1)) % (p + 1);

      cout << solve(val) << endl;
    }
  }
}