1337xyz1337xyz/codeforces-editorial-full-2026-07-16 huggingface dataset:
Tokitsukaze and Beautiful Subsegments
Contest: Codeforces Round 789 (Div. 1)
Difficulty rating: 2900
Tags: data structures

Tokitsukaze has a permutation $$$p$$$ of length $$$n$$$.

Let's call a segment $$$[l,r]$$$ beautiful if there exist $$$i$$$ and $$$j$$$ satisfying $$$p_i \cdot p_j = \max\{p_l, p_{l+1}, \ldots, p_r \}$$$, where $$$l \leq i < j \leq r$$$.

Now Tokitsukaze has $$$q$$$ queries, in the $$$i$$$-th query she wants to know how many beautiful subsegments $$$[x,y]$$$ there are in the segment $$$[l_i,r_i]$$$ (i. e. $$$l_i \leq x \leq y \leq r_i$$$).

Input format:
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1\leq n \leq 2 \cdot 10^5$$$; $$$1 \leq q \leq 10^6$$$) — the length of permutation $$$p$$$ and the number of queries.

The second line contains $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq n$$$) — the permutation $$$p$$$.

Each of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \leq l_i \leq r_i \leq n$$$) — the segment $$$[l_i,r_i]$$$ of this query.

Output format:
For each query, print one integer — the numbers of beautiful subsegments in the segment $$$[l_i,r_i]$$$.

Constraints:
Time limit: 4.0 s
Memory limit: 1024.0 MB

Note:
In the first example, for the first query, there are $$$2$$$ beautiful subsegments — $$$[1,2]$$$ and $$$[1,3]$$$.

Example input:
8 3
1 3 5 2 4 7 6 8
1 3
1 1
1 8

Example output:
2
0
10

Example input:
10 10
6 1 3 2 5 8 4 10 7 9
1 8
1 10
1 2
1 4
2 4
5 8
4 10
4 7
8 10
5 9

Example output:
17
25
1
5
2
0
4
1
0
0

Editorial:
Let's sort the queries $$$[l,r]$$$ with $$$r$$$ in ascending order.
Let's move $$$R$$$ from $$$1$$$ to $$$n$$$, and answer queries $$$[l,r]$$$ when $$$r=R$$$.
Use segment tree to solve this problem, each leaf node $$$[ul,ul]$$$ in the segment tree maintenance the number that how many intervals $$$[ul,R]$$$ are beautiful, then answering queries is to get sum.
Use monotone stack to maintain the maximum number. When moving $$$R$$$ one step right, the monotone stack will pop some and push one.
Let's think about the number $$$x$$$ in monotone stack, which means the maximum number of the intervals $$$[lp,R]$$$ ($$$l1 \le lp \le l2$$$) is $$$x$$$. If there exist a $$$ll$$$ in $$$[l1,l2]$$$ which satisfies $$$a_{ll}*a_R=x$$$, then before $$$x$$$ is poped, intervals $$$[l,rr]$$$ are beautiful($$$l \in [l1,l2]$$$,$$$l \le ll$$$ ,$$$R \le rr$$$).
So we can assume that $$$x$$$ will not be poped when it is pushed in the monotone stack, and do something like difference algorithm in the segment tree, by using the current $$$R$$$ as the time stamp. Each node in the segment tree has to maintain $$$a*R+b$$$,which like a linear function.
So when moving $$$R$$$ right step, let's enumerate the factor of $$$a_R$$$ to update old intervals in monotone stack, enumerate the multiple of $$$a_R$$$ to update the new interval in monotone stack and update the intervals which are poped.
Time complexity: $$$\mathcal{O}(nlognlogn+qlogn)$$$.
I_love_%username%
Contest: Codeforces Round 109 (Div. 2)
Difficulty rating: 800
Tags: brute force

Vasya adores sport programming. He can't write programs but he loves to watch the contests' progress. Vasya even has a favorite coder and Vasya pays special attention to him.

One day Vasya decided to collect the results of all contests where his favorite coder participated and track the progress of his coolness. For each contest where this coder participated, he wrote out a single non-negative number — the number of points his favorite coder earned in the contest. Vasya wrote out the points for the contest in the order, in which the contests run (naturally, no two contests ran simultaneously).

