We need to construct a binary string of length n = a+b with exactly a zeros, b ones, and exactly x positions where adjacent characters differ (i.e., the number of transitions). Equivalent to constructing a string with given counts and a given number of runs? Because each transition corresponds to a change between runs. If the string has runs of consecutive identical bits, the number of transitions = number of runs - 1. Actually if there are k runs (alternating groups), then transitions = k-1. But careful: runs: a sequence of same bits. For example, "01010" has runs: 0,1,0,1,0 => 5 runs, transitions=4. Yes.
So number of runs = x+1. And the runs alternate between 0 and 1. So if we have runs count = r = x+1, then the string starts with either 0 or 1. Then the total number of zeros a and ones b must be distributed among runs that alternate.
We need to output any string satisfying conditions.
Given constraints a,b <=100, x<a+b.
We need to guarantee existence.
Approach: Since answer always exists, we can construct as follows:
We need exactly x transitions. So r = x+1 runs.
If we have r runs, the numbers of zeros and ones are fixed per starting bit.
Let starting bit be 0. Then runs are: Run1: zeros, Run2: ones, Run3: zeros, Run4: ones, ... alternating. So zeros appear in runs 1,3,5,... (odd-indexed runs), ones in even-indexed runs.
Similarly if start with 1.
Let k = r (number of runs). If start with 0, number of zero runs = ceil(k/2), number of one runs = floor(k/2). If start with 1, zero runs = floor(k/2), one runs = ceil(k/2).
We need to assign lengths (positive integers) to each run such that sum zeros = a, sum ones = b.
Thus we need to distribute a zeros across ceil(k/2) runs (each at least 1) and b ones across floor(k/2) runs (or vice versa). Since total runs = x+1, and x can be as low as 1, then r >=2? Actually x >=1, so r>=2. Yes. So at least two runs.
But is it always possible to assign positive integers to runs to match counts? Not always because if runs count is too high, we might need many ones/zeros but each run must be at least 1. The problem guarantees existence, so there is some arrangement. But we need a constructive method.
Alternative method: we can construct by starting with a string of all zeros (a zeros) and all ones (b ones) initially, and then combine with transitions? Or we can think of building the string as: we need exactly x transitions. The maximum possible transitions for given a,b is 2min(a,b) if we alternate as much as possible? Actually if we alternate starting with whichever, the maximum number of transitions is 2min(a,b) maybe? Let's think: For a string of length n with a zeros, b ones, the maximum number of transitions is if we place bits alternating as much as possible, which would be something like: if a = b, maximum transitions = n-1 = 2a-1? But wait, "01010" has a=3? Actually zeros=3? "01010" has zeros? count: positions:1:0,2:1,3:0,4:1,5:0 => zeros=3, ones=2, transitions=4. n=5 => max transitions = 4? Can we get 4 transitions with 3 zeros and 2 ones? Yes. In general, maximum possible transitions = 2min(a,b) if one count is not equal? Actually if we start and end with the majority? Let's compute: For alternating pattern, the number of runs = either 2min(a,b) or 2min(a,b)+1? Because if we have more zeros than ones, we can start and end with zeros, so runs: zero, one, zero, one, ..., zero. That yields number of runs = 2ones + 1? Actually if a > b, we can have at most 2b+1 runs: b+1 zero runs and b one runs? Wait, if zeros > ones, then we can arrange: zeros placed in b+1 runs (since ones separate zeros). So runs = (b+1) + b = 2b+1. Then transitions = 2b. Similarly if ones > zeros, max transitions = 2a. If equal, we can have either 2a or 2a-1? Let's test a=b=2: possible strings: "0101" length 4: zeros=2, ones=2, transitions=3. That's runs=4, transitions=3. But can we have transitions=4? With length 4, max transitions is 3 because transitions = n-1 if all adjacent differ, which is 3. So maximum is n-1 when a and b are both at least 1? Actually if we want each adjacent differ, the string must alternate strictly. That requires that the counts differ by at most 1. Because for a strictly alternating pattern of length n, the numbers of zeros and ones differ by at most 1. Indeed, in an alternating string, the counts are either equal or differ by one. So the maximum number of transitions is at most n-1, but achieving n-1 requires alternating all the way, which imposes |a-b| <= 1. If |a-b| > 1, the maximum transitions is less than n-1, specifically 2min(a,b) + (maybe 1? Actually if a > b, you can have pattern: 0 1 0 1 ... 0 (start and end with 0). That yields transitions = 2b, because between each 1 and 0 changes, there are 2b changes? Let's compute: length = a+b. Suppose a > b. Start with 0, then pattern: 0 1 0 1 0 ... 0 (with b ones). The number of runs: (b+1) zero runs and b one runs = total runs 2b+1, transitions = 2b. So maximum transitions = 2min(a,b) if counts unequal, and = 2min(a,b)-1? Actually if equal, then we could start with 0 and end with 1: 0 1 0 1 ... 1? But let's derive: For a=b, we have two possibilities: start with 0, then pattern 0,1,0,1,... up to length n. Since n=2a, if we start with 0 and end with 1, the runs are a zeros and a ones? Actually sequence: positions: 1:0,2:1,3:0,4:1,... last even: 2a:1? If 2a is even, then position 2a is 1? But then number of zeros = a? Let's list for a=b=2: starting 0: possible strings of length 4 alternating: 0101 (ends with 1) zeros=2 ones=2 runs=4 transitions=3. 1010 (starts with1) also transitions=3. So max transitions = 2a-1? Actually 2min(a,b) = 22 =4, but transitions=3, so not 2min. So formula: max transitions = 2min(a,b) if |a-b| >=1? For a=3,b=2: max transitions? According to earlier pattern 0 1 0 1 0 => transitions =4. 2min=4, yes. For a=2,b=3: max transitions =4, matches 2min. For a=b=2: 2min=4, but we only get 3. So equal case is special: if a=b, then max transitions = 2a-1? Because you cannot have both start and end different because they would be same count? Actually consider a=b=1: n=2, possible strings: "01" or "10" both have 1 transition. 2min=2, but max=1. So indeed for equal counts, max transitions = 2a-1. More generally, maximum transitions = 2min(a,b) + (if a==b? -1) but we can say max = a+b-1 if |a-b| <=1? Actually if |a-b| = 0, then max = n-1? n-1 = 2a-1, which is less than 2a. So indeed it's n-1 = a+b-1 only if |a-b| <=1, but if |a-b| >=1? For a=3,b=1: n=4, a+b-1=3, but can we achieve 3 transitions? With 3 zeros and 1 one: pattern: 0 1 0 0? That has transitions: 0-1 change,1-0 change,0-0 no change => total 2. Actually max? Try 0 0 1 0: changes: 0-0 none,0-1 change,1-0 change =>2. 0 1 0 0 same. 1 0 0 0: changes:1-0,0-0,0-0 =>1. So max=2? Wait, 2min=2, yes. So max = 2min(a,b). For a=3,b=1, min=1, max=2? But 21=2, yes. But a+b-1=3, but can't reach 3 because need alternating but counts differ too much. So maximum transitions = min( a+b-1, 2min(a,b) )? Actually 2min is always <= a+b-1? Compare: 2m <= a+b-1? Since a+b = m+M, where M>=m. Then 2m <= m+M => m <= M, which is true. And equality only if m=M, then 2m = m+M => m=M, then 2m = a+b, but a+b-1 = 2m-1, so 2m > 2m-1. So 2m can be greater than a+b-1 when a=b. So maximum transitions = a+b-1 if a!=b? Actually if a!=b, then 2min(a,b) <= a+b-1? Let's check: a=3,b=2: 22=4, a+b-1=4, equal. a=5,b=3: 23=6, a+b-1=7, so 6<7. So for a!=b, 2min can be less than a+b-1 if difference >1? Actually a=5,b=3: 23=6, a+b-1=7, yes. So max = min( a+b-1, 2min(a,b) )? But 2min(a,b) is always <= a+b-1 except when a=b? Let's test a=b: 2a vs 2a-1, so 2a > 2a-1, so 2a is not achievable, so max = 2a-1 = a+b-1. So indeed max transitions = a+b-1 if a=b, else = 2min(a,b) (since then 2min(a,b) <= a+b-1). Actually for a=5,b=3, 2min=6, a+b-1=7, so 6 <=7. For a=2,b=5, 22=4, a+b-1=6, so 4<=6. So max = 2min for a!=b? But is it always achievable? For a=5,b=3, can we achieve 6 transitions? Let's try to construct: we need 6 transitions => runs =7. Since a>b, start with zeros? Let's try start with zero: runs: zero, one, zero, one, zero, one, zero => total zeros across 4 runs? Actually number of zero runs = ceil(7/2)=4, one runs = 3. Need zeros sum to 5, ones sum to 3. Each run must be at least 1. So zero runs total min sum =4, we need 5 so we can add extra 1 to any run. That's possible. So max 6 is achievable. For a=2,b=5, start with ones maybe: runs=7, one runs=4, zero runs=3, ones need 5, min 4, add 1 extra; zeros need 2, min 3? Actually zeros min sum =3, but we need only 2, contradiction. So maybe we need to start with zeros? Let's compute: if a=2,b=5, min count =2, max transitions = 2*2=4. So runs =5. For runs=5, need distribution. If start with zeros: zero runs=3 (ceil(5/2)=3), one runs=2. Zeros needed=2 but min zero runs sum=3 -> impossible. So start with ones: one runs=3, zero runs=2. Ones needed=5, min sum=3 -> add 2 extra possible; zeros needed=2, min sum=2 -> fits exactly. So runs=5 with start=1 works. So max 4 transitions is achievable. So general.
But we don't need to achieve max; x can be any between 1 and max inclusive? It says 1 ≤ x < a+b, and answer always exists. So x can be any possible number? Actually not any; but problem says "It is guaranteed that the answer always exists." So the given x is feasible. So we need to construct for any feasible x.
Construction: We can think in terms of starting with a string that has the maximum number of transitions (or minimal) and then reduce transitions by merging runs. Actually we can control the number of transitions by grouping identical bits.
One typical solution (as seen in Codeforces problem 1157B? Actually there is a known problem: CF 1462A? Not sure). But I recall a problem: Given a, b, x, construct binary string with a zeros, b ones, and exactly x changes. The common solution: start with all zeros together and all ones together (so 1 transition) then gradually increase transitions by inserting opposite bits into the middle, or something.
Let's think systematically.
We have a zeros and b ones. Suppose we initially put all zeros then all ones (or vice versa). That yields exactly 1 transition (if both groups non-empty). Actually string like 000...111... has exactly 1 transition (if we have at least one zero and one one). So we can start with minimal transitions = 1 (since a,b>=1). Actually minimal is 1 if both non-empty, because we can group all zeros together and all ones together. Could it be 0? No because both a and b are >=1, so string must contain both characters, so at least one transition? Actually it's possible to have 0 transitions only if the string is all zeros or all ones, but since both counts >=1, that's impossible. So minimal is 1.
So we have range from 1 up to max (as computed). x can be any integer in that range? I think the guarantee is that x is feasible, but is any integer between 1 and max feasible? Likely yes. Let's test: a=2,b=2, max=3. Is x=2 feasible? Possibly yes, e.g., "0011" has transitions=1? Wait "0011": positions: 1-2:0-0 same,2-3:0-1 change,3-4:1-1 same => 1 transition. So that's 1. "0101": transitions=3. "0110": transitions? 0-1 change,1-1 same,1-0 change =>2. So yes, 2 is feasible. So generally all integers between min and max inclusive are feasible? Probably yes, because we can adjust transitions by merging runs or splitting runs. But need to be sure. However problem statement just says answer exists, so we just need to construct for given x.
Simplest construction: Use greedy building by alternating as much as possible and then merging some runs to reduce transitions. Or start from max and decrease.
Idea: Start with the pattern that yields maximum transitions. Then we can reduce transitions by concatenating adjacent runs of same bit (i.e., making them one run). If we have two runs of same bit separated by a run of the opposite bit, we cannot directly merge; but we can "move" a bit from one run to another to effectively reduce runs count by 2? Wait.
Better: think in terms of building from scratch: we want exactly x transitions. So number of runs r = x+1. We need to assign lengths to runs, each at least 1, to sum to a+b, with zeros sum a and ones sum b. We can decide starting bit.
Let's denote r = x+1. We need to check whether starting with 0 or 1 works. For a given r, we can compute number of zero runs (zr) and one runs (or) based on start bit.
If start with 0: zr = ceil(r/2), or = floor(r/2). Minimum zeros needed = zr, minimum ones needed = or. So we need a >= zr and b >= or.
If start with 1: zr = floor(r/2), or = ceil(r/2). So we need a >= zr and b >= or.
At least one of these should hold because answer exists. So we can just check both and choose the one that satisfies.
But then we need to assign actual lengths. After ensuring min requirements, we can distribute extra zeros among zero runs, and extra ones among one runs arbitrarily. The simplest: assign 1 to each run initially, then add the remaining zeros to the first zero run (or any), and remaining ones to the first one run. That will produce a string.
For example, a=5,b=3,x=6: r=7. start with 0: zr=4, or=3. Need a>=4 (5>=4), b>=3 (3>=3). So works. Start with zeros. Zero runs: positions 1,3,5,7 (four runs). One runs: 2,4,6 (three runs). Assign 1 to each run initially: zeros: 4 ones, ones: 3 ones? Actually zeros sum=4, ones sum=3. But we need 5 zeros and 3 ones. Extra zero = 1. Add it to the first zero run. So lengths: zero run1: 2, zero run3:1, zero run5:1, zero run7:1; one runs: all 1. Then string: 00 1 0 1 0 1 0? Actually careful order: run1 zeros:2, run2 ones:1, run3 zeros:1, run4 ones:1, run5 zeros:1, run6 ones:1, run7 zeros:1 => "00101010"? That's length 2+1+1+1+1+1+1=8? Actually 2+1+1+1+1+1+1 = 8, correct. Transitions? between runs changes: that gives 6 transitions? Let's compute: "00101010": index:1:0,2:0 (same),2-3:0-1 change,3-4:1-0 change,4-5:0-1 change,5-6:1-0 change,6-7:0-1 change,7-8:1-0 change? Wait we need to list correctly: run1 zeros length2 => "00". run2 ones length1 => "1" -> string "001". run3 zeros length1 => "0" -> "0010". run4 ones length1 => "1" -> "00101". run5 zeros length1 => "0" -> "001010". run6 ones length1 => "1" -> "0010101". run7 zeros length1 => "0" -> "00101010". That's "00101010". Transitions: positions: 1-2:0-0 (0),2-3:0-1 (1),3-4:1-0 (2),4-5:0-1 (3),5-6:1-0 (4),6-7:0-1 (5),7-8:1-0 (6) -> total 6. So x=6 satisfied. But we have zeros count: zeros in string: positions:1,2,4,6,8? Actually let's count: "0,0,1,0,1,0,1,0" => zeros: indices 1,2,4,6,8 =5 zeros; ones:3,5,7 =3 ones. So works. This matches example? In example output for 5 3 6 is "01010100"? That's different but also valid. So our method works.
Check other examples: a=2,b=2,x=1: r=2. start with 0: zr=1, or=1? Actually r=2: start with 0 => zero runs=ceil(2/2)=1, one runs=1. Min zeros=1, min ones=1. a=2,b=2 both >=. Extra zeros=1, extra ones=1. Add extra zeros to zero run (run1) -> zeros length=2, ones length=1? Actually we need to assign 1 to each run initially: run1 zeros:1, run2 ones:1. Then extra zeros=1 add to zero run, extra ones=1 add to one run. So zeros length=2, ones length=2. So string "00"+"11" = "0011". That has transitions? "0011": only one transition (between 2 and 3). So x=1. Output example "1100" is the other variant. So fine.
a=3,b=3,x=3: r=4. start with 0: zr=2 (runs 1,3), or=2 (runs 2,4). Min zeros=2, ones=2. a=3,b=3 -> extra zeros=1, extra ones=1. Assign: run1 zeros:1+1=2, run3 zeros:1, run2 ones:1+? extra ones=1, add to first one run: run2 ones:2, run4 ones:1. So string: zeros run1:2 -> "00", ones run2:2 -> "11", zeros run3:1 -> "0", ones run4:1 -> "1" => "001101"? Actually that's "00"+"11"+"0"+"1" = "001101". Count zeros: positions:1,2,5? Wait "001101": characters: index1:0,2:0,3:1,4:1,5:0,6:1 => zeros at 1,2,5 =3 zeros, ones at 3,4,6=3 ones. Transitions: 2-3:0-1 change,3-4:1-1 same,4-5:1-0 change,5-6:0-1 change => total 3 changes? Actually we have changes at 2-3,4-5,5-6 = 3. So x=3 works. Example output "101100" also works. So our method is fine.
But we must ensure that we pick the correct starting bit. For some parameters, only one starting bit works. For example, a=2,b=5,x=4? Actually x=4 feasible? Let's test: a=2,b=5, max transitions = 4. So x=4 is max. Then r=5. start with 0: zr=3, or=2 => min zeros=3, but a=2 <3, not possible. start with 1: zr=2, or=3 => min ones=3, b=5>=3; min zeros=2, a=2>=2. So start with 1 works. So we can simply try both and choose whichever works.
But we can also deduce: if a >= b, start with 0? Not always because if a=b, both work? Actually if a=b, both start bits are symmetric, both work for many x, but maybe some x only one works? For a=b=2, r=3? x=2? r=3. start with 0: zr=2, or=1 => min zeros=2, ones=1: a=2,b=2 works. start with 1: zr=1, or=2 => min ones=2, zeros=1: also works. So both work for x=2? Let's test: x=2, r=3. With start=0: zeros runs=2, ones=1. Need zeros>=2, ones>=1. OK. With start=1: zeros>=1, ones>=2, OK. So both work. For a=b=2, x=1 (r=2): both work as seen. So both work for any feasible x? Not necessarily, but since answer exists, at least one works. So we can just test both.
Given that a,b <=100, we can compute easily.
Thus algorithm:
Input a,b,x.
Set r = x+1.
For two possible starting bits st = 0 and st = 1:
Compute if st == 0: zero_runs = (r+1)//2? Actually ceil(r/2) = (r+1)//2; one_runs = r//2.
If st == 1: zero_runs = r//2; one_runs = (r+1)//2.
Check if a >= zero_runs and b >= one_runs. If true, use this starting bit.
Then assign lengths:
Initialize array runs: For i from 1 to r:
if i odd: bit = st (since start with st, then alternates) Actually if i is 1-indexed, run i's bit = st if i odd, else 1-st.
For each run, initially set length = 1.
But careful: we need to ensure total zeros and ones match. We have allocated 1 to each run of zeros and ones.
Let extra_zeros = a - zero_runs (since we already assigned 1 zero to each zero run). Similarly extra_ones = b - one_runs.
Then we need to distribute extra_zeros among zero runs (any distribution) and extra_ones among one runs.
Simplest: add all extra_zeros to the first zero run (or any). Add all extra_ones to the first one run.
But careful: if we add all extra zeros to the first zero run, we might create a very long run but that's fine.
Now construct the string: iterate runs i=1..r, with bit and length.
But is there any edge case where extra_zeros or extra_ones negative? That would mean min condition fails, so we wouldn't pick that start.
Thus we have a valid string.
But we must ensure that extra_zeros and extra_ones are non-negative. Yes.
Now, is it always possible that the assignment yields exactly a zeros and b ones? Yes.
Now let's test some tricky cases.
Case: a=1,b=1,x=1: r=2. start=0: zero_runs=1, one_runs=1, a>=1,b>=1, OK. extra_zeros=0, extra_ones=0. String: "01". transitions? "01": one transition. Works. Similarly start=1 yields "10".
Case: a=1,b=2,x=1: r=2. start=0: zero_runs=1, one_runs=1, a>=1? a=1 OK, b>=1 OK, but b=2 so extra_ones=1. So string: zeros: run1 length 1, ones: run2 length 1+1=2 => "011". That's "0"+"11" = "011". zeros count 1, ones 2, transitions: between 1-2:0-1 change, then 2-3:1-1 same => 1 transition. Works. Also start=1: zero_runs=1? Actually start=1 => zero_runs = floor(2/2)=1, one_runs = ceil(2/2)=1? Actually r=2: start=1 => zero_runs=1, one_runs=1 as well. So both would work? But then extra_zeros=0, extra_ones=1 -> string: "1"+"0"? Wait run1 ones length 1, run2 zeros length 1, plus extra_ones? Actually careful: start=1 => run1: ones, run2: zeros. one_runs=1, zero_runs=1. So assign: run1 ones:1, run2 zeros:1. extra_ones = b - one_runs = 2-1=1. Need to add extra_ones to one runs, i.e., run1. So run1 becomes length 2. So string: "11"+"0" = "110". zeros=1, ones=2, transitions: between 2-3:1-0 change, and 1-2:1-1 same => 1 transition. Works too. So both work.
Case: a=100,b=1,x=1: r=2. start=0: zero_runs=1, one_runs=1, a>=1? yes, b>=1 yes. extra_zeros=99, extra_ones=0. So string: run1 zeros:1+99=100, run2 ones:1 => "0"*100 + "1" -> "000...001". transitions: between position 100 and 101: 0-1 change, only one. So works. start=1 would also work? start=1: zero_runs=1, one_runs=1, extra_zeros=99? Actually a=100, zero_runs=1, extra_zeros=99; b=1, one_runs=1, extra_ones=0 -> string: run1 ones:1, run2 zeros:1+99=100 => "1" + "0"*100 = "100...0". transitions: 1-2:1-0 change => only one. So both work.
Now test case where x is maximum: e.g., a=5,b=3,x=6 (max). Already done, works. a=5,b=3,x=4 (some intermediate). r=5. Let's try start=0: zero_runs=ceil(5/2)=3, one_runs=2. a=5>=3, b=3>=2. extra_zeros=2, extra_ones=1. Assign: zeros runs each 1, add extra zeros to first zero run: so run1 zeros=1+2=3, run3 zeros=1, run5 zeros=1? Actually careful: runs: 1 zero,2 one,3 zero,4 one,5 zero. So zeros runs: 1,3,5. Assign lengths: run1=3, run3=1, run5=1. Ones runs: 2 and 4: assign run2=1+1=2? Wait extra_ones=1, add to first one run (run2) => run2=2, run4=1. So string: zeros3: "000", ones2: "11", zero1: "0", one1: "1", zero1: "0" => "00011010"? Actually order: run1: "000", run2: "11", run3: "0", run4: "1", run5: "0" => "00011010". Count: zeros: 3+1+1=5, ones:2+1=3. Transitions: between runs: 000-11 change (1), 11-0 change (2), 0-1 change (3), 1-0 change (4) => 4 transitions. Works.
Now test a case where a and b are both 1 but x=1? Actually max for (1,1) is 1, so x=1 only. Works.
Edge: a=1,b=100,x=1? r=2 works. x=2? For a=1,b=100, max transitions? Since min=1, max=2*1=2? Actually if a=1,b=100, min=1, max=2? Let's check: Can we have 2 transitions? Yes: e.g., "10111...?" Actually need exactly one zero. To have 2 transitions, we need runs=3. Starting with 1: runs: 1(ones),0(zero),1(ones). That would need at least 2 ones? Actually ones runs=2 (since start=1, r=3 -> one_runs=2, zero_runs=1). Need b>=2, b=100 OK, a>=1 OK. So feasible. So x=2 works. Let's test our algorithm: a=1,b=100,x=2 => r=3. start=0: zero_runs=ceil(3/2)=2, one_runs=1 => a=1 <2 fails. start=1: zero_runs=1, one_runs=2 => a>=1 OK, b>=2 OK. So choose start=1. Then extra_zeros = a - zero_runs = 1-1=0. extra_ones = b - one_runs = 100-2=98. Assign: runs: run1 (ones) length=1+98? Actually we add extra_ones to first one run? Or distribute? We need to add 98 to one runs. With two one runs, we can add all to first one run or split. Let's add to first: run1 length = 1+98 =99, run2 (zero) length=1, run3 (ones) length=1. So string: "1"*99 + "0" + "1". That's "111...1101"? Actually run1 ones:99, run2 zero:1, run3 ones:1 => "1"*99 + "0" + "1" = "111...101". That has transitions: between 99th and 100th: 1-0 change, then 100th and 101st: 0-1 change => 2 transitions. zeros=1, ones=100. Works. But note that the string length is 101? Actually a+b=101. Yes.
Now consider a=1,b=100,x=99? That seems high. Max transitions = 2*min = 2, so x cannot be 99. So such input not given. So given x is feasible.
Thus this method works.
But we need to ensure that for a given feasible x, at least one of the two start bits satisfies the min condition. I think it's always true. Let's try to prove: Suppose we have a,b,r=x+1. Consider starting with the majority bit? If a >= b, then using start=0 gives zero_runs = ceil(r/2), one_runs = floor(r/2). Since a >= b, we might have a >= zero_runs? Not necessarily. For example, a=5,b=4, r could be large? Let's find a case where a>=b but zero_runs > a. Since zero_runs = ceil(r/2). r can be up to a+b? Actually x can be up to max, which for a>=b, max = 2b if a > b else 2b-1 if a==b. So r = x+1 <= max+1 = 2b+1 (if a>b) or 2b (if a==b). So zero_runs = ceil(r/2) <= ceil((2*b+1)/2) = ceil(b+0.5) = b+1? Actually (2b+1)/2 = b+0.5, ceil = b+1. So zero_runs <= b+1. Since a >= b+? if a > b, a could be as low as b+1? Actually if a > b, minimal a is b+1. So a >= b+1, so a >= zero_runs holds? But zero_runs could be b+1, and a could be exactly b+1, so okay. If a == b, then max transitions = 2a-1, so r <= 2a. zero_runs = ceil(r/2) <= ceil(2a/2) = a. So zero_runs <= a. So a >= zero_runs holds. Similarly for start=1 when b>=a. So I think at least one start bit will satisfy the condition. Indeed answer exists, so algorithm will find one.
Now, potential pitfalls: When r=1? But x >=1, so r >=2. So fine.
Now, we must output the string. Build by constructing runs.
Implementation steps:
-
Read a,b,x.
-
r = x+1.
-
For start in [0,1]:
- if start == 0:
zero_runs = (r+1)//2
one_runs = r//2
- else:
zero_runs = r//2
one_runs = (r+1)//2
- if a >= zero_runs and b >= one_runs:
chosen_start = start; break.
-
Now compute extra_zeros = a - zero_runs
extra_ones = b - one_runs
-
Initialize lengths array for runs (size r). For i in range(r):
bit = chosen_start if i%2==0 else 1-chosen_start (0-indexed)
if bit == 0:
length = 1 + (extra_zeros if i==first_zero_index? but we need to know first zero run). Actually we need to distribute extra zeros to zero runs, extra ones to one runs.
We can do: create list runs: each run is (bit, length) initially length=1.
Then iterate over runs, if bit == 0: we have extra_zeros to add. We can add all extra_zeros to the first zero run. Similarly for ones.
But careful: if we add all extra_zeros to the first zero run, that may be fine. However we need to ensure that we don't add to wrong runs. So we can:
first_zero = True (flag) and first_one = True.
For each run:
if bit == 0:
if first_zero:
length += extra_zeros
first_zero = False
else:
length = 1
else:
if first_one:
length += extra_ones
first_one = False
else:
length = 1
But this only adds extra to the first occurrence of each bit. However if there are multiple zero runs, only first gets extra. This yields a valid distribution because we only need to allocate the extra counts somewhere, any run works. So it's fine.
But need to consider case where there are zero extra? Then fine.
Also note: what if there is no zero run? That would happen if zero_runs=0, but a>=0? But a>=1, and zero_runs=0 would imply a=0, but a>=1. So zero_runs >=1? Actually if r=1? but r>=2, so zero_runs >=1 if chosen_start=0? For start=0, zero_runs = ceil(r/2) >=1. For start=1, zero_runs = floor(r/2) could be 0 if r=1? But r>=2, floor(r/2) could be 1 if r=2, floor(2/2)=1; r=3 =>1; r=4=>2; so always >=1. So there is always at least one zero run. Similarly one runs always >=1. So first flags will be triggered.
However, there is a subtle case: extra_zeros could be 0, so adding 0 is fine.
But caution: The order of runs: if we add all extra zeros to the first zero run, it might create a string that is not necessarily "balanced" but it's okay.
Example: a=3,b=3,x=2? Check if x=2 is feasible? For a=b=3, max=5? Actually a=b=3, max transitions = 2*3-1=5. So x=2 is feasible. r=3. start=0: zero_runs=2, one_runs=1, a>=2,b>=1. extra_zeros=1, extra_ones=2. So runs: 1 zero,2 one,3 zero. Add extra_zeros to first zero run: run1 zero length=2, run3 zero length=1. Add extra_ones to first one run: run2 one length=1+2=3. So string: "00"+"111"+"0" = "001110". zeros: run1 2, run3 1 => 3 zeros; ones: 3. Transitions: 00-111 change (1), 111-0 change (2) => 2 transitions. Works.
So this method works.
But check another: a=4,b=1,x=2? r=3. start=0: zero_runs=2, one_runs=1 -> a>=2? a=4>=2, b>=1? b=1>=1. So start=0 works. extra_zeros=2, extra_ones=0. Runs: zero, one, zero. Add extra_zeros to first zero run: run1 length=1+2=3, run2 length=1, run3 length=1. String: "000"+"1"+"0" = "00010". zeros: 3+1=4, ones:1, transitions: 0-0 none? Actually "00010": indices:1-2:0-0 (0),2-3:0-0 (0),3-4:0-1 (1),4-5:1-0 (2) => 2 transitions. Works. If we had started with 1, also works? start=1: zero_runs=1, one_runs=2 -> a>=1 yes, b>=2? b=1<2 fails. So only start=0 works.
All good.
But I recall that there might be a more straightforward method: construct by repeatedly appending the character with remaining count, and toggling when needed to increase transitions. But the runs distribution method is simple.
Edge Cases: We need to be careful when r is large and a or b are just enough to satisfy min condition. Then extra_zeros and extra_ones could be zero. That's fine.
Now, confirm with provided examples:
Example1: a=2,b=2,x=1 => r=2. start=0: zero_runs=1, one_runs=1, both conditions ok. extra_zeros=1, extra_ones=1. runs: run1 zero length=2, run2 one length=2 => "0011". Output example is "1100". That's different but still correct. Our algorithm would output "0011". That's valid per problem statement. So fine.
Example2: a=3,b=3,x=3 => r=4. start=0: zero_runs=2, one_runs=2, conditions ok. extra_zeros=1, extra_ones=1. runs: run1 zero length=2, run2 one length=2, run3 zero length=1, run4 one length=1 => "001101". But example output is "101100". Our output is different but should be accepted. However we must verify that our constructed string indeed has exactly a zeros and b ones and x transitions. For "001101": zeros count: positions 1,2,5? Actually we computed earlier: 0,0,1,1,0,1 => zeros: indices1,2,5 = 3 zeros; ones:3,4,6 =3 ones; transitions: 2-3 (0-1), 4-5 (1-0),5-6 (0-1) => 3 transitions. Yes. So acceptable.
But the example output for second is "101100". That is start=1? Let's see: "101100" is 1,0,1,1,0,0? Actually "101100": digits: 1,0,1,1,0,0. zeros: positions 2,5,6? Actually zeros: 0 at positions 2,5,6 => 3 zeros? Wait count zeros: characters: 1 (one),0 (zero),1 (one),1 (one),0 (zero),0 (zero) => zeros at 2,5,6 => 3 zeros; ones at 1,3,4 => 3 ones. Transitions: 1-2:1-0 change, 2-3:0-1 change, 3-4:1-1 same, 4-5:1-0 change, 5-6:0-0 same => changes at 1,2,4 => 3 changes. So it works. So our method yields a valid answer, not necessarily the same as example but that's allowed.
Example3: a=5,b=3,x=6 => r=7. start=0: zero_runs=4, one_runs=3, conditions ok. extra_zeros=1, extra_ones=0 (since b=3, one_runs=3, extra_ones=0). Runs: zero runs at indices 1,3,5,7. Add extra_zeros to first zero run: run1 zero length=2, run3=1, run5=1, run7=1. ones: each length 1. So string: run1:"00", run2:"1", run3:"0", run4:"1", run5:"0", run6:"1", run7:"0" => "00101010". That's "00101010". Example output is "01010100". Both have 5 zeros,3 ones, 6 transitions? Let's count "00101010": transitions we computed 6. "01010100": let's count: 0,1,0,1,0,1,0,0. Changes: 1-2:0-1,2-3:1-0,3-4:0-1,4-5:1-0,5-6:0-1,6-7:1-0,7-8:0-0? Actually 7:0,8:0 => no change. So transitions at 1,2,3,4,5,6 =6. So works. So our method is fine.
Thus algorithm is correct.
Now, we need to implement in C++? The problem statement didn't specify language; but we need to produce a solution in some language; typical for such problems, we can implement in C++. I'll produce a C++ solution.
Implementation details:
- Use integer variables a,b,x.
- Compute r = x+1.
- Loop start=0; start<=1; ++start:
- compute zero_runs, one_runs as described.
- if (a >= zero_runs && b >= one_runs) then chosen_start = start; break; (but careful: both start may satisfy; we can break at first that works, but either is fine.)
- After loop, we have chosen_start.
- Compute extra_zeros = a - zero_runs; extra_ones = b - one_runs.
- Build string s = "".
- For i from 0 to r-1:
- bit = (i % 2 == 0) ? chosen_start : (1 - chosen_start);
- int length = 1;
- if (bit == 0) {
// zero run
// if it's the first zero run, add extra_zeros
// we need to track if we have already added extra_zeros.
} else {
// one run
// if first one run, add extra_ones.
}
- Append length copies of char('0' + bit) to s.
But careful: We must know which run is first zero run and first one run. We can use flags bool firstZeroUsed = false, firstOneUsed = false initially. Inside loop, if bit==0:
if (!firstZeroUsed) {
length += extra_zeros;
firstZeroUsed = true;
}
similarly for ones.
But note: if there is only one zero run (e.g., r=2, start=0 => zero_runs=1, one_runs=1), then firstZeroUsed will be set correctly, and extra_zeros added to that run. Good.
If there are multiple zero runs, only the first gets the extra. That's okay.
Potential issue: When extra_zeros is large, adding it all to the first run is fine.
Now, what about the case where extra_zeros=0 and we still add to first? That's fine as we add 0.
Now, need to ensure that we don't inadvertently add extra_zeros to a run if there is no zero run? But there is at least one.
Now, after constructing, we could verify counts? But not needed.
Now, about choice of start: In the loop, we need to correctly compute zero_runs and one_runs for each start. For start=0:
zero_runs = (r+1)/2; // integer division ceiling
one_runs = r/2;
For start=1:
zero_runs = r/2;
one_runs = (r+1)/2;
In C++: (r+1)/2 for ceil, r/2 for floor.
Now, we should also consider that if both start bits satisfy, we might choose one arbitrarily; but we might want to choose the one that yields a non-negative extra? It will be non-negative. So okay.
Now, edge: what if r is very large such that zero_runs > a and one_runs > b for both start bits? That would mean no feasible start, but problem guarantees existence, so not possible.
Thus final solution.
But wait, is there any scenario where our distribution (adding all extra zeros to first zero run) might produce a string with x transitions? The number of transitions is determined solely by the number of runs and their order, not by the lengths of runs (as long as each run length >0). Because transitions occur at boundaries between runs. So any distribution of lengths among runs (positive integers) yields exactly r-1 = x transitions. So indeed, as long as each run length is at least 1, the transitions count is exactly x. So our method yields exactly x transitions, regardless of how we distribute extra zeros/ones among runs. So it's correct.
Thus we have a simple constructive solution.
Now, let's test with a random case: a=10,b=10,x=5 (feasible? max for equal is 19, so x=5 fine). r=6. start=0: zero_runs=3, one_runs=3, both >=? a>=3,b>=3. So chosen_start=0. extra_zeros=7, extra_ones=7. runs: 1 zero,2 one,3 zero,4 one,5 zero,6 one. Add extra zeros to first zero run: run1 length=8, run3=1, run5=1; add extra ones to first one run: run2 length=8, run4=1, run6=1. String: "0"*8 + "1"*8 + "0" + "1" + "0" + "1" = many transitions? boundaries: between run1-run2: change, run2-run3: change, run3-run4: change, run4-run5: change, run5-run6: change => 5 transitions. Good.
Now test case where zero_runs is 1 but extra_zeros is huge. Works.
Now, we should also consider if r > a+b? Impossible because x < a+b, so r <= a+b, but r could be equal to a+b only if x = a+b-1, which is max if |a-b|<=1? Actually a+b-1 is possible only if |a-b|<=1. Then r = a+b, which is possible. For example a=2,b=2,x=3 -> r=4 = a+b? Actually a+b=4, r=4. Then zero_runs = ? For start=0: zero_runs=2, one_runs=2, min zeros=2, ones=2, a=2,b=2 exactly. So extra zeros=0, extra ones=0. So each run length exactly 1, string alternating: "0101" or "1010". That's fine. For start=0, runs lengths all 1, string "0101"? Actually runs: zero,one,zero,one -> "0101". Good.
If r = a+b, then each run length must be 1. So that's consistent: a = zero_runs, b = one_runs exactly. So our distribution works because extra_zeros = 0, extra_ones = 0.
Now, also consider case where r = a+b but a and b differ by 1? For a=3,b=2,x=4? a+b=5, x=4 => r=5. Then zero_runs? start with majority? a=3,b=2, a>b. start=0: zero_runs = ceil(5/2)=3, one_runs=2. So a=3, b=2 exactly. So extra_zeros=0, extra_ones=0, all runs length 1: pattern "01010"? Actually runs: 0,1,0,1,0 => "01010". That's valid. So okay.
Thus our method covers all cases.
Implementation in C++:
#include <iostream>
#include <string>
using namespace std;
int main() {
int a, b, x;
cin >> a >> b >> x;
int r = x + 1; // number of runs
int start;
int zero_runs, one_runs;
// try start = 0
zero_runs = (r + 1) / 2;
one_runs = r / 2;
if (a >= zero_runs && b >= one_runs) {
start = 0;
} else {
// must work for start = 1
start = 1;
zero_runs = r / 2;
one_runs = (r + 1) / 2;
}
int extra_zeros = a - zero_runs;
int extra_ones = b - one_runs;
string s = "";
bool first_zero_used = false;
bool first_one_used = false;
for (int i = 0; i < r; ++i) {
char bit_char;
int len = 1;
if (i % 2 == 0) {
// same as start
bit_char = '0' + start;
} else {
bit_char = '0' + (1 - start);
}
if (bit_char == '0') {
if (!first_zero_used) {
len += extra_zeros;
first_zero_used = true;
}
} else {
if (!first_one_used) {
len += extra_ones;
first_one_used = true;
}
}
s.append(len, bit_char);
}
cout << s << endl;
return 0;
}
We should double-check the condition for choosing start: If start=0 works, we use it; else use start=1. But what if both work? It's fine to use start=0. But could there be a scenario where start=0 works but start=1 also works, but our algorithm chooses start=0, which is fine.
Now, is there any case where start=0 works but then distribution yields some run length zero? No, because we add extra to first run only, but other runs remain at least 1. So all runs positive.
Now, we must ensure that after adding extra_zeros to first zero run, we haven't inadvertently used extra_ones in a zero run etc. Fine.
Potential bug: In the for loop, we are using bit_char == '0' to detect zero runs. But what if start is 1, then first run is '1', which is not zero, so we correctly treat it as one run.
Now, about first_zero_used and first_one_used: We set them false initially. For the first zero run encountered (which may not be the first run overall), we add extra_zeros. That's fine. For one runs similarly.
Now, test with a case where start=1, runs: 1:1, 2:0, 3:1, 4:0,... So first zero run is second run. That will get extra_zeros. That's fine. Example: a=2,b=5,x=4: r=5, start=1 (since start=0 fails). zero_runs = r/2 = 2, one_runs = 3. extra_zeros = a - zero_runs = 0? a=2, zero_runs=2 => extra_zeros=0. extra_ones = b - one_runs = 5-3=2. So first one run (run1) gets extra_ones: len=1+2=3. Other runs: run2 zero len=1, run3 one len=1, run4 zero len=1, run5 one len=1. So string: "111"+"0"+"1"+"0"+"1" = "1110101". Transitions: between 3-4:1-0, 4-5:0-1,5-6:1-0,6-7:0-1 => 4 transitions. zeros: positions 4,6? Actually "1110101": indices:1:1,2:1,3:1,4:0,5:1,6:0,7:1 => zeros at 4,6 =2 zeros; ones at 1,2,3,5,7 =5 ones. Works.
Now, what about a=2,b=5,x=3? r=4. start=1? Let's test both: start=0: zero_runs=2, one_runs=2 => a>=2 yes, b>=2? b=5>=2, so start=0 works? But zero_runs=2 <= a=2, one_runs=2 <= b=5, yes. So start=0 also works. Our algorithm picks start=0 because it checks first. That's fine. But let's check: extra_zeros = 0, extra_ones = 3. runs: 1 zero,2 one,3 zero,4 one. Add extra_ones to first one run (run2) => len=1+3=4. So string: "0"+"1111"+"0"+"1" = "0111101"? Actually run1 zero:1, run2 one:4, run3 zero:1, run4 one:1 => "0"+"1111"+"0"+"1" = "0111101". That's length 7? 1+4+1+1=7, correct. zeros: two zeros (positions 1 and 5?) Actually "0111101": indices:1:0,2:1,3:1,4:1,5:1,6:0,7:1 => zeros at 1,6 =2 zeros; ones at 2,3,4,5,7 =5 ones. Transitions: 1-2:0-1 change, 5-6:1-0 change, 6-7:0-1 change => 3 changes? Wait check all: 1-2 change, 2-3 same, 3-4 same, 4-5 same, 5-6 change, 6-7 change => total 3 changes. So x=3 satisfied. So start=0 works as well. Good.
Thus algorithm robust.
Now, we need to ensure we handle case where extra_zeros or extra_ones could be negative? Not possible because we checked conditions.
Thus solution is correct.
Now, I think the problem expects such solution; I've seen similar Codeforces problem "Binary String Constructing" (CF 1003B?). Actually there is a known problem: "Binary String Constructing" with exactly x indices where adjacent differ. The solution often uses constructing by starting with the majority and then inserting the minority bits to increase transitions. But our approach is simpler: directly determine runs.
Now, we can discuss possible alternative: Building string by toggling based on remaining counts and desired transitions. But the run-based method is straightforward.
Now, I will produce final answer in the required format: Provide the code and explanation. Since the instruction is to "print only one string s", we need to output the string.
Now, for completeness, we can consider if the input can have a or b equal to 0? The problem constraints: 1 ≤ a, b ≤ 100, so both at least 1. So minimal transitions = 1. x is at least 1 and less than a+b. So we are safe.
Now, what about when x = a+b-1? That is maximum. Our method still works as discussed.
Now, confirm with some edge: a=100,b=100,x=199 (since a+b=200, x<200, max=199). r=200. start=0: zero_runs = (200+1)/2 = 100.5? Actually integer: (200+1)//2=100? In C++ integer division: (200+1)/2 = 100 (since 201/2=100 remainder 1? Actually 201/2=100.5, integer division truncates toward zero, so 100. But ceil(200/2)=100? Wait: 200/2=100, ceil is 100. Actually 200 is even, so ceil(200/2)=100, floor=100. So (r+1)/2 yields 100 when r=200? (200+1)/2 = 201/2 = 100 (since integer division truncates). That's floor, not ceil. Actually to compute ceil(r/2) in integer arithmetic, we do (r+1)/2 when using integer division that truncates. For r even, r/2 = (r+1)/2? For r=200, r/2=100, (r+1)/2=100 as well (since 201/2 truncates to 100). But ceil(200/2)=100. So it's correct. For r odd, say r=201, ceil(201/2)=101, (r+1)/2 = (202)/2=101. So formula (r+1)/2 works for ceil. For r even, say 200, ceil=100, (r+1)/2=100 (since 201/2 truncated is 100). So good. And floor(r/2) = r/2.
So in code, zero_runs = (r+1)/2, one_runs = r/2 for start=0. For start=1: zero_runs = r/2, one_runs = (r+1)/2.
Works.
Now, let's test a=100,b=100,x=199 with our code: r=200. start=0 works? zero_runs=100, one_runs=100, a>=100,b>=100, yes. extra_zeros=0, extra_ones=0. Then we build runs: 200 runs, alternating, each length 1. That gives string length 200, alternating, starting with 0. That's "010101...0"? Actually since start=0 and r even, final bit? With r=200 even, start=0 => pattern: 0,1,0,1,..., last run is run 200: since i from 0 to 199, i even for start? Let's compute: i=0 -> bit 0, i=1 -> 1, i=2 ->0,..., i=199 (odd) -> 1? Actually if r=200, i from 0 to 199. Even i: bit=0, odd i: bit=1. So last i=199 is odd => bit=1. So string ends with 1. That's fine. Transitions=199. Check counts: zeros at even indices? Actually with runs length 1, string: 0,1,0,1,... up to 200. Since starts with 0, zeros at positions 1,3,...,199? That's 100 zeros; ones at positions 2,4,...,200 =100 ones. Works. So it's valid.
Now, what about a=100,b=99,x=198? Since a+b=199, x=198 is max? For a=100,b=99, max transitions = 2min = 299 = 198. So x=198 works. r=199. start=0: zero_runs = ceil(199/2)=100, one_runs=99. a=100>=100,b=99>=99, extra_zeros=0, extra_ones=0. So runs: 199 runs alternating, starting with 0, ends with? Since r odd, start=0 => runs: 0,1,0,1,..., last run (199th) because odd count, bit=0. So string ends with 0. So zeros count: 100, ones:99. Transitions=198. Works.
Now, if we had chosen start=1 for that case, it would also work? start=1: zero_runs = floor(199/2)=99, one_runs=100, a=100>=99, b=99>=100? b=99<100 so fails. So only start=0 works. Our algorithm would try start=0 first, works.
Thus fine.
Now, possible pitfalls: we must ensure that we correctly handle the case where r is such that zero_runs > a for start=0, but start=1 may also fail? According to guarantee, it shouldn't happen. But let's verify with some random to be safe: Suppose a=1,b=100, x=99? That's impossible because max is 2. So such input not given.
Thus solution is correct.
Now, let's produce final answer.
But the user asked: "You are given three integers a, b and x. Your task is to construct a binary string s of length n = a + b such that there are exactly a zeroes, exactly b ones and exactly x indices i (where 1 ≤ i < n) such that s_i ≠ s_{i + 1}. It is guaranteed that the answer always exists." So they need code.
I'll provide final solution in C++.
But maybe we need to include some explanation? The instruction says "Output" to print the string. But as ChatGPT we should produce the code. However the conversation context is a typical programming problem; the user likely expects the solution code. In the "final" channel, we should output the code.
But also note that there is a "Note" about examples; but we just need to write the program.
Given that the instruction says "You are given ... Your task is to construct ... Output Print only one string s", it's typical to write a program that reads input and prints the string.
Thus I'll provide a complete C++ program.
But let's double-check for any off-by-one issues.
Our construction:
We determine start bit (0 or 1). Then we compute extra_zeros and extra_ones.
We then iterate i from 0 to r-1, determine bit for run i.
If i is even (0-indexed), bit = start; else bit = 1-start.
We use flags firstZeroUsed and firstOneUsed to add extra counts to the first occurrence of each type.
Now, what if the first zero run occurs after we have already added extra_ones to the first one run? That's fine.
Potential subtlety: For start=1, the first run is one, so first_one_used becomes true when i=0, and we add extra_ones to that run. The first zero run occurs at i=1 (since i odd => bit=0). Then we add extra_zeros to that run. That's correct.
Now, ensure that extra_zeros and extra_ones are not used elsewhere.
Now, what about the scenario where extra_zeros=0 but we still "use" the first zero run by setting firstZeroUsed to true but not adding any extra; that's fine.
Now, after loop, we output s.
Test with sample 3: a=5,b=3,x=6.
r=7. start=0 from our algorithm. zero_runs=4, one_runs=3. extra_zeros=1, extra_ones=0.
Loop:
i=0: bit=0, firstZeroUsed false => len=1+1=2, firstZeroUsed=true. append "00".
i=1: bit=1, firstOneUsed false => len=1+0=1, firstOneUsed=true. append "1".
i=2: bit=0, firstZeroUsed already true => len=1. append "0".
i=3: bit=1, firstOneUsed true => len=1. append "1".
i=4: bit=0, len=1 => "0".
i=5: bit=1, len=1 => "1".
i=6: bit=0, len=1 => "0".
String: "00101010". That's as we had.
But example output is "01010100". Both are valid.
Now, confirm that we didn't misinterpret x: It says exactly x indices i (1 <= i < n) with s_i != s_{i+1}. So number of transitions is x. Our runs approach yields exactly r-1 = x transitions regardless of lengths. So good.
Now, verify that the string length equals a+b: sum of lengths = zero_runs + extra_zeros + one_runs + extra_ones = (zero_runs + one_runs) + (extra_zeros + extra_ones) = r + (a - zero_runs) + (b - one_runs) = a + b. Good.
Now, check a scenario where we add extra_zeros to first zero run but the first zero run might be at i>0. That's fine.
Now, one more test: a=2,b=2,x=2? This should be feasible. r=3. start=0: zero_runs = ceil(3/2)=2, one_runs=1, a>=2,b>=1, works. extra_zeros=0, extra_ones=1. runs: i=0: bit=0, firstZeroUsed false -> len=1+0=1, firstZeroUsed true; i=1: bit=1, firstOneUsed false -> len=1+1=2, firstOneUsed true; i=2: bit=0, firstZeroUsed true -> len=1. So string: "0"+"11"+"0" = "0110". zeros: 2, ones:2, transitions: 0-1 change, 1-1 same, 1-0 change => 2 changes. Works.
Alternative start=1: would yield "1001" maybe.
So it's fine.
Thus solution correct.
Now, I'll provide the final answer: the C++ code.
But before finalizing, consider any possible oversight: The problem constraints a,b <= 100, but x can be up to a+b-1, which is up to 199. That's fine.
Now, about memory and time: trivial.
Now, finalize.
One more check: Suppose a=1,b=1,x=1, we get r=2. start=0 works. extra zeros=0, extra ones=0. String: i=0: bit=0, len=1 -> "0"; i=1: bit=1, len=1 -> "1". Output "01". That's valid.
Now, a=1,b=1,x=0? Not allowed because x>=1.
Now, consider a=1,b=2,x=2? r=3. start=0: zero_runs=2, one_runs=1, a=1<2 fails. So start=1: zero_runs=1, one_runs=2, a>=1 ok, b>=2 ok. extra_zeros=0, extra_ones=0 (since b=2, one_runs=2). So string: i=0: bit=1 (since start=1) -> len=1; i=1: bit=0 -> len=1; i=2: bit=1 -> len=1 => "101". Check: zeros=1, ones=2, transitions: 1-0 change,0-1 change =>2. Works.
Now, all good.
Thus final code.
Potential improvement: Instead of using flags, we could directly assign extra to the first run of each type by checking i == firstZeroIndex etc. But flags are fine.
Now, also note that some might argue about the order of adding extra: we add extra_zeros to first zero run; but if there are multiple zero runs, the extra zeros are all placed in the first run, which could be at the beginning or in the middle depending on start. That's fine.
Now, final code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, x;
cin >> a >> b >> x;
int r = x + 1; // number of runs
int start, zero_runs, one_runs;
// try start with 0
zero_runs = (r + 1) / 2;
one_runs = r / 2;
if (a >= zero_runs && b >= one_runs) {
start = 0;
} else {
start = 1;
zero_runs = r / 2;
one_runs = (r + 1) / 2;
}
int extra_zeros = a - zero_runs;
int extra_ones = b - one_runs;
string s;
bool first_zero = false, first_one = false;
for (int i = 0; i < r; ++i) {
char ch;
if (i % 2 == 0) ch = '0' + start;
else ch = '0' + (1 - start);
int cnt = 1;
if (ch == '0') {
if (!first_zero) {
cnt += extra_zeros;
first_zero = true;
}
} else {
if (!first_one) {
cnt += extra_ones;
first_one = true;
}
}
s.append(cnt, ch);
}
cout << s << endl;
return 0;
}
But note: In the flags initialization, we set first_zero = false, first_one = false. Actually we want to add extra the first time we encounter a zero run, so we check if not used. So we set bool first_zero = false; then when we see zero, if (!first_zero) then add and set first_zero = true. That's fine.
But there is a nuance: For the case where extra_zeros=0, we still mark first_zero = true, but that's fine because we don't want to add to any other zero run anyway.
Now, test with a scenario where there are zero runs but extra_zeros=0 and we mark first_zero true, and later zero runs get length 1. That's fine.
Now, final answer.
But also we can avoid flags by using simpler logic: assign extra zeros to the first zero run (i.e., the first run where bit is 0) by checking if bit is 0 and we haven't added yet. So flags are fine.
Now, final output.
I think this is correct.
However, I recall that in some solutions, they use a different approach: build by starting with the majority character and then alternating and inserting the other character to increase transitions. But this run-based approach is simpler and proven.
Now, produce final answer in ChatGPT: Provide the solution code in C++.
But as per instructions, I should output only the final answer? Possibly the code. Since the user says "You are given three integers ... Output Print only one string s", they might be expecting the program to do that. Usually we provide the full program.
Thus final. ```cpp
#include
#include
using namespace std;
int main() {
int a, b, x;
cin >> a >> b >> x;
int runs = x + 1; // number of runs
int start, zero_runs, one_runs;
// try to start with '0'
zero_runs = (runs + 1) / 2; // ceil(runs/2)
one_runs = runs / 2; // floor(runs/2)
if (a >= zero_runs && b >= one_runs) {
start = 0;
} else { // otherwise start with '1' must work
start = 1;
zero_runs = runs / 2;
one_runs = (runs + 1) / 2;
}
int extra_zeros = a - zero_runs;
int extra_ones = b - one_runs;
string ans;
bool first_zero_used = false, first_one_used = false;
for (int i = 0; i < runs; ++i) {
char bit;
if (i % 2 == 0) bit = '0' + start;
else bit = '0' + (1 - start);
int cnt = 1;
if (bit == '0') {
if (!first_zero_used) {
cnt += extra_zeros;
first_zero_used = true;
}
} else {
if (!first_one_used) {
cnt += extra_ones;
first_one_used = true;
}
}
ans.append(cnt, bit);
}
cout << ans << endl;
return 0;
}