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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

const double PI = acos(-1);

using namespace std;
typedef long long ll;
typedef long double ld;

const ll MOD = ll(1e9 + 7);
const ll INF = ll(1e18) + 7;

// repetition
#define REP(i, n) for (ll i = 0; i < n; i++)
#define RREP(i, n) for (ll i = n; i != 0; i--)
#define FOR(i, l, r) for (size_t i = (l); i < (r); ++i)

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll lcm(ll a, ll b) { return (a * b / gcd(a, b)); }

unsigned getDigit(unsigned num) {
  unsigned digit = 0;
  while (num != 0) {
    num /= 10;
    digit++;
  }
  return digit;
}

unsigned binarySearch(ll target, ll max, ll min) {
  while ((max - min) > 1) {
    if ((max + min) / 2 > target) {
      max = (max + min) / 2;
      min = min;
    } else {
      max = max;
      min = (max + min) / 2;
    }
  }
}

void f(int rest, string s) {
  char abc[] = "abc";
  if (rest == 0) {
    cout << s << endl;
  } else {
    for (int i = 0; i < 3; i++) {
      f(rest - 1, s + abc[i]);
    }
  }
}

/*
int main() {
 // n,m->size, k-> value
 // vector<vector<ll>> a(n ,vector<ll>(m, k));
 // vector
 // vector<ll> s(n,0);
 // vector<vector<ll>> v;
 // map<ll,ll> m;
 // priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
pq;
 // vector<pair<ll, ll>> epv(n, {0, 0});
 // vector<vector<char>> v(h, vector<char>(w));

 for(auto&& m: em) {
   epv.push_back({ m.second, m.first });
 }

 ll n, k, a;
 cin >> n >> k;

 vector<ll> vp;
 vector<ll> vn;

 ll c = 0;

 REP(i, n) {
   cin >> a;
   if(a >= 0 ) vp.push_back(a);
   else vn.push_back(a);
 }

 sort(vp.begin(), vp.end());
 sort(vn.begin(), vn.end());

 ll ps = vp.size();
 ll ns = vn.size();

 cout << "k: " << k << endl;
 cout << "ps: " << ps << endl;
 cout << "ns: " << ns << endl;

 if(k < ps * ns) {
   ll m = k%ns;
   ll d = k - (m+1);
   cout << vn[m] * vp[d] << endl;

 }else {
   ll kd = k - ps*ns;
   ll md = kd%ps;
   ll dd = kd - md;


   ll s = 0;
   REP(i, ps) {
     s = s+(ps-i-1);
     cout << "s: " << s << endl;
     cout << "kd: " << kd << endl;
     cout << "s: " << s << endl;
     cout << "kd: " << kd << endl;
     cout << "i: " << i << endl;
     if(s > kd) {
       cout << "vp[i]: " << vp[i] << endl;
       cout << "vp[ps-i]: " << vp[s-kd+i-1] << endl;
       cout << vp[i] * vp[s-kd+i-1] << endl;
       break;
     }
   }
 }

}


ll h, w;
vector<vector<char>> v;

vector<ll> dx = {-1, 0, 1, 0};
vector<ll> dy = {0, -1, 0, 1};

ll bfs(ll ox, ll oy) {
 vector<vector<ll>> vd(h, vector<ll>(w, -1));
 queue<pair<ll, ll>> q;
 q.push({ox, oy});
 vd[oy][ox] = 0;

 ll res = 0;
 while(!q.empty()) {
   pair<ll, ll> vn = q.front();
   q.pop();
   res = max(res, vd[vn.second][vn.first]);

   for(ll i = 0; i < 4; i++){
     ll nx = vn.first +dx[i];
     ll ny = vn.second + dy[i];

     if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
     if(v[ny][nx] == '#') continue;
     if(vd[ny][nx] == -1) {
       vd[ny][nx] = res + 1;
       q.push({nx, ny});
     }
   }
 }
 return res;
}

bool kai(string s) {
  bool flg = true;
  ll n = s.size();
  for(ll i=0; i<n/2; i++) {
    if(s[i] == s[n-1 -i]){
      flg = true;
    } else {
      flg = false;
      break;
    }
  }

  return flg;
}
*/

struct UnionFind {
  vector<ll> par;
  map<ll, ll> s;

  UnionFind(ll n) : par(n) {
    REP(i, n) {
      par[i] = i;
      s[i]++;
    }
  }

