A – Not
Should be easy as long as you know the basics of your chosen programming language. Can be computed with an if/else statement, the ternary operator (ans = x == 1 ? 0 : 1
), or the xor operator (ans = x ^ 1
)
B – Product Max
Answer is one of: $$$a \cdot c, a \cdot d, b \cdot c, b \cdot d$$$; find the maximum of these and print it.
Proof by exchange argument: No matter what you pick $$$x$$$ to be, you can improve the product by either maximizing or minimizing $$$y$$$, based on the sign of $$$x$$$. Same argument applies when $$$x$$$ and $$$y$$$ are swapped.
C – Ubiquity
There are $$$10$$$ possible digits among $$$n$$$ positions, thus the total number of sequences that satisfy the first constraint is $$$10^n$$$.
Now we want to subtract sequences that violate the second or third constraints. We can construct $$$9^n$$$ sequences without any 0s (violating the second constraint), same with sequences without any 9s (violating the third constraint).
However if we subtract $$$2 \cdot 9^n$$$, note that we have subtracted the sequences that violate both the second and third constraints twice. There are $$$8^n$$$ such sequences (as they lack both 0s and 9s), so we add them back to get the correct count. This pattern of overcounting, adding, overcounting, subtracting, repeat until a final accurate count is known as the inclusion-exclusion principle.
Final answer is thus $$$10^n - 2 \cdot 9^n + 8^n$$$. This can be calculated in $$$O(n)$$$ via repeated multiplication, or $$$O(\log n)$$$ via fast exponentiation. If you don't know how to work with large numbers under a modulus, see my tutorial on that subject.
D – Redistribution
Can be done with basic dynamic programming. Let $$$dp_i$$$ represent the number of sequences whose sum is $$$i$$$. $$$dp_0 = 1$$$, the empty sequence. Then $$$\displaystyle dp_i = \sum_{j=0}^{i-3} dp_j$$$, i.e. the sum of $$$dp_j$$$ for all integers $$$j$$$ between $$$0$$$ and $$$i-3$$$ inclusive, representing the addition of a number $$$\ge 3$$$ to each of the previous sequences.
This can be easily calculated in $$$O(s^2)$$$ and is good enough to pass, however it's easy to improve to $$$O(s)$$$ by accumulating the prefix sum.
An $$$O(\log s)$$$ solution is possible via matrix exponentiation.