Vasya considers a coder's performance in a contest amazing in two situations: he can break either his best or his worst performance record. First, it is amazing if during the contest the coder earns strictly more points that he earned on each past contest. Second, it is amazing if during the contest the coder earns strictly less points that he earned on each past contest. A coder's first contest isn't considered amazing. Now he wants to count the number of amazing performances the coder had throughout his whole history of participating in contests. But the list of earned points turned out long and Vasya can't code... That's why he asks you to help him.

Input format:
The first line contains the single integer n (1 ≤ n ≤ 1000) — the number of contests where the coder participated.

The next line contains n space-separated non-negative integer numbers — they are the points which the coder has earned. The points are given in the chronological order. All points do not exceed 10000.

Output format:
Print the single number — the number of amazing performances the coder has had during his whole history of participating in the contests.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first sample the performances number 2 and 3 are amazing.

In the second sample the performances number 2, 4, 9 and 10 are amazing.

Example input:
5
100 50 200 150 200

Example output:
2

Example input:
10
4664 6496 5814 7010 5762 5736 6944 4850 3698 7242

Example output:
4

Editorial:
Combination
Contest: Codeforces Round 109 (Div. 2)
Difficulty rating: 1100
Tags: greedy, sortings

Ilya plays a card game by the following rules.

A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded.

More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.

Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?

Input format:
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has.

Each of the next n lines contains two non-negative space-separated integers — ai and bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly.

Output format:
Print the single number — the maximum number of points you can score in one round by the described rules.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first sample none of two cards brings extra moves, so you should play the one that will bring more points.

In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards.

Example input:
2
1 0
2 0

Example output:
2

Example input:
3
1 0
2 0
0 2

Example output:
3

Editorial:
Traveling Salesman Problem
Contest: Codeforces Round 812 (Div. 2)
Difficulty rating: 800
Tags: geometry, greedy, implementation

You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).

More formally, if you are standing at the point $$$(x, y)$$$, you can:

- go left, and move to $$$(x - 1, y)$$$, or
- go right, and move to $$$(x + 1, y)$$$, or
- go up, and move to $$$(x, y + 1)$$$, or
- go down, and move to $$$(x, y - 1)$$$.

There are $$$n$$$ boxes on this plane. The $$$i$$$-th box has coordinates $$$(x_i,y_i)$$$. It is guaranteed that the boxes are either on the $$$x$$$-axis or the $$$y$$$-axis. That is, either $$$x_i=0$$$ or $$$y_i=0$$$.

You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point $$$(0,0)$$$.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of boxes.

The $$$i$$$-th line of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-100 \le x_i, y_i \le 100$$$) — the coordinate of the $$$i$$$-th box. It is guaranteed that either $$$x_i=0$$$ or $$$y_i=0$$$.

Do note that the sum of $$$n$$$ over all test cases is not bounded.

Output format:
For each test case output a single integer — the minimum number of moves required.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below.

$$$$$$(0,0) \to (1,0) \to (1,1) \to (1, 2) \to (0,2) \to (-1,2) \to (-1,1) \to (-1,0) \to (-1,-1) \to (-1,-2) \to (0,-2) \to (0,-1) \to (0,0)$$$$$$

In the second test case, a possible sequence of moves that uses the minimum number of moves required is shown below.

$$$$$$(0,0) \to (0,1) \to (0,2) \to (-1, 2) \to (-2,2) \to (-3,2) \to (-3,1) \to (-3,0) \to (-3,-1) \to (-2,-1) \to (-1,-1) \to (0,-1) \to (0,0)$$$$$$

In the third test case, we can collect all boxes without making any moves.

Example input:
3
4
0 -2
1 0
-1 0
0 2
3
0 2
-3 0
0 -1
1
0 0

Example output:
12
12
0

Editorial:
Optimal Reduction
Contest: Codeforces Round 812 (Div. 2)
Difficulty rating: 1000
Tags: constructive algorithms, sortings

Consider an array $$$a$$$ of $$$n$$$ positive integers.

You may perform the following operation:

- select two indices $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$), then
- decrease all elements $$$a_l, a_{l + 1}, \dots, a_r$$$ by $$$1$$$.

Let's call $$$f(a)$$$ the minimum number of operations needed to change array $$$a$$$ into an array of $$$n$$$ zeros.