  ll root(ll x) {
    if (par[x] == x)
      return x;
    return root(par[x]);
  }

  void unite(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return;

    if (size(rx) > size(ry)) {
      s[rx] = s[rx] + s[ry];
      par[root(y)] = rx;
    } else {
      s[ry] = s[ry] + s[rx];
      par[root(x)] = ry;
    }
  }

  bool same(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return s[root(x)]; }
};

// base^{exp}%mod
ll mod_pow(ll base, ll exp, ll mod) {
  if (exp == 0)
    return 1;
  if (exp == 1)
    return base % mod;
  if (exp % 2 == 1)
    return (base * mod_pow(base, exp - 1, mod)) % mod;
  ll t = mod_pow(base, exp / 2, mod);
  return (t * t) % mod;
}

// nPr
ll mod_permutation(ll n, ll r) {
  ll t = 1;
  while (r != 0) {
    t = (t % MOD) * (n % MOD);
    n--;
    r--;
  }
  return t;
}

ll mod_factorial(ll r) {
  ll v = 1;
  while (r != 0) {
    v = (v % MOD) * (r % MOD);
    r--;
  }
  return v;
}

vector<ll> prime_factorization(ll n) {
  vector<ll> P;
  ll nn = sqrt(n) + 2;
  vector<ll> prime(nn + 1, true);

  for (ll i = 2; i <= nn; i++) {
    if (prime[i]) {
      for (ll j = 2 * i; j <= nn; j += i) {
        prime[j] = false;
      }
      P.emplace_back(i);
    }
  }

  vector<ll> d;
  REP(i, P.size()) {
    while (n % P[i] == 0) {
      d.push_back(P[i]);
      n /= P[i];
    }
  }
  if (n > nn)
    d.push_back(n);
  return d;
}

vector<ll> sieve(ll n) {
  vector<ll> prime(n, true);
  prime[0] = false;
  prime[1] = false;
  for (ll i = 2; i < n; i++) {
    if (prime[i]) {
      for (ll j = 2 * i; j < n; j++) {
        if (j % i == 0)
          prime[j] = false;
      }
    }
  }
  return prime;
}

bool is_prime(ll n) {
  ll nn = sqrt(n) + 2;
  vector<ll> prime(nn + 1, true);
  if (n % 2 == 0) {
    return false;
  }
  vector<ll> primes = sieve(nn);
  REP(i, primes.size()) {
    if (n % primes[i] == 0) {
      return false;
    }
  }
  return true;
}

struct Node {
  vector<ll> edges;
  vector<ll> costs;
  bool done;
  ll cost;

  friend bool operator<(const Node &lhs, const Node &rhs) {
    return rhs.cost < lhs.cost;
  }
};

ll mod_conb(ll n, ll r, ll mod) {
  return (mod_factorial(n) / mod_factorial(n - r) / mod_factorial(r)) % mod;
}

vector<vector<ll>> vv;
ll c = 0;
vector<ll> v;

ll n, m, q;
// indexからmまでのn桁の数列の列挙
void dfs2(ll index, ll count) {
  if (count < n) {
    for (ll i = index; i <= m; i++) {
      v[count] = i;
      dfs2(i, count + 1);
    }
  } else {
    vv.push_back(v);
  }
}

ll total(vector<ll> v) {
  ll t = 0;
  REP(i, v.size()) { t += v[i]; }
  return t;
}

// vector<ll> dx = {-1, 0, 1, 0};
// vector<ll> dy = {0, -1, 0, 1};

// void dfs(ll oy, ll ox, ll color) {
//   hw[oy][ox] = color;
//   seen[oy][ox] = true;
//   for(ll i = 0; i < 4; i++){
//     ll ny = oy + dy[i];
//     ll nx = ox + dx[i];
//     if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
//     if(seen[ny][nx] == false) {
//       ll co = qa.top();
//       qa.pop();
//       dfs(ny, nx, co);
//     }

//   }
// }

// std::ifstream in("sub1_06.txt");
// std::cin.rdbuf(in.rdbuf());

