Translation Notice
This article was machine-translated using DeepSeek-R1.
- Original Version: Authored in Chinese by myself
- Accuracy Advisory: Potential discrepancies may exist between translations
- Precedence: The Chinese text shall prevail in case of ambiguity
- Feedback: Technical suggestions regarding translation quality are welcomed
C - Knight Fork
Problem Statement
Determine if there exists an integer coordinate point on the 2D plane whose Euclidean distances to both $(x_1,y_1)$ and $(x_2,y_2)$ are $\sqrt{5}$.
Input Format
$x_1~y_1~x_2~y_2$
Output Format
Print Yes
if such a point exists; otherwise, print No
.
Sample Cases
$x_1$ | $y_1$ | $x_2$ | $y_2$ | Output |
---|---|---|---|---|
$0$ | $0$ | $3$ | $3$ | Yes |
$0$ | $1$ | $2$ | $3$ | No |
$1000000000$ | $1000000000$ | $999999999$ | $999999999$ | Yes |
Analysis
$$ (a-c)^2+(b-d)^2=5\\ \{a-c,b-d\}=\{1,2\} $$
Thus, for the point $(0,0)$, the points with distance $\sqrt{5}$ are shown below (similar for other points):
We generate all points with distance $\sqrt{5}$ from $(x_1,y_1)$ and check their distances to $(x_2,y_2)$.
Code
|
|
D - Prime Sum Game
==Water problem warning==
Problem Statement
Takahashi and Aoki play a game as follows:
- Takahashi selects an integer $A\le N\le B$.
- Aoki selects an integer $C\le M\le D$.
- If $N+M$ is prime, Aoki wins. Otherwise, Takahashi wins.
Determine the winner when both play optimally.
$1\le A\le B\le 100$
$1\le C\le D\le 100$
Input Format
$A~B~C~D$
Output Format
Print the winner’s name: Takahashi
or Aoki
.
Sample Cases
$A$ | $B$ | $C$ | $D$ | Output |
---|---|---|---|---|
$2$ | $3$ | $3$ | $4$ | Aoki |
$1$ | $100$ | $50$ | $60$ | Takahashi |
$3$ | $14$ | $1$ | $5$ | Aoki |
Analysis
The key is understanding “optimal play”:
- Takahashi wins if there exists an $N$ such that $N+M$ is never prime for any $M$.
- Aoki wins if for every $N$, there exists an $M$ making $N+M$ prime.
Given small constraints, we brute-force all pairs. Time complexity is $\mathcal O(BD)$.
Code
P.S. Surprisingly, runtime is 4ms (expected ~30ms).
|
|
E - Subtree K-th Max
Problem Statement
Given a tree with $N$ nodes (1-based) rooted at node 1. Each node $v$ has a value $X_v$.
Answer $Q$ queries $(V_i,K_i)$:
- Find the K-th largest value (without deduplication) in the subtree rooted at $V_i$.
$2\le N,Q\le 10^5$
$0\le X_i\le 10^9$
$1\le A_i,B_i,V_i\le N$
==$1\le K_i\le 20$==
Input Format
$N~Q$
$X_1~\dots~X_N$
$A_1~B_1$
$\vdots$
$A_{N-1}~B_{N-1}$
$V_1~K_1$
$\vdots$
$V_Q~K_Q$
Output Format
Print $Q$ lines. The i-th line contains the answer to the i-th query.
Sample Cases
See AtCoder for details.
Analysis
Given $1\le K\le 20$, precompute the top-20 values for each node’s subtree. Use DFS to merge node values and children’s top-20 lists. Sorting can be optimized with priority_queue
. Time complexity is $\mathcal O(N+Q)$.
Code
Sample implementation uses DFS with priority_queue
(190ms runtime):
|
|