Determine if for all permutations$$$^\dagger$$$ $$$b$$$ of $$$a$$$, $$$f(a) \leq f(b)$$$ is true.

$$$^\dagger$$$ An array $$$b$$$ is a permutation of an array $$$a$$$ if $$$b$$$ consists of the elements of $$$a$$$ in arbitrary order. For example, $$$[4,2,3,4]$$$ is a permutation of $$$[3,2,4,4]$$$ while $$$[1,2,2]$$$ is not a permutation of $$$[1,2,3]$$$.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the array $$$a$$$.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — description of the array $$$a$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output format:
For each test case, print "YES" (without quotes) if for all permutations $$$b$$$ of $$$a$$$, $$$f(a) \leq f(b)$$$ is true, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, we can change all elements to $$$0$$$ in $$$5$$$ operations. It can be shown that no permutation of $$$[2, 3, 5, 4]$$$ requires less than $$$5$$$ operations to change all elements to $$$0$$$.

In the third test case, we need $$$5$$$ operations to change all elements to $$$0$$$, while $$$[2, 3, 3, 1]$$$ only needs $$$3$$$ operations.

Example input:
3
4
2 3 5 4
3
1 2 3
4
3 1 3 2

Example output:
YES
YES
NO

Editorial:
Build Permutation
Contest: Codeforces Round 812 (Div. 2)
Difficulty rating: 1200
Tags: constructive algorithms, dp, math

A $$$\mathbf{0}$$$-indexed array $$$a$$$ of size $$$n$$$ is called good if for all valid indices $$$i$$$ ($$$0 \le i \le n-1$$$), $$$a_i + i$$$ is a perfect square$$$^\dagger$$$.

Given an integer $$$n$$$. Find a permutation$$$^\ddagger$$$ $$$p$$$ of $$$[0,1,2,\ldots,n-1]$$$ that is good or determine that no such permutation exists.

$$$^\dagger$$$ An integer $$$x$$$ is said to be a perfect square if there exists an integer $$$y$$$ such that $$$x = y^2$$$.

$$$^\ddagger$$$ An array $$$b$$$ is a permutation of an array $$$a$$$ if $$$b$$$ consists of the elements of $$$a$$$ in arbitrary order. For example, $$$[4,2,3,4]$$$ is a permutation of $$$[3,2,4,4]$$$ while $$$[1,2,2]$$$ is not a permutation of $$$[1,2,3]$$$.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the permutation $$$p$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output format:
For each test case, output $$$n$$$ distinct integers $$$p_0, p_1, \dots, p_{n-1}$$$ ($$$0 \le p_i \le n-1$$$) — the permutation $$$p$$$ — if the answer exists, and $$$-1$$$ otherwise.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, we have $$$n=3$$$. The array $$$p = [1, 0, 2]$$$ is good since $$$1 + 0 = 1^2$$$, $$$0 + 1 = 1^2$$$, and $$$2 + 2 = 2^2$$$

In the second test case, we have $$$n=4$$$. The array $$$p = [0, 3, 2, 1]$$$ is good since $$$0 + 0 = 0^2$$$, $$$3 + 1 = 2^2$$$, $$$2+2 = 2^2$$$, and $$$1+3 = 2^2$$$.

Example input:
3
3
4
7

Example output:
1 0 2 
0 3 2 1 
1 0 2 6 5 4 3

Editorial:
Cross Swapping
Contest: Codeforces Round 812 (Div. 2)
Difficulty rating: 2400
Tags: 2-sat, data structures, dsu, greedy, matrices

You are given a square matrix $$$A$$$ of size $$$n \times n$$$ whose elements are integers. We will denote the element on the intersection of the $$$i$$$-th row and the $$$j$$$-th column as $$$A_{i,j}$$$.

You can perform operations on the matrix. In each operation, you can choose an integer $$$k$$$, then for each index $$$i$$$ ($$$1 \leq i \leq n$$$), swap $$$A_{i, k}$$$ with $$$A_{k, i}$$$. Note that cell $$$A_{k, k}$$$ remains unchanged.

For example, for $$$n = 4$$$ and $$$k = 3$$$, this matrix will be transformed like this:

The operation $$$k = 3$$$ swaps the blue row with the green column.

