Language: C++

#include <iostream>
#include <iomanip>
#include<math.h>
#include<list>
#include <algorithm>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<string>
#include <numeric>
#include <queue>
#include <sstream>
#include <bitset>
#include<stack>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;


template<typename T>
T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }

template<typename T>
map<T, T> getPrimeFactor(T n) {
	map<T, T> res;
	for (T i = 2; i * i <= n; ++i) {
		while (n % i == 0) {
			res[i] ++;
			n /= i;
		}
	}
	if (n != 1) res[n] = 1;
	return res;
}

template<typename T>
bool IsPrimeNumber(T num)
{
	if (num <= 2) return true;
	else if (num % 2 == 0) return false;

	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			return false;
		}
	}

	return true;
}

long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

// 繰り返し二乗法
ll pow2(ll n, ll p, ll mod) {
	if (p == 0 || n == 1) {
		return 1;
	}
	else {
		ll ret = pow2(n * n % mod, p / 2, mod);
		if (p % 2 == 1) {
			ret *= n;
		}
		return ret % mod;
	}
}


#define rep(i,s,e) for(ll i=s;i<e;i++)
#define repeq(i,s,e) for(ll i=s;i<=e;i++)

map<string, ll> dic;
map<ll,string> dic2;

string getString(ll num) {
	if (num == 0) {
		return "0";
	}
	auto itr = dic2.find(num);
	if (itr != dic2.end()) {
		return itr->second;
	}

	string ret = (bitset<sizeof(num) * CHAR_BIT>(num)).to_string();
	rep(i, 0, ret.size()) {
		if (ret[i] == '1') {
			ret = ret.substr(i);
			break;
		}
	}
	dic2[num] = ret;
	return ret;
}

ll func(string a) {
	auto itr = dic.find(a);
	if (itr != dic.end()) {
		return itr->second;
	}

	ll d = 0;
	rep(i, 0, a.size()) {
		if (a[i] == '1') {
			d++;
		}
	}
	if (d == 0) {
		return 0;
	}

	string str_d = getString(d);
	string b = a.substr(0, str_d.size()-1);

	string ret = "";
	ll c = 0;
	if (b!="") {
		c = stoll(b, 0, 2);
	}
	rep(i, str_d.size() - 1, a.size()) {
		c *= 2;
		if (a[i] == '1') {
			c += 1;
		}

		if (c / d) {
			ret += "1";
		}
		else {
			ret += "0";
		}
		c = c % d;
	}
	return 1 + func(getString(c));
}

int main() {
	ll N;
	string X;
	std::cin >> N >> X;


	rep(i, 0, N) {
		string tmp = X;
		if (tmp[i] == '1') {
			tmp[i] = '0';
		}
		else {
			tmp[i] = '1';
		}

		std::cout << func(tmp) << endl;
	}

	return 0;
}


Compiler output:
a.cc: In function 'std::string getString(ll)':
a.cc:101:44: error: 'CHAR_BIT' was not declared in this scope
  101 |         string ret = (bitset<sizeof(num) * CHAR_BIT>(num)).to_string();
      |                                            ^~~~~~~~
a.cc:16:1: note: 'CHAR_BIT' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   15 | #include<stack>
  +++ |+#include <climits>
   16 | 
a.cc:101:52: error: template argument 1 is invalid
  101 |         string ret = (bitset<sizeof(num) * CHAR_BIT>(num)).to_string();
      |                                                    ^