Contest hosted on DMOJ https://dmoj.ca/contest/dcc1
P1 — The Cathedral of Learning
If $$$a > b$$$, the answer is NO
, as Alice always goes up and Bob always goes down. Otherwise, $$$a \leq b$$$, and the answer is YES
if and only if the parity (oddness or evenness) of $$$a$$$ and $$$b$$$ is equal (equivalently, that $$$b - a$$$ is even). This is because the difference between them reduces by $$$2$$$ each step, and if the difference is odd, they'll end up on neighboring floors and then pass and miss each other on the next step.
Time complexity: $$$O(1)$$$
P2 — Square Sum
Assume that $$$A \leq B$$$ (otherwise, swap them). Now let's count the valid pairs $$$(a, b)$$$ where $$$a + b = c^2$$$ for some non-negative integer $$$c$$$.
Let's also define a useful function $$$\displaystyle P(n) = \sum_{i = 1}^{n} n^2 = \frac {n(n+1)(2n+1)} {6}$$$, representing the sum of the first $$$n$$$ positive integer squares, or the $$$n$$$-th pyramidal number. Note that computing this might overflow a standard 64-bit integer type, so you either need to use modular inversion, or use a 128-bit type if your programming language supports it.
We could split the possibilities into three cases, and sum their results:
- $$$0 \leq c^2 \leq A$$$. Each such valid $$$c$$$ has $$$c^2 + 1$$$ possible pairs: $$$(0, c^2), (1, c^2 - 1), ..., (c^2, 0)$$$. Thus, the total number of pairs from this case is $$$\displaystyle \sum_{c = 0} ^ {\lfloor\sqrt{A}\rfloor} c^2 + 1 = P(\lfloor\sqrt{A}\rfloor) + \lfloor\sqrt{A}\rfloor + 1$$$.
- $$$A < c^2 \leq B$$$. Each such valid $$$c$$$ has $$$A + 1$$$ possible pairs: $$$(0, c^2), (1, c^2 - 1), ..., (A, c^2 - A)$$$. Thus, the total number of pairs from this case is $$$(\lfloor\sqrt{B}\rfloor - \lfloor\sqrt{A}\rfloor) \cdot (A + 1)$$$.
- $$$B < c^2 \leq A+B$$$. Each such valid $$$c$$$ has $$$A + B + 1 - c^2$$$ possible pairs: $$$(c^2 - B, B), (c^2 - B + 1, B - 1), ..., (A, c^2 - A)$$$. Thus, the total number of pairs from this case is $$$\displaystyle \sum_{c = \lfloor\sqrt{B}\rfloor + 1} ^ {\lfloor\sqrt{A + B}\rfloor} A + B + 1 - c^2 = (\lfloor\sqrt{A + B}\rfloor - \lfloor\sqrt{B}\rfloor) \cdot (A + B + 1) - (P(\lfloor\sqrt{A + B}\rfloor) - P(\lfloor\sqrt{B}\rfloor))$$$.
Time complexity: $$$O(T)$$$ or $$$O(T \log (A + B))$$$, depending on how you implement the floored square roots.
P3 — Soccer Court
Let's define the balance of a court to be the sum of the numbers of the left half, minus the sum of the numbers of the right half. It is easy to see that the balance of a valid court should be $$$0$$$.
First, we fix the column the midline should be between (let's say it's between column $$$x$$$ and $$$x+1$$$). We slowly increase the width of the prospective court until at least one of the borders can't be extended further. Let's initialize an array $$$C_0, ... C_N$$$, at first with all zeros. Now for each width $$$k$$$ (increasing from $$$1$$$ upward), we want to add $$$\displaystyle \sum_{i = 1}^{j} s_{i, x - (k - 1)} - s_{i, x + k}$$$ to each $$$C_j$$$. This can be done in $$$O(N)$$$ using a variable for the running sum.
Now, note that the balance of a court with top row $$$i$$$ and bottom row $$$j$$$ is $$$C_j - C_{i-1}$$$. Thus, this reduces to finding $$$x, y$$$ where $$$C_x = C_y$$$ and maximum $$$y - x$$$; this can be done using a hashmap in $$$O(N)$$$.
This procedure needs to be run for $$$O(M^2)$$$ possible combinations of midlines and width, so the total running time is $$$O(NM^2)$$$.