You can perform this operation any number of times. Find the lexicographically smallest matrix$$$^\dagger$$$ you can obtain after performing arbitrary number of operations.

$$${}^\dagger$$$ For two matrices $$$A$$$ and $$$B$$$ of size $$$n \times n$$$, let $$$a_{(i-1) \cdot n + j} = A_{i,j}$$$ and $$$b_{(i-1) \cdot n + j} = B_{i,j}$$$. Then, the matrix $$$A$$$ is lexicographically smaller than the matrix $$$B$$$ when there exists an index $$$i$$$ ($$$1 \leq i \leq n^2$$$) such that $$$a_i < b_i$$$ and for all indices $$$j$$$ such that $$$1 \leq j < i$$$, $$$a_j = b_j$$$.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the size of the matrix.

The $$$i$$$-th line of the next $$$n$$$ lines contains $$$n$$$ integers $$$A_{i, 1}, A_{i, 2}, \dots, A_{i, n}$$$ ($$$1 \le A_{i, j} \le 10^9$$$) — description of the matrix $$$A$$$.

It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^6$$$.

Output format:
For each test case, print $$$n$$$ lines with $$$n$$$ integers each — the lexicographically smallest matrix.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
Note that in every picture below the matrix is transformed in such a way that the blue rows are swapped with the green columns.

In the first test case, we can perform $$$1$$$ operation for $$$k = 3$$$. The matrix will be transformed as below:

Example input:
2
3
2 1 2
2 1 2
1 1 2
4
3 3 1 2
1 1 3 1
3 2 3 2
2 3 3 1

Example output:
2 1 1
2 1 1
2 2 2
3 1 1 2
3 1 2 1
3 3 3 3
2 3 2 1

Editorial:
Lost Array
Contest: Codeforces Round 812 (Div. 2)
Difficulty rating: 2900
Tags: bitmasks, combinatorics, constructive algorithms, dp, math

A long time ago, Spyofgame invented the famous array $$$a$$$ ($$$1$$$-indexed) of length $$$n$$$ that contains information about the world and life. After that, he decided to convert it into the matrix $$$b$$$ ($$$0$$$-indexed) of size $$$(n + 1) \times (n + 1)$$$ which contains information about the world, life and beyond.

Spyofgame converted $$$a$$$ into $$$b$$$ with the following rules.

- $$$b_{i,0} = 0$$$ if $$$0 \leq i \leq n$$$;
- $$$b_{0,i} = a_{i}$$$ if $$$1 \leq i \leq n$$$;
- $$$b_{i,j} = b_{i,j-1} \oplus b_{i-1,j}$$$ if $$$1 \leq i, j \leq n$$$.

Here $$$\oplus$$$ denotes the bitwise XOR operation.

Today, archaeologists have discovered the famous matrix $$$b$$$. However, many elements of the matrix has been lost. They only know the values of $$$b_{i,n}$$$ for $$$1 \leq i \leq n$$$ (note that these are some elements of the last column, not the last row).

The archaeologists want to know what a possible array of $$$a$$$ is. Can you help them reconstruct any array that could be $$$a$$$?

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 5 \cdot 10^5$$$).

The second line contains $$$n$$$ integers $$$b_{1,n}, b_{2,n}, \ldots, b_{n,n}$$$ ($$$0 \leq b_{i,n} < 2^{30}$$$).

Output format:
If some array $$$a$$$ is consistent with the information, print a line containing $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. If there are multiple solutions, output any.

If such an array does not exist, output $$$-1$$$ instead.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
If we let $$$a = [1,2,3]$$$, then $$$b$$$ will be:

$$$\bf{0}$$$$$$\bf{1}$$$$$$\bf{2}$$$$$$\bf{3}$$$$$$\bf{0}$$$$$$1$$$$$$3$$$$$$0$$$$$$\bf{0}$$$$$$1$$$$$$2$$$$$$2$$$$$$\bf{0}$$$$$$1$$$$$$3$$$$$$1$$$

The values of $$$b_{1,n}, b_{2,n}, \ldots, b_{n,n}$$$ generated are $$$[0,2,1]$$$ which is consistent with what the archaeologists have discovered.

Example input:
3
0 2 1

Example output:
1 2 3

