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

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
#define mod 1000000007
#define mod2 998244353
#define PI 3.14159265
#define ll long long
#define ld long double
#define pi pair<ll, ll>
#define pb push_back
#define vi vector<ll>
#define sz size()
#define setbits(x) __builtin_popcount(x)
#define fi first
#define se second
#define lz length()
#define all(x) (x).begin(), (x).end()
#define scf(x) scanf("%lld", &x)
#define rep(i, n) for (ll i = 0; i < n; i++)
const int INF = (int)1e9;
const ll IINF = 1LL << 62;
const int maxn = 10000005;

// int divr[maxn];
/**/
/*void divisor()
{for(int i=2;i<sqrt(maxn);i++)
     {
          if(!divr[i])
          {
               for(int j=i*i;j<maxn;j+=i)if(!divr[j])divr[j]=i;
          }
     }
     for(int i=1;i<maxn;i++)if(!divr[i])divr[i]=i;
}*/

/*const int N=1000005;
ll n;
struct line {
    long long m, c;
    long long eval(long long x) { return m * x + c; }
    long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m);
}
};
vector<pair<pi,ll>> rect;
deque<line> dq;
*/
/*int find(int a){
     if(a==par[a])return a;
     return par[a]=find(par[a]);
}
void unite(int a,int b){
     a=find(a),b=find(b);
     if(a==b)return ;
     par[b]=a;
}
*/
///////GAUSS ELIMINATION
/*void gausselimination()
{
    int n,i,j,k;
    cout.precision(4);        //set precision
    cout.setf(ios::fixed);
    cout<<"\\nEnter the no. of equations\\n";
    cin>>n;                //input the no. of equations
    float a[n][n+1],x[n];        //declare an array to store the elements of
augmented-matrix cout<<"\\nEnter the elements of the augmented-matrix
row-wise:\\n"; for (i=0;i<n;i++) for (j=0;j<=n;j++) cin>>a[i][j];    //input the
elements of array for (i=0;i<n;i++)                    //Pivotisation for
(k=i+1;k<n;k++) if (abs(a[i][i])<abs(a[k][i])) for (j=0;j<=n;j++)
                {
                    double temp=a[i][j];
                    a[i][j]=a[k][j];
                    a[k][j]=temp;
                }
    cout<<"\\nThe matrix after Pivotisation is:\\n";
    for (i=0;i<n;i++)            //print the new matrix
    {
        for (j=0;j<=n;j++)
            cout<<a[i][j]<<setw(16);
        cout<<"\\n";
    }
    for (i=0;i<n-1;i++)            //loop to perform the gauss elimination
        for (k=i+1;k<n;k++)
            {
                double t=a[k][i]/a[i][i];
                for (j=0;j<=n;j++)
                    a[k][j]=a[k][j]-t*a[i][j];    //make the elements below the
pivot elements equal to zero or elimnate the variables
            }

    cout<<"\\n\\nThe matrix after gauss-elimination is as follows:\\n";
    for (i=0;i<n;i++)            //print the new matrix
    {
        for (j=0;j<=n;j++)
            cout<<a[i][j]<<setw(16);
        cout<<"\\n";
    }
    for (i=n-1;i>=0;i--)                //back-substitution
    {                        //x is an array whose values correspond to the
values of x,y,z.. x[i]=a[i][n];                //make the variable to be
calculated equal to the rhs of the last equation for (j=i+1;j<n;j++) if (j!=i)
//then subtract all the lhs values except the coefficient of the variable whose
value                                   is being calculated
                x[i]=x[i]-a[i][j]*x[j];
        x[i]=x[i]/a[i][i];            //now finally divide the rhs by the
coefficient of the variable to be calculated
    }
    cout<<"\\nThe values of the variables are as follows:\\n";
    for (i=0;i<n;i++)
        cout<<x[i]<<endl;            // Print the values of x, y,z,....
    return 0;
}*/
ll modexpo(ll a, ll b, int mt) {
  int ans = 1LL;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a) % mt;
    a = (a * a) % mt;
    b >>= 1;
  }
  return ans;
}
int getMod(string str, int n, int k) {
  int pwrTwo[n];
  pwrTwo[0] = 1 % k;
  for (int i = 1; i < n; i++) {
    pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
    pwrTwo[i] %= k;
  }
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n) {
    if (str[j] == '1') {
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int st = 0;
  for (int i = 0; i < x.length(); i++) {
    if (x[i] == '1')
      st++;
  }
  int rem1 = getMod(x, n, st + 1);
  int rem2;
  if ((st - 1) != 0)
    rem2 = getMod(x, n, st - 1);
  vector<int> res;
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      int tmp1 = modexpo(2, n - i - 1, st + 1);
      int mt = st + 1;
      int total = (tmp1 + rem1) % mt;
      int ct = 1;
      while (total > 0) {
        int stbits = setbits(total);
        total = total % stbits;
        ct++;
      }
      res.pb(ct);
    } else {
      int mt = st - 1;
      if (mt == 0) {
        res.pb(0);
        continue;
      }
      int tmp1 = modexpo(2, n - i - 1, st - 1);

      int total = (rem2 - tmp1 + mt) % mt;
      // if(i==n-2){cout<<tmp1<<" "<<total<<" "<<rem1<<"\\n";}
      int ct = 1;
      while (total > 0) {
        int stbits = setbits(total);
        total = total % stbits;
        ct++;
      }
      res.pb(ct);
    }
  }
  for (int i = 0; i < res.size(); i++)
    cout << res[i] << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
