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$$$.
Time complexity: $$$O(1)$$$