/*
ll bfs(ll ox, ll oy) {
  vector<vector<ll>> vd(h, vector<ll>(w, -1));
  queue<pair<ll, ll>> q;
  q.push({ox, oy});
  vd[oy][ox] = 0;

  ll res = 0;
  while(!q.empty()) {
    pair<ll, ll> vn = q.front();
    q.pop();
    res = max(res, vd[vn.second][vn.first]);

    for(ll i = 0; i < 4; i++){
      ll nx = vn.first +dx[i];
      ll ny = vn.second + dy[i];

      if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
      if(v[ny][nx] == '#') continue;
      if(vd[ny][nx] == -1) {
        vd[ny][nx] = res + 1;
        q.push({nx, ny});
      }
    }
  }
  return res;
}
*/

vector<ll> llvec(ll n) {
  vector<ll> c(n, 0);

  REP(i, n) { cin >> c[i]; }
  return c;
}

ll bitCount(ll n) {
  ll c = 0;

  for (ll i = 0; i < 64; i++) {
    if (n & 1)
      c++;
    n = n >> 1;
  }
  return c;
}

ll count(ll n) {
  ll c = 0;
  while (n != 0) {
    n = n % bitCount(n);
    c++;
  }
  return c;
}

int main() {
  ll n;
  string s;
  cin >> n >> s;

  ll c = 0;
  REP(i, n) {
    if (s[i] == '1')
      c++;
  }
  ll i_c = c + 1;
  ll d_c = c - 1;

  ll i_t = 0;
  ll d_t = 0;
  REP(i, n) {
    if (s[i] == '1') {
      i_t += mod_pow(2, n - 1 - i, i_c);
      i_t = i_t % i_c;
    }
    if (d_c != 0) {
      if (s[i] == '1')
        d_t += mod_pow(2, n - i - 1, d_c);
      d_t = d_t % d_c;
    }
  }

  long long MOD1 = 0, MOD2 = 0, Y = c - 1, Z = c + 1;
  if (Y != 0) {
    for (int i = 0; i < n; i++) {
      MOD1 *= 2;
      MOD1 += s[i] - '0';
      MOD1 %= Y;
    }
  }
  for (int i = 0; i < n; i++) {
    MOD2 *= 2;
    MOD2 += s[i] - '0';
    MOD2 %= Z;
  }

  // cout << "mod1: " << MOD1 << endl;
  // cout << "mod2: " << MOD2 << endl;
  // // cout << "y: " << Y << endl;
  // // cout << "z: " << Z << endl;
  // // cout << "i_c: " << i_c << endl;
  // // cout << "d_c: " << d_c << endl;
  // cout << "d_t: " << d_t << endl;
  // cout << "i_t: " << i_t << endl;

  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (d_c != 0) {
        ll next = (d_t - mod_pow(2, n - 1 - i, d_c) % d_c + d_c) % d_c;
        cout << count(next) + 1 << endl;
      } else {
        cout << 0 << endl;
      }
    } else {
      ll next = (i_t + mod_pow(2, n - 1 - i, i_c) % i_c) % i_c;
      cout << count(next) + 1 << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

const double PI = acos(-1);

using namespace std;
typedef long long ll;
typedef long double ld;

const ll MOD = ll(1e9 + 7);
const ll INF = ll(1e18) + 7;

// repetition
#define REP(i, n) for (ll i = 0; i < n; i++)
#define RREP(i, n) for (ll i = n; i != 0; i--)
#define FOR(i, l, r) for (size_t i = (l); i < (r); ++i)

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll lcm(ll a, ll b) { return (a * b / gcd(a, b)); }

unsigned getDigit(unsigned num) {
  unsigned digit = 0;
  while (num != 0) {
    num /= 10;
    digit++;
  }
  return digit;
}

unsigned binarySearch(ll target, ll max, ll min) {
  while ((max - min) > 1) {
    if ((max + min) / 2 > target) {
      max = (max + min) / 2;
      min = min;
    } else {
      max = max;
      min = (max + min) / 2;
    }
  }
}

void f(int rest, string s) {
  char abc[] = "abc";
  if (rest == 0) {
    cout << s << endl;
  } else {
    for (int i = 0; i < 3; i++) {
      f(rest - 1, s + abc[i]);
    }
  }
}

/*
int main() {
 // n,m->size, k-> value
 // vector<vector<ll>> a(n ,vector<ll>(m, k));
 // vector
 // vector<ll> s(n,0);
 // vector<vector<ll>> v;
 // map<ll,ll> m;
 // priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
pq;
 // vector<pair<ll, ll>> epv(n, {0, 0});
 // vector<vector<char>> v(h, vector<char>(w));

 for(auto&& m: em) {
   epv.push_back({ m.second, m.first });
 }

 ll n, k, a;
 cin >> n >> k;

 vector<ll> vp;
 vector<ll> vn;

 ll c = 0;

 REP(i, n) {
   cin >> a;
   if(a >= 0 ) vp.push_back(a);
   else vn.push_back(a);
 }

 sort(vp.begin(), vp.end());
 sort(vn.begin(), vn.end());

 ll ps = vp.size();
 ll ns = vn.size();

 cout << "k: " << k << endl;
 cout << "ps: " << ps << endl;
 cout << "ns: " << ns << endl;

 if(k < ps * ns) {
   ll m = k%ns;
   ll d = k - (m+1);
   cout << vn[m] * vp[d] << endl;

 }else {
   ll kd = k - ps*ns;
   ll md = kd%ps;
   ll dd = kd - md;


   ll s = 0;
   REP(i, ps) {
     s = s+(ps-i-1);
     cout << "s: " << s << endl;
     cout << "kd: " << kd << endl;
     cout << "s: " << s << endl;
     cout << "kd: " << kd << endl;
     cout << "i: " << i << endl;
     if(s > kd) {
       cout << "vp[i]: " << vp[i] << endl;
       cout << "vp[ps-i]: " << vp[s-kd+i-1] << endl;
       cout << vp[i] * vp[s-kd+i-1] << endl;
       break;
     }
   }
 }

}


ll h, w;
vector<vector<char>> v;

vector<ll> dx = {-1, 0, 1, 0};
vector<ll> dy = {0, -1, 0, 1};

ll bfs(ll ox, ll oy) {
 vector<vector<ll>> vd(h, vector<ll>(w, -1));
 queue<pair<ll, ll>> q;
 q.push({ox, oy});
 vd[oy][ox] = 0;

 ll res = 0;
 while(!q.empty()) {
   pair<ll, ll> vn = q.front();
   q.pop();
   res = max(res, vd[vn.second][vn.first]);

   for(ll i = 0; i < 4; i++){
     ll nx = vn.first +dx[i];
     ll ny = vn.second + dy[i];

     if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
     if(v[ny][nx] == '#') continue;
     if(vd[ny][nx] == -1) {
       vd[ny][nx] = res + 1;
       q.push({nx, ny});
     }
   }
 }
 return res;
}

bool kai(string s) {
  bool flg = true;
  ll n = s.size();
  for(ll i=0; i<n/2; i++) {
    if(s[i] == s[n-1 -i]){
      flg = true;
    } else {
      flg = false;
      break;
    }
  }

  return flg;
}
*/

struct UnionFind {
  vector<ll> par;
  map<ll, ll> s;

  UnionFind(ll n) : par(n) {
    REP(i, n) {
      par[i] = i;
      s[i]++;
    }
  }

  ll root(ll x) {
    if (par[x] == x)
      return x;
    return root(par[x]);
  }

  void unite(ll x, ll y) {
    ll rx = root(x);
    ll ry = root(y);
    if (rx == ry)
      return;

    if (size(rx) > size(ry)) {
      s[rx] = s[rx] + s[ry];
      par[root(y)] = rx;
    } else {
      s[ry] = s[ry] + s[rx];
      par[root(x)] = ry;
    }
  }

  bool same(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return s[root(x)]; }
};

// base^{exp}%mod
ll mod_pow(ll base, ll exp, ll mod) {
  if (exp == 1)
    return base % mod;
  if (exp % 2 == 1)
    return (base * mod_pow(base, exp - 1, mod)) % mod;
  ll t = mod_pow(base, exp / 2, mod);
  return (t * t) % mod;
}

// nPr
ll mod_permutation(ll n, ll r) {
  ll t = 1;
  while (r != 0) {
    t = (t % MOD) * (n % MOD);
    n--;
    r--;
  }
  return t;
}

ll mod_factorial(ll r) {
  ll v = 1;
  while (r != 0) {
    v = (v % MOD) * (r % MOD);
    r--;
  }
  return v;
}

vector<ll> prime_factorization(ll n) {
  vector<ll> P;
  ll nn = sqrt(n) + 2;
  vector<ll> prime(nn + 1, true);

  for (ll i = 2; i <= nn; i++) {
    if (prime[i]) {
      for (ll j = 2 * i; j <= nn; j += i) {
        prime[j] = false;
      }
      P.emplace_back(i);
    }
  }

  vector<ll> d;
  REP(i, P.size()) {
    while (n % P[i] == 0) {
      d.push_back(P[i]);
      n /= P[i];
    }
  }
  if (n > nn)
    d.push_back(n);
  return d;
}

vector<ll> sieve(ll n) {
  vector<ll> prime(n, true);
  prime[0] = false;
  prime[1] = false;
  for (ll i = 2; i < n; i++) {
    if (prime[i]) {
      for (ll j = 2 * i; j < n; j++) {
        if (j % i == 0)
          prime[j] = false;
      }
    }
  }
  return prime;
}

bool is_prime(ll n) {
  ll nn = sqrt(n) + 2;
  vector<ll> prime(nn + 1, true);
  if (n % 2 == 0) {
    return false;
  }
  vector<ll> primes = sieve(nn);
  REP(i, primes.size()) {
    if (n % primes[i] == 0) {
      return false;
    }
  }
  return true;
}

struct Node {
  vector<ll> edges;
  vector<ll> costs;
  bool done;
  ll cost;

  friend bool operator<(const Node &lhs, const Node &rhs) {
    return rhs.cost < lhs.cost;
  }
};

ll mod_conb(ll n, ll r, ll mod) {
  return (mod_factorial(n) / mod_factorial(n - r) / mod_factorial(r)) % mod;
}

vector<vector<ll>> vv;
ll c = 0;
vector<ll> v;

ll n, m, q;
// indexからmまでのn桁の数列の列挙
void dfs2(ll index, ll count) {
  if (count < n) {
    for (ll i = index; i <= m; i++) {
      v[count] = i;
      dfs2(i, count + 1);
    }
  } else {
    vv.push_back(v);
  }
}

ll total(vector<ll> v) {
  ll t = 0;
  REP(i, v.size()) { t += v[i]; }
  return t;
}

// vector<ll> dx = {-1, 0, 1, 0};
// vector<ll> dy = {0, -1, 0, 1};

// void dfs(ll oy, ll ox, ll color) {
//   hw[oy][ox] = color;
//   seen[oy][ox] = true;
//   for(ll i = 0; i < 4; i++){
//     ll ny = oy + dy[i];
//     ll nx = ox + dx[i];
//     if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
//     if(seen[ny][nx] == false) {
//       ll co = qa.top();
//       qa.pop();
//       dfs(ny, nx, co);
//     }

//   }
// }

// std::ifstream in("sub1_06.txt");
// std::cin.rdbuf(in.rdbuf());

/*
ll bfs(ll ox, ll oy) {
  vector<vector<ll>> vd(h, vector<ll>(w, -1));
  queue<pair<ll, ll>> q;
  q.push({ox, oy});
  vd[oy][ox] = 0;

  ll res = 0;
  while(!q.empty()) {
    pair<ll, ll> vn = q.front();
    q.pop();
    res = max(res, vd[vn.second][vn.first]);

    for(ll i = 0; i < 4; i++){
      ll nx = vn.first +dx[i];
      ll ny = vn.second + dy[i];

      if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
      if(v[ny][nx] == '#') continue;
      if(vd[ny][nx] == -1) {
        vd[ny][nx] = res + 1;
        q.push({nx, ny});
      }
    }
  }
  return res;
}
*/

vector<ll> llvec(ll n) {
  vector<ll> c(n, 0);

  REP(i, n) { cin >> c[i]; }
  return c;
}

int main() {
  // std::ifstream in("01-Handmade-00");
  // std::cin.rdbuf(in.rdbuf());
  ll n;
  string s;
  cin >> n;
  cin >> s;
  bitset<200001> b(s);
  vector<ll> p(200001, 0);
  vector<ll> t;

  REP(i, 200001 + 1) { p[i] = bitset<200001>(i).count(); }

  REP(i, n) {

    ll pop_n = (b ^ bitset<200001>(1 << i)).count();
    ll n = (b ^ bitset<200001>(1 << i)).to_ullong();
    ll c = 0;
    t.push_back(c);

    // cout <<((b^bitset<8>(1<<i)) & bitset<8>((b^bitset<8>(1<<i)).count())) <<
    // endl; cout <<((b^bitset<8>(1<<i)) |
    // bitset<8>((b^bitset<8>(1<<i)).count())) << endl;
  }

  REP(i, t.size()) { cout << t[t.size() - i - 1] << endl; }

  return 0;
}