We are given an integer X represented as a binary string of length N, possibly with leading zeros. For each bit position i from 1 to N (top means most significant? They say "the i-th bit from the top" meaning the most significant bit is first. So we have a binary string of length N, index 1..N from left (most significant) to right (least significant). We define X_i as X with the i-th bit inverted (i.e., flip that bit). Then we need to compute f(X_i) for each i, where f(n) is the number of operations until n becomes 0 in the process: while n>0: n = n % popcount(n). Note: f(n) includes the number of operations until zero? In example: n=7, two operations to get to 0, so f(7)=2. So f counts operations performed. Also for n=0? Not needed.
We need to compute for each i: f(X_i). X_i is X with bit i flipped. N up to 200,000, so we need efficient per bit O(log N) or O(1) after precomputation.
We need to handle numbers up to length N=200k bits, so X can be huge (2^200000). Can't convert to integer. We need to compute f(X_i) using properties of the process, perhaps related to modulo popcount.
Observation: The process: n -> n mod popcount(n). This reduces n quickly. Since popcount(n) <= number of bits of n (i.e., floor(log2(n))+1?), but n can be huge. For large numbers, popcount(n) is at most number of bits, which is up to 200k. So we can compute n mod popcount(n) efficiently if we know the original number mod some value. But need to compute f, which is number of steps. Since each step reduces n dramatically (n becomes less than popcount(n) ≤ bits of n), after first step, n becomes less than popcount(original) ≤ number of ones in original. So after first step, the number is at most 200k (since popcount ≤ N). Then we can simulate the process for numbers up to 200k quickly, maybe precompute f for numbers up to something like 200k or maybe up to 200k? Because after the first step, the number is at most the popcount of the original, which is at most N. But note: When we flip a bit, the popcount changes by ±1. So popcount of X_i is either pc+1 or pc-1 (if original pc = number of ones in X). So the first step reduces to a remainder modulo (pc ± 1). That remainder will be at most (pc ± 1)-1, which is at most pc (if +1) or pc-2? Actually at most popcount-1, so at most N. So after first step, we are dealing with numbers less than or equal to N. So we can precompute f for all numbers from 0 up to something like maybe 2*10^5? Since N max 200k, and popcount can be up to N, so after first step the number is less than N, and subsequent steps will reduce further. But careful: In the subsequent steps, the popcount of the new number (which is less than N) will be at most log2(N) maybe? Actually popcount of a number m is at most number of bits of m, which is floor(log2(m))+1. For m up to 200k (≈ 2^18), popcount ≤ 18. So the next step reduces to m mod popcount(m) which is less than popcount(m) ≤ 18, so then subsequent steps reduce quickly. So we can precompute f for numbers up to N (or maybe up to some bound like 200k) using DP/memoization. That's doable: we can compute f(n) for n from 0 to N (or up to maybe 300k) by iterating: f(0)=0; for n>0, f(n) = 1 + f(n % popcount(n)). But computing n % popcount(n) for each n up to 200k is cheap. However note that for n up to 200k, popcount(n) is at most about 18, so modulus is quick. So we can precompute f for n up to 200k. But X_i may be as large as 2^N, we cannot compute f directly. But we can compute the first step's result: compute X_i mod popcount(X_i). Then after that we have a number less than popcount(X_i) ≤ N+1 maybe. But careful: When we flip a bit, popcount might become 0? If X originally has pc=0 (all zeros), then flipping a bit gives pc=1. But X can have leading zeros, but it says X is an integer with N digits in binary, possibly with leading zeros. So pc could be zero? If X=000...0, then pc=0. But then original process for n=0? Actually f(0) maybe defined? But X_i after flipping a bit becomes 1 followed by zeros? That's a positive integer. So we need to handle pc=0 case separately: popcount(0)=0? Actually popcount(0)=0. But then division by popcount would be division by zero, but the process is defined only for positive n? They say: "replace n with the remainder when n is divided by popcount(n). (It can be proved that, under the constraints of this problem, n always becomes 0 after a finite number of operations.)" For n=0, maybe they don't apply operation; but f(0) should be 0 operations? Usually f(0) would be 0. But if popcount(n)=0, operation is invalid. But they said constraints guarantee finite steps; for positive n, popcount(n) > 0 always. So for X_i, popcount is at least 1 because flipping a bit from 0 to 1 yields at least one 1. If X originally all zeros, flipping a bit gives exactly one 1. So popcount > 0. So we are safe.
Thus the main challenge: For each i (1..N), compute:
Let p = popcount(X) = total number of '1's in original string.
When we flip bit i, the new popcount p_i = p + 1 if X[i]=='0' else p - 1 if X[i]=='1'.
Let M = p_i (the new popcount). We need to compute f(X_i). Since X_i is huge, we cannot directly compute X_i mod M. However we can compute X_i mod M efficiently using the original X and precomputed powers of 2 modulo M, because flipping a bit changes the number.
Let X be the integer value of the given binary string (but huge). Represent bits: most significant bit at position 1 corresponds to value 2^{N-1}? Actually if string is s[1..N] (index 1 leftmost), then X = sum_{k=1}^{N} (s[k] == '1') * 2^{N-k}. Because the leftmost bit has weight 2^{N-1}, rightmost weight 2^0.
When we flip bit i, we change the value: X_i = X + delta_i, where delta_i = (new bit - old bit) * 2^{N-i}. If old bit = 0 and new = 1, delta = +2^{N-i}; if old = 1 and new = 0, delta = -2^{N-i}.
We need to compute X_i mod M. Since X can be huge, we need to compute X mod M for each M (which depends on i). Since M differs per i (p+1 or p-1), we need to compute X mod M for two different moduli (p+1 and p-1) for all i. But there could be up to 200k positions, and p can be up to 200k, but p+1 and p-1 are at most 200k+1. However we need to compute X mod M for each M, and then adjust by delta_i mod M. But naive recomputation for each i would be O(N^2) if we compute modular reduction for each modulus separately. However we can precompute prefix and suffix contributions using exponentiation? Since M varies per i, we need an efficient way to compute X mod M for many different moduli. But note that there are only two distinct moduli: p+1 and p-1 (unless p=0 or p=1? p-1 could be 0? If p=1 and we flip a '1' then p_i = 0? Actually flipping a '1' when p=1 gives p_i = 0, but popcount=0 would be invalid. Let's check: If original p=1, flipping the only '1' yields a number with 0 ones, but is that allowed? X_i would be 0? Actually if X originally has exactly one '1', flipping that bit to 0 makes all bits zero, so X_i = 0. But f(0) is probably 0? But does the process defined for n=0? They say replace n with remainder when divided by popcount(n). If n=0, popcount(0)=0, division by zero. But they may not need to process because we start from n>0? For X_i=0, there are zero operations needed. So we need to handle the case where p_i = 0 specially. But note: if we flip a '1' when p=1, then the new number is 0, so f(0) should be 0. That's a valid output. So we need to handle p_i = 0 as a separate case. Also if p=0 originally (all zeros), flipping a 0 to 1 gives p_i=1, fine; but flipping a 0 to 1 when p=0 is only case; flipping a 1 doesn't exist. So we need to consider p_i = p-1 could be 0 only if p=1. So we need to handle that.
Thus we need to compute X mod M for M = p+1 (for positions where bit is '0') and for M = p-1 (for positions where bit is '1'), with the special case that if p-1 == 0 (i.e., p=1 and flipping a '1'), then f(X_i)=0 because X_i=0.
Similarly, if p+1 == 0? That would require p=-1, impossible. So only p-1 can be zero.
Now, p can be up to 200k. So we have two moduli: A = p+1, B = p-1 (if p>1, else B=0 case). We need to compute X mod A and X mod B. But note that X is huge (N bits). We can compute X mod a given modulus by iterating through bits and accumulating: val = (val*2 + bit) % mod. That's O(N) per modulus. Since we only have two moduli, that's fine! O(N) for each modulus = O(N) total. That's acceptable. But careful: For each i, we need to adjust X_i mod M. X_i = X + delta_i. But delta_i is ±2^{N-i}. So we can compute X_mod_M, then compute delta_mod_M, then (X_mod_M + delta_mod_M) mod M gives X_i mod M. However careful: If we flip a '1' to '0', delta = -2^{N-i}, so we need to subtract that mod M. So we need to compute 2^{N-i} mod M for each i and each M? But we have only two M values. We can precompute for each modulus M, the values of 2^{k} mod M for k from 0 to N? Actually we need for each position i, the exponent e = N-i (weight of that bit). So we can precompute pow2_mod_M[e] for e from 0..N-1? That's O(N) per modulus. So overall O(N) time. That's fine.
But careful: For each i, we need to compute X_i mod M where M is either A or B depending on the bit. So we can do:
Precompute:
-
For modulus A = p+1:
modA_rem = X % A (by scanning bits)
pow2_modA[k] for k=0..N-1 (maybe compute from least significant side? Actually we need 2^{N-1-i}? Let's define index: bit at position i (1-indexed from left) has weight 2^{N-i}. So we need 2^{N-i} mod A. We can compute an array pow2_modA[i] for i from 1..N: pow2_modA[i] = 2^{N-i} mod A. That can be computed by starting from pow = 1 (2^0) and then iterating from rightmost? Alternatively compute 2^{N-1} mod A and then divide? Better: precompute from rightmost: Let pow2_modA_right[j] = 2^{j} mod A for j from 0..N-1 (where j=0 corresponds to LSB). Then for position i (from left), weight exponent = N-i, so we need pow2_modA_right[N-i]. So we can compute an array pow2_exp[e] for e=0..N-1. That's O(N) time per modulus.
-
For modulus B = p-1 (if p>1; else B may be 0 for p=1). For p=1, we only have one modulus? Actually if p=1, for bits that are '0' (if any) we need modulus A = p+1 = 2; for bits that are '1' (the single '1') we have special case because flipping yields 0, so answer 0. So we only need to compute A case.
Similarly, if p=0, then all bits are '0'; flipping any bit (to '1') gives p_i = 1, modulus = 1. Then we need X mod 1 = 0 (since any number mod 1 is 0). But careful: popcount=1, then the first operation: n -> n % 1 = 0. So f(X_i) = 1? Actually for X_i, n positive, popcount=1, so remainder of n divided by 1 is 0. So after one operation it becomes 0. So f(X_i)=1. But we can compute via our method: X_i mod 1 = 0, so first step yields 0, then f(0)=0, so total f = 1. But we can also handle special case p_i=1: if p_i=1, then regardless of n, n%1 =0, so f(n)=1 for any positive n (since one operation reduces to 0). Actually check: If n>0 and popcount(n)=1, then n is a power of two? Wait, if popcount(n)=1, then n is of the form 2^k. Then n%1 =0 indeed, because 1 divides any integer. So yes, one operation. So for p_i=1, f=1 automatically. So we could treat modulus 1 specially: X_mod_1 = 0, but then after first step we have 0, f=1. So we could compute using generic method: compute X_i mod 1 = 0, then next number = 0, so f(0)=0, so total f = 1. But our plan is: after computing X_i mod M (first remainder), we then compute f of that remainder (which is ≤ M-1) using precomputed f values for numbers up to N (or up to 200k). But if M=1, the remainder is 0, and we can get f(0)=0, and then total f = 1 + f(0) = 1. So we need to add 1 for the first operation. So overall f(X_i) = 1 + f(r) where r = X_i mod M, with M = popcount(X_i). But careful: For original n, f(n) is defined as number of operations until zero. The recurrence: f(0)=0; for n>0, f(n) = 1 + f( n % popcount(n) ). So that holds.
So for each X_i, we can compute M = p_i, compute r = X_i mod M, then answer = 1 + f(r), where f(r) is precomputed for r up to maybe 200k. However note: r < M, and M ≤ N+1, so r < N+1, which is ≤ 200,001. So we can precompute f for all numbers up to 200,000+ (maybe 200,001 or up to 200,000). But note: M could be p+1 which could be up to N+1 (if p=N). So r could be up to N (since r < M). So precomputing up to N is enough. However r might be exactly N? For N=200k, p could be N (if X is all ones) then flipping a 1 -> p_i = N-1, M=N-1, so r < N-1. So max r is N-1. But for flipping a 0 when p=N? p+1 = N+1, so M=N+1, r < N+1, could be up to N. So we need f up to N (maybe N+1). So we can precompute f for numbers up to 200,000+ (like 200,005) which is trivial.
But careful: There is also the case p_i = 0 (n=0), answer = 0.
Thus approach:
-
Input N, binary string X_str (length N).
-
Compute p = count of '1's in X_str.
-
Precompute f array for numbers from 0 up to max_n = 200000 (or maybe up to 200001). Use DP: for n from 0 to max_n, compute popcount(n), then next = n % popcount(n) (if popcount>0; for n=0, popcount=0, but we treat f[0]=0 and stop). But for n>0, popcount>0. So compute: f[n] = 1 + f[next]. Since n up to 200k, we can compute iteratively. But careful: For n small (like 1), popcount(1)=1, next=1%1=0, f[1]=1+ f[0]=1. Good.
We can also compute popcounts for numbers up to max_n using builtin or precompute.
-
We need to compute, for each bit i (1-indexed from left), the answer.
Let bit = X_str[i-1] (since 0-indexed in code). If bit == '0': M = p+1; else (bit == '1'): if p == 1: special case M=0? Actually if p==1 and bit =='1', then flipping makes p_i = 0 => X_i=0, answer = 0. Else M = p-1.
Then we need to compute r = X_i mod M. Compute:
X_mod_M = X % M. But we need X_i_mod_M = (X + delta) mod M, where delta = +2^{N-i} if flipping 0->1, delta = -2^{N-i} if flipping 1->0.
So:
r = (X_mod_M + delta_mod_M) mod M. Ensure positive.
Then answer = 1 + f[r] (since r < M, and we already have f for r). But careful: If M == 1? Then r = 0 always because X_mod_1=0, delta_mod_1=0, r=0, answer = 1 + f[0] = 1. Works.
Need to also consider M might be 0? Then answer 0.
However what about the case when M = p-1 = 0 (i.e., p=1, bit '1') we have already handled as answer=0.
For p=0, bit '0' case: M = p+1 = 1, answer = 1 (since X_i mod 1 =0, f[0]=0, answer=1). This matches.
-
So we need to compute X_mod_A and X_mod_B, where A = p+1, B = p-1 (if p>1). And also need powers of 2 modulo A and B for each exponent e = N-i (i from 0..N-1). Also need to compute delta_mod for each i accordingly.
Thus we can compute:
Let mod_vals = [A, B] if B>0 else just A.
For each modulus M in {A, B if applicable}, compute:
- rem_M = X % M: iterate through bits from left to right: val = 0; for each bit char c in X_str: val = (val * 2 + (c-'0')) % M.
- pow2_mod_M: an array of length N, where pow2_mod_M[j] = 2^{j} mod M? But we need exponent for each bit i (0-index from left): weight = 2^{N-1-i} if using 0-index? Let's define indices clearly.
Let's index bits from 0 to N-1 left to right. Then bit at index i (0 = MSB) contributes 2^{N-1-i} to X. So flipping this bit changes by delta = ±2^{N-1-i}. In 0-indexed, i (0..N-1) corresponds to position from top i+1.
Thus we need for each i, the value 2^{N-1-i} mod M. We can precompute an array pow2_mod_M_rev: Let's compute from the least significant side: For exponent e = 0 to N-1 (where e = 0 for LSB), we can compute pow2_mod_M[e] = (2^e) % M. Then for bit i, exponent = (N-1-i) = (N-1 - i). So we can use pow2_mod_M[ N-1-i ].
We can compute pow2_mod_M[0] = 1 % M. Then for e from 1 to N-1: pow2_mod_M[e] = (pow2_mod_M[e-1] * 2) % M.
Alternatively, we can compute an array directly from left: start with cur = 2^{N-1} mod M? That might be less convenient.
So for each modulus M, we compute pow2_mod_M array of length N (exponents 0..N-1).
Now, for each i (0-index), we determine M and compute delta_mod:
if original bit = '0': delta_mod = + pow2_mod_M[ N-1-i ] mod M
else (bit='1'): delta_mod = - pow2_mod_M[ N-1-i ] mod M (i.e., (M - pow2_mod_M[ N-1-i ]) % M, but careful: if M is 0? not)
Then r = (rem_M + delta_mod) % M.
Answer = 1 + f[r] (if M > 0; if M==0, answer=0).
But note: If M = 1, pow2_mod_M[any] = 0? Actually 2^e mod 1 = 0. So delta_mod = 0, rem_M = 0, r = 0, answer = 1 + f[0] = 1, correct.
But we must handle the case where p_i = p+1 or p-1 could be 0 only when p=1 and flipping a '1'. Already handled.
Now, there's a nuance: f(r) is precomputed for numbers up to maybe N. But r < M and M could be up to N+1, so r up to N. However r could be as large as N (when M = N+1). f(N) we need to compute, N up to 200k. That's fine.
One more nuance: For p_i = p-1, if p-1 > 0, we have M = p-1. But also note: r = X_i mod M, but X_i = X - 2^{N-1-i} when flipping a '1'. But we compute X_mod_M using the original X. However careful: When we compute X_mod_M, we need the modulus M = p-1. But the original X mod M is computed. However, X_i = X - 2^{k} where k = N-1-i. So X_i mod M = (X mod M - 2^{k} mod M) mod M. That's correct. So no issues.
But note: The precomputed rem_M for modulus M = p-1 is valid only if p-1 > 0. For p=1, we skip.
But we also need to consider the case p = N (all ones) and flipping a 1: then M = N-1. That's okay.
Now, is this always correct? Let's double-check the definition: f(n) counts the number of operations until n becomes 0. Our formula f(n) = 1 + f(n % popcount(n)) holds for n>0. So for X_i > 0, we can compute first step remainder r = X_i mod M, then answer = 1 + f(r). So we just need r.
Now, is there any edge case where X_i = 0? That happens when flipping the only '1' in a number with p=1, and also maybe when the number is originally 0 and flipping a 0? No, flipping a 0 to 1 gives 1, not 0. So only case is p=1 and flipping the sole '1' yields X_i = 0. Also if p=0 and we flip a 0, we get 1, not 0. So yes.
Now, does the process guarantee that after first step, r is less than M, and we have precomputed f for numbers up to M-1, which is ≤ N? But note: f(r) may depend on numbers larger than N? Because r < M, M ≤ N+1, so r ≤ N. But f(r) could involve a subsequent step where the next number could be up to popcount(r)-1, which is ≤ number of bits of r, which is ≤ floor(log2(r))+1, much smaller than N. So precomputing up to N covers all r and all subsequent steps. So fine.
Now, complexity: O(N) to compute p; O(max_n) (200k) to precompute f; O(N) to compute X mod A and maybe X mod B; O(N) to compute pow2_mod_A and pow2_mod_B (if needed); then O(N) to loop through bits, compute r, lookup f[r], output answer. So O(N) overall.
But careful: For modulus B, if p-1 > 0, we compute rem_B and pow2_mod_B. However, note that p-1 could be as large as N-1, which is fine. But we also need to handle the case where p-1 == 0? Already special.
Now, consider p=1: For bits that are '0', we need A = p+1 = 2; for the '1' bit, answer = 0. So we need to compute X_mod_2 and pow2_mod_2. That's easy. So we still need rem_A and pow2_A. But we do not need rem_B because B=0. So we need to conditionally compute for B only if p-1 > 0.
Similarly, if p=0: only A=1. So we compute X_mod_1 = 0 trivially, pow2_mod_1 all zeros. So we can compute anyway, but careful: for modulus 1, pow2_mod_1[any] = 0, but we may compute by the same method, but mod 1: any number % 1 = 0, so loops work. However need to avoid division by zero? In computing pow2_mod for modulus 1: we would do pow2[0] = 1 % 1 = 0; then pow2[e] = (pow2[e-1] * 2) % 1 = 0. So fine. So we can compute even if M=1. But note: The modulus for p+1 may be 1 when p=0. That's okay. So we can just always compute for A (p+1) and for B (p-1) if B>0. But we need to be careful if p-1 = 0, we skip because M=0 leads to division by zero in modular arithmetic. So we treat that separately.
Now, we need to compute f array up to max_val = max possible r. r < M ≤ max(p+1, p-1) ≤ N+1. So we can precompute f up to N+1, say up to 200005.
Now, let's test with examples.
Example 1:
N=3, X="011" -> bits: 0,1,1. p=2.
A = p+1 = 3, B = p-1 = 1.
Precompute f up to maybe 3.
popcounts: f[0]=0; f[1]=1 (1%1=0), f[2]: popcount(2)=1? 2 in binary '10', popcount=1, 2%1=0 -> f[2]=1; f[3]: popcount(3)=2, 3%2=1, f[1]=1, so f[3]=2.
Now compute X_mod_A= X mod 3. X = binary 011 = 3? Actually "011" is 3 in decimal (since 04+12+1=3). So X_mod_3=0. pow2_mod_A: exponents: N-1-i: for i=0 (MSB) weight 2^{2}=4 mod 3 = 1; i=1 weight 2^{1}=2 mod 3 = 2; i=2 weight 2^{0}=1 mod 3 = 1.
X_mod_B= X mod 1 = 0; pow2_mod_B: all 0 since mod 1.
Now for each i:
i=0 (bit '0'): M=3, delta_mod = + pow2[2? Actually i=0 => exponent N-1-i = 2 -> pow2_mod_A[2]? We need pow2 array indexed by exponent. Let's define: pow2_A[exp] = 2^{exp} % A. For i=0, exp = N-1-0 = 2. pow2_A[2] = (2^2)%3=1. So delta_mod = +1. rem_M = 0. r = (0+1)%3=1. answer = 1 + f[1] = 1+1=2. Output 2. matches.
i=1 (bit '1'): Since bit=1, M = B = 1 (since p-1=1). But wait p=2, p-1=1 >0, so M=1. But note: for bit='1', flipping gives p_i=1. So M=1. Then we compute? Using generic formula: M=1, rem_M=0, delta_mod = - pow2_mod_B[ N-1-i ]? But B=1, pow2_mod_B[exp]=0. delta_mod = -0 =0. r = 0%1 =0. answer = 1 + f[0] = 1. Output 1. matches.
i=2 (bit '1'): similarly M=1, answer=1. Output 1. matches example.
Great.
Example 2: We'll trust.
Now, need to ensure that for p=1, flipping the '1' gives answer 0, not 1. Let's test: N=1, X="1", p=1. For i=1 (only bit, '1'): flipping gives X_i=0, answer should be 0. Our method: bit='1', p=1, so M = p-1 = 0 -> we treat answer=0. Good. For flipping '0' if there is any? Suppose N=2, X="01", p=1. For bit1 '0': M=p+1=2; compute X_mod_2 and pow2_mod_2; r = (X_mod_2 + 2^{N-1-i}) % 2. X=1? Actually "01" = 1. X_mod_2 = 1. For i=0 (MSB '0'), weight 2^{1}=2 mod 2 = 0? Actually 2 mod2=0, delta_mod=+0, r=1%2? Wait X_mod_2 = 1, delta_mod = +0? Let's compute: exponent N-1-i = 1? N=2, i=0 => exponent=1, pow2_mod_2[1] = 2%2=0. So r = 1%2=1. answer = 1+f[1] = 1+1=2. Check manually: X_i = flipping MSB from 0 to 1 gives "11" = 3, popcount=2, 3%2=1, then popcount(1)=1, 1%1=0 -> two ops, f=2. Good. For bit2 '1' (LSB): flipping gives "00"=0, answer=0. Works.
Now, consider p=0: X all zeros. N=3, X="000", p=0. Then A = p+1 = 1. For each bit (all '0'), M=1, answer = 1. Let's test: Flipping any bit yields binary 001 = 1, popcount=1, 1%1=0 -> one operation, f=1. Good.
Now, we also need to consider that f(1) = 1 (since popcount=1 => remainder=0). f(0)=0. So our f array computed as described will have f[1]=1.
Implementation details:
-
We'll store X_str as a string of length N.
-
Compute p = count of '1' in X_str.
-
Precompute f up to max_n = 200000 (maybe N+5). Actually N can be 200000, but we need up to max(r). r < M ≤ max(p+1, p-1). p ≤ N, so max M is N+1. So max r could be N (if M=N+1 and r = N). So we need up to N. But N may be 200000, so allocate array size N+5.
-
Compute f:
f[0] = 0.
For n from 1 to max_n:
pop = popcount(n) (number of bits set). We can compute using built-in __builtin_popcount or a manual function. Since max_n ~ 200k, __builtin_popcount works for 32-bit int. Use __builtin_popcount(n) for speed. Or precompute popcounts for all n using DP: popcount[n] = popcount[n>>1] + (n&1). But __builtin_popcount is fine and fast. But careful: __builtin_popcount expects unsigned int; n <= 200000 fits. Use C++.
next = n % pop; (pop > 0 always for n>0)
f[n] = 1 + f[next].
-
Compute rem_A: X_mod_A where A = p+1.
remA = 0;
for each char c in X_str:
remA = (remA * 2 + (c-'0')) % A;
(If A maybe 1? Then (remA * 2 + bit) % 1 = 0, okay.)
-
Compute pow2_mod_A: vector powA(N); // but we need exponent from 0..N-1. We'll compute as:
powA[0] = 1 % A; // 2^0 mod A
for e=1..N-1: powA[e] = (powA[e-1] * 2) % A;
But careful: exponent e corresponds to 2^e. For bit i (0-index), exponent = N-1-i, so we need powA[N-1-i]. So we need powA array indexed by e.
Alternatively, we can compute an array powA_rev[i] directly for each bit i by scanning from right. That might be easier: compute from rightmost bit, maintaining cur = 1 % A; assign to i from N-1 downto 0: pow_for_bit[i] = cur; cur = (cur*2) % A; But then exponent for i is the weight of that bit (2^{N-1-i}). That works. But we also need pow for B similarly. We'll compute exponent arrays of length N.
Given we have to compute for both A and possibly B, we can just compute pow arrays for exponents 0..N-1.
- Similarly for B if p-1 > 0: compute remB, powB.
Now, loop for i from 0 to N-1:
original_bit = X_str[i] - '0'.
if original_bit == 0:
M = A
delta_mod = powA[N-1-i] // because +2^{N-1-i} mod M
rem_M = remA
else: // bit == 1
if p == 1: // special case leads to M=0?
// Actually if p==1 and bit==1, flipping yields 0. So answer = 0 directly.
// We can compute separately.
// But we could also note M = p-1 = 0, but we skip.
So we check: if p == 1 and original_bit == 1: answer = 0; print; continue.
else:
M = B // p-1 >0
delta_mod = (M - powB[N-1-i]) % M // because -2^{k} mod M = (M - (2^{k} mod M)) % M
rem_M = remB
// Now compute r = (rem_M + delta_mod) % M
// answer = 1 + f[r]
// print answer
But careful: In the case original_bit==0, M = A. But what if A == 0? Impossible because p >=0, so A = p+1 >= 1.
Also for original_bit==1 and p>1, M = B >0.
Now, we must also consider p=0: original_bit all 0. Then original_bit==0 for all i, M = A = 1. So we compute powA array: 2^e mod 1 = 0, so delta_mod=0, remA=0, r=0, answer = 1+f[0]=1. Works.
Now, what about p=N (all ones)? Then original_bit==1 for all i, M = B = N-1 (assuming N>1). p>1 unless N=1. For N=1, p=1, handled special. So for N>1, p>1, so we compute remB and powB. That's fine.
Now, there is a nuance: The popcount of X_i is M. But the first operation's remainder is X_i mod M. However, we assumed that M = p_i. But p_i is the popcount of X_i. That is correct because we flipped exactly one bit, so popcount changes by ±1.
Now, f(r) we precomputed for numbers up to max_n. However, r < M ≤ N+1, so r ≤ N. Our precomputed f array only goes up to maybe N (or N+5). So we need to allocate size at least N+2.
Now, we need to be careful about the case where M = 1. Then r = 0. f[0] = 0, answer=1. That's fine.
Now, check if any scenario where after first step, r is not < M? It will be by definition of modulus. So safe.
Now, we should test with the second example maybe manually a bit, but trust.
Now, possible pitfalls: For large N, computing pow2_mod arrays for A and B each of size N (200k) is fine. But we must be careful with integer overflow? We'll use int because mod values up to N+1 ≤ 200001, and multiplication by 2 stays within int range. Use long for safety but int is okay.
Now, also need to compute X_mod_A and X_mod_B by iterating through bits. That's O(N) each.
Now, what about the case p-1 > 0 but maybe p-1 = 1? That's fine.
Now, check edge case: p=1 and original_bit=0: we compute M = A=2, delta_mod = +powA[exp] etc. But note: For original_bit=0, there is no special case. However, for p=1, we still have B = p-1 = 0, but we only need to handle original_bit=1 specially. So the code for original_bit==1 should first check if p==1 (and bit=='1') then answer=0. That covers it.
Now, what about p=1 and there are multiple '1's? That's impossible because p=1 means exactly one '1'. So only one position is bit=1, others are 0. So for that one position, answer=0; for zeros, use A.
Now, p=0: all bits are zero, original_bit always 0. So we compute A=1, answer=1. Good.
Now, we should also consider N=1, X="0": p=0, i=0: M=1, answer=1. Should be? Flipping yields 1, popcount=1, one operation => 1. Yes.
Now, N=1, X="1": p=1, i=0: bit=1, answer=0 (since flips to 0). Good.
Now, let's think about the initial process: The definition of f(n) includes the final step when n becomes 0. For n>0, f(n)=1+f(n % popcount(n)). So for n positive, f(n)≥1. For n=0, f(0)=0. So our computed answer = 1 + f(r) is correct for n>0. But is there any chance that X_i = 0? In that case we would be using formula incorrectly because we'd be dividing by popcount(0)=0. So we need to handle X_i = 0 separately (answer=0). That is exactly the case p==1 and flipping the single '1' to 0, which yields X_i=0. Are there any other cases? If X has leading zeros, but the integer X might be zero? For example, X="000", p=0. Flipping any bit yields a number with a single 1, which is positive. So only case X_i=0 is when X=0? Actually X=0 means all bits zero. Flipping any bit yields a positive number. So X_i=0 only if X has exactly one '1' and we flip that '1' to 0. So indeed only that case.
Now, what about the case where after first step r = 0, we get answer = 1 + f(0) = 1, which matches the process: one operation to get to 0. Good.
Now, precomputing f up to max_n: we need to compute popcount for numbers up to max_n. But careful: The recurrence f(n) = 1 + f( n % popcount(n) ) uses n % popcount(n). For n up to 200k, popcount(n) is at most 18. So we can compute f directly using loop and memoization. But we need to ensure we compute f for all numbers up to max_n in increasing order? Since n % popcount(n) is always less than n (except when popcount(n)=1? Then n % 1 = 0 < n, so okay). So the recurrence uses a smaller number (unless n=0). So we can compute f in a simple loop from 1 to max_n, using DP because the next value is always smaller (or zero). So we can compute f sequentially: for n from 1 to max_n: compute next; f[n] = 1 + f[next]; Since next < n, f[next] already computed if we go in increasing order? Actually next could be larger than n? Let's check: n % popcount(n) yields a number less than popcount(n) ≤ number of bits of n. Could that be greater than n? No, because remainder < divisor, and divisor ≤ number of bits, which is at most log2(n)+1, which for n>2 is less than n. For n=1, remainder=0 < 1. For n=2, remainder=0 < 2. So indeed next < n for all n>0. So we can compute f in increasing order and f[next] is already computed because next < n. So we can just loop n=1..max_n, compute next, set f[n]=1+f[next]. That's fine.
But we need to compute popcount(n) quickly. Using __builtin_popcount is fine. Alternatively, we can precompute popcounts for all numbers up to max_n using a simple DP: pc[0]=0; for i=1..max_n: pc[i] = pc[i>>1] + (i&1). That's O(max_n) and likely faster than calling __builtin_popcount each time? __builtin_popcount is also fast but loop overhead may be similar. We'll just use __builtin_popcount for simplicity, as max_n=200k is small.
Now, what about the case when N is 200k and p is close to 200k, then A = p+1 ~ 200001, and B = p-1 ~ 199999. These are mod values within 200k, so pow2_mod arrays size N each. That's fine.
But we need to compute pow2_mod for modulus up to 200001. The values 2^e mod M for e up to 200k can be computed iteratively with multiplication mod M. That's O(N) per modulus.
Now, we also need to compute X_mod for A and B. That's O(N) each.
Now, memory: We'll hold strings length N; arrays of size N for pow2_mod_A and possibly pow2_mod_B. That's about 200k each, total ~400k ints, fine.
Now, we must careful about the indexing for pow2_mod arrays: we need to use exponent = N-1-i. Let's compute pow2 array for exponents from 0 to N-1. For each modulus, we can compute an array pow2_exp of length N. We'll compute pow2_exp[0] = 1 % M; for e=1..N-1: pow2_exp[e] = (pow2_exp[e-1]*2)%M. Then for bit i, we need pow2_exp[N-1-i].
Alternatively, compute an array pow2_bit indexed by i: pow2_bit[i] = 2^{N-1-i} % M. Could compute by starting from rightmost: cur = 1 % M; for i from N-1 downto 0: pow2_bit[i] = cur; cur = (cur*2)%M; This yields exactly the needed values. That might be easier.
We'll adopt the latter: For a given modulus M, compute vector pow2(N); Set cur = 1 % M; For i = N-1 downto 0: pow2[i] = cur; cur = (cur * 2) % M; This gives pow2[i] = 2^{N-1-i} % M.
Now, when we need delta_mod for bit i: if flipping 0->1, delta_mod = +pow2[i]; if flipping 1->0, delta_mod = -pow2[i] mod M.
Now, we also need to compute X_mod_M. That can be computed by scanning left to right: val = 0; for i=0..N-1: val = (val*2 + (X_str[i]-'0')) % M. That's fine.
Now, for modulus B, we need to compute only if p>1. However, note that if p>1, p-1 could be 1 as well. That's fine.
Now, handle special case: p==1 and bit=='1' => answer 0.
Now, also handle case p==0? That's fine; our A=1, all bits are '0', we will compute as described.
Now, for p==1, we compute A = 2, but we also need to compute remA and powA. For the bit that is '1', we output 0 without using those. So we need to compute remA and powA for all bits, because for zero bits we need them. That's fine.
Now, we also might consider scenario where p==N (all ones) and N=200k. Then p>1, so B = N-1. We need to compute remB and powB, which are O(N). That's okay.
Now, confirm that we don't need to handle p_i=0 elsewhere.
Now, let's think about correctness deeper: The process f(n) counts number of operations until n=0. For n>0, we used f(n)=1+f(n % popcount(n)). This is correct because the first operation yields n1 = n % popcount(n), and then we continue. So for X_i, we compute M = popcount(X_i), r = X_i % M, answer = 1 + f(r). Since r < M ≤ N+1, and we have f(r) precomputed, all good.
But wait: Is it always true that f(r) as defined for r (which is < M) equals the number of operations needed starting from r? Yes.
Now, double-check for large X_i: The first operation uses popcount(X_i). The number r may be up to N. But note that for r, popcount(r) might be up to, say, log2(r)+1. That's ≤ 18. So subsequent steps are quick. The precomputed f works.
Now, does any X_i produce r that is equal to a number we didn't precompute because we only went up to N? But r < M ≤ N+1, so r ≤ N (since r < M and M ≤ N+1, if M = N+1, r can be at most N). So we need f up to N. So allocate size N+2.
But wait: What about the case N=200000, p=199999 (some), flipping a 0 yields M=p+1=200000, r < 200000 so max r is 199999. That's ≤ N-1? Actually N=200000, so p+1=200001? No, if p=N? Then N=200000, p=200000 (all ones). But flipping a 0? There are no zeros. So if p is less than N, p+1 could be up to N (if p = N-1). For example, N=200000, p=199999, then flipping a 0 yields M=200000, r < 200000 so r ≤ 199999. So max r can be N-1? But consider p = N-1, flipping a 1? That yields M = p-1 = N-2, r ≤ N-3. So the maximum r may be N-1. However what about p = N? Then flipping a 0 is impossible; flipping a 1 gives M = N-1, r ≤ N-2. So max r is N-1? Actually the scenario where M = N+1 could happen if p = N? No, p cannot exceed N. So M = p+1 ≤ N+1 only if p = N, but then flipping a 0 yields M = N+1? But if p=N, there are no zeros. So M = N+1 occurs only if p = N and we flip a 0? That doesn't happen. So the maximum M is at most N (when p = N-1 and flipping a 0 yields M = N). Or if p = N-1 and flipping a 0? That gives M = N. So r < N, so r ≤ N-1. So max r is N-1. But what about N=1 edge? For N=1, p=0: M=1, r=0, okay. For N=1, p=1: flipping the 1 yields 0, answer=0; flipping 0? p=0? Not applicable. So max r at most N-1. However if p = N? Actually N=2, p=2, flipping a 1: M=1, r=0. So still N-1=1. But maybe there is a scenario where M = N+1? Let's test N=2, p=2? That's all ones. Flipping a 0 can't. p=1? flipping a 0 yields M=2 = N. So M=N. So indeed M never exceeds N except possibly if p = N? p+1 = N+1, but no zero bit to flip. So maximum M we actually compute is: For bits that are '0', M = p+1 ≤ (N-1)+1 = N if p ≤ N-1. But if p = N, there are no zeros, so we never compute M = p+1. For bits that are '1', M = p-1 ≤ N-1. So the maximum M we ever need is N. And r < M, so r ≤ N-1. So we only need f up to N-1? But N may be 200000, so N-1 = 199999, but just to be safe, allocate up to N+1 is fine.
But careful: In the special case p = 0, M = 1, r = 0, so fine.
Thus we can allocate f size N+5.
Now, one more nuance: When we compute X_mod_M for modulus M, we must ensure that the method works for M possibly equal to 1. In that case, all operations result in 0, which is fine.
Now, let's test with a random small example to ensure the code works.
I'll simulate a few manually.
Example: N=4, X="1010" -> p=2 (bits 1 and 3? Actually bits: 1,0,1,0 -> two ones). A=3, B=1.
Compute f up to 4: f[0]=0; f1=1; f2=1; f3: popcount=2, 3%2=1 => 1+f1=2; f4: popcount=1, 4%1=0 => 1.
Precompute:
remA = X mod 3 = 1010 binary = 10 decimal? Actually 1010_2 = 10, mod3 = 1. (Check: 10%3=1). powA: compute 2^{N-1-i} mod 3 for each i:
i=0 (MSB): weight 2^3=8 mod3=2
i=1: weight 2^2=4 mod3=1
i=2: weight 2^1=2 mod3=2? Actually 2 mod3=2
i=3: weight 2^0=1 mod3=1
We'll compute as per method: cur=1%3=1 for LSB i=3: pow[3]=1; cur=2%3=2; i=2: pow[2]=2; cur=4%3=1; i=1: pow[1]=1; cur=2%3=2; i=0: pow[0]=2.
remB: X mod 1 = 0; powB: all 0.
Now compute answers:
i=0 bit=1: p>1? p=2>1, so M=B=1. For bit=1, we use delta_mod = -powB[0] mod 1 = 0. rem_M=0, r=0, answer=1+f0=1. But is that correct? Flipping MSB from 1 to 0 gives X_i = "0010" = 2, popcount=1, one operation => f=1. Yes.
i=1 bit=0: M=A=3, delta_mod = +powA[1]=+1, rem_M=1, r=(1+1)%3=2, answer=1+f2=1+1=2. Check: Flipping second bit from 0 to 1 gives "1110"? Actually original 1010, flip bit2 (0->1) yields "1110"? Wait bit1 (MSB) is 1, bit2 is 0 becomes 1 => "1110"? Actually bits: positions: 1:1, 2:0, 3:1, 4:0. Flipping position 2 yields: 1,1,1,0 => binary 1110 = 14. popcount=3. 14%3=2, then popcount(2)=1, 2%1=0 => two ops, f=2. Yes.
i=2 bit=1: M=B=1, answer=1.
Check: Flipping bit3 from 1 to 0 gives "1000" = 8, popcount=1 => 1 op. Yes.
i=3 bit=0: M=A=3, delta_mod = +powA[3]=+1, rem_M=1, r=(1+1)%3=2, answer=2. Flipping last bit: "1011" = 11, popcount=3? 1011 has three 1's? Actually 1011 has three ones? 1+0+1+1 = 3, yes. 11%3=2, then same => 2 ops. Good.
So algorithm works.
Now, we need to handle the case where p = 1 and flipping the '1' leads to answer=0. But also consider p = 1 and flipping a '0'? That's fine.
Now, what about p=1 and N=1? Then only bit is '1', answer=0. That's fine.
Now, note: For original_bit==1, we used M = B = p-1 (if p>1). But we also need to compute delta_mod = -powB[exp] mod M. This is equivalent to (M - powB[exp]) % M. However, if powB[exp] == 0, then delta_mod = 0. That's fine.
Now, ensure that we compute X_mod_B correctly: we must compute X % B for B = p-1. However, when p-1=1, B=1, X_mod_1 = 0. That's fine.
Now, for p>1, B>0.
Now, complexity: O(N) + O(max_n). N up to 200k, max_n up to 200k, so total ~400k operations, which is trivial.
One more nuance: The input X may have leading zeros. Our string length is exactly N. So we treat all bits as given.
Now, implementation steps:
- Read N, string X.
- Compute p = count of '1' in X.
- Determine max_f = N + 2 (maybe just N+5) but to be safe, we can set MAX = 200000 + 5.
But N might be up to 200k, so define MAX = N+5.
Precompute f[0..MAX] as int.
For i from 1 to MAX: pop = __builtin_popcount(i); next = i % pop; f[i] = 1 + f[next].
(Note: if i is 0? skip.)
- Precompute for modulus A = p+1:
- Compute remA = 0; for char c in X: remA = (remA*2 + (c-'0')) % A.
- Compute powA array of length N:
cur = 1 % A; // if A=1, cur=0.
for i from N-1 down to 0: powA[i] = cur; cur = (cur * 2) % A;
- If p > 1, precompute for modulus B = p-1:
- remB similarly.
- powB similarly.
(If p == 1, we skip B; also if p == 0? p-1 = -1, not applicable; so only when p>1.)
- For each i from 0 to N-1:
if X[i] == '0':
M = A
delta = powA[i] // because powA[i] is 2^{N-1-i} mod M
r = (remA + delta) % M
ans = 1 + f[r]
print ans
else: // X[i] == '1'
if p == 1:
// only one '1', flipping it yields 0
print 0
else:
M = B
// delta = - powB[i] mod M
delta = (M - powB[i]) % M // ensure positive
r = (remB + delta) % M
ans = 1 + f[r]
print ans
Now, test with examples.
Example1: N=3, X=011, p=2.
max_f = N+5=8.
f: 0,1,1,2,... good.
A=3, remA computed: 02+0=0, 02+1=1, 12+1=3%3=0 -> remA=0.
powA: N=3. cur=1%3=1 at i=2: powA[2]=1; cur=2; i=1: powA[1]=2; cur=4%3=1; i=0: powA[0]=1? Wait earlier we had powA[0]=2? Let's recompute: For i from N-1 downto 0: cur=1; i=2: pow[2]=1; cur=2; i=1: pow[1]=2; cur=4%3=1; i=0: pow[0]=1; But earlier we had pow[0]=2. Which is correct? 2^{N-1-0} = 2^{2}=4 mod3 = 1. Actually 4 mod3 = 1, not 2. I earlier incorrectly said 2. Let's recalc: N=3, so weights: bit0 (MSB) weight 2^{2}=4, mod3 = 1. So powA[0] should be 1. My earlier manual calc gave 2 because I mistakenly used 2^3? Actually 2^3=8 mod3=2, but weight for MSB in 3-bit number is 2^{2}=4. So correct: powA[0]=1. Let's verify with our algorithm: starting cur=1 (2^0 mod3=1). For i=2 (LSB), weight 2^{0}=1 mod3=1 -> pow[2]=1; cur= (12)%3=2; i=1, weight 2^{1}=2 mod3=2 -> pow[1]=2; cur=(2*2)%3=4%3=1; i=0, weight 2^{2}=4 mod3=1 -> pow[0]=1. Yes.
Now, for i=0 (bit '0'): delta = +powA[0]=1, remA=0, r=1%3=1, ans=1+f1=2. Good.
i=1 (bit '1'): p>1 so M=B=1, we need powB[1]? B=1, powB all 0, delta = (1-0)%1=0, remB=0, r=0, ans=1+f0=1. Good.
i=2 (bit '1'): similar ans=1.
Works.
Example2: Not manually but trust.
Now, test p=1 case: N=2, X="01". p=1.
A=2, compute remA: 02+0? Actually X="01": i0=0 -> val=0; i1=1 -> val = (02+1)%2 =1. So remA=1.
powA: N=2, cur=1%2=1; i=1 (LSB): powA[1]=1; cur=2%2=0; i=0: powA[0]=0? Wait cur=0 before i=0? Let's simulate: cur=1; i=1 -> pow[1]=1; cur=(1*2)%2=0; i=0 -> pow[0]=0; So powA[0]=0, powA[1]=1. Now:
i=0 bit=0: M=A=2, delta=+powA[0]=0, remA=1, r=(1+0)%2=1, ans=1+f1=2. Check earlier we thought f=2. Good.
i=1 bit=1: p==1 and bit==1 => print 0. Good.
Now, test p=0: N=3, X="000". p=0.
A=1, remA=0. powA: cur=1%1=0; for i=2: pow[2]=0; cur=0; i=1: pow[1]=0; cur=0; i=0: pow[0]=0. All zeros.
Now for each i bit=0: M=1, delta=0, r=0, ans=1+f0=1. Output three lines of 1. Good.
Now, test p=N (all ones) with N=3, X="111". p=3.
A=4, but we will not use A because bits are all 1, only M=B=2.
B=2, compute remB: X mod 2: iterate: start 0, 1: (02+1)%2=1; next 1: (12+1)%2=3%2=1; next 1: (1*2+1)%2=3%2=1. So remB=1.
powB: N=3, cur=1%2=1; i=2: pow[2]=1; cur=2%2=0; i=1: pow[1]=0; cur=0; i=0: pow[0]=0. So powB = [0,0,1]? Actually i=2 (LSB) pow=1, i=1 pow=0, i=0 pow=0.
Now compute answers:
i=0 bit=1: p>1, M=2, delta = (M - powB[0])%2 = (2-0)%2=0, rem=1, r= (1+0)%2=1, ans=1+f1=2.
Check: Flipping MSB: 111 -> 011 = 3. popcount(3)=2, 3%2=1, then popcount(1)=1, 1%1=0 => 2 ops. Yes.
i=1 bit=1: delta = (2 - powB[1])%2 = (2-0)=0, r=(1+0)%2=1, ans=2.
Flipping middle: 111 -> 101 =5, popcount=2, 5%2=1, then 2 ops. Yes.
i=2 bit=1: delta = (2 - powB[2])%2 = (2-1)=1%2=1, r=(1+1)%2=0, ans=1+f0=1.
Flipping LSB: 111 -> 110 =6, popcount=2, 6%2=0 => 1 op. Yes.
Works.
Now, test a scenario where p-1=1 and bit=1: N=4, X has p=2? Then B=1, earlier we did that, works.
Now, note that for M=1, delta calculation using (M - powB[i]) % M: If M=1, then (1 - 0) % 1 = 0. Since powB[i] = 0, delta=0, remB=0, r=0, ans=1. That works. But our code for original_bit==1 and p>1, we set M = B = p-1. If p=2, B=1, M=1, we compute delta as (M - powB[i]) % M. That is fine. But we also need to compute remB and powB for B=1. So that works.
Thus overall algorithm is sound.
Now, we need to implement efficiently in C++.
Potential pitfalls:
- For large N, the string length is N, indexing from 0.
- When computing pow arrays, we need to use modulo M. For M=1, all operations result in 0, careful with multiplication: cur = (cur * 2) % M, but M=1, any number %1 = 0, so cur becomes 0 after first multiplication? Actually start cur = 1 % 1 = 0; then pow[last] = 0; cur = (0*2)%1 = 0; So all zeros. That's correct.
- For rem calculation, similar: val = (val*2 + digit) % M, if M=1, everything yields 0. So rem=0. Good.
- Need to use integer types: int is fine up to 200k, but multiplication may exceed 32-bit? e.g., val2, val <= M-1 <= 200k, so val2 <= 400k, fits in int. But for safety, use long long? Not needed.
Now, we should also consider the case when p = N and we flip a 1. For B = N-1, M can be up to 199999. Computation is fine.
Now, think about memory: We'll allocate vectors for powA and maybe powB of size N. But we can avoid storing both if we compute on the fly? Since we need pow values per i, we need to access quickly. So storing is fine.
Now, we might also combine steps: For each i we need to compute delta based on original bit. We can precompute both powA and powB.
Now, edge: If p=1, we compute powA and remA but we won't use them for bit=1 (we output 0). That's okay.
If p=0, we compute powA (M=1) and use for all bits. Good.
If p>1, we compute both A and B.
Now, we also need to handle the case when p=0? Then p-1 = -1, not valid. So we skip B.
Now, check for p=1 and we still compute B? p-1 =0, not >0, so skip B. Good.
Now, is there any case where p = N? Then p>1 unless N=1. So we compute B, but A we may not use for bits (since all bits are 1). However we still compute A? We might not need A at all if there are no zero bits. But we don't know ahead of time, but we can compute A only if there is at least one zero? That would require scanning bits to know. However it's simpler to always compute both A and B (except when not needed, like B when p<=1). But we can compute A always (since p+1 always >=1). So we compute remA and powA always. For B, compute only if p>1. That's fine.
But what about the case where p=N (all ones) and we compute A unnecessarily? That's O(N) time and memory. It's okay; N=200k, we can afford two O(N) passes. But we might also save memory by not storing powA if we don't need it? But we might need it only if there is at least one '0' bit. However, we can just compute it anyway; memory is not a big issue (2*200k ints ~ 1.6 MB). But for performance, it's fine.
Now, let's consider the possibility of overflow in f array indexing: next = n % popcount(n). popcount(n) <= 18 for n up to 200k, so next < 18, definitely within max_n. So safe.
Now, one more nuance: The definition of f(n) includes the operation count. For n>0, f(n)=1+f(n%popcount(n)). But suppose n%popcount(n) = 0, then f(n)=1. That's fine.
Now, we should also verify that our precomputed f works for numbers up to N. Since we only need f(r) for r up to N-1, but we compute up to N+5. Good.
Now, one more check: In our algorithm, we compute r = (rem_M + delta_mod) % M, where rem_M = X % M. However, we must ensure that this computation yields the correct remainder for X_i = X + delta. Because we used the original X mod M. But note: X_i = X + delta. However, we need to compute X_i mod M. Since we have X mod M, we add delta mod M, and take mod M. This is correct.
Now, confirm that delta_mod is correctly computed as +2^{N-1-i} (if flipping 0->1) or -2^{N-1-i} (if flipping 1->0). Because flipping a 0 to 1 adds that weight; flipping a 1 to 0 subtracts that weight. Good.
Now, verify that for bit '1', delta_mod = (M - powB[i]) % M indeed gives -powB[i] mod M. If powB[i] = 0, then delta_mod = 0. Good.
Now, we must also consider that for bit '1', if we use M = B, and B might be 1, then powB[i]=0, delta_mod= (1-0)%1=0, fine.
Now, we must handle the scenario where M = 0? Only for p=1 and bit=1, we output 0 directly, so not used.
Now, we need to be careful about integer division by zero? In precomputation of f, we only compute for n>0 and popcount(n) > 0. So safe.
Now, implementation details in C++:
-
Read N (int).
-
Read string X (maybe with spaces? Use cin>>).
-
p = count(X.begin(), X.end(), '1').
-
const int MAX = 200005; but to be dynamic, we can set max_n = N + 5; but N can be 200k, plus small. We'll allocate vector f(max_n+1). Actually we need up to N (maybe N). Let's set max_f = N+5; as int.
-
Precompute f:
vector f(max_f);
f[0] = 0;
for (int i = 1; i < max_f; ++i) {
int pc = __builtin_popcount(i); // returns number of 1 bits
int next = i % pc;
f[i] = 1 + f[next];
}
But careful: __builtin_popcount expects unsigned int; i is int, okay.
Alternatively, we could compute pc via builtin.
Now, define A = p + 1; B = p - 1 (if p > 1).
Compute remA and powA:
vector powA(N);
int remA = 0;
for (char c : X) {
remA = (remA * 2 + (c - '0')) % A;
}
int cur = 1 % A;
for (int i = N-1; i >= 0; --i) {
powA[i] = cur;
cur = (cur * 2) % A;
}
Similarly for B if needed:
if (p > 1) {
vector powB(N);
remB = 0; for (char c: X) remB = (remB*2 + (c-'0')) % B;
cur = 1 % B;
for (int i = N-1; i >= 0; --i) {
powB[i] = cur;
cur = (cur * 2) % B;
}
}
Now, iterate i:
for (int i = 0; i < N; ++i) {
if (X[i] == '0') {
int M = A;
int delta = powA[i]; // positive
int r = (remA + delta) % M;
cout << 1 + f[r] << '\n';
} else { // '1'
if (p == 1) {
cout << 0 << '\n';
} else {
int M = B;
int delta = (M - powB[i]) % M;
int r = (remB + delta) % M;
cout << 1 + f[r] << '\n';
}
}
}
We should be cautious: In the case M=1, powB[i] = 0, delta = (1-0)%1 = 0, remB = 0, r = 0. So ans = 1+f[0]=1. That's correct.
Now, test with N=1, X="1": p=1, A=2, compute remA: (0*2+1)%2=1. powA: N=1, cur=1%2=1; i=0: powA[0]=1. Loop: i=0, X[0]=='1', p==1 => print 0. That's correct. Also check that we don't need powA for this case.
Now, test N=1, X="0": p=0, A=1, remA=0, powA: cur=1%1=0, powA[0]=0. Loop: i=0, X[0]=='0', M=1, delta=0, r=0, ans=1+f[0]=1. Good.
Now, we need to ensure that f array is large enough to index r. Since r < M and M ≤ N maybe, but max r could be N-1? In N=1, r=0, fine. For N=2, p=1? Not zero. If p=1, flipping a 0 yields M=2, r could be 1. f[1] exists if max_f >=2. We set max_f = N+5 = 7, so fine.
Now, confirm that for p>1 and M might be N-1, r could be up to N-2. So max_f = N+5 covers.
Now, one more nuance: In precomputing f up to max_f, we used __builtin_popcount(i). But for i up to 200k, that's fine. However, we must be aware that __builtin_popcount counts bits in the binary representation of the integer, which for i up to 200k has up to 18 bits. So correct.
Now, we must also consider that we might need f for numbers up to maybe N (200k). So we compute f[0..N+5].
Now, time complexity: O(N + max_f). Good.
Now, let's test with a bigger random test to be confident.
But we must also consider that for M = A or B, we computed pow arrays. For large N (200k) and M maybe up to 200k, the pow computation involves multiplication and modulo, which is fine.
Now, we should also consider that for M = 1, pow array values are all 0. In the loop, delta = powA[i] = 0, r = (remA + 0)%1 = 0, ans=1. Good.
Now, think about the possibility of integer overflow in cur = (cur * 2) % M: cur < M ≤ 200k, so cur*2 ≤ 400k < 2^31, safe.
Now, maybe we can further optimize: For f array, we can compute only up to max_r needed. But that's minor.
Now, verify the process with some custom numbers.
Take N=5, X = "11010" (p=3). A=4, B=2.
Compute f up to maybe 6.
f[0]=0, f[1]=1, f[2]=1, f[3]=2, f[4]=1, f[5]=? popcount(5)=2, 5%2=1, f[5]=1+f1=2, f[6]=popcount(6)=2, 6%2=0 =>1.
Now compute:
remA = X mod4: binary 11010 = 26? Actually 11010_2 = 16+8+0+2+0 = 26. 26%4=2.
powA: N=5, compute cur=1%4=1; i=4 (LSB): pow[4]=1; cur=2; i=3: pow[3]=2; cur=4%4=0; i=2: pow[2]=0; cur=0; i=1: pow[1]=0; cur=0; i=0: pow[0]=0. So powA = [0,0,0,2,1].
remB = X mod2: 26%2=0.
powB: M=2, cur=1%2=1; i=4: pow[4]=1; cur=0; i=3: pow[3]=0; cur=0; i=2: pow[2]=0; cur=0; i=1: pow[1]=0; cur=0; i=0: pow[0]=0. So powB = [0,0,0,0,1]? Actually i=3? Let's recalc: i indices: 4:1; 3:0; 2:0; 1:0; 0:0. So powB = [0,0,0,0,1]? But size 5, index 4=1, index 3=0, index 2=0, index 1=0, index 0=0. However careful: LSB is i=4? Actually if we index string X[0]..X[4] left to right, then i=4 is last character. So powB[4]=1, good.
Now evaluate each i:
i=0: X[0]='1' -> p>1? p=3>1 => M=B=2. delta = (2 - powB[0])%2 = (2-0)=0; r = (remB+0)%2 = 0; ans=1+f0=1. Check: Flip MSB: 11010 -> 01010 = 10, popcount=2, 10%2=0 => 1 op. Good.
i=1: X[1]='1' -> M=2, delta = (2 - powB[1])%2 = 0; r=0; ans=1. Flip: 11010 -> 10010 = 18, popcount=2, 18%2=0 =>1 op. Good.
i=2: X[2]='0' -> M=A=4, delta = powA[2]=0; r = (remA +0)%4 = 2; ans=1+f2=1+1=2. Flip: 11010 -> 11110 = 30, popcount=4, 30%4=2, then 2%popcount(2)=2%1=0 =>2 ops. Good.
i=3: X[3]='1' -> M=2, delta = (2 - powB[3])%2 = (2-0)=0; r=0; ans=1. Flip: 11010 -> 11000 = 24, popcount=2, 24%2=0 =>1 op. Good.
i=4: X[4]='0' -> M=4, delta = powA[4]=1; r = (2+1)%4=3; ans=1+f3=1+2=3. Flip: 11010 -> 11011 = 27, popcount=4, 27%4=3, then 3%popcount(3)=3%2=1, then 1%1=0 =>3 ops. Good.
Works.
Now, all seems good.
Potential issues: For large N and p close to N, powA and powB computations will involve many mod operations. That's fine.
Now, consider the possibility that M (p+1 or p-1) could be 0? Already handled.
Now, consider the case p=N? Then A = N+1, but we may not use A because all bits are 1. But we still compute remA and powA for A=N+1. N+1 can be up to 200001. That's fine. But note: In computing powA for modulus A=N+1, the values 2^{e} mod A can be computed without issue.
Now, check for a potential bug: When computing remA for A=N+1, and N large, the value of remA might be as large as A-1. That's fine.
Now, one more nuance: In our algorithm, we compute f(r) where r < M, and we precomputed f up to max_f = N+5. But if M = A = p+1 and p = N, A = N+1. However we only use A if there is a zero bit; if p=N, there are no zero bits, so we never use A. So r never equals something >= N+1? Actually for p=N, we only compute for bits=1, using B=N-1, so r <= N-2. So safe.
But if p = N-1, then there is at least one zero? Actually p=N-1 means exactly one zero. So we will use A for that zero bit. A = p+1 = N. So M = N. r < N, so r ≤ N-1. So within max_f if max_f = N+5 includes N-1. So safe.
Now, what about p=0? M=1, r=0.
Now, what about p=1? For zero bits, M=2, r ≤1.
So all good.
Now, we must also ensure that f array is computed for indices up to maybe N (including N). For p=N-1 and zero bit, M=N, r can be N-1. So we need f[N-1]. That's within N+5. So we should compute f up to N (maybe N+1) to be safe. So we can set max_f = N + 2. Since N up to 200k, plus small buffer.
Now, we might also need f for N itself if r could be N? As argued, r < M and M <= N when using A? M = p+1 ≤ N if p ≤ N-1. Actually if p = N-1, M = N, r < N, so max r = N-1. If p = N-2, M = N-1, r ≤ N-2. So r never equals N. So f(N) not needed. But we can still compute it for safety.
Now, potential integer overflow in computing cur*2? Already safe.
Now, overall algorithm is O(N).
Now, we should also consider the case when p=0 and we compute A=1. In remA calculation: we do remA = (remA*2 + bit) % 1, which always 0. That's fine.
Now, final step: Output each answer on separate line.
Now, confirm with second example quickly maybe with a small script but trust.
Now, think about any alternative approach: Could also use prefix sums for mod values, but our method is simple.
Now, double-check the definition of f: "the number of times the following operation will be done when we repeat it until n becomes 0". This includes the operation that makes n become 0. So for n=1, operation: 1%1=0, so one operation, f(1)=1. Our recurrence gives f(1)=1+ f(0)=1, correct.
Now, one more nuance: In precomputing f, we rely on popcount(n) for n up to 200k. popcount(0)=0 but we handle n=0 separately. For n>0, popcount(n)>=1. So safe.
Now, edge: For n=0, f(0)=0 by definition. But we never call f(0) from recurrence? Actually we call f(0) as base case. So set f[0]=0.
Now, implement.
Now, code skeleton:
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
string X;
cin >> N >> X;
int p = count(X.begin(), X.end(), '1');
int max_f = N + 5;
vector f(max_f, 0);
for (int i = 1; i < max_f; ++i) {
int pc = __builtin_popcount(i);
int nxt = i % pc;
f[i] = 1 + f[nxt];
}
int A = p + 1;
vector<int> powA(N);
int remA = 0;
for (char c : X) {
remA = (remA * 2 + (c - '0')) % A;
}
int cur = 1 % A;
for (int i = N-1; i >= 0; --i) {
powA[i] = cur;
cur = (cur * 2) % A;
}
int B = 0;
vector<int> powB;
int remB = 0;
if (p > 1) {
B = p - 1;
powB.resize(N);
for (char c : X) {
remB = (remB * 2 + (c - '0')) % B;
}
cur = 1 % B;
for (int i = N-1; i >= 0; --i) {
powB[i] = cur;
cur = (cur * 2) % B;
}
}
for (int i = 0; i < N; ++i) {
if (X[i] == '0') {
int M = A;
int delta = powA[i];
int r = (remA + delta) % M;
cout << 1 + f[r] << '\n';
} else {
if (p == 1) {
cout << 0 << '\n';
} else {
int M = B;
int delta = (M - powB[i]) % M;
int r = (remB + delta) % M;
cout << 1 + f[r] << '\n';
}
}
}
return 0;
}
Now, we need to ensure that when M = 1, the delta calculation using (M - powB[i]) % M works because % 1 yields 0 regardless, but we compute (1 - 0) % 1 = 1 % 1 = 0. That's fine. So we can leave as is.
But one potential bug: In computing delta for bit '1', we did (M - powB[i]) % M. When powB[i] == 0, this gives M % M = 0, correct. When powB[i] != 0, it gives a positive number less than M. Good.
Now, confirm that for M > 0, the expression (M - powB[i]) % M yields a value between 0 and M-1 inclusive. In C++, % with negative numbers? (M - powB[i]) is non-negative because M >= powB[i]? powB[i] is modulo M, so 0 <= powB[i] < M. So M - powB[i] is between 1 and M (if powB[i]=0, then M - 0 = M, which mod M = 0). So (M - powB[i]) % M works as (M - powB[i]) % M = 0 when powB[i]=0, else M - powB[i] because it's less than M? Actually M - powB[i] is in [1, M-1] if powB[i] > 0, so (M - powB[i]) % M = M - powB[i] because it's already less than M. So it's fine. So we could also just set delta = M - powB[i]; if delta == M then delta = 0; but using % is fine.
Now, check: For M=1, powB[i]=0, M - powB[i] = 1, 1%1 = 0, delta = 0, correct.
Thus all good.
Now, note about precomputing f: We used __builtin_popcount(i) for i up to max_f (like 200005). That's fine.
But there is a nuance: When computing f for numbers up to max_f, we assume that for any i, the next value i % pc is within range. Since pc can be up to 18, next is at most 17, definitely within max_f.
Now, but we need to compute f for numbers up to maybe N, but N could be 200000, but we compute up to N+5, which includes 200005. That's fine.
Now, confirm that f array indices used are r values, which are less than M, and M ≤ N for bits where we use A? Actually for bits where X[i]=='0', M = A = p+1. p can be as high as N, but if p=N, there are no zeros. So maximum p for zeros is N-1. So M = p+1 ≤ N. So r ≤ N-1. For bits where X[i]=='1', M = B = p-1, with p ≥ 2, so M ≤ N-1, r ≤ N-2. So f array up to N-1 is sufficient. But we compute up to N+5 anyway.
Now, ensure we don't access out of bounds for f[r]. r is computed as (rem_M+delta) % M, so 0 <= r < M <= N. But if M = N (when p = N-1 and flipping a zero), r < N, so max r = N-1. f[N-1] exists if max_f >= N. So we need f size at least N. Since we set max_f = N+5, safe.
Now, handle the case p=0: A=1, M=1, r=0, f[0] exists.
Now, everything seems fine.
Now, we should also consider if there is any situation where p_i = 1 and we compute r = X_i mod 1 = 0, ans = 1+f[0]=1. That's correct.
Now, test with some random numbers via mental or code.
Now, think about performance: The loop to compute f up to 200k is quick. The loops for rem and pow are O(N) each, about 200k each, total maybe 1 million operations, fine.
Now, memory: We allocate powA and powB each of size N (200k). That's about 1.6 MB for two int vectors (int 4 bytes each) = 1.6 MB, plus f array of 200k ints = 0.8 MB, total < 3 MB, fine.
Now, we can also consider optimizing by not storing powB if not needed? But not necessary.
Now, possible improvements: Since we only need powA[i] and powB[i] during iteration, we could compute them on the fly in reverse order while iterating? For each i from N-1 down to 0, we could compute cur and store in an array. But storing is simplest.
Now, one more nuance: The input X might be up to 200k digits, so we need to use string. It's given as a string without spaces. We'll use cin>>.
Now, we need to ensure we use fast I/O because of large output (200k lines). So we can add ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); to speed up.
Now, final code.
Now, let's test with the second example (provided). Since we don't have actual output but given sample, we can try to simulate quickly or trust.
Now, implement.
But wait: There might be a corner case where p = 0? Already considered.
Now, also consider the case where p = 1 and there is exactly one '1'. In that case, for the '1' bit, answer = 0. For all '0' bits, we compute as above. That's correct.
Now, confirm that for p=1, we compute A = 2, powA and remA. For '0' bits, we compute r = (remA + powA[i]) % 2. That should give correct f. Let's test with N=3, X="010" (p=1). Compute: X=2? Actually "010" binary = 2. remA = 2%2=0? Let's compute: bits: 0,1,0: remA: start 0: (02+0)=0; then (02+1)=1%2=1; then (1*2+0)=2%2=0 => remA=0. powA: N=3, cur=1%2=1; i=2: pow[2]=1; cur=2%2=0; i=1: pow[1]=0; cur=0; i=0: pow[0]=0. So bits:
i=0 bit=0: delta=powA[0]=0, r=0, ans=1.
i=1 bit=1: p==1 => ans=0.
i=2 bit=0: delta=powA[2]=1, r=(0+1)%2=1, ans=1+f1=2.
Check manually: X_i for i=0: flip first 0 to 1 -> "110" = 6, popcount=2, 6%2=0 => 1 op => ans=1. Good.
i=1: flip the only 1 to 0 -> "000"=0 => ans=0. Good.
i=2: flip last 0 to 1 -> "011"=3, popcount=2, 3%2=1, then popcount(1)=1 => 1%1=0 => 2 ops => ans=2. Good.
So algorithm works.
Now, let's think about possible overflow in cur multiplication: cur * 2 might exceed int range? cur < M <= 200001, so cur*2 <= 400002, which fits in signed 32-bit. So safe.
Now, double-check the pow calculation: For modulus M, cur = 1 % M. For M=1, cur = 0. Then pow[last] = 0. Then cur = (0*2)%1 = 0, all zeros. That's fine.
Now, consider M maybe large, but still within int.
Now, ensure that we don't have any off-by-one errors in exponent. Let's confirm with small N.
N=3, X="011". Already did.
Now, verify that powA[i] indeed is 2^{N-1-i} % A. With our method: cur initialized to 1 % A (which is 2^0 mod A). Starting from i = N-1 (LSB), we set pow[i] = cur, then cur = cur*2 % A. So after setting pow for i = N-1, cur becomes 2^1 mod A. Then for i = N-2, pow[i] = cur = 2^1 mod A. Then cur becomes 2^2 mod A. So indeed pow[i] for i from LSB to MSB gives 2^{0}, 2^{1}, 2^{2} etc? Wait: Let's map: For i = N-1 (least significant), we set pow[N-1] = cur = 2^0 mod A. That corresponds to exponent 0, which is correct for LSB weight 2^0. But our delta for bit i needs to be 2^{N-1-i}. For i = N-1 (LSB), N-1-i = 0, so we need 2^0. So pow[N-1] should be 2^0 mod A. Good.
For i = N-2, N-1-i = 1, need 2^1 mod A. And pow[N-2] after assignment becomes cur which is 2^1 mod A. So correct.
Thus pow[i] = 2^{N-1-i} mod M. So delta = pow[i] (or -pow[i]) accordingly.
Now, for the "bit from the top" indexing: the top is leftmost, i=0 in code. So matches.
Now, one more edge: When p = 0, A=1, powA[i] = 0 for all i. So delta = 0. remA=0. r=0. ans=1. Good.
Now, potential issue: In computing f array, we used __builtin_popcount which counts bits in the binary representation of the integer. For numbers up to 200k, it's fine. However, we should ensure that for n=0, we don't call it. So we start from 1.
Now, let's consider the possibility that popcount(n) could be 0? For n>0, popcount>0. So safe.
Now, check if any n in our f computation could have popcount=0? No.
Now, consider the scenario where in the process for some r, we might need to compute f for numbers slightly above N? But we only compute f for numbers up to N+5, and r is < M ≤ N. So fine.
Now, all good.
Now, I will write the final solution.
But before final, consider if there is any need to use long long for rem and cur? Not needed as values are small.
Now, note: The input X string length N may be up to 200k, but reading as string is fine.
Now, we must ensure we flush output efficiently.
Now, implement.
Potential improvement: For p>1, we compute powB and remB, but we also compute powA and remA. That's okay.
Now, I'll produce final answer.
But also, we can consider if p+1 may be 0? No.
Now, we should also handle the case N=0? Not possible as per constraints (N>=1).
Now, we should test with max constraints using mental but no issue.
Now, final code.
Now, I'll provide the solution explanation in the final answer, but we need to output code only? Actually problem expects code output. But the instruction says "Print N lines. The i-th line should contain the value f(X_i)." So we just produce code.
In the final answer, I'll include the solution reasoning and then the code. But for the platform, only code is needed. However, as this is a text response, I'll provide explanation and code.
Now, final code in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
string X;
cin >> N >> X;
int p = count(X.begin(), X.end(), '1');
int max_f = N + 5;
vector<int> f(max_f, 0);
f[0] = 0;
for (int i = 1; i < max_f; ++i) {
int pc = __builtin_popcount(i);
int nxt = i % pc;
f[i] = 1 + f[nxt];
}
int A = p + 1;
vector<int> powA(N);
int remA = 0;
for (char c : X) {
remA = (remA * 2 + (c - '0')) % A;
}
int cur = 1 % A;
for (int i = N-1; i >= 0; --i) {
powA[i] = cur;
cur = (cur * 2) % A;
}
int B = 0;
vector<int> powB;
int remB = 0;
if (p > 1) {
B = p - 1;
powB.resize(N);
for (char c : X) {
remB = (remB * 2 + (c - '0')) % B;
}
cur = 1 % B;
for (int i = N-1; i >= 0; --i) {
powB[i] = cur;
cur = (cur * 2) % B;
}
}
for (int i = 0; i < N; ++i) {
if (X[i] == '0') {
int M = A;
int delta = powA[i];
int r = (remA + delta) % M;
cout << 1 + f[r] << '\n';
} else {
if (p == 1) {
cout << 0 << '\n';
} else {
int M = B;
int delta = (M - powB[i]) % M;
int r = (remB + delta) % M;
cout << 1 + f[r] << '\n';
}
}
}
return 0;
}
One more check: In the loop where we compute f, we used max_f = N+5. But we need to ensure that f[nxt] is within range. nxt = i % pc. Since i < max_f, and pc >= 1, nxt <= i-1, so nxt < max_f. But careful: when i is close to max_f-1, nxt could be up to max_f-2, within range. So safe.
Now, for i up to N+4, but we might need f for indices up to N maybe, but we compute up to N+4, so fine.
But one edge: if N is 200000, max_f = 200005. When i = 200004, popcount(i) maybe something like 10, nxt = i % pc <= 9, which is far less than max_f. So safe.
Now, confirm that for any i in f loop, pc = __builtin_popcount(i) is at least 1. For i>0, yes.
Now, we should also ensure that f array is sized at least max_f (including index max_f-1). So vector f(max_f) gives indices 0..max_f-1. Good.
Now, we could also compute f only up to N, but for safety we do N+5.
Now, final answer.
But there is a nuance: In the precomputation of powA, we used cur = 1 % A. For A maybe 1, cur=0, then powA[i] = 0 for all i. That's correct.
Now, regarding performance: For p>1, we compute powB even if there are no '1' bits? Actually if p>1, there are at least two '1's, so there are '1' bits. So powB will be used. So no waste.
Now, we might also consider that for p=0, B is not computed, and for p=1, B not computed. In both cases, we handle bit '1' specially.
Now, what about p>1 but there are also zero bits? That's fine.
Now, everything looks correct.
Now, produce final output.
I'll also include some comments in the code for clarity.
Now, before final, let's test with the provided second example quickly using a mental or quick script? I'll try to simulate some to be extra sure, but not necessary.
Now, also think about potential integer overflow in computing f array: f values are at most maybe log steps. For n up to 200k, f(n) maybe at most something like 5? Actually let's compute maximum number of steps: The process reduces quickly. For n up to 200k, the first step reduces to less than popcount(n) <= 18, then subsequent steps reduce to at most 5 maybe. So f(n) is small (maybe < 10). So 1+f[r] is small. So output values fit in int.
Now, final answer: Provide code.
But the problem might be from AtCoder (typical style). They often require fast I/O.
Now, also note that in AtCoder, they sometimes use long long, but not needed.
Now, I'll output final code.
Now, consider the possibility that the popcount of X_i could be 0 only when p=1 and flipping the '1'. We handle that.
Now, answer is ready.
One more check: In the code, for X[i]=='1' and p>1, we compute delta = (M - powB[i]) % M. For M maybe 1, this is safe. But if M=1, powB[i] = 0, delta = (1-0)%1 = 0, as we argued. So ans = 1+f[0]=1, correct. So we could also omit the p==1 special case by including M=0? But we don't compute B when p=1, so it's fine.
Now, final code.
Now, I'll write final answer with explanation and code. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
string X;
cin >> N >> X;
// popcount of the original number
int p = count(X.begin(), X.end(), '1');
// precompute f(n) for n up to N (r will be < popcount ≤ N)
const int MAX = N + 5;
vector<int> f(MAX, 0);
for (int i = 1; i < MAX; ++i) {
int pc = __builtin_popcount(i); // number of ones in i
int nxt = i % pc; // next value after one operation
f[i] = 1 + f[nxt];
}
// modulus A = p+1 (used when flipping a '0')
int A = p + 1;
vector<int> powA(N); // powA[i] = 2^{N-1-i} mod A
int remA = 0; // X mod A
for (char c : X) {
remA = (remA * 2 + (c - '0')) % A;
}
int cur = 1 % A;
for (int i = N-1; i >= 0; --i) {
powA[i] = cur;
cur = (cur * 2) % A;
}
// modulus B = p-1 (used when flipping a '1', only if p>1)
int B = 0;
vector<int> powB;
int remB = 0;
if (p > 1) {
B = p - 1;
powB.resize(N);
for (char c : X) {
remB = (remB * 2 + (c - '0')) % B;
}
cur = 1 % B;
for (int i = N-1; i >= 0; --i) {
powB[i] = cur;
cur = (cur * 2) % B;
}
}
// answer for each flipped bit
for (int i = 0; i < N; ++i) {
if (X[i] == '0') {
int M = A;
int delta = powA[i]; // + 2^{N-1-i}
int r = (remA + delta) % M;
cout << 1 + f[r] << '\n';
} else { // bit == '1'
if (p == 1) { // flipping the only '1' gives zero
cout << 0 << '\n';
} else {
int M = B;
int delta = (M - powB[i]) % M; // - 2^{N-1-i} mod M
int r = (remB + delta) % M;
cout << 1 + f[r] << '\n';
}
}
}
return 0;
}