Suppose that you can use $$$x$$$ operations of type $$$1$$$ and $$$y$$$ operations of type $$$2$$$. Try to reorder the operations in such a way that $$$a$$$ becomes the minimum possible.
How many operations do you need in the worst case? ($$$a = 10^9$$$, $$$b = 1$$$)
Iterate over the number of operations of type $$$2$$$.
Notice how it is never better to increase $$$b$$$ after dividing ($$$\lfloor \frac{a}{b+1} \rfloor \le \lfloor \frac{a}{b} \rfloor$$$).
So we can try to increase $$$b$$$ to a certain value and then divide $$$a$$$ by $$$b$$$ until it is $$$0$$$. Being careful as not to do this with $$$b<2$$$, the number of times we divide is going to be $$$O(\log a)$$$. In particular, if you reach $$$b \geq 2$$$ (this requires at most $$$1$$$ move), you need at most $$$\lfloor \log_2(10^9) \rfloor = 29$$$ moves to finish.
Let $$$y$$$ be the number of moves of type $$$2$$$; we can try all values of $$$y$$$ ($$$0 \leq y \leq 30$$$) and, for each $$$y$$$, check how many moves of type $$$1$$$ are necessary. This is a $$$O(\log^2 a)$$$ solution.
If we notice that it is never convenient to increase $$$b$$$ over $$$6$$$, we can also achieve a solution with better complexity.
You can make a $$$k$$$-similar array by assigning $$$a_i = x$$$ for some $$$l \leq i \leq r$$$ and $$$1 \leq x \leq k$$$.
How many $$$k$$$-similar arrays can you make if $$$x$$$ is already equal to some $$$a_i$$$ ($$$l \leq i \leq r$$$)?
How many $$$k$$$-similar arrays can you make if either $$$x < a_l$$$ or $$$x > a_r$$$?
How many $$$k$$$-similar arrays can you make if none of the previous conditions holds?
Let's consider each value $$$x$$$ from $$$1$$$ to $$$k$$$.
- If $$$x < a_l$$$, you can replace $$$a_l$$$ with $$$x$$$ (and you get $$$1$$$ $$$k$$$-similar array). There are $$$a_l-1$$$ such values of $$$x$$$.
- If $$$x > a_r$$$, you can replace $$$a_r$$$ with $$$x$$$ (and you get $$$1$$$ $$$k$$$-similar array). There are $$$k-a_r$$$ such values of $$$x$$$.
- If $$$a_l < x < a_r$$$, and $$$x \neq a_i$$$ for all $$$i$$$ in $$$[l, r]$$$, you can either replace the rightmost $$$b_i$$$ which is less than $$$x$$$, or the leftmost $$$b_i$$$ which is greater than $$$x$$$ (and you get $$$2$$$ $$$k$$$-similar arrays). There are $$$(a_r - a_l + 1) - (r - l + 1)$$$ such values of $$$x$$$.
- If $$$x = a_i$$$ for some $$$i$$$ in $$$[l, r]$$$, no $$$k$$$-similar arrays can be made.
The total count is $$$(a_l-1)+(k-a_r)+2((a_r - a_l + 1) - (r - l + 1))$$$, which simplifies to $$$k + (a_r - a_l + 1) - 2(r - l + 1)$$$.
2000C - Numeric String Template
Let $$$\lfloor \frac{a}{b} \rfloor = a~\mathrm{mod}~b = k$$$. Is there an upper bound for $$$k$$$?
For a fixed $$$k$$$, can you count the number of special pairs such that $$$a \leq x$$$ and $$$b \leq y$$$ in $$$O(1)$$$?
We can notice that, if $$$\lfloor \frac{a}{b} \rfloor = a~\mathrm{mod}~b = k$$$, then $$$a$$$ can be written as $$$kb+k$$$ ($$$b > k$$$). Since $$$b > k$$$, we have that $$$k^2 < kb+k = a \leq x$$$. Hence $$$k \leq \sqrt x$$$.
Now let's count special pairs for any fixed $$$k$$$ ($$$1 \leq k \leq \sqrt x$$$). For each $$$k$$$, you have to count the number of $$$b$$$ such that $$$b > k$$$, $$$1 \leq b \leq y$$$, $$$1 \leq kb+k \leq x$$$. The second condition is equivalent to $$$1 \leq b \leq x/k-1$$$.
Therefore, for any fixed $$$k > 0$$$, the number of special pairs ($$$a\leq x$$$; $$$b \leq y$$$) is $$$max(0, min(y,x/k-1) - k)$$$. The result is the sum of the number of special pairs for each $$$k$$$.
Complexity is $$$O(t \cdot \sqrt x)$$$.
Brute force doesn't work (even if you optimize it): there are relatively few solutions.
There may be very few possible values of $$$b_{i,j}$$$, if $$$b_{i-1,j}$$$ is fixed. The problem arises when you have to find a value for a cell with, e.g., $$$4$$$ fixed neighbors. Try to find a possible property of the neighbors of $$$(i, j)$$$, such that at least a solution for $$$b_{i,j}$$$ exists.
The least common multiple of all integers from $$$1$$$ to $$$16$$$ is less than $$$10^6$$$.
Build a matrix with a checkerboard pattern: let $$$b_{i, j} = 720720$$$ if $$$i + j$$$ is even, and $$$720720+a_{i, j}^4$$$ otherwise. The difference between two adjacent cells is obviously a fourth power of an integer. We choose $$$720720$$$ because it is $$$\operatorname{lcm}(1, 2, \dots, 16)$$$. This ensures that $$$b_{i, j}$$$ is a multiple of $$$a_{i, j}$$$, because it is either $$$720720$$$ itself or the sum of two multiples of $$$a_{i, j}$$$.
2000E - Photoshoot for Gorillas
Solve the problem with $$$d=2$$$, it can be generalized easily.
What happens if you can't swap coins?
If you can swap coins, you have to calculate $$$\max(dp[parent[j]] + |a[j] - a[i]|)$$$ for each $$$i$$$ with a fixed $$$dist(1, i)$$$. How?
Divide the nodes in groups based on the distance from the root and sort them in increasing order. Then, calculate $$$dp[i]$$$ — the maximum score that you can reach after $$$dist(1, i)$$$ moves if you put a coin in node $$$i$$$. You can calculate $$$dp[i]$$$ if you know $$$dp[j]$$$ for each $$$j$$$ that belongs to the previous group. There are two cases:
if before the eventual swap the coin on node $$$i$$$ is red, the previous position of the red coin is fixed, and the blue coin should reach either the minimum or the maximum $$$a[j]$$$ among the $$$j$$$ that belong to the same group of $$$i$$$;
if before the eventual swap the coin on node $$$i$$$ is blue, you should maximize the score $$$dp[parent[j]] + |a[j] - a[i]|$$$ ($$$j$$$ and $$$i$$$ in the same group). This can be done efficiently by sorting the $$$a[i]$$$ and calculating the answer separately for $$$a[j] \leq a[i]$$$ and $$$a[j] > a[i]$$$; for each $$$i$$$ in the group, the optimal $$$j$$$ is either the same of last processed node or the last processed node.
The answer is $$$max(dp[i])$$$.
2000F - Color Rows and Columns
Why isn't the answer $$$2^{n-1}$$$? What would you overcount?
Find a $O(n^2\log(n) solution).
Is it really necessary to copy all the pairs $$$(i, j)$$$ to $$$(i+1, j+b_i)$$$?
For each $$$i$$$, you can choose either $$$a_i = b_i$$$ or $$$a_i = b_i - \sum_{k=1}^{i-1} a_k$$$. If $$$\sum_{k=1}^{i-1} a_k = 0$$$, the two options coincide and you have to avoid overcounting them.
This leads to an $$$O(n^2\log(n))$$$ solution: let $$$dp[i]$$$ be a map such that $$$dp[i][j]$$$ corresponds to the number of ways to create a hybrid prefix $$$[1, i]$$$ with sum $$$j$$$. The transitions are $$$dp[i][j] \rightarrow dp[i+1][j+b[i]]$$$ (if you choose $$$b_i = a_i$$$, and $$$j \neq 0$$$), $$$dp[i][j] \rightarrow dp[i+1][b[i]]$$$ (if you choose $$$b_i = \sum_{k=1}^{i} a_k$$$).
Let's try to get rid of the first layer of the dp. It turns out that the operations required are \begin{itemize} \item move all $$$dp[j]$$$ to $$$dp[j+b[i]]$$$ \item calculate the sum of all $$$dp[j]$$$ in some moment \item change the value of a $$$dp[j]$$$ \end{itemize} and they can be handled in $$$O(n\log(n))$$$ with ``Venice technique'' (\url{https://mirror.codeforces.com/blog/entry/58316}). It's not necessary to use a multiset (a map is enough) because you don't have to handle min / max queries.
$$$dp$$$ is now a map such that $$$dp[j]$$$ corresponds to the number of ways to create a hybrid prefix $$$[1, i]$$$ such that $$$\sum_{k=1}^{i} a_k - b_k = j$$$. Calculate the dp for all prefixes from left to right. if $$$b_i = a_i$$$, you don't need to change any value of the dp; if $$$b_i = \sum_{k=1}^{i} a_k$$$, you have to set $$$dp[\sum_{k=1}^{i} -b_k]$$$ to the total number of hybrid arrays of length $$$i-1$$$.