We are given an array a of n integers. We need to make all elements equal to zero by applying exactly three operations. Operation: select a segment [l, r] (length len = r-l+1). For each index i in the segment, we can add a multiple of len to it. That is, we add b_i to a_i, and each b_i must be divisible by len. There is no restriction that b_i must be the same; they can be different multiples of len. After three operations, all a_i must be zero.
It is guaranteed that it is always possible.
We need to output exactly three operations, each with its segment bounds and the list of numbers to add (b_i). The b_i can be up to 10^18 absolute, within 64-bit signed.
We have to find a general construction for any n and any array a, using exactly three operations.
The operation is powerful: we can add arbitrary multiples of len to each element within the segment, as long as each addition is a multiple of len.
Observation: Because we can add arbitrary multiples, we can effectively change each element modulo something? Since len divides b_i, the addition's effect modulo len is 0. So each operation on a segment of length len only changes the values of those elements by multiples of len. That means that the value modulo len remains unchanged? Actually if we add a multiple of len to a_i, then a_i mod len stays the same. However, we can change the value arbitrarily as long as the adjustment is a multiple of len. So we can set each a_i to any number that is congruent to its original modulo len, by adding an appropriate multiple. But we have only three operations, and we need to zero all entries.
We need to achieve zeros from arbitrary a_i.
This problem is reminiscent of typical constructions in Codeforces problems where you use three operations to zero array. Usually, we can use: first operation to fix everything except last element; second to fix everything except first element; third to adjust both first and last? But here the operation is segment-based with length multiple constraint.
Alternatively, there's known problem: "Make it zero" with exactly three operations, where you can choose a subarray and add a multiple of its length to each element. This is from Codeforces Round #... Possibly problem "1365A" no.
Let's think. For any integer array a, we need to produce 3 steps.
Because we can add different multiples per element, the operation essentially allows us to add any vector v to the subarray, where each component of v is divisible by len. So v = len * c, where c is an integer vector (could be negative). So we are adding len * c_i to a_i for i in [l, r].
Goal: After three such operations, the final array is zero.
Let’s denote initial array a (vector). After three operations, we have:
a + len1 * c^1 (with support on segment1) + len2 * c^2 (support segment2) + len3 * c^3 (support segment3) = 0.
We can choose segments and integer coefficients c^i (one per element per operation). All c^i are integers (any, possibly huge). We need to output b_i = len_k * c_i for the chosen segment.
We must output exactly three operations; they may be in any order but we need to produce them. The operations are applied sequentially; but since we are constructing the plan, we can think backwards or design steps.
Observation: Because we can use very large numbers, we can essentially set any values we want as long as we respect the modulo condition within each step. But we have only three steps. Perhaps we can use the following strategy:
- Step 1: Choose segment [1, n-1] (length n-1). Add appropriate multiples to make all elements in that segment become zero? However, for element i in [1, n-1], after adding len1 * c_i, we want a_i + len1 * c_i = 0. That would require c_i = -a_i / len1. But a_i may not be divisible by len1. So we cannot directly set to zero because the added value must be a multiple of len1, so we can only change a_i by multiples of len1. So we can only set a_i to values that are congruent to original modulo len1. So to make it zero, we need a_i ≡ 0 (mod len1). In general, a_i mod len1 may not be 0. So we cannot set each individually to zero unless we first adjust modulo.
Thus we need to combine operations to adjust the residues.
Because we have three operations, we might be able to adjust modulo constraints to eventually zero.
We can choose segments of length 1: if len=1, then we can add any integer (since multiple of 1). So with a length-1 segment, we can set that element to any integer (by adding the appropriate number). So we could directly zero any single element in one operation. But we have only three operations; we cannot zero each individually unless we combine multiple elements in one operation. However, we can zero a segment of length 1 in each operation, but we have only three ops, so we could zero at most three elements. For n up to 100k, that's insufficient.
Therefore, we need to use longer segments to affect many elements at once.
We can think in terms of linear algebra over integers: We have three operations, each adds a vector that is supported on a segment and is a multiple of its length. The set of reachable states from initial a is a + combinations of such vectors. We need to reach 0.
Since it's always possible, there must be a constructive method.
Maybe we can use the following plan:
- Operation1: Use segment [1, n] (whole array) of length n. Add b_i such that after this, all a_i become divisible by something? Actually we can choose b_i to make a_i ≡ 0 mod n? Not exactly; we can add multiples of n, so a_i changes by multiples of n, so a_i mod n stays the same. So we cannot change a_i mod n. But we might not need that.
Another idea: Use first two operations to make the array such that every element is a multiple of n (or something). Then third operation with segment [1, n] can zero them because we can add appropriate multiples of n to each to reach zero? If each element is a multiple of n, say a_i = n * k_i, then we can add -n * k_i to each in one operation using segment [1, n] (since len=n, multiples of n allowed). That would zero all in one operation. So if we can transform the array into a state where all elements are multiples of n, using two operations, then third operation zeros it. So we need two operations to make all elements multiples of n.
But is it always possible to do that in two operations? Possibly yes by using segments of length n-1 or 1.
Let's denote we want after two operations, for all i, a_i ≡ 0 mod n. Initially we have arbitrary a_i. We have two operations, each with some segment length L1, L2, and we add multiples of L1, L2. The effect on modulo n is: For each element, the added value is a multiple of the segment length L. So the change modulo n is (L * c) mod n. So we can adjust each element by any multiple of gcd(L, n) modulo n? Actually, the set of possible changes modulo n for an element that lies in the segment is { (L * t) mod n : t ∈ Z } = multiples of d = gcd(L, n) modulo n. Because the additive subgroup generated by L in Z_n is dZ_n where d = gcd(L,n). So after operations, the final value modulo n is a_i + (some combination of multiples of d1, d2, ...) depending on which operations cover that index. Since each operation covers a set of indices, the net change for index i is sum over operations that include i of L_op * t_{i,op}. So modulo n, we have a_i + sum (L_op * t_{i,op}) mod n.
We want final a_i ≡ 0 mod n.
For each index, we have control over t_{i,op} (the integer coefficient). This is like we need to assign integers for each operation for each index covered. But note that within an operation, the coefficients for different indices are independent (we can choose b_i arbitrarily as multiples of len). So for each index i, we have two operations (if index is covered in both), we can pick integer multipliers to achieve desired adjustment modulo n. However, these multipliers are not independent across indices? They are independent per index per operation; there is no cross-index constraint. So we can treat each index separately: For each i, we need to find integers x (for operation 1 if i in seg1) and y (for operation 2 if i in seg2) such that:
a_i + L1 * x + L2 * y ≡ 0 (mod n).
If i is covered by both operations, we have two variables; if covered by only one, we have one variable; if covered by none, then a_i must already be 0 mod n. So we need to design the segments so that every index is covered by at least one operation, and we can solve the linear diophantine condition modulo n. Since we have two operations, we could cover all indices.
But we also need to be able to assign integer multipliers (x,y) that are actual integers (not just modulo n) and produce b_i values within range (but we can choose arbitrarily large, so fine). So the only constraint is existence of integer solution to a_i + L1 x + L2 y ≡ 0 mod n. This is a linear congruence in two variables.
Given L1, L2, n, we can ask: For any given a_i, does there exist integers x, y such that L1 x + L2 y ≡ -a_i (mod n)? This is possible if and only if gcd(L1, L2, n) divides -a_i mod n? Actually, the set of values that can be represented as L1 x + L2 y mod n is the additive subgroup generated by L1 and L2 in Z_n, which is dZ_n where d = gcd(L1, L2, n) maybe? More precisely, the subgroup H = { u L1 + v L2 mod n | u,v∈Z } is equal to the set of multiples of d = gcd(L1, L2, n) in Z_n. Because the subgroup generated by L1 and L2 in Z_n is generated by their gcd modulo n. Indeed, the ideal (L1, L2) in Z is (g) where g = gcd(L1, L2). In Z_n, the image of that ideal modulo n is the set of multiples of d = gcd(g, n). Because the set { L1 x + L2 y mod n } = { k*g mod n | k∈Z } = multiples of g' where g' = gcd(g, n). Since g = gcd(L1, L2), and then d = gcd(g, n) = gcd(L1, L2, n). So the condition is: a_i must be a multiple of d modulo n for there to exist x,y. But we can also have the case where index i is covered by only one operation: then we need L1 x ≡ -a_i mod n, which requires that d1 = gcd(L1, n) divides a_i. So if we cover an index with only one operation, the modulus condition is stricter. So it's better to cover each index by both operations to have more flexibility. However, we can design L1 and L2 such that gcd(L1, L2, n) = 1. Then for any a_i, we can solve L1 x + L2 y ≡ -a_i (mod n). Because if d=1, the subgroup is whole Z_n, so any residue is achievable. So with two operations covering all indices, and with lengths L1, L2 such that gcd(L1, L2, n)=1, we can adjust each element to be ≡ 0 mod n independently, by choosing appropriate multipliers per index. That seems promising.
But careful: For each index i, we need to assign integer multipliers x_i (for op1) and y_i (for op2). However, these multipliers are per operation and per index, but within a single operation, the multipliers for different indices are independent. There's no constraint that all indices in the same operation must have the same multiplier; we can choose different b_i for each index. So indeed, we can choose x_i and y_i independently for each i.
Therefore, if we pick two segments that cover all indices, and ensure gcd(L1, L2, n)=1, then we can for each i find integers x_i, y_i (maybe using extended Euclidean algorithm) such that L1 x_i + L2 y_i ≡ -a_i (mod n). Then we set b_i for op1 = L1 * x_i, and for op2 = L2 * y_i. Then after these two operations, each a_i becomes a_i + L1 x_i + L2 y_i, which is ≡ 0 mod n. But we need that exactly equals some multiple of n, not just modulo n? Actually, the congruence means there exists integer k_i such that a_i + L1 x_i + L2 y_i = n * k_i. But it might not be exactly n * k_i; it is some integer that is a multiple of n only if we enforce the congruence? Wait: L1 x_i + L2 y_i ≡ -a_i (mod n) means that a_i + L1 x_i + L2 y_i ≡ 0 mod n. So it is a multiple of n: there exists integer t_i such that a_i + L1 x_i + L2 y_i = n * t_i. So indeed, after two ops, each element is a multiple of n. So then in the third operation, we can use segment [1, n] (length n) and for each i add -n * t_i (i.e., b_i = -n * t_i) to zero them.
But we have to ensure that the b_i we output for each operation are within -10^18..10^18. Since a_i up to 1e9 and we can control multipliers, we can likely keep numbers moderate by using bounded solutions.
Now, we need to choose two segments that cover all indices and have lengths L1, L2 with gcd(L1, L2, n) = 1. Additionally, we need to ensure that these segments are valid (1 ≤ l ≤ r ≤ n). Also, we want to output exactly three operations. So we need to produce the first two operations (the covering ones) and the third operation (zeroing with full segment). However, we must be careful: The third operation uses full segment [1, n] (length n). That's allowed as long as n≥1. So overall, three operations.
But is it always possible to choose two segments that cover all indices and have lengths with gcd(L1, L2, n) = 1? We can choose segments arbitrarily. For any n, we need to find two lengths L1, L2 from 1..n such that the segments cover all indices and gcd(L1, L2, n) = 1. Also each segment must be contiguous.
We can choose: Operation1: segment [1, n-1] length L1 = n-1. Operation2: segment [2, n] length L2 = n-1 if n>1? Actually [2,n] length = n-1. Both lengths = n-1. Then gcd(L1, L2, n) = gcd(n-1, n) = gcd(n-1, n) = 1 always because gcd(n, n-1)=1. So that works! Also, these two segments cover all indices? [1, n-1] covers indices 1 to n-1. [2, n] covers indices 2 to n. The union is all indices 1..n, because index 1 is covered only by first, index n only by second, and indices 2..n-1 by both. So indeed cover all.
But note: For n=1, segment [1, n-1] would be [1,0] invalid. So we need to handle n=1 separately. n=1 is trivial: we can zero the single element in one operation with segment [1,1] adding -a1 (since len=1). But we need exactly three operations. So we might need to output three operations (maybe some dummy operations?). The problem statement says we want to make all elements zero by doing the following operation exactly three times. So for n=1, we still need three operations. But we can have operations that do nothing? Possibly we can do operations with segment that doesn't change anything (add 0). Or we can use same segment multiple times. Since we can add multiples of len, we could add 0 to keep values unchanged. So we can do: first op: add 0, second op: add 0, third op: add -a1 to zero. That works, as long as we output valid segments and b_i. We'll handle n=1 as special case.
For n>=2, the plan is:
-
Operation 1: segment [1, n-1] (length n-1). We will add values b_i = (n-1) * x_i for each i in [1, n-1], where x_i are integers chosen such that after two ops, each a_i becomes multiple of n.
-
Operation 2: segment [2, n] (length n-1). Add values b_i = (n-1) * y_i for i in [2, n].
-
Operation 3: segment [1, n] (length n). Add values b_i = -n * t_i, where t_i is the integer such that after op1 and op2, a_i' = n * t_i.
Goal: Choose x_i (i=1..n-1) and y_i (i=2..n) such that for each i:
If i = 1: only op1 applies. Then after op1 and op2, a_1' = a_1 + (n-1)*x_1 (since y_1 doesn't exist). We need a_1' ≡ 0 mod n. So condition: a_1 + (n-1) * x_1 ≡ 0 mod n.
If i = n: only op2 applies. Then a_n' = a_n + (n-1)*y_n (since x_n doesn't exist). Condition: a_n + (n-1)*y_n ≡ 0 mod n.
For i = 2..n-1: both op1 and op2 apply. So a_i' = a_i + (n-1)*x_i + (n-1)*y_i? Wait careful: For i in [2, n-1], it is covered by both segments. So we have contributions: from op1: (n-1)x_i (since segment1 length L1 = n-1). From op2: (n-1)y_i. So total: a_i + (n-1)(x_i + y_i). Condition: a_i + (n-1)(x_i + y_i) ≡ 0 mod n.
We have flexibility: for interior indices, we can define z_i = x_i + y_i, and we need to choose integers x_i, y_i such that we can assign both, but we only care about sum for mod condition. But we also need to output actual b_i for each operation, so we need separate x_i, y_i. However, we can just set y_i = 0 and choose x_i such that condition holds for interior? But careful: For interior i, if we set y_i=0, then condition becomes a_i + (n-1)*x_i ≡ 0 mod n. This is a linear congruence in x_i. Since gcd(n-1, n) = 1, there is a unique solution modulo n for x_i. So we can find integer x_i such that (n-1)*x_i ≡ -a_i (mod n). Because gcd=1, it is always solvable. Then we can set x_i accordingly, and y_i = 0. That works, as long as we also handle edges similarly: for i=1, we solve (n-1)*x_1 ≡ -a_1 (mod n) and set y_1 nonexistent; for i=n, we solve (n-1)*y_n ≡ -a_n (mod n) and set x_n nonexistent.
But wait: For interior i, if we set y_i=0, then we use only op1 to adjust, leaving op2's addition as 0. That's fine. So essentially, we can accomplish the mod n condition using only the first operation for all indices except maybe n, and only second for n? But we need to cover index n; we only have op2 covering n. So we need to solve for y_n. That's also possible because gcd(n-1,n)=1. So we can adjust each element individually using only the operation that covers it? However, index 1 is only covered by op1, index n only by op2, others could be done by either op1 or op2 or both. Since gcd(n-1,n)=1, each congruence (n-1)*x ≡ -a_i (mod n) has a solution for x modulo n. So we could simply:
-
For i in [1, n-1], choose x_i such that (n-1)*x_i ≡ -a_i (mod n). Then after op1, for i=1..n-1, a_i + (n-1)*x_i ≡ 0 mod n. But note for i from 2..n-1, we also have op2 later; but if we also add something from op2, it would disturb the modulo condition. So we must set y_i = 0 for i=2..n-1. That's okay.
-
For i = n, we choose y_n such that (n-1)*y_n ≡ -a_n (mod n). Then after op2, a_n + (n-1)*y_n ≡ 0 mod n.
But wait: After op1, we have added (n-1)*x_i to indices 1..n-1. Then after op2, we add (n-1)*y_i to indices 2..n. For i in 2..n-1, if we set y_i = 0, then they get only the addition from op1. Good. For i=1, no op2 addition. For i=n, only op2 addition.
After these two ops, all a_i are multiples of n. However, we must check that for i=2..n-1, the addition from op1 already makes a_i multiple of n; we add nothing from op2, so it remains multiple of n. For i=1, same. For i=n, we add only from op2.
Thus we can achieve the condition using only one operation per element (except we need both operations to exist, but they can have zero contributions for many indices). So the plan simplifies: In op1, set b_i for i=1..n-1 such that a_i + b_i ≡ 0 (mod n) and b_i is multiple of (n-1). In op2, set b_n such that a_n + b_n ≡ 0 (mod n) and b_n is multiple of (n-1), and set b_i=0 for i=2..n-1 (or any but must be multiple of (n-1); zero is fine). Then after two ops, all values are multiples of n. Then op3 zero them with whole array.
But is it always possible to find integer x_i such that (n-1)*x_i ≡ -a_i (mod n) and that the resulting b_i = (n-1)*x_i is within allowed range? Since a_i up to 1e9, n up to 1e5, we can find x_i modulo n. Specifically, since gcd(n-1, n)=1, the modular inverse of (n-1) modulo n exists. Let inv = (n-1)^{-1} mod n. Then we can set x_i = (-a_i * inv) mod n. That gives x_i in [0, n-1]. Then b_i = (n-1) * x_i. But is a_i + b_i guaranteed to be a multiple of n? By construction modulo n, yes: a_i + (n-1)*x_i ≡ 0 mod n. However, a_i + (n-1)*x_i may not be exactly 0; it is some integer that is a multiple of n. But could it be that a_i + (n-1)*x_i is not exactly n * something, but some multiple? It is, because it's ≡0 mod n. So we can compute t_i = (a_i + (n-1)*x_i) / n. But we have to ensure that this division yields integer; yes because it's multiple of n. But note: a_i could be negative; still works because modulo arithmetic works for negative numbers if we adjust properly.
But wait: For i=1..n-1, we are adding (n-1)*x_i. However, after op2, we may also add something for i in 2..n-1 if we set non-zero y_i; we set them zero, so fine.
But there is a subtlety: After op1, a_i for i=1..n-1 become a_i' = a_i + (n-1)*x_i. After op2, for i=n, a_n' = a_n + (n-1)*y_n. But what about i=2..n-1 after op2? They have no addition (y_i=0). So they remain as a_i'. Since a_i' is multiple of n, okay. For i=1, also remains. So after two ops, all entries are multiples of n.
But then op3: we need to subtract exactly the value of each entry to zero. Since each a_i'' is some integer multiple of n, we can set b_i^{(3)} = -a_i''. That is a multiple of n? Yes because a_i'' is multiple of n, so -a_i'' is divisible by n. So op3 uses segment [1,n] with length n, and we output b_i = -a_i''.
Now the question: Are the numbers within -10^18..10^18? Let's estimate magnitudes. a_i up to 1e9. x_i is chosen mod n, so 0 <= x_i <= n-1 (if we take positive remainder). So (n-1)x_i <= (n-1)(n-1) ≈ 1e10 for n=1e5 -> 1e10, which is within 1e18. Then a_i' = a_i + (n-1)x_i could be up to 1e9 + 1e10 ≈ 1.1e10, still fine. Then t_i = a_i' / n up to ~ 1.1e10 / 1e5 = 1.1e5? Actually division by n yields up to about 1e5, because a_i' is about n^2? Wait n up to 100,000, (n-1)x_i up to (1e5)(1e5)=1e10, plus a_i 1e9 gives ~1.1e10. Divided by n=1e5 gives up to 1.1e5. So a_i' = n * t_i, with t_i up to ~1e5. Then op3's b_i = -nt_i, magnitude up to n * t_i ≈ 1e5 * 1e5 = 1e10. So all numbers within 1e10, well below 1e18. So safe.
But there is a nuance: For interior indices we are using only op1 to adjust, leaving op2 zero. However, op2's segment is [2,n], so we must output b_i for i=2..n. For i=2..n-1 we output 0, which is divisible by len (n-1) because 0 is divisible by any number. For i=n, we output (n-1)*y_n. That's fine. Similarly, op1's b_i for i=1..n-1: for i=1..n-1 we output (n-1)*x_i. That includes indices 2..n-1 as well.
Now, note: For i=2..n-1, we have both op1 and op2 contributions. After op1, we added (n-1)*x_i. After op2, we add 0. That's fine.
But there's a potential issue: For i=1, we added (n-1)*x_1. For i=n, we added (n-1)*y_n. So indeed after two ops, all elements are multiples of n.
But is it necessary that we used only one operation per element? Yes, but we need to ensure that the modulo condition is solved correctly: For interior indices, if we set y_i=0, then the condition is a_i + (n-1)*x_i ≡ 0 mod n. Since gcd(n-1,n)=1, we can always find x_i mod n. So works.
So algorithm for n>=2:
-
Compute inv = modular inverse of (n-1) modulo n. Because n and n-1 are coprime, inv = something. But we can compute x_i as ( (-a_i) * inv ) mod n, giving a residue between 0 and n-1. However, note that (-a_i) mod n might be negative, but we can compute (n - (a_i % n)) % n etc. But careful: The congruence is (n-1)*x ≡ -a_i (mod n). Multiply both sides by inv: x ≡ -a_i * inv (mod n). So x_i can be taken as ( (-a_i % n) * inv ) % n. Since we want x_i integer, we can compute as ((n - (a_i % n)) % n) * inv % n. But if a_i negative, a_i % n yields non-negative remainder (in programming, careful with modulo for negative numbers). We can use Python's modulo which yields non-negative. We'll handle properly.
-
For i=1..n-1: let x = the solution modulo n, we can take the smallest non-negative solution: x_i = ( (-a_i) mod n ) * inv mod n. Then b1_i = (n-1) * x_i.
But wait: We need to ensure that after adding b1_i, a_i becomes exactly multiple of n. Let's compute a_i' = a_i + (n-1)*x_i. Since we chose x_i such that (n-1)*x_i ≡ -a_i (mod n), then a_i' ≡ 0 mod n. But is a_i' guaranteed to be exactly divisible by n? Yes, because congruence means a_i' = a_i + (n-1)*x_i = n * k for some integer k, possibly negative. But we must check that k is integer; it will be, but we need to compute k later for op3. However, there is a subtlety: x_i computed modulo n ensures that (n-1)*x_i is congruent to -a_i modulo n, but not necessarily equal to -a_i. So a_i' = a_i + (n-1)*x_i might not be exactly zero but some multiple of n. That's fine.
But is there any risk that a_i' could be not exactly a multiple of n due to the fact that we used modular arithmetic? No: a_i + (n-1)*x_i is an integer. By construction (n-1)*x_i ≡ -a_i (mod n) implies that the difference between a_i + (n-1)*x_i and 0 is a multiple of n. So a_i' is a multiple of n. So we can compute t_i = (a_i' ) // n.
But careful: For i=1..n-1, after op1 we add (n-1)*x_i, then after op2 we add maybe nothing. So final after two ops is a_i' (for i=1..n-1) and for i=n, we compute y_n similarly.
Now we must compute the values after op1 and op2 sequentially to compute op3's b_i. But note: op2 may affect indices 2..n-1, but we set y_i=0. So final value for interior indices remains a_i' from op1. For index n, final value is a_n' = a_n + (n-1)*y_n.
But there is also index 1, final a_1' = a_1 + (n-1)*x_1.
Thus all final values are multiples of n.
So op3: b_i = - (final value) for all i.
Now, we have to output exactly three operations. The order is as we apply: first operation (the one we output first) will be applied first. So we must output op1, op2, op3? But we could also reorder; but we need to follow the plan where after first two ops we get multiples of n, then third zeros. So the order matters: The third operation must be after the first two. So output: first line "1 n-1"? Actually we need to output the operations in the order they are performed. So we will output operation1: segment [1, n-1] with b list; operation2: segment [2, n] with b list; operation3: segment [1, n] with b list.
But is there any requirement that the operations be distinct? No.
Now, we must consider the case n=1. For n=1, lengths: n-1 = 0 invalid. So we need a different construction. Since we have three operations exactly, we can:
- Operation1: segment [1,1] (len=1). Add 0. (b=0)
- Operation2: segment [1,1] (len=1). Add 0.
- Operation3: segment [1,1] (len=1). Add -a1.
That would make array zero after third operation. But is that allowed? The operation description: "Select a segment, for each number in this segment we can add a multiple of len to it". For len=1, any integer is multiple of 1. So adding 0 is allowed. So yes.
But we could also combine to use different segments? There is only one index.
Thus solution seems straightforward.
But is there any hidden catch? We must ensure that after first two ops, the values are multiples of n. For n>=2, using the described method works because gcd(n-1, n)=1. However, what about the case n=2? Then n-1 = 1. Segment [1,1] length 1 and segment [2,2] length 1. Both lengths are 1. gcd(1,1,2)=1. Works. For i=1: need to solve 1x ≡ -a1 mod 2 => x ≡ -a1 mod 2. So x = (-a1) mod 2, either 0 or 1. Then b1 = 1x. So a1' = a1 + x. Is a1' even? a1 + x ≡ 0 mod 2? Yes. For i=2: similarly y. Then after two ops, both numbers even. Then op3 with segment [1,2] length 2, we add -a_i' which are even, so multiples of 2. Good.
Edge Cases: n=2, the segments are [1,1] and [2,2]; they are length 1, not n-1 =1. That's fine.
But we said earlier segment [1, n-1] for n=2 would be [1,1] yes. And [2,n] = [2,2]. So it's consistent.
Now test with given example:
n=4, a=[1,3,2,4].
Compute inv of (n-1)=3 mod 4. 3 mod4, inverse is 3 because 3*3=9≡1 mod4. So inv=3.
For i=1: a1=1. (-a1 mod4)=(-1 mod4)=3. x1 = 3inv mod4 = 33=9 mod4 = 1. So b1 for op1: (n-1)x1 = 31=3? Wait (n-1)=3, so b1=3? But example output first operation is "1 1" and "-1". That doesn't match. Hmm maybe they used a different construction. Let's compute our method:
n=4, op1: segment [1,3] (since n-1=3). For i=1,2,3.
Compute x_i:
i=1: a1=1 -> (-1 mod4)=3 -> times inv=3 -> 9 mod4=1 -> x1=1 -> b1=31=3.
i=2: a2=3 -> (-3 mod4)=1 -> times inv=3 -> 3 mod4=3 -> x2=3 -> b2=33=9.
i=3: a3=2 -> (-2 mod4)=2 -> times inv=3 -> 6 mod4=2 -> x3=2 -> b3=3*2=6.
So op1 would add [3,9,6] to indices 1,2,3.
op2: segment [2,4] length 3. For i=2,3,4 we set y_i=0 for i=2,3 and compute y_n for i=4.
i=4: a4=4 -> (-4 mod4)=0 -> times inv=3 -> 0 -> y4=0 -> b4=3*0=0.
So op2 adds [0,0,0] to indices 2,3,4? Actually we need to output b for indices 2,3,4: b2=0, b3=0, b4=0.
After op1: a becomes [1+3=4, 3+9=12, 2+6=8, 4].
After op2: unchanged: [4,12,8,4].
All are multiples of 4? 4 yes, 12 yes, 8 yes, 4 yes. Then op3: segment [1,4] length 4, add [-4, -12, -8, -4] to zero. So the operations would be:
1 3
3 9 6
2 4
0 0 0
1 4
-4 -12 -8 -4
But the example output is different:
1 1
-1
3 4
4 2
2 4
-3 -6 -6
So they used a different approach. However, our method is valid; it meets the constraints. The problem statement says "It can be proven that it is always possible." and we just need to output any valid sequence. So our output doesn't have to match the example. However, we need to ensure that the numbers we output are within allowed range and that the operations are exactly three. Our numbers (3,9,6 etc.) are within 1e18, and segments valid. So it's fine.
But we should check that we are not missing something: The operation definition says: "Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different)." So we must ensure that the b_i we output are divisible by len. In op1, len=3, b_i: 3,9,6 are all divisible by 3. Good. In op2, len=3, b_i: 0,0,0 divisible by 3. In op3, len=4, b_i: -4,-12,-8,-4 divisible by 4? -4/4=-1, -12/4=-3, -8/4=-2, -4/4=-1 yes.
Thus our construction works.
But is there any issue with negative a_i? Suppose a_i negative, the modulo operation might produce x_i non-negative. a_i' = a_i + (n-1)*x_i could be negative but still multiple of n. Then op3 adds negative of that to zero out. That's fine.
But we must verify that after op1 and op2, the numbers indeed become multiples of n, not just modulo n. Since we choose x_i from modular equation, we guarantee congruence. However, due to the fact that (n-1)*x_i is not necessarily exactly -a_i mod n? Wait, congruence means (n-1)x_i = -a_i + nk for some integer k. Then a_i + (n-1)x_i = nk. So it's exactly a multiple of n. So yes.
But we need to compute x_i such that the resulting multiple is exactly n*k; but we don't need to know k. However, we need to compute the final values for op3, which are a_i + (n-1)*x_i for i=1..n-1 and a_n + (n-1)*y_n for i=n. Since we used modular inverse to find x_i modulo n, we get x_i = some value between 0 and n-1. Then (n-1)x_i could be as large as (n-1)(n-1) ≈ n^2. a_i is up to 1e9, which could be larger than n^2? n up to 100k, n^2=1e10, so a_i could be up to 1e9, which is less than n^2 but maybe comparable. But if a_i is huge positive (1e9) and (n-1)*x_i is about n^2 (1e10), the sum is about 1.1e10. That's okay. But what if a_i is huge negative, say -1e9? Then a_i + (n-1)*x_i might be negative but still multiple of n. Should be fine.
But we must ensure that the computed x_i (0 ≤ x_i < n) yields a_i + (n-1)x_i being divisible by n. Let's test: a_i = 1, n=4, n-1=3, x_i=1 yields a_i+3=4 divisible by 4. Good. a_i = -1, n=4. Compute (-a_i mod n) = (-(-1) mod4?) Actually we need (-a_i) mod n. If a_i=-1, then -a_i = 1, mod4 =1. Then x_i = 1inv=3? Actually we need x_i such that 3x ≡ -(-1)=1 mod4. The solution is x ≡ 3 mod4 because 33=9≡1. So x_i=3. Then a_i + 3*3 = -1+9=8 divisible by 4. So works.
Now consider a_i = 2, n=4, we got x=2, a_i+3*2=2+6=8 divisible. So good.
Now consider a_i = 4, n=4. (-4 mod4)=0, x=0, a_i+0=4 divisible.
Thus correct.
Now, does this method require the computation of modular inverse? Since n and n-1 are coprime, the inverse of (n-1) mod n is actually something like: inv = n-1? Because (n-1)(n-1) = n^2 -2n +1 ≡ 1 mod n? Because n^2 ≡0 mod n, -2n≡0, so (n-1)^2 ≡ 1 mod n. Actually (n-1)^2 = n^2 -2n +1 ≡ 1 (mod n). So the inverse of (n-1) modulo n is (n-1) itself! Because (n-1)(n-1) ≡ 1 mod n. So inv = n-1 mod n. Indeed, (n-1)*(n-1) = (n-1)^2 = n^2 - 2n + 1 ≡ 1 mod n. So the modular inverse of n-1 modulo n is n-1. This simplifies computation: x_i = ( (-a_i) mod n ) * (n-1) mod n. But careful: Since we multiply by (n-1) modulo n, which is just -1 mod n? Actually (n-1) ≡ -1 mod n. So x_i = ( (-a_i) mod n ) * (-1) mod n = - (-a_i) mod n? Let's compute: ( -a_i mod n ) * (n-1) mod n = ( -a_i mod n ) * (-1) mod n = - ( -a_i mod n ) mod n = a_i mod n? Actually, let's do properly: Let r = (-a_i) mod n, meaning r ∈ [0,n-1] such that r ≡ -a_i (mod n). Then we want x_i = r * (n-1) mod n. Since (n-1) ≡ -1 (mod n), r * (-1) ≡ -r mod n. And -r mod n ≡ (n - r) mod n. But r ≡ -a_i, so -r ≡ a_i mod n. So x_i ≡ a_i (mod n). That is, x_i = a_i mod n (the non-negative remainder). Let's verify: a_i=1, n=4: a_i mod4 =1, so x_i=1. That matches earlier? For a1=1 we got x1=1. For a2=3, a_i mod4=3, x=3? earlier we got x2=3. For a3=2, x=2. For a4=4, a_i mod4=0, x=0. So indeed x_i = a_i mod n works! Let's check the condition: (n-1)x_i ≡? (n-1)(a_i mod n) mod n = -(a_i mod n) mod n? Actually (n-1) ≡ -1, so (n-1)*x_i ≡ -x_i ≡ -(a_i mod n) mod n. But we need (n-1)*x_i ≡ -a_i mod n. Since a_i ≡ (a_i mod n) mod n, -a_i ≡ -(a_i mod n) mod n. So yes, condition holds. So we can simply set x_i = a_i mod n (i.e., the remainder when dividing a_i by n, in range [0, n-1]). Then (n-1)*x_i = (n-1) * (a_i mod n). Let's test: a_i = 1, mod=1, (n-1)*1=3, a_i+3=4 multiple of 4. Good. For a_i=3, mod=3, (n-1)3=9, 3+9=12 multiple of 4. For a_i=2, mod=2, 32=6, 2+6=8 multiple of 4. For a_i=4, mod=0, addition 0, 4 multiple of 4.
So indeed a very simple formula: For any integer a, (a mod n) is the remainder r in 0..n-1, and we have a + (n-1)r = a + (n-1)r. Is that always divisible by n? Let's check algebraically: Write a = qn + r, with 0 ≤ r < n. Then a + (n-1)r = qn + r + (n-1)r = qn + nr = n*(q + r). Indeed! Because r + (n-1)r = nr. So a + (n-1)r = n(q + r). Perfect! So it's exactly n*(q + r). That's a beautiful identity. So indeed, if we take r = a_i mod n (the non-negative remainder), then adding (n-1)r to a_i yields a multiple of n, specifically n(q_i + r_i). That is a nice simplification.
Similarly for a_n we would add (n-1)*r_n (with r_n = a_n mod n) to make it multiple of n? But careful: For a_n we are adding via op2 (segment [2,n]) which also uses length n-1. So we would set y_n = r_n (a_n mod n) and b_n = (n-1)*r_n. Then a_n + (n-1)r_n = n(q_n + r_n). So yes.
But what about interior indices? If we use only op1, we set x_i = a_i mod n for i=1..n-1. That yields a_i' = n*(q_i + r_i). After op2, we add nothing for i=2..n-1. So they remain multiples of n.
Now note: For i=2..n-1, we used x_i = a_i mod n. However, after op1, they become n*(q_i + r_i). But is that the final value before op3? Yes.
But we also have op2 covering indices 2..n-1. If we set their b_i = 0, fine.
But wait: For index n, we used op2 with y_n = a_n mod n. So final for n: n*(q_n + r_n).
Thus all good.
But we need to be careful: In our earlier derivation, we used x_i from modular equation, which gave x_i = a_i mod n. That is neat and avoids modular inverse.
Now compute final array after two ops: For i, value = n * (q_i + r_i) where a_i = q_i*n + r_i, with 0 ≤ r_i < n.
Then op3: add -n*(q_i + r_i) to zero.
Thus the b_i for op1: for i=1..n-1, b_i = (n-1)*r_i.
For op2: for i=2..n, b_i = 0 for i=2..n-1, and for i=n, b_n = (n-1)r_n.
For op3: for i=1..n, b_i = -n(q_i + r_i).
But wait: For op2, we could also set b_i for i=2..n-1 as something else if we wanted, but zero is fine.
But we must ensure that the segment [2,n] has length n-1, and we output exactly n-1 numbers: for indices 2,3,...,n. So we need to list them in order. For indices 2..n-1, we output 0, and for index n output (n-1)*r_n.
But note: For i=2..n-1, r_i is not necessarily zero, but we are not adding anything. That's fine because we already adjusted them via op1. However, we must be careful: After op1, we added (n-1)*r_i to index i (i=1..n-1). That includes i=2..n-1. So after op1, those indices are already multiples of n. Then op2 adds nothing. So final is as computed.
But wait, is there any risk that op2 might disturb the modulo condition if we set b_i to 0? No, adding zero does not change.
Thus the construction is extremely simple:
For n >= 2:
- Compute for each i: q_i = a_i // n (integer division rounding towards zero? Actually need to handle negative a_i carefully because remainder definition for negative numbers: we need r_i in [0, n-1] such that a_i = q_i * n + r_i. For negative a_i, integer division in most languages truncates towards zero (or floor?). But we need the standard Euclidean remainder: 0 ≤ r < n. In mathematics, we can write a_i = n * floor(a_i / n) + r with 0 ≤ r < n if we use floor division for negative numbers. In Python, // does floor division for negative numbers? Actually in Python, // does floor division when used with negative numbers? Wait: In Python, the // operator does floor division (i.e., rounds down to -infinity) for integers. So a // n gives floor(a/n). Then a % n gives remainder 0 ≤ r < n. That matches. In C++ we have to be careful because % yields remainder with sign of dividend, but we can adjust. Since we are implementing solution (in Python likely) we can use Python's divmod which yields (quotient, remainder) with remainder in [0, n-1] for positive n.
But we must be precise: We need r_i = a_i mod n, where mod yields remainder between 0 and n-1. So we can compute r_i = ((a_i % n) + n) % n if necessary, but Python's % already yields non-negative for positive modulus.
Now q_i = (a_i - r_i) // n, but we can compute directly q_i = a_i // n if we use floor division; but for negative a_i, floor division yields the floor, which is exactly the q we need because a_i = q_i*n + r_i with 0 ≤ r_i < n. So q_i = a_i // n in Python works.
Then after op1 and op2, value = n*(q_i + r_i) for i=1..n-1? For i=n, also n*(q_n + r_n). For i=1..n-1, we added (n-1)r_i, so value = a_i + (n-1)r_i = nq_i + r_i + (n-1)r_i = nq_i + nr_i = n*(q_i + r_i). For i=n, value = a_n + (n-1)r_n = n(q_n + r_n). So uniform formula for all i? For i=1, we did not add anything from op2, but we did add from op1: yes, same. So after two ops, all i have value = n*(q_i + r_i). However, for i=1, we did not add op2, so value = n*(q_1 + r_1). For i=2..n-1, same. For i=n, same.
Thus final values are consistent.
Now op3: b_i = -n*(q_i + r_i). Since q_i + r_i may be up to about (|a_i|/n + n). For a_i up to 1e9, q_i up to about 1e9/n ≈ 1e4, plus r_i up to n-1 ≈ 1e5, sum up to ~1.1e5, times n up to 1e5 gives ~1e10, within 1e18.
All good.
Now we need to output operations.
Edge Cases: n=1. Then we cannot use n-1 segment. So handle separately.
For n=1: We need three operations. We can do:
Operation1: [1,1] add 0.
Operation2: [1,1] add 0.
Operation3: [1,1] add -a1.
But we must ensure that after three ops, array becomes zero. After first two, no change; after third, zero. So valid.
But could there be simpler? It's fine.
Now check if n=2: n-1=1. Then op1: segment [1,1] (len=1). For i=1, r1 = a1 mod 2. b1 = 1 * r1. op2: segment [2,2] (len=1). For i=2, b2 = 1 * r2. op3: segment [1,2] (len=2). b_i = -2*(q_i + r_i). This works.
But note that op1 and op2 both have length 1, which is fine.
Now consider n=3: n-1=2. op1: [1,2] len=2. For i=1,2, b_i = 2r_i. op2: [2,3] len=2. For i=2,3: we set b2=0, b3=2r3. op3: [1,3] len=3, b_i = -3*(q_i+r_i). Should work.
But note: For i=2, after op1 we added 2*r2, making it multiple of 3. Then op2 adds 0, so still multiple of 3. Good.
Now verify with example using this simplified approach:
n=4, a=[1,3,2,4]
Compute r's: a1=1 => r1=1, b1 for op1 = 3*1=3? Wait n-1=3, so b1=3. a2=3 => r2=3, b2=9; a3=2 => r3=2, b3=6. So op1: [1,3] with [3,9,6] (same as before). Op2: [2,4] with b2=0, b3=0, b4? a4=4 => r4=0, b4=0. So op2: [2,4] with [0,0,0]. Op3: compute q+r: For each, q = a_i // n (floor division). Let's compute:
a1=1 => q1 = 1//4 = 0, r1=1 => q1+r1=1 => -4*1 = -4.
a2=3 => q2=0, r2=3 => sum=3 => -12.
a3=2 => q3=0, r3=2 => sum=2 => -8.
a4=4 => q4=1? Actually 4//4=1, r4=0 => sum=1 => -4.
So op3: [1,4] with [-4,-12,-8,-4].
That matches earlier.
Now this is valid.
But we must ensure that the output format: For each operation, we print two lines: first line l r, second line the b values separated by spaces. There should be exactly three operations.
Potential pitfalls:
-
For op2, we need to output exactly (r-l+1) numbers, in order from l to r. For [2,n], that is n-1 numbers. We set b2..b_{n-1}=0, and b_n = (n-1)*r_n. So we can construct list: for j in range(2, n+1): if j == n: b = (n-1)*r_n else b = 0. But careful: For n=2, segment [2,2] length 1, we output b2 = (n-1)r2 = 1r2, not 0. That's fine.
-
For op1, segment [1, n-1] length n-1. For j in 1..n-1: b_j = (n-1)*r_j.
But what about r_j for j=1..n-1? r_j is computed as a_j mod n.
- For op3, segment [1,n] length n: b_j = -n*(q_j + r_j) for all j.
Now, note that for n=2, op1 is [1,1], op2 is [2,2], op3 [1,2].
Now, check if any b_i could be out of [-10^18, 10^18]? As argued, q_i + r_i ≤ (|a_i|/n + n). With |a_i| ≤ 1e9, n ≤ 1e5, so worst-case |a_i| = 1e9, n=1 => But n=1 is separate; for n>=2, q_i + r_i ≤ 1e9/2 + 2? Actually worst-case n=2, a_i = 1e9, then q_i = floor(1e9/2)=500000000, r_i = 0 or 1? If a_i = 1e9, 1e9 mod 2 = 0 or 1? 1e9 is even? 1,000,000,000 is even? 1e9 = 1000000000, which is divisible by 2? 1000000000/2=500000000, remainder 0. So q=500000000, r=0, sum=500000000. Times n=2 gives 1e9, within 1e18. For negative a_i = -1e9, in Python floor division: -1e9 // 2 = -500000000? Actually -1e9/2 = -500000000.0, floor is -500000000? Wait floor of -500000000.0 is -500000000, but if remainder must be non-negative, we need to compute r = a % n. For a = -1e9, n=2, a % 2 in Python yields? -1e9 % 2 = 0? Because -1e9 is even, so remainder 0. So q = (a - r)/n = -1e9/2 = -500000000, sum q+r = -500000000, times n = -1e9, absolute 1e9. So fine.
But what about n large, say n=100000, a_i = -1e9. Then q = floor division: -1e9 // 100000 = -10000? Actually 1e9/1e5 = 10000, so -1e9 // 100000 = -10000 (since floor of -10000 is -10000). r = a % n = (-1e9) % 100000 =? -1e9 mod 100000: Since -1e9 = -10000100000 + 0? Actually -10000100000 = -1e9 exactly, so remainder 0. So q+r = -10000. Times n = -1e9. So fine.
Worst-case magnitude: Suppose a_i is close to 1e9, n is small (like 2), then q+r up to 5e8, times n=2 gives up to 1e9. For n larger, q smaller but r up to n-1 ≈ 1e5, times n gives up to 1e10. So well within 1e18.
So our construction is safe.
Now, we should also consider that the problem might require that we don't use zero-length segments? Already handled.
Now, confirm that the operation's condition "b_i should be divisible by r-l+1" is satisfied: For op1, len = n-1, b_i = (n-1)*r_i, so divisible. For op2, len = n-1, b_i = 0 or (n-1)r_n, both divisible. For op3, len = n, b_i = -n(q_i+r_i), divisible by n.
Thus solution is valid.
But is there any scenario where n=2 and op2 segment [2,2] is length 1, and we set b2 = (n-1)r2 = 1r2, divisible by 1. Good.
Now, we need to produce exactly three operations. For n=1, we cannot use n-1. So we need a separate plan. But perhaps we can still use the same idea with n-1=0? Not possible. So handle n=1 separately.
Alternatively, we could use a different construction for n=1: Use three operations, each with segment [1,1] and add 0,0,-a1. That's fine.
But check if there is any requirement that the operations must be distinct? Not mentioned.
Now, potential issues: For n=2, op1 segment [1,1] and op2 segment [2,2] and op3 segment [1,2] is three ops. Works.
Now, what about n=0? Not possible.
Thus solution seems trivial.
But wait: The problem statement says: "Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different)." So we need to add numbers, not necessarily the same multiple. That's what we do.
Now, we must output exactly six lines (three operations, each two lines). The output format in the example shows a space after l and r? Actually example:
1 1
-1
3 4
4 2
2 4
-3 -6 -6
There's a space after "1 1 " maybe they printed a trailing space. But we can output without extra spaces.
Now, implement in code.
Implementation steps:
- Read n.
- Read array a (list of ints).
- If n == 1:
- Output three operations:
- "1 1" then "0"
- "1 1" then "0"
- "1 1" then f"{-a[0]}" (or just -a[0])
- Done.
- Else (n >= 2):
- Compute remainders r_i = a_i % n (with mod yielding non-negative). In Python, r_i = a[i] % n.
- Compute q_i = (a[i] - r_i) // n? Actually a[i] // n in Python gives floor division, which for negative numbers yields floor. That matches because r = a % n ensures a = nfloor(a/n) + r. So q = (a[i] - r_i) // n is simpler: q = a[i] // n. Because if a is negative, // gives floor, which matches. So we can compute q_i = a[i] // n (since a = nq + r). But careful: For negative numbers, e.g., a=-3, n=4: in Python -3//4 = -1, and -3%4 = 1, indeed -3 = 4*(-1) + 1. So q = -1, r=1. So using // works.
- Compute for each i: final_mult = q_i + r_i. (This is integer, could be negative if q negative and r positive? Actually r is non-negative, q may be negative. Example: a=-3,n=4 => q=-1, r=1 => sum=0. That's fine.)
- Prepare op1:
- l=1, r=n-1
- b list: for i from 0 to n-2 (0-indexed): b = (n-1) * r_i. Append to list.
- Prepare op2:
- l=2, r=n
- b list: for i from 1 to n-1 (0-indexed indices 1..n-1): if i == n-1 (last index): b = (n-1) * r_{n-1} (i.e., r_n). else: b = 0.
- Prepare op3:
- l=1, r=n
- b list: for i from 0 to n-1: b = -n * (q_i + r_i). (Note: q_i + r_i = final_mult)
- Print each operation: first line f"{l} {r}", second line the b values separated by space. Ensure no trailing spaces.
But we need to be careful about the order: Should we output op1, op2, op3 exactly in that order? Yes.
Now, double-check for n=2:
n=2, a=[a0, a1].
r = [a0%2, a1%2]
q = [a0//2, a1//2]
op1: [1,1] => b0 = (1)r0 = r0.
op2: [2,2] => b list: index 1 only, b1 = (1)r1.
op3: [1,2] => b0 = -2(q0+r0), b1 = -2(q1+r1).
Test with a=[1,2]: r0=1, r1=0; q0=0, q1=1? 2//2=1, r1=0. So op1: add 1 to a0 => a0 becomes 2. op2: add 0 to a1 => remains 2. Now both are 2. op3: add -2*(0+1)= -2 to a0? Actually q0+r0=1 -> -2; a0 becomes 0. a1: q1+r1=1 -> -2; a1 becomes 0. Works.
Now test with negative numbers: n=3, a=[-1, -2, -3]. Compute r: In Python, -1%3=2, -2%3=1, -3%3=0. q: -1//3 = -1 (since floor division: -1/3 ≈ -0.333 floor -1), -2//3 = -1? Actually -2//3 = -1 because floor of -0.666 is -1, remainder 1, okay. -3//3 = -1? Wait -3//3 = -1? Actually -3/3 = -1 exactly, floor is -1, but remainder would be 0. That's correct: -3 = 3*(-1) + 0. So q = -1, r=0.
Now op1: [1,2] len=2, b1 = 2r0 = 22=4, b2 = 2r1=21=2. So after op1: a0: -1+4=3, a1: -2+2=0, a2: -3 unchanged.
op2: [2,3] len=2, b2=0 (since index1? Actually careful: indices: 0->1,1->2,2->3. For op2: l=2,r=3 => indices 1 and 2 (0-indexed). b for index1 (i=1) = 0 (since not last), b for index2 = 2r2 = 20=0. So op2 adds nothing. So after op2: a0=3, a1=0, a2=-3.
Now all values? 3,0,-3. Are they multiples of 3? 3 is multiple, 0 is multiple, -3 is multiple. So op3: add -n*(q+r) for each: For i0: q0+r0 = -1+2=1? Wait q0=-1, r0=2 => sum=1 -> -31 = -3, so a0 becomes 0. i1: q1=-1, r1=1 => sum=0 -> add 0. i2: q2=-1, r2=0 => sum=-1 -> add -3(-1)=3? Actually -n*(q+r) = -3 * (-1) = 3, so a2 becomes 0. Works.
Thus works for negative.
Now consider n=2, a=[-1,-1]. r0 = (-1)%2 = 1 (since -1 = 2*(-1)+1), r1=1. q0 = -1//2 = -1, q1 = -1//2 = -1. op1: b0=1 => a0=0. op2: b1=1 => a1=0. Already zeros before op3! But op3 will then add 0 to both? Actually final after op2 already zero, but we still need to apply op3 exactly three operations. That's fine: op3 will add zeros (since q+r = q+r: q0+r0 = -1+1=0, so b = -2*0 = 0; similarly for a1). So we output op3 with zeros. That's allowed.
Thus all good.
Now, check constraints: b_i must be between -10^18 and 10^18. Our numbers are within that.
Now, consider if n=100000, a_i up to 1e9, r_i up to 99999, (n-1)r_i up to 9999999999 ≈ 9.9998e9 < 1e10, fine. final_mult = q_i + r_i: q_i up to floor(1e9/100000) = 10000, plus r_i up to 99999, sum up to 109999, times n=100000 gives up to 1.09999e10, still within 1e18.
If a_i negative large magnitude -1e9, similar.
So safe.
Now, is there any scenario where the computed b_i might exceed 10^18? For n up to 100000, worst-case maybe if a_i is up to 10^9? Not. But if a_i can be up to 10^9, it's fine. However, the input constraints say a_i between -10^9 and 10^9. So it's safe.
Thus solution is trivial.
But we must ensure that for n >= 2, the segment [1, n-1] is valid (i.e., n-1 >= 1). For n=2, it's [1,1] ok.
Now, we also need to consider that the operation count is exactly three. This construction uses three operations, exactly as required.
Now, verify that the problem statement's example uses a different method, but that's okay.
Now, implement in code:
Pseudo:
def solve():
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
if n == 1:
# Output three ops
print("1 1")
print("0")
print("1 1")
print("0")
print("1 1")
print(-a[0])
else:
# compute remainders and quotients
r = [x % n for x in a] # Python % yields non-negative
q = [ (a[i] - r[i]) // n for i in range(n) ] # alternative: a[i] // n works, but careful with negative? Actually a[i] // n gives floor, which matches because r = a[i] % n ensures a = n*(a[i] // n) + r? In Python, yes because // is floor division when combined with %. So we can simply q = [a[i] // n for i in range(n)].
# Let's use q = a[i] // n.
q = [a[i] // n for i in range(n)]
# But we need to confirm that a[i] = n*q[i] + r[i] holds: Yes, by Python semantics.
# compute final_mult = q_i + r_i
mult = [q[i] + r[i] for i in range(n)]
# Operation 1: [1, n-1]
l1, r1 = 1, n-1
b1 = [ (n-1) * r[i] for i in range(n-1) ] # indices 0..n-2
# Operation 2: [2, n]
l2, r2 = 2, n
b2 = []
for i in range(1, n): # indices 1..n-1 (0-index)
if i == n-1: # last index corresponds to i = n-1 (0-index) -> position n
b2.append( (n-1) * r[i] )
else:
b2.append(0)
# Operation 3: [1, n]
l3, r3 = 1, n
b3 = [ -n * mult[i] for i in range(n) ]
# Print
# op1
print(l1, r1)
print(' '.join(map(str, b1)))
# op2
print(l2, r2)
print(' '.join(map(str, b2)))
# op3
print(l3, r3)
print(' '.join(map(str, b3)))
But wait: For n=2, op2: range(1,2) i=1, which is n-1 (since n=2 => n-1=1) so only that one, we append (n-1)r[1] = 1r[1]. That's correct.
For n=2, op1: b1 list has length 1: (n-1)r[0] = 1r[0].
Ok.
Now, check with n=3 example above: a=[-1,-2,-3], r=[2,1,0], b1 = [ (2)2=4, (2)1=2] => "4 2". b2: indices i=1,2: i=1 not last (since n-1=2? n=3 => n-1=2, last index is i=2? Actually n-1 = 2, zero-index last is index 2. So for i=1 (index 1) -> not last -> b=0; i=2 -> last -> b = (2)r[2]=20=0. So b2 = [0,0]. b3: mult = q+r: q = [-1//3? In Python -1//3 = -1, -2//3 = -1, -3//3 = -1] => q = [-1,-1,-1]; r=[2,1,0]; mult = [1,0,-1]; b3 = [-31 = -3, -30 = 0, -3*(-1)=3]. So outputs: op1: "1 2\n4 2", op2: "2 3\n0 0", op3: "1 3\n-3 0 3". Check if after applying: start [-1,-2,-3]; after op1: [3,0,-3]; after op2: unchanged; after op3: [0,0,0]. Works.
Now, one more check: For n=2, a=[1,2], r=[1,0], q=[0,1], b1=[1], b2=[0? Actually b2 = (1)r[1]=0], b3 = [-2(0+1)=-2, -2*(1+0)=-2] => op1: "1 1\n1", op2: "2 2\n0", op3: "1 2\n-2 -2". After op1: [2,2]; op2: [2,2]; op3: [0,0]. Good.
Now, is there any case where q_i + r_i may be large positive/negative that leads to overflow? Not with given constraints.
Now, confirm that the solution uses exactly three operations, and each b_i divisible by length.
Now, we need to consider potential pitfalls with Python's negative numbers and modulus: The identity a + (n-1)r = n(q+r) holds if r = a mod n (0 ≤ r < n) and q = floor(a/n). This holds for all integers a. So it's solid.
Now, check for a_i = -10^9, n=100000. Compute r = (-10^9) % 100000. Since -10^9 = -10000100000 + 0? Actually -10^9 / 100000 = -10000 exactly, because 10000010000 = 1e9. So remainder 0. So r=0, q = -10000, q+r = -10000, b1 = (n-1)0=0, b2 similar, b3 = -100000 * (-10000) = 1e9? Actually -n(q+r) = -100000 * (-10000) = 1,000,000,000. That's exactly -a? It adds 1e9, resulting zero? Let's simulate: a=-1e9, after op1 add 0 -> -1e9, after op2 add 0 -> -1e9, after op3 add 1e9 -> 0. Works.
Now, check for a_i huge negative but not multiple of n: e.g., a=-999999999, n=100000. Compute r = (-999999999) % 100000 =? 100000* -10000 = -1,000,000,000, remainder = 1? Actually -1,000,000,000 + 1 = -999,999,999, so r = 1? Since remainder must be non-negative, r = 1. q = floor(-999999999/100000) = -10000? Actually -999999999/100000 = -9999.99999, floor is -10000? Wait floor of -9999.99999 is -10000? Because floor goes to more negative. So -10000. So q = -10000. Then a = -10000*100000 + 1? -1,000,000,000 + 1 = -999,999,999. So r=1, q=-10000. Then op1 adds (n-1)r = 999991 = 99999, so a becomes -999,999,999 + 99,999 = -900,000,000? Actually -999,999,999 + 99,999 = -999,900,000? Wait compute: -999,999,999 + 99,999 = -999,900,000? Actually -999,999,999 + 100,000 = -999,899,999, but we add 99,999 gives -999,900,000? Let's do: -999,999,999 + 99,999 = - (999,999,999 - 99,999) = -999,900,000. Yes. After op2 maybe adds 0, so final after two ops = -999,900,000. Is that divisible by 100,000? -999,900,000 / 100,000 = -9999, remainder? 100,000 * -9999 = -999,900,000 exactly. So it is divisible. Then op3 adds 100,000 * 9999 = 999,900,000 to zero. Works.
All good.
Now, consider n=2 and a_i = -1, as before.
Thus the solution is correct.
Now, we should also think if there is any scenario where the segment [1, n-1] or [2, n] might be invalid because n-1 < 1? That only occurs for n=1, handled separately. For n=2, n-1=1, valid.
Now, also note that for n=2, op1 and op2 both are length 1 segments; op3 length 2.
Now, potential memory: storing arrays of size n, fine.
Now, finalize.
But we must ensure that for n >= 2, the second operation's b list length equals r2-l2+1 = n-1. Our construction yields exactly n-1 numbers: for i from 2 to n, that's n-1 indices. For n=2, we have one number. For n=3, we have two numbers (indices 2 and 3). In our loop for i in range(1, n): that yields n-1 entries (since range(1,n) has n-1 elements). For i=1..n-2 we put 0, for i=n-1 we put (n-1)*r[n-1]. So length = n-1. Good.
Now, verify with n=3: range(1,3) -> i=1,2. For i=1 (index 1) not last? n-1=2, last index is 2, so i=1 not last => 0. i=2 is last => (2)r[2]. So b2 = [0, 2r2]. That's two numbers, corresponding to indices 2 and 3. So order: first number for index 2, second for index 3. That matches.
Now, check for n=4: range(1,4) -> i=1,2,3. last index = 3, so i=1,2 -> 0; i=3 -> (3)r3. So b2 = [0,0,3r3]. That's three numbers for indices 2,3,4. That matches: index2:0, index3:0, index4:3*r4. But wait r3 corresponds to index 3? Actually r indexing: a[0] -> index1, a[1]->2, a[2]->3, a[3]->4. So for i=3 (0-index) corresponds to index 4. So b2 list: for i=1 => index2 => 0; i=2 => index3 => 0; i=3 => index4 => (n-1)*r[3]. So correct.
Thus correct.
Now, consider if we could simplify further: For op2, we could also set b_i = (n-1)*r_i for i=2..n, not just i=n? But that would disturb the values that were already set by op1. Since we already set i=2..n-1 via op1 to multiples of n, adding extra (n-1)*r_i would break that unless r_i=0. So we must keep them zero. So our assignment is correct.
But what about index1? It is only covered by op1. So we must adjust it fully there.
Now, confirm that after op1 and op2, all values are multiples of n. For index1: a1 + (n-1)r1 = n(q1+r1). Good. For index2..n-1: after op1, they become n*(q_i+r_i). Then op2 adds 0, so remain n*(q_i+r_i). For index n: after op1, it was unchanged (since not covered), then op2 adds (n-1)r_n, giving n(q_n+r_n). So all multiples.
Thus the method is sound.
Now, for completeness, we might also think of an alternative approach: using segments [1,n] and [1,1] etc., but this is simplest.
Now, edge case: n=2 and a_i already multiples of 2? Then r_i=0, op1 and op2 add 0, op3 adds 0, works.
Now, output format: The example had a space after l and r? Actually they printed "1 1 " with a trailing space? But we can avoid trailing spaces. Usually it's fine to print "1 1" without trailing space.
Also note that the second line for each operation should have exactly r-l+1 integers separated by spaces. No trailing space is required but often accepted. We'll produce without trailing space.
Now, consider that the problem might require that the b_i values are within -10^18 and 10^18. Our numbers are within that. Also note that we used integer arithmetic; Python ints can handle arbitrary large, but we are within limit.
Now, we need to implement solve() reading from stdin and writing to stdout.
Potential issues: For n=1, we output three ops. But does our general method also work for n=1? Let's see: If we try to apply the same method, n-1=0 invalid segment. So we must treat separately.
Alternatively, we could also handle n=1 by using three operations all with segment [1,1], adding 0,0,-a1. That's fine.
But could there be a more elegant uniform solution? Maybe we could use segments of length 1 and n? But for n=1, length n is 1, same. However we need exactly three operations. We could do: op1: [1,1] add -a1? Then array zero after first op, but we must do three ops. So we need to pad with no-ops. So special-case is fine.
Now, confirm that the problem guarantees n >= 1.
Now, we should test a few more cases manually.
Case: n=5, a random.
Ok.
Now, think about potential pitfalls: The remainder r_i computed as a_i % n in Python yields a non-negative number. However, if we use other languages like C++, the modulus operator may yield negative remainder for negative a_i. But we are writing in Python, so it's fine.
Now, final code.
But before finalizing, we should check if there is any hidden constraint: The added integers b_i must be divisible by the segment length. We satisfy.
Now, ensure that for op2, we used b_i = 0 for i from 2 to n-1. Is zero divisible by n-1? Yes, 0 is divisible by any non-zero integer.
Now, we must output exactly three operations. Good.
Now, we may also consider if the problem expects the operations to be in a specific order (like we need to apply them sequentially as printed). Our order leads to zero after third. So it's fine.
Now, let's think about proof that it's always possible: Our construction shows it.
Now, let's also verify for n=2 and a = [0,0]. r=[0,0], q=[0,0], b1=0, b2=0, b3=0. After three ops array stays zero. That's fine.
Now, we should double-check that after op1 and op2, the array indeed becomes multiples of n for all indices, even if we used r_i = a_i mod n. But what about index 1? For index 1, we added (n-1)r_1. Using identity a1 + (n-1)r1 = n(q1 + r1). That's true because a1 = nq1 + r1 (with 0 <= r1 < n). So it's a multiple of n. Good.
Now, for index n, we added (n-1)r_n, and a_n = nq_n + r_n, so sum = nq_n + r_n + (n-1)r_n = nq_n + nr_n = n*(q_n + r_n). Good.
Thus all indices become multiples of n after two ops.
Now, we must be careful with integer division for q. In Python, a // n yields floor division. And a % n yields remainder with same sign? Actually Python guarantees that a = (a // n) * n + a % n, with 0 <= a % n < n. So our usage is correct.
Now, implementation details:
We'll compute r = [x % n for x in a].
But note: For negative x, x % n yields positive remainder, as desired.
Now, compute q = [ (a[i] - r[i]) // n ]? Actually a[i] // n already gives floor division, which equals (a[i] - r[i]) // n? Let's check: a[i] = nq + r, with 0 ≤ r < n. Then q = floor(a[i]/n). In Python, a[i] // n gives exactly floor(a[i]/n). So we can compute q = a[i] // n directly. For example, a=-3, n=4: -3//4 = -1 (floor), and r = -3 % 4 = 1, so a = 4(-1) + 1 works. So q = -1 correct. So we can compute q = a[i] // n.
But we must be careful: In some languages, integer division for negative numbers truncates toward zero. But in Python it's floor, which is what we need. So it's fine.
Now, compute mult = [q[i] + r[i] for i in range(n)].
Now, produce outputs.
Now, consider n=100000, a list length 100k. Printing all numbers as strings might be large, but acceptable.
Now, we should also ensure that for op1, we don't accidentally output an empty list when n-1 = 0, but we only call for n>=2, so n-1>=1.
Now, let's test with some random small n using brute force? Not necessary but we can mentally.
Edge Cases: n=2, a=[-2,-3]. r: -2%2=0, -3%2=1? Actually -3%2 = 1 because -3 = 2*(-2) + 1. So r0=0, r1=1. q: -2//2 = -1, -3//2 = -2? Since floor(-1.5) = -2. So q0=-1, q1=-2. Then op1: b0 = 10 =0; op2: b1 = 11=1; after op1: a0=-2, a1=-3; after op2: a0=-2, a1=-2; now values: -2, -2. Are they multiples of 2? -2 is multiple of 2 (2*(-1)). Then op3: mult0 = q0+r0 = -1+0 = -1, b0 = -2*(-1)=2; mult1 = q1+r1 = -2+1 = -1, b1 = -2*(-1)=2. So after op3: a0=0, a1=0. Works.
Now, check for a_i that are already multiples of n: r_i=0, b_i=0 for all, op3 zero them? Actually op3 will add -n*(q_i+0) = -n*q_i = -a_i, zeroing. So fine.
Now, we must also consider that the problem may have alternative solutions but ours is valid.
Now, double-check if there is any constraint that we must not exceed 10^18 for b_i. As computed, b_i magnitude ≤ ~1e10, far less.
Now, also check for n=100000, a_i = 10^9, r_i = 10^9 % 100000 = 10^9 - 10000010000 = 0? Actually 10000010000 = 1,000,000,000, exactly, so r=0, q=10000, mult=10000, b3 = -100000*10000 = -1,000,000,000, within 1e9? Actually -1e9, okay.
Now, test for n=100000, a_i = -10^9, similar.
Thus implementation is done.
Now, let's think about memory and performance: O(n) time, O(n) memory.
Now, produce final answer.
We'll include a comment to explain reasoning.
Now, one more nuance: The problem statement says "the following operation exactly three times". It doesn't say we cannot use the same segment multiple times. So using same segment [1, n-1] and [2,n] and [1,n] is fine.
Now, what about n=2? Our op1 [1,1], op2 [2,2], op3 [1,2] are distinct.
Now, could there be any issue with the requirement "Select a segment, for each number in this segment we can add a multiple of len to it"? It says "we can add a multiple of len to it". It doesn't say we must add a multiple; we can add a multiple. So adding zero is allowed.
Thus all good.
Now, let's finalize the solution.
But before final, let's also test n=2, a=[-2, 1]. r0=0, r1=1? Actually 1%2=1. q0=-1, q1=0? 1//2=0. So op1: b0=0; op2: b1=1; after op1: [-2,1]; after op2: [-2,2]; now both? -2 is multiple of 2, 2 is multiple of 2. op3: mult0 = -1+0 = -1 => b0 = -2*(-1)=2; mult1 = 0+1 = 1 => b1 = -2*1 = -2; final: [-2+2=0, 2-2=0]. Works.
Now, consider n=3, a=[0,0,0]. r=[0,0,0], q=[0,0,0], b1=[0,0], b2=[0,0], b3=[0,0,0]. Works.
Now, consider n=3, a=[1000000000, -1000000000, 123456789]. Compute manually to see magnitude. n=3, n-1=2. r0 = 1e9 % 3 = 1e9 = 3333333333 + 1? 3333333333=999999999, remainder 1. So r0=1, q0=333333333. b1 for op1: 21=2. r1 = (-1e9) % 3 =? -1e9 = 3(-333333334) + 2? Because 3*(-333333334) = -1,000,000,002, remainder 2? Actually -1,000,000,002 + 2 = -1,000,000,000. So r1=2, q1 = floor(-1e9/3) = -333333334 (since -3333333343 = -1,000,000,002, remainder 2). So q1=-333333334. b1 for op1 index2? Actually op1 covers indices 1,2, so b for index2 (i=1) = 2r1 = 4. b for index1 = 2. After op1: a0=1e9+2=1000000002, a1=-1e9+4 = -999999996, a2=123456789 unchanged. r2 = 123456789 % 3 = sum digits? 1+2+3+4+5+6+7+8+9=45 divisible by 3, so r2=0? Actually 123456789 /3 = 41152263 exactly, so r2=0, q2=41152263. op2: segment [2,3] length 2, b for index2? index2 (i=1) = 0 (since not last), index3 (i=2) = 2r2 = 0. So op2 adds nothing. After two ops: a0=1000000002, a1=-999999996, a2=123456789. Check divisibility by 3: 1000000002/3=333333334, yes; -999999996/3 = -333333332, yes; 123456789/3=41152263, yes. op3: mult0 = q0+r0 = 333333333+1=333333334, b0 = -3333333334 = -1000000002; mult1 = q1+r1 = -333333334+2 = -333333332, b1 = -3*(-333333332)=999999996; mult2 = 41152263+0=41152263, b2 = -3*41152263 = -123456789. After op3: all zero. Works.
Thus magnitudes: b1 up to ~2e9? Actually 2*1=2 small. b3 up to ~1e9. All within range.
Now, check for n=100000, a_i = 10^9, we saw b_i up to 1e9. For a_i = -10^9, b_i up to 1e9. So no issue.
Thus solution works.
Now, we could also use an alternative that yields smaller numbers? Not needed.
Now, implement solve().
Implementation details:
-
Use sys.stdin.read() maybe for speed? Not necessary.
-
But we need to output many numbers; use ' '.join(map(str, list)) for efficiency.
Now, one more check: For n=1, we need to output three ops. In the example output, they have a blank line? Actually the example has six lines:
1 1
-1
3 4
4 2
2 4
-3 -6 -6
So for n=1, we could output similar style.
Now, for n=1, our output would be:
1 1
0
1 1
0
1 1
< -a1 >
But ensure that the first operation's b_i is a single number 0, etc.
Now, also check that b_i can be negative; it's allowed.
Now, finalize.
Potential Pitfall: For n=1, n-1 = 0, but we handle separately. So done.
Now, think about if n could be 0? No.
Now, let's also think if there is any scenario where the identity a + (n-1)r = n(q+r) fails due to Python's // and %? It's mathematically true for any integer a, n>0, with r = a mod n (0 ≤ r < n) and q = floor(a/n). Since a = nq + r, then a + (n-1)r = nq + r + (n-1)r = nq + nr = n(q + r). So it's a mathematical identity, independent of language. So it's solid.
Thus our construction is proven.
Now, we might present the solution in explanation: use operations on prefix of length n-1 and suffix of length n-1 to make each element divisible by n, then zero with whole array.
Now, let's think about potential alternative: Could there be a simpler approach with two operations? Not needed.
Now, confirm that we are using exactly three operations. The third operation zeros the multiples.
Now, could we also use a construction with only two operations if we allowed? But problem requires three, so we must output three. Our third operation is needed to zero.
Now, final code.
But before finalizing, we need to consider if the problem has additional constraints like we cannot use segment length 0. Already handled.
Now, produce Python code.
Now, test with the given example to see output format difference; but it's fine.
We'll implement.
Now, check large n: output lines can be long; but printing is okay.
Now, double-check for n=2, the op1 segment [1,1] length 1, b list length 1. op2 segment [2,2] length 1, b list length 1. op3 segment [1,2] length 2, b list length 2.
Now, what about n=2, op2's b list: we computed as for i in range(1, n): i=1 is last index (since n-1=1), so b2 = (n-1)r[1] = 1r[1]. So output "2 2" then that number.
Now, for n=2, op1's b list: b1 = (n-1)r[0] = 1r[0].
Works.
Now, we should ensure that the output for op2 for n>2 includes zeros for interior indices. That's okay.
Now, I think solution is complete.
Now, I'll write final answer.
But wait: The problem statement says "The output should contain six lines representing three operations." So we need exactly six lines. Our output will have six lines.
Now, let's consider if there is any hidden requirement that the b_i must be within -10^18 to 10^18, we are safe.
Now, I'll produce final solution.
One more nuance: In Python, for very large n (like 100000), (n-1)*r_i could be up to ~1e10, which fits in Python int. But if we convert to string, it's fine.
Now, time to write.
Now, let's implement solve():
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
if n == 1:
# output three ops
out_lines = []
# op1
out_lines.append("1 1")
out_lines.append("0")
# op2
out_lines.append("1 1")
out_lines.append("0")
# op3
out_lines.append("1 1")
out_lines.append(str(-a[0]))
sys.stdout.write("\n".join(out_lines))
return
# n >= 2
# compute remainders and quotients
r = [x % n for x in a] # non-negative remainder
q = [x // n for x in a] # floor division
# op1: [1, n-1]
l1, r1 = 1, n-1
b1 = [(n-1) * r[i] for i in range(n-1)]
# op2: [2, n]
l2, r2 = 2, n
b2 = []
for i in range(1, n): # indices 1..n-1
if i == n-1: # last index (i = n-1) corresponds to position n
b2.append((n-1) * r[i])
else:
b2.append(0)
# op3: [1, n]
l3, r3 = 1, n
b3 = [-n * (q[i] + r[i]) for i in range(n)]
# Build output
out = []
out.append(f"{l1} {r1}")
out.append(" ".join(map(str, b1)))
out.append(f"{l2} {r2}")
out.append(" ".join(map(str, b2)))
out.append(f"{l3} {r3}")
out.append(" ".join(map(str, b3)))
sys.stdout.write("\n".join(out))
if name == "main":
solve()
Let's test with the given example:
Input:
4
1 3 2 4
Our output would be:
1 3
3 9 6
2 4
0 0 0
1 4
-4 -12 -8 -4
But the example output is different. But it's still valid.
Now, check if any formatting issues: The example output had a space after "1 1 " maybe but not required.
Now, we might want to remove the trailing newline? Usually it's fine.
Now, also check for n=2, a=[1,2] output:
1 1
1
2 2
0
1 2
-2 -2
That's six lines.
Now, confirm that for n=1, output:
1 1
0
1 1
0
1 1
-1 (if a=[1])
But if a[0] = 1, -a0 = -1. Works.
Now, could there be any scenario where the b_i for op3 exceeds 10^18? Already argued no.
Now, final answer.
But wait, I just realized: For n=1, the length of segment is 1, so adding -a1 is a multiple of 1. Good.
Now, we might also consider if the problem expects the b_i to be in the range -10^18 to 10^18 inclusive. For n=1 and a1 = -10^9, b = -(-10^9)=10^9, fine. For n=1 and a1 = 10^9, b = -10^9, fine.
Now, check for n=1 and a1 = 10^18? But a_i is constrained to -1e9 to 1e9, so no.
Thus done.
Now, we should consider if there is any scenario where (n-1)*r_i might be exactly 0? That's fine.
Now, one more check: In op2, for interior indices we set b=0, which is divisible by len. Good.
Now, is there any requirement that the b_i must be non-zero? No.
Now, finalize.
Now, I'll produce final answer with explanation.
But before final, let's test with n=5, random a using mental math: a = [7, -2, 0, 4, -5]. n=5, n-1=4.
Compute r: 7%5=2, -2%5=3 (since -2 = 5*(-1)+3), 0%5=0, 4%5=4, -5%5=0. q: 7//5=1, -2//5 = -1 (floor), 0//5=0, 4//5=0, -5//5 = -1.
op1: [1,4] b = [42=8, 43=12, 40=0, 44=16] -> [8,12,0,16].
After op1: [7+8=15, -2+12=10, 0+0=0, 4+16=20, -5] -> [15,10,0,20,-5].
op2: [2,5] b: indices 2,3,4,5 => positions 2,3,4,5. For i=1? Wait indices: we need b for indices 2..5. In our loop: i from 1 to 4 (0-index). i=1 (index2) not last (last is i=4) -> b=0; i=2 (index3) not last -> b=0; i=3 (index4) not last -> b=0; i=4 (index5) last -> b=4r[4]=40=0. So all zeros.
After op2: same [15,10,0,20,-5].
Now check if all multiples of 5? 15 yes, 10 yes, 0 yes, 20 yes, -5 yes.
op3: mult = q+r: for i0: q0=1,r0=2 => sum=3, b=-5*3=-15; i1: q1=-1,r1=3 => sum=2, b=-10; i2: 0+0=0, b=0; i3: 0+4=4, b=-20; i4: -1+0=-1, b=5.
After op3: [15-15=0,10-10=0,0-0=0,20-20=0,-5+5=0]. Works.
Now, check magnitudes: b1 up to 16, b3 up to 20, fine.
Thus solution works.
Now, finalize answer.
We'll output code.
But consider that some judges might expect to read from input exactly as given, but our reading method using sys.stdin.read().split() handles extra spaces.
Now, final.
Now, I'm going to produce final answer in the "final" channel with explanation and code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
# Special case n = 1: three operations on the single element
if n == 1:
out = [
"1 1", "0",
"1 1", "0",
"1 1", str(-a[0])
]
sys.stdout.write("\n".join(out))
return
# n >= 2
# r[i] = a[i] mod n (0 <= r[i] < n)
r = [x % n for x in a]
# q[i] = floor(a[i] / n), so that a[i] = n * q[i] + r[i]
q = [x // n for x in a]
# Operation 1: prefix [1, n-1] of length n-1
l1, r1 = 1, n - 1
b1 = [(n - 1) * r[i] for i in range(n - 1)]
# Operation 2: suffix [2, n] of length n-1
l2, r2 = 2, n
b2 = []
for i in range(1, n): # indices 1 .. n-1 (0‑based)
if i == n - 1: # the last index -> position n
b2.append((n - 1) * r[i])
else:
b2.append(0)
# Operation 3: whole array [1, n] of length n
l3, r3 = 1, n
b3 = [-n * (q[i] + r[i]) for i in range(n)]
out = [
f"{l1} {r1}",
" ".join(map(str, b1)),
f"{l2} {r2}",
" ".join(map(str, b2)),
f"{l3} {r3}",
" ".join(map(str, b3))
]
sys.stdout.write("\n".join(out))
if name == "main":
solve()