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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

ll F[2100010];
int c;

ll Mul(ll a, ll b) { return ((a % c) * (b % c)) % c; }

void fact(void) {
  F[0] = 1;
  F[1] = 1;
  for (int i = 2; i <= 2100000; i++) {
    F[i] = Mul(i, F[i - 1]);
  }
}
ll square(ll x) { return (x * x) % c; }

ll Power(ll x, ll y) {
  if (y == 0)
    return 1;
  else if (y == 1)
    return x % c;
  else if (y % 2 == 0)
    return square(Power(x, y / 2)) % c;
  else
    return square(Power(x, y / 2)) * x % c;
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      cnt++;
  }
  int sum1 = 0;
  int sum2 = 0;
  int power = 1;
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      sum1 = (sum1 + power) % (cnt + 1);
    }
    power = (power * 2) % (cnt + 1);
  }
  power = 1;
  if (cnt - 1 != 0) {
    for (int i = N - 1; i >= 0; i--) {
      if (X[i] == '1') {
        sum2 = (sum2 + power) % (cnt - 1);
      }
      power = (power * 2) % (cnt - 1);
    }
  }
  for (int i = 0; i < N; i++) {
    int num = 0;
    string S = X;
    int ans = 1;
    power = 1;
    if (S[i] == '0') {
      num = sum1;
      c = cnt + 1;
      num = (num + Power(2ll, (ll)(N - i - 1))) % c;
    } else {
      num = sum2;
      c = cnt - 1;
      if (c != 0) {
        num = (num - Power(2ll, (ll)(N - i - 1))) % c;
      }
    }
    if (num < 0)
      num += c;
    if (c == 0) {
      cout << 0 << endl;
      continue;
    }
    while (num > 0) {
      ans++;
      c = 0;
      power = 1;
      int tmp = num;
      int next = 0;
      for (int j = 0; j < 30; j++) {
        if (tmp % 2 == 1)
          c++;
        tmp /= 2;
      }
      for (int j = 0; j < 30; j++) {
        if (num % 2 == 1) {
          next = (next + power) % c;
        }
        power = (power * 2) % c;
        num /= 2;
      }
      num = next;
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

int main() {
  int N;
  string X;
  cin >> N >> X;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      cnt++;
  }
  for (int i = 0; i < N; i++) {
    int c = cnt;
    string S = X;
    if (S[i] == '0') {
      S[i] = '1';
      c++;
    } else {
      S[i] = '0';
      c--;
    }
    if (c == 0) {
      cout << 0 << endl;
      continue;
    }
    int num = 0;
    int power = 1;
    int ans = 1;
    for (int j = N - 1; j >= 0; j--) {
      if (S[j] == '1') {
        num = (num + power) % c;
      }
      power = (power * 2) % c;
    }
    while (num > 0) {
      ans++;
      c = 0;
      power = 1;
      int tmp = num;
      int next = 0;
      for (int j = 0; j < 30; j++) {
        if (tmp % 2 == 1)
          c++;
        tmp /= 2;
      }
      for (int j = 0; j < 30; j++) {
        if (num % 2 == 1) {
          next = (next + power) % c;
        }
        power = (power * 2) % c;
        num /= 2;
      }
      num = next;
    }
    cout << ans << endl;
  }
}