There are N empty boxes arranged in a row from left to right. The integer i is written on the i-th box from the left (1 \leq i \leq N).
For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.
We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:
For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.
Does there exist a good set of choices? If the answer is yes, find one good set of choices.
Constraints
All values in
input are integers.
1 \leq N \leq 2 \times 10^5
a_i is 0 or 1.
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
If a good set of choices does not exist, print -1.
If a good set of choices exists, print one such set of choices in the following format:
M
b_1 b_2 ... b_M
where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.
Examples
Input
3
1 0 0
Output
1
1
Input
5
0 0 0 0 0
Output
0
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given N boxes with numbers 1..N. For each box i we can either put a ball (1) or not (0). Let x_i ∈ {0,1} be decision for box i.
The condition: For each i from 1 to N, the sum over boxes with numbers that are multiples of i (i.e., j such that i|j) of x_j ≡ a_i (mod 2). So we have a system of linear equations over GF(2) (mod 2). Variables: x_1,...,x_N ∈ GF(2). Equations: for each i, sum_{j: i|j} x_j = a_i (mod 2).
We need to decide if solution exists; if yes, output any solution.
N up to 2e5, so we need O(N log N) or similar.
We have equations based on divisibility. This is reminiscent of Mobius inversion. Indeed, over integers we have: Let f(i) = sum_{j: i|j} x_j. Then we know that x_i can be recovered via inclusion-exclusion: x_i = sum_{j: i|j} μ(j/i) f(j) ? But over integers, if we define f(i) as sum of x_j for multiples of i, then x_i = sum_{k>=1} μ(k) f(ik) if we consider all multiples? Actually, the Möbius inversion formula: If f(i) = sum_{d|i} g(d) then g(i) = sum_{d|i} μ(d) f(i/d). But here we have f(i) = sum_{j: i|j} x_j. That's like f(i) = sum_{k: k >= 1, ik ≤ N} x_{ik}. If we define g(j) = x_j, then f(i) = sum_{j: i|j} g(j). This is the Dirichlet convolution with the constant 1 function on the divisibility poset. The inversion is: g(i) = sum_{j: i|j} μ(j/i) f(j). Indeed, if we set d = j/i, then g(i) = sum_{d: id ≤ N} μ(d) f(id). Over integers, it's exactly: x_i = sum_{k: ik ≤ N} μ(k) * f(i*k). But careful: The standard inversion for multiplicative functions: If f(n) = sum_{d|n} g(d) then g(n) = sum_{d|n} μ(d) f(n/d). That's for summation over divisors of n. Here we have f(i) = sum_{k: i|k} g(k) which is like f(i) = sum_{k} g(k) * [i|k]. This is the dual relation. The inversion is indeed: g(i) = sum_{k: i|k} μ(k/i) f(k). Yes.
So over integers, given f(i) (which are the sums modulo 2 we want to be a_i), we can compute x_i as above. But note that we are working modulo 2, so we can reduce mod 2. However, the inversion formula uses integer μ (values -1,0,1). Mod 2, -1 ≡ 1 mod 2 (since -1 mod 2 = 1). But careful: μ(d) can be 0, 1, -1. In mod 2, 1 and -1 are the same (both ≡ 1 mod 2). But 0 remains 0. So the mod 2 version: x_i ≡ sum_{k: ik ≤ N} (μ(k) mod 2) * f(ik) (mod 2), where we treat μ(k) mod 2 as 1 if |μ(k)|=1 (i.e., k is squarefree with an odd number of prime factors? Actually μ(k)=(-1)^r if k is product of r distinct primes; its absolute value is 1. So μ(k) mod 2 is 1 if μ(k) = -1? Wait: mod 2, -1 ≡ 1, so both 1 and -1 become 1. So effectively, if μ(k) ≠ 0 (i.e., k is squarefree), then μ(k) mod 2 = 1 (since both 1 and -1 ≡ 1 mod 2). But careful: In GF(2), addition is XOR, and scalar multiplication is trivial: 1 * f = f, 0f = 0. So the coefficient is 1 if μ(k) is odd? But μ(k) is -1,0,1. In GF(2), -1 ≡ 1, so both -1 and 1 map to 1. So effectively, if μ(k) ≠ 0, we include f(ik). But is this correct? Let's test small cases.
For N=3, suppose we want x_1, x_2, x_3. We have f(1)=x1+x2+x3, f(2)=x2, f(3)=x3. The inversion: x1 = f(1) - f(2) - f(3) + f(6)?? But f(6) doesn't exist. Actually inversion formula over all natural numbers: x1 = sum_{k≥1} μ(k) f(k). But we only have f up to N. For i=1, we have x1 = sum_{k=1..N} μ(k) f(k) with f(k) defined only for k≤N. That works if we treat f(k)=0 for k>N? But original f(k) only defined for k≤N and sum over multiples up to N. So inversion with finite range: For i, we have x_i = sum_{d: i|j, j≤N} μ(j/i) f(j). For i=1: x1 = sum_{j=1..N} μ(j) f(j). Let's compute: x1 = μ(1)f(1) + μ(2)f(2) + μ(3)f(3) + μ(4)f(4)+... μ(1)=1, μ(2)=-1, μ(3)=-1, μ(4)=0,... So x1 = f(1) - f(2) - f(3) + ... In mod2, minus is same as plus, so x1 ≡ f(1) + f(2) + f(3) + ... (since -1≡1). But then we would get x1 = f(1)+f(2)+f(3)+... mod2? Let's test: Suppose x1=1, x2=0, x3=0. Then f(1)=1, f(2)=0, f(3)=0. According to mod2 formula x1 ≡ f(1)+f(2)+f(3)=1, OK. Suppose x1=0, x2=1, x3=0: f(1)=1, f(2)=1, f(3)=0 => f(1)+f(2)+f(3)=0 mod2? Actually 1+1+0=0, but x1=0, correct. Suppose x1=0, x2=0, x3=1: f(1)=1, f(2)=0, f(3)=1 => sum=0, correct. Suppose x1=1, x2=1, x3=0: f(1)=0 (since 1+1=0 mod2), f(2)=1, f(3)=0 => sum=1, x1=1 correct. So it seems for i=1, x1 = sum_{j=1..N} (μ(j) mod2) f(j) mod2 where (μ(j) mod2) = 1 if j is squarefree (since μ(j) ≠0). Actually all squarefree j have μ(j)=±1, so mod2 they are 1. So x1 = XOR over all squarefree j≤N of f(j). That matches test: N=3, squarefree numbers:1,2,3 => x1 = f1 xor f2 xor f3. That matches our earlier computed? In example above, f1 xor f2 xor f3 =? For x1=1,x2=0,x3=0: f1=1,f2=0,f3=0 => xor=1. For x1=0,x2=1,x3=0: f1=1,f2=1,f3=0 => xor=0, OK. For x1=0,x2=0,x3=1: f1=1,f2=0,f3=1 => xor=0? Actually 1 xor 0 xor 1 =0, ok. For x1=1,x2=1,x3=1: f1=1+1+1=1 mod2? Actually x1=1,x2=1,x3=1 => f1 = 1+1+1=1 (mod2), f2=1, f3=1 => xor: 1 xor 1 xor 1 =1, x1=1 correct. So it works.
Now for i=2: Inversion: x2 = sum_{k:2|j, j≤N} μ(j/2) f(j). So x2 = sum_{d: d≥1, 2d≤N} μ(d) f(2d). In mod2, again μ(d) non-zero (squarefree) contributes 1. So x2 = XOR over d squarefree, 2d≤N of f(2d). That is XOR over multiples of 2 whose quotient with 2 is squarefree.
Let's test with previous examples. For N=3: Multiples of 2: j=2 only. Quotient d=1, μ(1)=1 => x2 = f2. Indeed x2 = f2 (since f2 = x2). Works.
For i=3: j=3, d=1 => x3 = f3.
For i=4 (if N>=4): Multiples: 4,8,... Quotients: d=1 for 4 (μ(1)=1), d=2 for 8? but 8 maybe >N. So x4 = f4 XOR? Actually also multiples like 12? Wait, we need all j such that 4|j and j≤N. Then x4 = XOR over those j of (μ(j/4) mod2) f(j). For j=4, j/4=1 squarefree => include f4. For j=12, j/4=3 (squarefree) => include f12. For j=20, j/4=5 => include f20. For j=8, j/4=2 (squarefree) => include f8. For j=24, j/4=6 (μ(6)=? 6=23, μ(6)=1? Actually μ(6)=? 6=23 distinct primes so μ(6)=(-1)^2=1? Wait, μ(n)=(-1)^k where k is number of prime factors if n is squarefree. For 6, k=2, so μ(6)=1. But modulo 2, 1 is 1. So squarefree numbers all have μ ≡ 1 mod2. So indeed, any d that is squarefree (including 1,2,3,5,6,7,10,...) contributes. So x4 = XOR of f(j) for j multiples of 4 such that j/4 is squarefree. That is equivalent to: j is a multiple of 4 and j/4 has no squared prime factor.
Thus, given a_i (desired f(i) mod2), we can compute x_i uniquely via the inversion formula over GF(2). So a solution exists if and only if the computed x_i satisfy the original equations? Since the inversion gives a candidate solution, but we need to verify that it indeed satisfies the equations? In principle, if we define f(i) = a_i, then compute x via inversion, and then compute f'(i) = sum_{i|j} x_j, we must have f'(i) ≡ a_i? Not necessarily: The inversion formula is derived under the assumption that the sums are taken over all natural numbers (infinite). But with a finite N, we restrict the multiples to those ≤ N. The inversion formula that yields x_i in terms of f(j) is valid if we consider the f(j) defined for all j that are multiples of i, and we sum over those j. However, there is a subtlety: The relationship between x and f is linear and triangular (if we order by divisibility). We can attempt to solve recursively from largest to smallest.
Let's examine the system:
For i from N down to 1, we can solve uniquely: For box i, the equation for i involves x_i plus sum of x_j for j>i that are multiples of i. So we can compute x_i from equations i, if we already know x_j for j>i. Specifically, equation i: sum_{k: i|k} x_k = a_i (mod 2). The sum includes x_i and all multiples > i. So if we process i from N down to 1, we can set x_i = a_i - sum_{k: i|k, k>i} x_k (mod 2). This yields a unique solution. That solution will automatically satisfy all equations? Let's test: If we compute x_N = a_N (since no multiples >N). Then for i=N-1,... we compute accordingly. This process will produce a solution that satisfies each equation by construction, because we set x_i to make the equation hold given the previously determined x_j for j>i. However, we must check that for i=1, the equation includes all x_j, and we set x_1 accordingly. There's no conflict because we haven't set x_1 yet. So this greedy method from i=N down to 1 yields a solution. But wait, does it always satisfy all equations? Let's see: For i, we set x_i = a_i - sum_{k>i, i|k} x_k (mod 2). Then the total sum for i becomes exactly a_i. That's fine. However, for a smaller i' (like i'=1), we might have already set x_i'? Actually we go from high to low, so when we get to smaller i, we adjust x_i accordingly. Since each equation only depends on x_i and multiples > i, and we compute x_i based on those multiples, it ensures equation i holds. After we set x_i, we do not modify it later. So this yields a solution. The only potential issue is that the equations might be inconsistent? But this greedy method always produces some x_i, and by construction it satisfies all equations? Wait, but we need to check that for i=1, the equation involves x_1 and all other x_j, which we have already determined (since we processed from N down to 2, then compute x_1). So it will satisfy equation 1 by construction. So there's no inconsistency; any set of a_i yields a solution via this greedy algorithm. Is that always true? Let's test with small N. Suppose N=1: equation: x1 = a1. That's fine. N=2: equations: for i=1: x1+x2 = a1; i=2: x2 = a2. Process i=2: set x2 = a2. Then i=1: set x1 = a1 - x2 = a1 - a2 mod2. That gives a solution. So always possible? Then why would there be a case where answer is -1? The problem statement implies that it might be impossible. But maybe I'm missing something: The condition is that the total number of balls in boxes with multiples of i is congruent to a_i mod 2. For each i, it's an equation. But we have N variables and N equations. The matrix is upper triangular with 1's on diagonal if we order i decreasing? Actually if we order equations by i from N down to 1, and variables also from N down to 1, the matrix is lower triangular? Let's be precise. Write equations in order i = N, N-1, ..., 1. For i=N, equation: x_N = a_N. So that's simple. For i=N-1, equation: x_{N-1} + (if N is multiple of N-1? Only if N is multiple of N-1, which is true only if N = N-1? Actually N is not a multiple of N-1 unless N=2? In general, the sum over multiples of i includes i, 2i, 3i,... So for i=N-1, the multiples are (N-1) and 2*(N-1) if ≤ N. For large N, 2*(N-1) > N unless N <=2? So generally only the box i itself. But there may be other multiples if i is small. However, if we process i from N down to 1, then for each i, the equation involves x_i and x_j for j > i that are multiples of i. Since we have already determined x_j for j > i, we can solve for x_i uniquely. So the system is always solvable, because it's a triangular system with ones on the diagonal. That suggests that for any a_i, there exists a unique solution. But is there any possibility that the equation for i involves only x_i? That's fine. So there is always a solution. Then why would the problem ask to output -1 if no solution exists? Could there be a hidden condition: The x_i must be either 0 or 1 (they are). That's fine. So it seems always solvable.
But maybe my ordering is flawed: For i, the multiples include numbers less than i? Actually multiples of i are numbers like i, 2i, 3i,... All multiples are ≥ i (since positive integer multiple). So yes, all multiples are ≥ i. So the equation for i only involves x_j with j ≥ i. So if we process i from high to low, we can indeed compute x_i from equation i because all x_j with j > i are already known. This yields a unique solution. So the system is always consistent. So why would the problem have a -1? Let's double-check: In the condition, "for every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2." This includes all boxes with numbers that are multiples of i. Since i itself is a multiple, yes. So the system is triangular. So indeed there's always exactly one solution.
But maybe the twist is that we don't have to use exactly N variables? No, we have to decide for each box. So it's exactly N variables. So always solvable.
But the problem statement includes the possibility of printing -1. Maybe there is some additional constraint not stated? Could it be that the boxes are arranged and we can put at most one ball, but that's already considered. Or maybe the condition is for every i from 1 to N, the sum over boxes with numbers that are multiples of i (including i) is a_i mod 2. That is exactly what we have.
Perhaps the catch is that a_i is given for i=1..N, but the sum condition for i must hold. But if we compute x_i greedily from N down to 1, we get a solution. So always possible.
Wait, check with small N like N=3, a = (1,0,0) from example. That yields solution: x1=1, x2=0, x3=0 as output. That matches greedy: x3 = a3 = 0; x2 = a2 = 0 (since 2*2=4>3); x1 = a1 - x2 - x3 = 1 - 0 - 0 = 1. Works.
Second example: N=5, a all zeros => x5=0; x4= a4=0? Multiples of 4: 4,8>5 => so x4 = a4=0; x3 = a3=0 (since 3,6>5); x2 = a2 - x4? Actually multiples of 2: 2,4. So x2 + x4 = a2 = 0 => x2 = -x4 = 0; x1 = a1 - x2 - x3 - x4 - x5 = 0. So all zero.
So seems always solvable.
But maybe there is a hidden condition that the balls cannot be placed arbitrarily? No.
Could it be that the problem originally is from AtCoder? I recall an AtCoder problem "Snuke's Coloring" or something. Actually there is a known AtCoder problem: "Snuke's Coloring" but not this. There is also "Snuke's Favorite Game"? Not sure.
I think I've seen a problem: "Boxes and Balls" where you have conditions on multiples, and the answer might be -1 sometimes. Let's think: Could it be that the condition is: For every i, the total number of balls in boxes with numbers divisible by i is a_i (exact equality, not mod 2)? But the problem states "congruent to a_i modulo 2". So mod2.
Maybe the system is not always solvable because the equations for i might involve boxes with numbers > N? No, only up to N.
Wait, there is a nuance: The condition says "total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2." This means that for each i, sum_{j: i|j, j≤N} x_j ≡ a_i (mod 2). As argued, if we define f(i) = sum_{i|j} x_j, then we have f(i) = a_i. And we want to find x. This is essentially solving a system of linear equations over GF(2). The matrix is the transpose of the divisor matrix? Actually it's the incidence matrix of the relation i|j. This matrix is invertible? Over GF(2), is it always invertible? Let's check its determinant. Since the matrix is triangular if we order rows and columns by increasing i? Let's order columns (variables) by index j, rows (equations) by i. The equation for i involves variables j where i|j. If we order columns from 1 to N and rows from 1 to N, then the matrix is not triangular because for i=1, it involves all j; for i=2, it involves 2,4,6,...; for i=3: 3,6,9,... So there is no clear triangularity if we order by increasing i. But if we order rows by decreasing i and columns by decreasing j, does it become triangular? Let's examine: For i=N, the only multiple ≤ N is N itself. So row N has a 1 at column N. For i=N-1, multiples are N-1 and maybe 2*(N-1) if ≤ N, but 2*(N-1) = 2N-2, which is > N for N>2. For N=5, i=4 multiples: 4,8>5; so row 4 has a 1 at column 4. For i=3 multiples: 3,6>5; column 3. For i=2 multiples: 2,4; both columns 2 and 4. But note column 4 is > column 2? If we order columns decreasing, columns: 5,4,3,2,1. Row 2 involves columns 2 and 4. Column 4 is larger than 2? In decreasing order, 4 comes before 2. So row 2 would have 1's at columns 4 and 2. Since we process rows decreasing i, we need to compute x in decreasing column order? Let's see: If we process i from N down to 1, for each i we need x_i and x_j for j>i. But note that j>i means numerically larger. So if we determine x_N first, then x_{N-1}, etc., we are processing variables in decreasing order. For row i, the variables involved are those with indices ≥ i, but not necessarily all > i are known because we are processing decreasing. When we are at row i, we have already determined variables for indices > i (since we processed those rows? Actually processing rows in decreasing order: we start with i=N. At that point we only know x_N? But we haven't computed any variables yet; we are about to compute them. The greedy method: we can solve the system by back substitution. Starting from i=N, equation: x_N = a_N. So set x_N = a_N. Then for i=N-1, equation: x_{N-1} + (if 2*(N-1) ≤ N then x_{2(N-1)}) = a_{N-1}. But note 2(N-1) might be ≤ N only if N-1 ≤ N/2, i.e., N-1 ≤ N/2 → N ≤ 2? For N>2, only x_{N-1} appears. So we set x_{N-1}=a_{N-1}. Continue. When we reach i such that there are multiples > i, those multiples have already been assigned values because they are > i, and we have assigned them earlier (since we processed larger indices first). So we can solve for x_i = a_i - sum_{j>i, i|j} x_j. This works for any i. Thus there is always a unique solution. So indeed always solvable.
But why does the problem mention -1? Possibly there is an additional constraint: "Snuke can choose either to put a ball in it or to put nothing in it." That's it. So no extra.
Maybe I misinterpret the condition: "total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2." Could it be that the set of boxes with multiples of i includes boxes that are multiples of i, but also boxes with numbers that have i as a multiple? No, "boxes with multiples of i written on them" means boxes whose numbers are multiples of i. So it's correct.
Another thought: In some problems, the condition is for every i, the sum over boxes with numbers that are divisors of i (not multiples) is given. That would be a different system that might be not always solvable. But here it's multiples.
Maybe the intended solution is to use Möbius inversion and realize that there's always a unique solution, and the only tricky part is to compute it efficiently. But then why ask for -1? Could it be that the input a_i can be arbitrary 0/1, and we must output the set of boxes containing balls, but there is an additional condition that the number of balls M should be printed and then the list. That's fine.
I recall an AtCoder problem "Balls and Boxes" from ABC/ARC? Actually there is a known AtCoder problem: "Snuke's Coloring" not this. There's also "Snuke's Favorite Game"? Hmm.
Maybe I should search memory: There's an AtCoder problem "AtCoder Beginner Contest 134 F" something? Not that.
Wait, I think I've seen a problem: "Boxes and Balls" from Codeforces? Possibly CF 1151E? Not sure.
Let's try to derive the solution formula: x_i = sum_{j: i|j} μ(j/i) a_j mod 2. Since μ(j/i) mod 2 = 1 if j/i is squarefree (i.e., j/i has no square factor). So we can compute x_i by iterating over multiples of i and checking if the quotient is squarefree. That is O(N log N) if we precompute squarefree flags.
But the greedy algorithm from high to low also works and is O(N log N) (for each i, sum over multiples j>i). That could be O(N * harmonic) ~ N log N. Both fine.
But is there any case where the greedy algorithm might fail because we don't know whether the equation for i is independent? As argued, it always yields a solution. But maybe there is a hidden constraint: a_i must be 0 or 1, but we already have that.
Could it be that the problem statement originally included an additional condition: The total number of balls must be minimized? No.
Given the examples, both have solutions. The second example output is 0 (empty). So they provide an answer.
Maybe the intended solution is to invert using Möbius but there is no impossibility. So why -1? Possibly for some constraints (like when N is large but a_i are arbitrary) the equations might be overdetermined? But we have N equations and N unknowns, and the coefficient matrix is invertible over GF(2). Let's check its determinant. Since the matrix (with rows and columns ordered by increasing index) is the incidence matrix of divisibility. Is it invertible? It might be that its determinant is 1 (mod 2) always. Let's test small N: N=1: matrix [1] det=1. N=2: rows: (1,1) for i=1; (0,1) for i=2? Wait, if we order rows i=1,2 and columns j=1,2, the entries: row1: j=1,2 because 1|1,1|2 => 1,1. row2: j=2 because 2|2 => 0,1. Matrix = [[1,1],[0,1]] mod2, determinant = 11 - 10 =1 mod2. N=3: rows: i=1: j=1,2,3 => 1,1,1; i=2: j=2 => 0,1,0; i=3: j=3 => 0,0,1. Matrix = [[1,1,1],[0,1,0],[0,0,1]] mod2. Determinant = 1det([[1,0],[0,1]]) -1det([[0,0],[0,1]]) +1det([[0,1],[0,0]])? Actually compute: using first row: cofactor at (1,1): 1det([[1,0],[0,1]]) = 11 =1; (1,2): 1det([[0,0],[0,1]]) = 10 =0; (1,3): 1det([[0,1],[0,0]]) = 0; sum =1. So det=1. N=4: rows: i=1: j=1,2,3,4 => 1,1,1,1; i=2: j=2,4 => 0,1,0,1; i=3: j=3 => 0,0,1,0; i=4: j=4 => 0,0,0,1. Matrix:
[1,1,1,1]
[0,1,0,1]
[0,0,1,0]
[0,0,0,1]
Determinant? Upper triangular? Not exactly: row2 has 1 at col4, which is > col2? In ordering 1..4, it's not triangular, but still we can compute determinant. It seems to be 1. Actually we can use row operations: Subtract row4 from row2? But mod2, row2 + row4 gives [0,1,0,0] because 1+1=0 at col4. So matrix can be reduced to identity, so invertible. So seems always invertible.
But is there a case where the matrix is singular over GF(2)? Consider N=6 maybe? Let's try N=6 manually. Variables x1..x6. Equations:
What about N=8? i=2 includes x2,x4,x6,x8. That's fine. i=4 includes x4,x8. So still triangular with decreasing order.
In general, for each i, the set of multiples includes i and some numbers greater than i. So if we solve from the largest i downwards, we can uniquely determine x_i. So always solvable.
Thus answer is never -1. So why would the problem include -1? Possibly because originally the condition was about divisors, not multiples. There's a known AtCoder problem "ABC 146 F" or something? Actually AtCoder Beginner Contest 146 F is about strings. Not.
I recall an AtCoder problem "Multiple of 2019" not.
Maybe I'm missing that the condition is for every i from 1 to N, the total number of balls in boxes whose numbers are multiples of i is EXACTLY a_i (not modulo 2). But the statement says "congruent to a_i modulo 2", which implies parity. So mod2.
But consider: If it's parity, then the system is over GF(2) and always solvable as argued. If it were exact equality (non-modular), then there would be constraints, but they'd be over integers and might be impossible because sums could exceed 1 etc. But they gave a_i as 0 or 1, so they could be exact numbers but then the total number of balls in those boxes must be exactly 0 or 1, which could be impossible because the sums must be consistent with overlapping boxes. That would be a more interesting problem. But the statement says "congruent to a_i modulo 2". So it's parity.
Let me double-check the original problem statement from AtCoder if possible. The problem might be from AtCoder ABC 134 E? Not.
Actually I recall a problem: "Snuke's Coloring" is about grid. Not.
Another memory: There is an AtCoder problem "Boxes and Candies"? Not.
Maybe the problem is from "Codeforces Round #649" something: "XOR and OR"? Not.
I'll search memory: There is an AtCoder problem "AtCoder Grand Contest 025 B" something with multiples and modulo 2. Not sure.
I can try to find by googling: "There are N empty boxes arranged in a row from left to right." This phrase might be from AtCoder "ABC 146 D"? Not.
Wait, I think this is from AtCoder "ABC 134 F"? No.
Actually I recall a problem from AtCoder "ABC 159 F"? Not.
Maybe it's from AtCoder "ARC 107 B"? Not.
Let's think: The output format: first line M (number of balls) and then list of boxes. That is typical for problems where solution is not unique and they ask for any. And the possibility of -1.
But if the system always has a solution, then why would they ask to output -1? Possibly because there might be no solution if N is large? That can't be.
Wait, could it be that the condition must hold for EVERY integer i from 1 to N, but the boxes are only those with numbers 1..N? That's what we used. Yes.
Maybe there is a catch: The condition says "the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2." But the boxes with multiples of i might include boxes with numbers that are multiples of i, but those numbers could be greater than N? No, they are among 1..N.
Thus, I'm leaning that the answer always exists, so we never output -1. But the problem includes -1, maybe as a red herring or because in a more general version (like modulo something else) it might be impossible.
Let's check editorial of known problem: I think there is an AtCoder problem "AtCoder Regular Contest 116 C - Multiple Sequences"? No.
Maybe it's from "AtCoder Beginner Contest 179 F"? Not.
Actually I recall a problem: "Snuke's Coloring" is about painting. Not.
Let's try to recall an AtCoder problem with the exact phrasing: "There are N empty boxes arranged in a row from left to right. The integer i is written on the i-th box from the left (1 ≤ i ≤ N). For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it. We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied: * For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2. Does there exist a good set of choices? If the answer is yes, find one good set of choices." This sounds exactly like AtCoder ABC 134 D? Wait, ABC 134 D had "Preparing Boxes". Yes! I think it's "AtCoder Beginner Contest 134 D - Preparing Boxes". I quickly recall: ABC 134 D was about putting balls into boxes, with condition on multiples, and they ask to determine if possible and output one configuration. I remember the solution involved working backwards. And indeed, that problem had the possibility of outputting -1? Let me check memory: ABC 134 D "Preparing Boxes" (https://atcoder.jp/contests/abc134/tasks/abc134_d). Problem statement: There are N empty boxes numbered 1 to N. For each box, you decide to put a ball or not. The condition: For each integer i from 1 to N, the total number of balls in boxes with numbers that are multiples of i has a parity equal to a_i. Determine if possible, and if possible, output the boxes that contain a ball. Yes, that's exactly the problem! I recall that the solution is to determine b_i (the decision) from i=N down to 1, and it's always possible. But I think the official editorial says that there is always a unique solution. Wait, does it always exist? I think I recall that indeed the answer always exists. But why would the problem ask "Does there exist a good set of choices?" Maybe to check understanding? Actually, I remember now: In the AtCoder problem, the answer always exists, so you never output -1. But they included the possibility of -1 perhaps for generality? Or maybe some a_i may lead to contradiction? Let me double-check the editorial: I found (from memory) that the solution is: For i from N down to 1, set b_i = a_i xor (sum of b_j for j that are multiples of i and > i). Since a_i are given, this yields a solution. There is no condition for impossibility. So the answer always exists. In fact, I recall that the problem statement says: "Determine if it is possible... If it is impossible, print -1; otherwise, output M and the boxes." But I think the editorial confirms that it's always possible. Wait, I'm not entirely sure. Let's check: Actually, I recall a similar problem from AtCoder ABC 134 D: I solved it myself; I remember the solution: we compute ans[i] from N downto 1: ans[i] = a[i] ^ (sum of ans[j] for j that are multiples of i and j>i). And we output all i where ans[i]=1. That's it. There was no check for impossibility. But I think the problem statement did ask to output -1 if impossible. However, it turned out that there is always a solution. Maybe they wanted to trick? Or maybe there is a condition: a_i are either 0 or 1? Yes. The problem as I recall: "There are N boxes numbered 1 to N. For each i from 1 to N, the parity of the number of balls in boxes whose numbers are multiples of i must equal a_i. Find a configuration." And the solution is always possible. I think many participants realized this. So indeed the answer never is -1.
But to be safe, we should implement as described and not output -1, but maybe there is a rare case where it's impossible? Let's test with N=1: a1 can be 0 or 1 => solution exists. N=2: any a1,a2? For a1=0,a2=1: set x2=1; then x1 = a1 - x2 = 0 - 1 mod2 = 1, so solution exists. For a1=1,a2=1: x2=1; x1=1-1=0. So works. So seems always.
But what about large N where the equations might conflict? Let's try to see if the system matrix is invertible. The matrix M_{i,j} = 1 if i|j else 0. This matrix is known to be invertible over integers? Its determinant is something like product of something? Over GF(2), the determinant might be 1 always because we can perform elimination. Consider the matrix with rows and columns ordered by increasing i. Perform elimination from bottom to top? Actually, since for each i, the diagonal entry M_{i,i}=1. We can do Gaussian elimination from the last row upwards: For i=N downto 1, we can subtract multiples of row i from rows above to eliminate the entries in column i. Because for any j < i, if i|j? Wait, column i corresponds to variable x_i. Which rows involve x_i? Those rows r such that r|i. Since r ≤ i, and r divides i. For rows r < i, if r|i, then row r has a 1 in column i. When we eliminate from bottom, row i (with i = current index) has a 1 at column i, and all rows above that also have a 1 at column i if they divide i. So we can XOR row i into those rows to eliminate that column. This is possible because we have a 1 on the diagonal. This process yields an identity matrix. So the matrix is invertible over GF(2). So always consistent.
Thus, the answer is always yes. So we will never print -1. However, the problem statement includes -1, so we should still implement check but it's guaranteed to never trigger, or we can just ignore and always compute.
But maybe there is a subtlety: The input a_i are 0 or 1, but we must output the boxes that contain a ball. The solution yields x_i ∈ {0,1}. So we can output those with x_i=1.
But there is also requirement to output M first and then the indices. So we need to collect indices where x_i = 1.
Now, the main challenge: Efficient computation for N up to 2e5. The greedy algorithm: for i from N down to 1, compute sum_{j: i|j, j>i} x_j. That sum mod 2 can be computed by iterating over multiples of i. That would be O(N log N) because for each i, number of multiples ≤ N is about N/i, and total sum of N/i for i=1..N is N * H_N ≈ N log N, which is acceptable for 2e5 (N log N ~ 2e5 * 12 = 2.4e6, actually harmonic sum ~ ln N + gamma ~ 12.2? Wait ln(2e5) ≈ 12.2, but sum_{i=1..N} N/i = N * H_N, H_N ≈ ln N + γ ≈ 12.2, so NH_N ≈ 2.4e6, that's fine). Even if we do naive loops, it's okay. But careful: For each i, we need to sum x_j for j multiples > i. We can precompute all x_j first? But we are processing from N downwards, so when we process i, we have already computed x_j for j > i. So we can simply loop over multiples: for (j = 2i; j <= N; j += i) and add x_j to sum. That's O(N log N). That's acceptable.
But note that the sum we compute is over multiples j > i, but the equation includes j = i as well. So x_i = a_i - sum_{j>i, i|j} x_j (mod 2). So we need sum of x_j for multiples j > i. That's exactly what we do.
Implementation steps:
Read N, array a[1..N].
Initialize x array of size N+1 with 0.
For i from N down to 1:
sum = 0
for j = 2*i; j <= N; j += i:
sum ^= x[j] (since mod2, XOR)
x[i] = a[i] ^ sum (because x_i = a_i - sum mod2, but subtraction same as addition mod2)
Another test: N=6, a = [1,1,1,0,0,0]. Compute:
i=6: x6 = a6=0
i=5: x5=0
i=4: x4=0
i=3: multiples >3: j=6 -> x6=0 sum=0 -> x3 = 1 ^ 0 =1
i=2: multiples >2: j=4,6: x4=0, x6=0 sum=0 -> x2 = 1 ^ 0 =1
i=1: multiples >1: j=2,3,4,5,6: x2=1,x3=1,x4=0,x5=0,x6=0 => sum=1 xor1=0? Actually 1 xor 1 = 0, plus others 0 -> total 0. So x1 = a1 ^ sum = 1 ^ 0 =1.
So x = [1,1,1,0,0,0]. Check: i=1 sum all = 1+1+1 = 3 mod2=1 ok; i=2 sum of 2,4,6 = 1+0+0=1 ok; i=3 sum of 3,6 = 1+0=1 ok; i=4 sum of 4=0; i=5=0; i=6=0. Works.
Thus algorithm works.
But we must be careful about potential integer overflow or speed: N=200k, the nested loops sum over multiples. That's fine.
However, note that for each i we loop over multiples j = 2i, 3i,... up to N. The total number of operations is sum_{i=1}^{N} floor(N/i) - 1 (since we start from 2*i). Approx N * H_N - N ≈ 2e5 * 12 - 2e5 = 2.2e6, which is fine.
But we need to store a as 0/1. Use boolean or int.
Now, about output: The problem asks: print M and then b_1 ... b_M in any order. So we can just list indices with x_i=1.
But there is a nuance: In the sample output for second example, they print "0" and then nothing? Actually format: they print "0" on first line, and presumably nothing after? Usually you print the count and then maybe an empty line? The sample shows:
Input:
5
0 0 0 0 0
Output:
0
So just "0" and no second line. That's acceptable: we print M and if M>0, we print the list on the next line or same line? In the problem statement: they say print M, and then b_1 b_2 ... b_M. Usually that means first line contains M, second line contains the list separated by spaces. But in sample 2, they only show "0". They might omit the second line because M=0. So we should follow: print M, and if M>0 print the list in the next line (or same line with newline? Usually it's two lines: first line M, second line the list. For M=0, just first line is fine. We can implement accordingly.
Now, what about -1? As argued, we never need to print -1. But to be safe, we could still check if after computing we get a contradiction? However, the algorithm we used always produces a solution. But maybe there is a subtlety: The greedy algorithm from N down to 1 gives a solution that satisfies all equations. So it's a constructive proof of existence. So we can skip -1.
But the problem statement says "If the answer is yes, find one good set of choices." It doesn't say "if no, print -1". So they explicitly ask to print -1 only when it doesn't exist. So we should still output -1 if no solution, but we believe it's always yes.
Given that it's an AtCoder problem, I know that the answer always exists. But let me confirm by checking the editorial: I recall that AtCoder ABC 134 D - Preparing Boxes solution indeed never outputs -1. Actually, some participants might have thought it might be impossible, but the editorial says there is always exactly one solution. Yes.
Thus, we can implement the solution as described.
Now, there is also an alternative using Möbius: x_i = sum_{j: i|j} μ(j/i) a_j mod 2. Since μ mod 2 is 1 if j/i squarefree, 0 otherwise. That is equivalent.
But the greedy approach is simpler.
Let's double-check for correctness in terms of parity: We used XOR (^) for sum mod 2. Since sum_{j>i,i|j} x_j mod2 is just XOR of those x_j. Then we set x_i = a_i XOR (that sum). That's correct because equation: x_i XOR (sum) = a_i => x_i = a_i XOR sum.
So implementation:
vector a(N+1);
vector x(N+1, 0);
for (int i = N; i >= 1; --i) {
int sum = 0;
for (int j = 2*i; j <= N; j += i) {
sum ^= x[j]; // since mod2 addition is XOR
}
x[i] = a[i] ^ sum;
}
Then collect indices.
We can optimize slightly by using bitset? Not needed.
Now, about memory: N up to 2e5, arrays of int are fine.
Now, one more check: The algorithm uses x[j] for j > i which have been computed earlier because we are going from N down to 1. That's correct.
Now, potential pitfalls: The input format: N on first line, then N numbers a_1 ... a_N. They may be given as space-separated. So read accordingly.
Now, test on some random small N to verify consistency: Let's write a quick mental check for N=7 with random a. But I trust.
Thus, answer: Always exists, compute via backward summation.
Time Complexity: O(N log N). For N=200k, around 2.5 million operations, which is fast.
Now, we should implement in C++ (since I'm ChatGPT, but the answer expects code? Actually the user didn't specify language. Usually they want solution explanation and perhaps code? The user asked: "There are N empty boxes ... Does there exist a good set of choices? If the answer is yes, find one good set of choices." This is a problem statement, and they expect us to provide solution. In the context, we are to output as per problem. But the user is not asking for code, but maybe a reasoning and output? The user gave input examples. Likely they want the solution algorithm. But the initial instruction: "You are an AI... Provide answer." So perhaps they expect the final output for given inputs? But the problem description is generic, not a specific instance. Actually the user wrote a problem statement and then gave examples. It looks like they are sharing a problem from an online judge and maybe expecting an answer? But the chat says "There are N empty boxes ..." and then "Examples: Input: 3 1 0 0 Output: 1 1" etc. So it's a coding problem. Usually the assistant would provide a solution explanation and code. However, the final answer should be the solution to the problem: either -1 or the boxes. But since the input is not provided except examples, we need to produce answer for a specific input? Wait, the user didn't provide their own input; they provided the problem statement and examples. This is typical in a coding contest: they ask to write a program that reads input and outputs accordingly. But here the user is probably asking the assistant to solve the problem in general and maybe provide code. However, the conversation history: The user said: "There are N empty boxes arranged in a row from left to right... Output... Examples ..." That's it. So it's a problem statement. The assistant is to respond with a solution, likely by providing the algorithm and maybe code. Since the instruction is "You are an AI assistant that solves problems", I'd provide an explanation and then provide code in a common language (like C++). But sometimes the assistant directly outputs the answer for the given test case? But there is no specific test case; they gave examples but not a specific input to solve. So I think the proper response is to explain the solution and provide a generic code.
But maybe the user wants the assistant to act as if it's solving the problem for any input. In such cases, the assistant might output something like: "Here's how to solve: ..." But the instruction: "You are an AI assistant that solves problems. Provide answer." Could be ambiguous. Often in these scenarios, the user gives a problem statement and expects the assistant to output the solution for the given examples? But they didn't ask "What is the output for this input?" They just gave the problem. Perhaps they want the assistant to produce a program that solves it? However, the typical response pattern in this assistant is to provide a solution description and code.
I'll provide a clear explanation of the solution (the greedy backward method) and then present a C++ implementation. Also mention that the answer always exists, so we never print -1. Then I'll show how the code works.
But to be safe, I'll also include a check (though unnecessary) and output -1 only if some inconsistency arises (maybe after forward verification). But we can skip.
Now, let's also consider memory: Use vector for a and ans.
Now, I'd like to consider the possibility of outputting -1: Since the problem says "If the answer is yes, find one good set of choices." It implies there may be cases where it's impossible. But we concluded it's always possible. Could there be a scenario where the input constraints differ? Let's double-check the original AtCoder problem: Actually, I recall that in ABC 134 D, the problem indeed asked to output -1 if impossible. But the editorial said it's always possible. Maybe there was a misprint? Or maybe I'm misremembering. Let's quickly check AtCoder ABC 134 D statement (I'll try to recall): "There are N boxes numbered 1 to N. Each box can contain at most one ball. Determine if there exists a way to put balls into boxes such that for each i=1,...,N, the number of balls in boxes whose index is a multiple of i has parity equal to a_i. If it is impossible, print -1; otherwise, print the boxes containing balls." Something like that. And the solution: compute b_i from i=N down to 1. Yes. I think it's always possible. But why would they ask to output -1 if it's never needed? Possibly because they didn't realize or they wanted to test understanding that it's always possible? Or maybe I'm wrong and there is a case when N is large and a_i are not arbitrary? No.
Let me test with N=2 and a = (0,1) we found solution. N=2 a=(1,0): solution x2=0, x1=1. N=3 all combos? Should always work.
Perhaps there is a hidden condition: The balls are placed, and the condition must hold for each i, but we also have the limitation that each box can contain at most one ball, which we already obey. So fine.
Maybe the problem originally had that the total number of balls in boxes with multiples of i is exactly a_i (not modulo 2), but then the input a_i are 0 or 1. In that case, sometimes impossible. But they specifically say "congruent to a_i modulo 2". So it's parity.
I think it's safe to assume always possible.
Thus final answer: We'll output the solution as per algorithm.
Now, let's consider an edge case: N = 200000, a_i all 1? Let's test performance: O(N log N) loops may be okay, but we must ensure we don't exceed time if we use Python. But I'm writing C++.
Now, one more nuance: The sum_{j>i,i|j} x_j can be computed via prefix sums? Not necessary.
But we can also compute using a sieve-like method to accumulate contributions. However, the simple double loop is fine.
Now, implement:
code
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N+1);
for (int i = 1; i <= N; ++i) cin >> a[i];
vector<int> x(N+1, 0);
for (int i = N; i >= 1; --i) {
int sum = 0;
for (int j = i*2; j <= N; j += i) {
sum ^= x[j];
}
x[i] = a[i] ^ sum;
}
vector<int> ans;
for (int i = 1; i <= N; ++i) {
if (x[i]) ans.push_back(i);
}
cout << ans.size() << endl;
if (!ans.empty()) {
for (int i = 0; i < ans.size(); ++i) {
if (i) cout << " ";
cout << ans[i];
}
cout << endl;
}
return 0;
}
That should work.
But we must be careful: When N is large, the inner loop for i=1 will iterate over ~200k multiples, that's okay. Total operations as said.
Now, what about the case when a_i are given as 0/1, but maybe they are given as integers? Yes.
Now, about reading input: Could be spaces and newlines.
Now, I'd like to note that the answer always exists, so -1 never printed. But if we want to be thorough, we can after computing x, verify by recomputing all conditions to ensure they match a_i; if any mismatch, output -1. But that would be O(N log N) again and still should not happen. But okay.
Given the problem expects -1 only if impossible, and we know it's never impossible, we can just output the answer.
But maybe there is a twist: The problem might be from a contest where the solution is not always possible? Let's re-express the equations: For i = 1, sum over all x_j = a_1 mod2. For i = 2, sum over x_{2j} = a_2 mod2. For i = 3, sum over x_{3j} = a_3 mod2, etc.
Suppose we add all equations? Not needed.
Try to find a counterexample: Could there be a case where the greedy algorithm gives a solution, but the solution fails for some i because we assumed we can set x_i arbitrarily, but maybe the equation for i involves x_i but also other variables that we haven't set yet? Actually, we set x_i from equation i, but equation i also involves x_i and multiples > i. By setting x_i, we satisfy equation i. However, note that for some smaller k, equation k includes x_i as well. But when we later set x_k, we are not changing x_i, so equation k will be satisfied because we will set x_k accordingly, but does the presence of x_i in equation k cause any issue? Let's check: For k < i, equation k includes x_i because i is a multiple of k (since k < i, i may or may not be a multiple of k). For k such that k divides i, equation k includes x_i. When we later process k, we compute sum over multiples > k, which includes x_i (since i > k and k|i). So when we compute x_k, we take into account the already determined x_i. Then we set x_k to satisfy equation k. So all equations will be satisfied. This is standard back substitution. So it's consistent.
Thus always solvable.
Therefore answer always exists.
Now, provide final response: Explanation + code.
But the user might expect just the algorithm. I'll give a thorough explanation and then the code.
Now, I'll craft the answer:
We can solve by processing i from N down to 1. For each i, the condition for i involves x_i and x_j for j > i that are multiples of i. Since x_j for j > i are already decided, we can determine x_i uniquely as:
x_i = a_i ⊕ (⊕_{j: i|j, j>i} x_j)
where ⊕ denotes XOR (addition modulo 2). This works because the sum over multiples of i is x_i plus the XOR of those already known x_j.
After computing all x_i, we output the indices where x_i = 1.
The total complexity is O(N log N) and the answer always exists (the system is always solvable).
I'll provide C++ code as above.
Potential pitfalls: Make sure to use 1-indexed arrays.
Now, as a final note: For N up to 2e5, the double loop is efficient.
Thus, I will respond with explanation and code. The system of equations is always solvable. We can determine the contents of each box uniquely by processing from the largest index down to the smallest.
For each (i) from (N) down to (1):
Let (s = \bigoplus_{j > i,\ i \mid j} x_j) (XOR of already decided boxes that are multiples of (i)).
Set (x_i = a_i \oplus s).
After computing all (x_i), the boxes with (x_i = 1) form a valid configuration.
Complexity: (O(N \log N)) (each (i) iterates over its multiples).
cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> a(N + 1);
for (int i = 1; i <= N; ++i) cin >> a[i];
vector<int> x(N + 1, 0);
for (int i = N; i >= 1; --i) {
int s = 0;
for (int j = i + i; j <= N; j += i) s ^= x[j];
x[i] = a[i] ^ s;
}
vector<int> ans;
for (int i = 1; i <= N; ++i) if (x[i]) ans.push_back(i);
cout << ans.size() << '\n';
if (!ans.empty()) {
for (size_t i = 0; i < ans.size(); ++i) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
}
return 0;
}