Example input:
1
199633

Example output:
199633

Editorial:
SSeeeeiinngg DDoouubbllee
Contest: Codeforces Round 836 (Div. 2)
Difficulty rating: 800
Tags: constructive algorithms, strings

A palindrome is a string that reads the same backward as forward. For example, the strings $$$\texttt{z}$$$, $$$\texttt{aaa}$$$, $$$\texttt{aba}$$$, and $$$\texttt{abccba}$$$ are palindromes, but $$$\texttt{codeforces}$$$ and $$$\texttt{ab}$$$ are not.

The double of a string $$$s$$$ is obtained by writing each character twice. For example, the double of $$$\texttt{seeing}$$$ is $$$\texttt{sseeeeiinngg}$$$.

Given a string $$$s$$$, rearrange its double to form a palindrome. Output the rearranged string. It can be proven that such a rearrangement always exists.

Input format:
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

The only line of each test case contains a single string $$$s$$$ ($$$1 \leq |s| \leq 100$$$) consisting only of lowercase English letters.

Note that the sum of $$$|s|$$$ over all test cases is not bounded.

Output format:
For each test case, output a palindromic string of length $$$2 \cdot |s|$$$ that is a rearrangement of the double of $$$s$$$.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, the double of $$$\texttt{a}$$$ is $$$\texttt{aa}$$$, which is already a palindrome.

In the second test case, the double of $$$\texttt{sururu}$$$ is $$$\texttt{ssuurruurruu}$$$. If we move the first $$$\texttt{s}$$$ to the end, we get $$$\texttt{suurruurruus}$$$, which is a palindrome.

In the third test case, the double of $$$\texttt{errorgorn}$$$ is $$$\texttt{eerrrroorrggoorrnn}$$$. We can rearrange the characters to form $$$\texttt{rgnororerrerorongr}$$$, which is a palindrome.

Example input:
4
a
sururu
errorgorn
anutforajaroftuna

Example output:
aa
suurruurruus
rgnororerrerorongr
aannuuttffoorraajjaarrooffttuunnaa

Editorial:
Output $$$s + \text{reverse}(s)$$$.
It works, since each character in $$$s$$$ occurs exactly twice (once in $$$s$$$, once in $$$\text{reverse}(s)$$$), and the result is a palindrome.
XOR = Average
Contest: Codeforces Round 836 (Div. 2)
Difficulty rating: 900
Tags: constructive algorithms

You are given an integer $$$n$$$. Find a sequence of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ such that $$$1 \leq a_i \leq 10^9$$$ for all $$$i$$$ and $$$$$$a_1 \oplus a_2 \oplus \dots \oplus a_n = \frac{a_1 + a_2 + \dots + a_n}{n},$$$$$$ where $$$\oplus$$$ represents the bitwise XOR.

It can be proven that there exists a sequence of integers that satisfies all the conditions above.

Input format:
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.

The first and only line of each test case contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the sequence you have to find.

The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output format:
For each test case, output $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ satisfying the conditions in the statement.

If there are several possible answers, you can output any of them.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, $$$69 = \frac{69}{1} = 69$$$.

In the second test case, $$$13 \oplus 2 \oplus 8 \oplus 1 = \frac{13 + 2 + 8 + 1}{4} = 6$$$.

Example input:
3
1
4
3

Example output:
69
13 2 8 1
7 7 7

Editorial:
Let us consider the cases when $$$n$$$ is odd and when its even.
• $$$n$$$ is odd: We can see that printing $$$\underbrace{1,\dots,1}_{n\text{ times}}$$$ will lead to an average of $$$1$$$ and an XOR of $$$1$$$ (since $$$1 \oplus 1 = 0$$$). Similarly, you could print any integer $$$n$$$ times to pass this case.
• $$$n$$$ is even: We use a slight modification of the solution for odd $$$n$$$ here. Instead of printing the same number $$$n$$$ times, we print $$$1, 3, \underbrace{2, \dots, 2}_{n-2\text{ times}}$$$. Both the XOR and the average of $$$1$$$ and $$$3$$$ are $$$2$$$. Therefore the average of the total sequence remains $$$2$$$, and the XOR of the whole sequence is also $$$2$$$.
Note that there are other possible solutions, but the simplest one is described here.