#### A. Mex of 2

**Solution**

If neither of $$$A$$$ or $$$B$$$ equals to zero, print $$$0$$$. Otherwise if neither of them equal $$$1$$$ then print $$$1$$$, else if neither of them equal $$$2$$$, print $$$2$$$.

#### B. Array & Xor

**Solution**

If all the entries of the array are zero, then the answer is $$$0$$$.

Otherwise, if the xor of all the elements in the array is zero, then the answer is $$$1$$$. (We can select the entire array in the move).

Finally, in all other cases the answer is $$$2$$$. This is because we can select the entire array in the first move, and then again select the entire array in the second move.

#### C. Grids & Modulus

**Solution**

The first observation is that the moves along $$$x$$$ and $$$y$$$ axis are independent of each other. So we can solve for each direction independently and then directly multiply number of ways for each direction. This will give us total number of ways for the whole grid.

So the simplified problem is how to solve for a single direction. We are given $$$N$$$ adjacent circular positions and we have to find the number of possible jump sizes so that we can reach from $$$x_1$$$ to $$$x_2$$$ using any number of moves (possibly zero). Thus,

where $$$a$$$ and $$$b$$$ are some integers. So, the above equation is satisfied when $$$x_2-x_1 \text{ mod } \gcd(\text{jump_size},N) \equiv 0$$$.

We know that the GCD of $$$N$$$ with any integer will be a divisor of $$$N$$$. So, we will iterate over all the divisors of $$$N$$$ and pre-calculate how many of them have that divisor as GCD and if that divisor is dividing $$$x_2-x_1$$$, then we can add their contribution to the answer. Hence, this way we can calculate number of ways for single direction for all the queries. To find the complete answer multiply answer of each direction and output for each query.

Time complexity: $$$O(Q(\text{div}(N)+\text{div}(M))$$$, where $$$\text{div}(x)$$$ denotes number of divisors of $$$x$$$.

#### D.Expected Valley Sum

**Solution**

#### E. Grid and Chip

**Solution**

First, lets build up some observations. For the following discussion, take any possible way which starts from $$$(1,1)$$$ and returns to $$$(1,1)$$$ visiting all cells exactly once.

If at some step, we move from $$$(i,j)$$$ to $$$(i+1, j)$$$, then clearly, we must have moved from $$$(i-1,j+1)$$$ to $$$(i, j+1)$$$, as there are only two ways of reaching $$$(i,j+1)$$$, either from $$$(i,j)$$$ or from $$$(i-1, j +1)$$$, but we have already moved from $$$(i,j)$$$ to $$$(i + 1,j)$$$. Building up on this, we can see that if $$$(x_1,y_1)$$$ and $$$(x_2,y_2)$$$ are two different cells such that $$$x_1 + y_1$$$ is equal to $$$x_2 + y_2$$$, then the move taken from $$$(x_1,y_1)$$$ and $$$(x_2,y_2)$$$ are equal.

Let's use $$$A(x,y)$$$ to denote the move taken from $$$(x,y)$$$. Building up on the above discussion we can see that since this the graph is actually a toroid, the point above $$$(1,a)$$$ is equivalent to $$$(H, a)$$$, and similarly continuing $$$A(1,a)$$$ and $$$A(H, a+1)$$$ are also the same. So, the total freedom in moves is heavily restricted.

The total number of distinct diagonals on the toroid are only $$$d = \gcd(H,W)$$$. So, if we fix the first $$$d$$$ moves, then all the remaining moves are automatically decided. But we cannot take any arbitrary moves for the first $$$d$$$ moves, as not all such solutions will visit all the cells. To solve this, we can fix the cells we will be reaching after the first $$$d$$$ moves. Lets say we reach the cell $$$(x, y)$$$ where $$$x+y = d + 2$$$. Now the number of steps required to reach back to $$$(1,1)$$$ from $$$(1,1)$$$, assuming the first $$$d$$$ moves takes us to $$$(x,y)$$$ is $$$\text{lcm}(\frac{H}{\gcd(H, x-1)}, \frac{W}{\gcd(W, y-1)})$$$. If this is equal to $$$\frac{HW}{d}$$$, then this solution will visit all the cells and thus be counted in the solution. So, the final answer is summation of $$$\binom{d}{x-1}$$$ over all these valid $$$(x,y)$$$.

Time complexity: $$$O(\gcd(H, W)\log(\gcd(H,W)))$$$.

#### F. Grid And Paths

**Solution**

**Alternate Problem**

The first observation is the following grid can be considered as a tree, each cell of form $$$(2i+1, 2i+1)$$$ is a node of the tree, and if $$$(2i, 2i + 1)$$$ is an empty cell then there is an edge between the node corresponding to the cell $$$(2i-1, 2i+1)$$$ and the cell $$$(2i+1, 2i+1)$$$, similarly for the other edges and nodes.

So our problem is now greatly simplified. We are given a tree, where each node can either have $$$0$$$, $$$1$$$, or $$$2$$$ children, and atmost one of those child is marked. Find the expected number of robots needed to reach from the root to a special leaf node.