#define mod 1000000007
#define mod2 998244353
#define PI 3.14159265
#define ll long long
#define ld long double
#define pi pair<ll, ll>
#define pb push_back
#define vi vector<ll>
#define sz size()
#define setbits(x) __builtin_popcount(x)
#define fi first
#define se second
#define lz length()
#define all(x) (x).begin(), (x).end()
#define scf(x) scanf("%lld", &x)
#define rep(i, n) for (ll i = 0; i < n; i++)
const int INF = (int)1e9;
const ll IINF = 1LL << 62;
const int maxn = 10000005;

// int divr[maxn];
/**/
/*void divisor()
{for(int i=2;i<sqrt(maxn);i++)
     {
          if(!divr[i])
          {
               for(int j=i*i;j<maxn;j+=i)if(!divr[j])divr[j]=i;
          }
     }
     for(int i=1;i<maxn;i++)if(!divr[i])divr[i]=i;
}*/

/*const int N=1000005;
ll n;
struct line {
    long long m, c;
    long long eval(long long x) { return m * x + c; }
    long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m);
}
};
vector<pair<pi,ll>> rect;
deque<line> dq;
*/
/*int find(int a){
     if(a==par[a])return a;
     return par[a]=find(par[a]);
}
void unite(int a,int b){
     a=find(a),b=find(b);
     if(a==b)return ;
     par[b]=a;
}
*/
///////GAUSS ELIMINATION
/*void gausselimination()
{
    int n,i,j,k;
    cout.precision(4);        //set precision
    cout.setf(ios::fixed);
    cout<<"\\nEnter the no. of equations\\n";
    cin>>n;                //input the no. of equations
    float a[n][n+1],x[n];        //declare an array to store the elements of
augmented-matrix cout<<"\\nEnter the elements of the augmented-matrix
row-wise:\\n"; for (i=0;i<n;i++) for (j=0;j<=n;j++) cin>>a[i][j];    //input the
elements of array for (i=0;i<n;i++)                    //Pivotisation for
(k=i+1;k<n;k++) if (abs(a[i][i])<abs(a[k][i])) for (j=0;j<=n;j++)
                {
                    double temp=a[i][j];
                    a[i][j]=a[k][j];
                    a[k][j]=temp;
                }
    cout<<"\\nThe matrix after Pivotisation is:\\n";
    for (i=0;i<n;i++)            //print the new matrix
    {
        for (j=0;j<=n;j++)
            cout<<a[i][j]<<setw(16);
        cout<<"\\n";
    }
    for (i=0;i<n-1;i++)            //loop to perform the gauss elimination
        for (k=i+1;k<n;k++)
            {
                double t=a[k][i]/a[i][i];
                for (j=0;j<=n;j++)
                    a[k][j]=a[k][j]-t*a[i][j];    //make the elements below the
pivot elements equal to zero or elimnate the variables
            }

    cout<<"\\n\\nThe matrix after gauss-elimination is as follows:\\n";
    for (i=0;i<n;i++)            //print the new matrix
    {
        for (j=0;j<=n;j++)
            cout<<a[i][j]<<setw(16);
        cout<<"\\n";
    }
    for (i=n-1;i>=0;i--)                //back-substitution
    {                        //x is an array whose values correspond to the
values of x,y,z.. x[i]=a[i][n];                //make the variable to be
calculated equal to the rhs of the last equation for (j=i+1;j<n;j++) if (j!=i)
//then subtract all the lhs values except the coefficient of the variable whose
value                                   is being calculated
                x[i]=x[i]-a[i][j]*x[j];
        x[i]=x[i]/a[i][i];            //now finally divide the rhs by the
coefficient of the variable to be calculated
    }
    cout<<"\\nThe values of the variables are as follows:\\n";
    for (i=0;i<n;i++)
        cout<<x[i]<<endl;            // Print the values of x, y,z,....
    return 0;
}*/
ll modexpo(ll a, ll b, int mt) {
  int ans = 1LL;
  while (b > 0) {
    if (b & 1)
      ans = (ans * a) % mt;
    a = (a * a) % mt;
    b >>= 1;
  }
  return ans;
}
int getMod(string str, int n, int k) {
  int pwrTwo[n];
  pwrTwo[0] = 1 % k;
  for (int i = 1; i < n; i++) {
    pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
    pwrTwo[i] %= k;
  }
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n) {
    if (str[j] == '1') {
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int st = 0;
  for (int i = 0; i < x.length(); i++) {
    if (x[i] == '1')
      st++;
  }
  int rem1 = getMod(x, n, st + 1);
  int rem2 = getMod(x, n, st - 1);
  vector<int> res;
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      int tmp1 = modexpo(2, n - i - 1, st + 1);
      int mt = st + 1;
      int total = (tmp1 + rem1) % mt;
      int ct = 1;
      while (total > 0) {
        int stbits = setbits(total);
        total = total % stbits;
        ct++;
      }
      res.pb(ct);
    } else {
      int tmp1 = modexpo(2, n - i - 1, st - 1);
      int mt = st - 1;
      int total = (rem2 - tmp1 + mt) % mt;
      // if(i==n-2){cout<<tmp1<<" "<<total<<" "<<rem1<<"\\n";}
      int ct = 1;
      while (total > 0) {
        int stbits = setbits(total);
        total = total % stbits;
        ct++;
      }
      res.pb(ct);
    }
  }
  for (int i = 0; i < res.size(); i++)
    cout << res[i] << "\\n";
}