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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ld;

typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define _ % MOD
#define __ %= MOD

#define each(it, s) for (auto it = s.begin(); it != s.end(); ++it)
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.begin(), v.end(), greater<auto>())
#define fill(a) memset(a, 0, sizeof(a))

#define swap(a, b)                                                             \\
  {                                                                            \\
    a = a + b;                                                                 \\
    b = a - b;                                                                 \\
    a = a - b;                                                                 \\
  }
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define fbo find_by_order
#define ook order_of_key
ll cnt = 0;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll power(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res % m;
} // modular exponent
const int maxn = 2e5 + 5;
ll dp[maxn];
ll n;
ll steps(ll x) {
  if (x == 0)
    return 0;
  int ans = 0;
  while (x > 0) {
    ans++;
    ll ppc = __builtin_popcount(x);
    x = x % ppc;
  }
  return ans;
}
void work() {
  rep(i, maxn) { dp[i] = steps(i); }
}
int main() {
  ios_base::sync_with_stdio(false); // don't use printf and scanf
  cin.tie(NULL);                    // cout<<fixed<<setprecision
  cin >> n;
  string s;
  cin >> s;
  work();
  // cout<<"yes";
  ll len = s.length();
  for (ll i = len - 1; i >= 0; i--) {
    if (s[i] == '1')
      cnt++;
  }
  // only for cnt+1 and cnt-1
  if (cnt == 0) {
    rep(i, n) { cout << 1 << '\\n'; }
    return 0;
  }
  ll cnt1 = cnt + 1;
  ll num = 0;
  for (ll i = len - 1, j = 0; i >= 0; i--, j++) {
    num += (s[i] - '0') * power(2, j, cnt1);
    num %= cnt1;
  }
  if (cnt == 1) {
    rep(i, n) {
      if (s[i] == '1')
        cout << 0 << '\\n';
      else {
        num += (power(2, n - i - 1, cnt1));
        num %= cnt1;
        cout << dp[num] + 1 << '\\n';
        num -= (power(2, n - i - 1, cnt1));
        num += cnt1;
        num %= cnt1;
      }
    }
    return 0;
  }
  ll cnt2 = cnt - 1;
  ll num2 = 0;
  for (ll i = len - 1, j = 0; i >= 0; i--, j++) {
    num2 += (s[i] - '0') * power(2, j, cnt2);
    num2 %= cnt2;
  }
  rep(i, n) {
    if (s[i] == '1') {
      num2 -= (power(2, n - i - 1, cnt2));
      num2 += cnt2;
      num2 %= cnt2;
      cout << dp[num2] + 1 << '\\n';
      num2 += (power(2, n - i - 1, cnt2));
      num2 += cnt2;
      num2 %= cnt2;
    } else {
      num += (power(2, n - i - 1, cnt1));
      num %= cnt1;
      cout << dp[num] + 1 << '\\n';
      num -= (power(2, n - i - 1, cnt1));
      num += cnt1;
      num %= cnt1;
    }
  }
  return 0;
}
// JUST ASK YOURSELF DID YOU GIVE YOUR BEST ? ISSE ZYADA
// KUCH KAR BHI NAHI SAKTE !! ENJOY AND GIVE YOUR BEST!!


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ld;

typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define _ % MOD
#define __ %= MOD

#define each(it, s) for (auto it = s.begin(); it != s.end(); ++it)
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.begin(), v.end(), greater<auto>())
#define fill(a) memset(a, 0, sizeof(a))

#define swap(a, b)                                                             \\
  {                                                                            \\
    a = a + b;                                                                 \\
    b = a - b;                                                                 \\
    a = a - b;                                                                 \\
  }
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (ll i = a; i <= (n); ++i)
#define repD(i, a, n) for (ll i = a; i >= (n); --i)
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define fbo find_by_order
#define ook order_of_key
ll cnt = 0;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll power(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % m;
    y = y >> 1;
    x = (x * x) % m;
  }
  return res % m;
} // modular exponent
const int maxn = 2e5 + 5;
ll dp[maxn];
ll n;
void work() {
  rep(i, maxn) {
    ll steps = 0;
    while (i) {
      i = i % (__builtin_popcount(i));
      steps++;
    }
    dp[i] = steps;
    watch(steps);
  }
}
int main() {
  ios_base::sync_with_stdio(false); // don't use printf and scanf
  cin.tie(NULL);                    // cout<<fixed<<setprecision
  cin >> n;
  string s;
  cin >> s;
  work();
  cout << "yes";
  // ll len=s.length();
  // for(ll i=len-1;i>=0;i--)
  // {
  //   if(s[i]=='1') cnt++;
  // }
  // // only for cnt+1 and cnt-1
  // if(cnt==0)
  // {
  //   rep(i,n)
  //   {
  //     cout<<0<<'\\n';
  //   }
  //   return 0;
  // }
  // ll cnt1=cnt+1;
  // ll num=0;
  // for(ll i=len-1,j=0;i>=0;i--,j++)
  // {
  //   num+=(s[i]-'0')*power(2,j,cnt1);
  //   num%=cnt1;
  // }
  // if(cnt==1)
  // {
  //   rep(i,n)
  //   {
  //     if(s[i]=='1') cout<<0<<'\\n';
  //     else
  //     {
  //       num+=(power(2,n-i-1,cnt1));
  //       num%=cnt1;
  //       cout<<dp[num]<<'\\n';
  //       num-=(power(2,n-i-1,cnt1));
  //       num+=cnt1;
  //       num%=cnt1;
  //     }
  //   }
  //   return 0;
  // }
  // ll cnt2=cnt-1;
  // ll num2=0;
  // for(ll i=len-1,j=0;i>=0;i--,j++)
  // {
  //   num2+=(s[i]-'0')*power(2,j,cnt2);
  //   num2%=cnt2;
  // }
  // rep(i,n)
  // {
  //   if(s[i]=='1')
  //   {
  //     num2-=(power(2,n-i-1,cnt2));
  //     num2+=cnt2;
  //     num2%=cnt2;
  //     cout<<dp[num2]<<'\\n';
  //     num2+=(power(2,n-i-1,cnt2));
  //     num2+=cnt2;
  //     num2%=cnt2;
  //   }
  //   else
  //   {
  //     num+=(power(2,n-i-1,cnt1));
  //     num%=cnt1;
  //     cout<<dp[num]<<'\\n';
  //     num-=(power(2,n-i-1,cnt1));
  //     num+=cnt1;
  //     num%=cnt1;
  //   }
  // }
  // return 0;
}
// JUST ASK YOURSELF DID YOU GIVE YOUR BEST ? ISSE ZYADA
// KUCH KAR BHI NAHI SAKTE !! ENJOY AND GIVE YOUR BEST!!