Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll powmod(ll n, ll mod) // 2^n % mod
{
  if (n == 0)
    return ll(1) % mod;
  if (n == 1)
    return ll(2) % mod;
  if (n % 2 == 0) {
    ll p = powmod(n / 2, mod);
    return p * p % mod;
  } else {
    return powmod(n - 1, mod) * 2 % mod;
  }
}

ll countbit(ll t) {
  ll res = 0;
  rep(i, 30) {
    if (ll(1 << i) & t)
      res++;
  }
  // cout << res << endl;
  return res;
}

int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  ll cnt = 0;
  rep(i, N) {
    if (S[i] == '1')
      cnt++;
  }

  ll cntm1 = 0;
  if (cnt > ll(1)) // cnt-1で割るとき
  {
    rep(i, N) {
      if (S[i] == '1') {
        cntm1 = (cntm1 + powmod(N - 1 - i, cnt - 1)) % (cnt - 1);
      }
    }
  }
  // cout << "a " << cntm1 << endl;
  ll cntp1 = 0;
  rep(i, N) {
    if (S[i] == '1') {
      cntp1 = (cntp1 + powmod(N - 1 - i, cnt + 1)) % (cnt + 1);
    }
  }

  // cout << "b " << cntp1 << endl;
  rep(i, N) {
    ll ans = 1;
    if (S[i] == '0') {
      ll temp = (cntp1 + powmod(N - 1 - i, cnt + 1)) % (cnt + 1);
      // cout << temp << " ";
      while (temp != 0) {
        ll mo = countbit(temp);
        temp = temp % mo;
        ans++;
      }
      cout << ans << endl;
    } else {
      if (cnt == 1) {
        cout << "0" << endl;
      } else {
        ll temp = (cntm1 - powmod(N - 1 - i, cnt - 1) + (cnt - 1)) % (cnt - 1);
        while (temp != 0) {
          ll mo = countbit(temp);
          temp = temp % mo;
          ans++;
        }
        cout << ans << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int powmod(int n, int mod) // 2^n % mod
{
  if (n == 0)
    return 1 % mod;
  if (n == 1)
    return 2 % mod;
  if (n % 2 == 0) {
    int p = powmod(n / 2, mod);
    return p * p % mod;
  } else {
    return powmod(n - 1, mod) * 2 % mod;
  }
}

int countbit(int t) {
  int res = 0;
  rep(i, 30) {
    if ((1 << i) & t)
      res++;
  }
  // cout << res << endl;
  return res;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int cnt = 0;
  rep(i, N) {
    if (S[i] == '1')
      cnt++;
  }

  int cntm1 = 0;
  if (cnt > 1) // cnt-1で割るとき
  {
    rep(i, N) {
      if (S[i] == '1') {
        cntm1 = (cntm1 + powmod(N - 1 - i, cnt - 1)) % (cnt - 1);
      }
    }
  }
  // cout << "a " << cntm1 << endl;
  int cntp1 = 0;
  rep(i, N) {
    if (S[i] == '1') {
      cntp1 = (cntp1 + powmod(N - 1 - i, cnt + 1)) % (cnt + 1);
    }
  }

  // cout << "b " << cntp1 << endl;
  rep(i, N) {
    int ans = 1;
    if (S[i] == '0') {
      int temp = (cntp1 + powmod(N - 1 - i, cnt + 1)) % (cnt + 1);
      // cout << temp << " ";
      while (temp != 0) {
        int mo = countbit(temp);
        temp = temp % mo;
        ans++;
      }
      cout << ans << endl;
    } else {
      if (cnt == 1) {
        cout << "0" << endl;
      } else {
        int temp = (cntm1 - powmod(N - 1 - i, cnt - 1) + (cnt - 1)) % (cnt - 1);
        while (temp != 0) {
          int mo = countbit(temp);
          temp = temp % mo;
          ans++;
        }
        cout << ans << endl;
      }
    }
  }
  return 0;
}