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
A - Three Dice
A person rolled three dice with top faces showing $a$, $b$, and $c$. Find the sum of their bottom faces.
The dice used are standard, meaning the sum of opposite faces is $7$.
$1\le a,b,c\le 6$
Input Format
$a~b~c$
Output Format
Print the answer.
Samples
$a$ | $b$ | $c$ | Answer |
---|---|---|---|
$1$ | $4$ | $3$ | $13$ |
$5$ | $6$ | $4$ | $6$ |
Analysis
Since opposite faces sum to $7$, the answer is $(7-a)+(7-b)+(7-c)=21-a-b-c$.
Code
|
|
B - 180°
Given a string $S$ consisting of 0
, 1
, 6
, 8
, 9
. Rotate it 180° and output the result.
How to rotate a string 180°:
- Reverse the string.
- Replace
6
with9
and9
with6
.
$1\le |S|\le10^5$
Input Format
$S$
Output Format
Print the rotated string.
Samples
$S$ | Output |
---|---|
0601889 |
6881090 |
86910 |
01698 |
01010 |
01010 |
Analysis
Directly simulate the rotation process.
Code
|
|
C - Made Up
Given three sequences $A$, $B$, $C$ of length $N$. Count the number of pairs $(i,j)$ satisfying $A_i=B_{C_j}$.
$1\le N\le 10^5$
$1\le A_i,B_i,C_i\le N$
Input Format
$N$
$A_1~A_2~\dots~A_N$
$B_1~B_2~\dots~B_N$
$C_1~C_2~\dots~C_N$
Output Format
Print the count.
Samples
Sample Input 1
|
|
Sample Output 1
|
|
Valid pairs: $(1,1),(1,3),(2,2),(3,2)$.
Sample Input 2
|
|
Sample Output 2
|
|
All pairs are valid.
Sample Input 3
|
|
Sample Output 3
|
|
No valid pairs.
Analysis
An $O(n^2)$ brute-force approach would TLE. Instead, count frequencies using bucket arrays $\mathrm{acnt}$ and $\mathrm{bcnt}$, then compute the sum $\sum_{i=1}^n\mathrm{acnt}_i\mathrm{bcnt}_i$.
Code
Note: Use long long
to avoid overflow!
|
|
D - aab aba baa
Find the $K$-th lexicographically smallest string consisting of $A$ a
s and $B
b`s.
$1\le A,B\le30$
$1\le K\le S$ ($S$ is the total number of valid strings)
Input Format
$A~B~K$
Output Format
Print the required string.
Samples
$A$ | $B$ | $K$ | Output |
---|---|---|---|
$2$ | $2$ | $4$ | baab |
$30$ | $30$ | $118264581564861424$ | (30 b s + 30 a s) |
Analysis
Let $\mathrm{dp}(a,b)$ be the count of strings with $a$ a
s and $b` `b`s. The recursive formula is:
$\mathrm{dp}(a,b)=\mathrm{dp}(a-1,b)+\mathrm{dp}(a,b-1)$.
Construct the string greedily by comparing $K$ with $\mathrm{dp}(a-1,b)$ at each step.
Code
|
|
E - Count Descendants
Given an $N$-node tree rooted at node 1. The parent of node $i$ ($2\le i\le N$) is $P_i$. Answer $Q$ queries: count nodes $u$ where:
- The path from $u$ to root has exactly $D_i$ edges.
- $U_i$ lies on this path (including endpoints).
$1\le N\le 2\times10^5$
$1\le P_i
$1\le Q\le 2\times10^5$
$1\le U_i\le N$
$0\le D_i < N$
Input Format
$N$
$P_2~P_3~\dots~P_N$
$Q$
$U_1~D_1$
$U_2~D_2$
$\vdots$
$U_Q~D_Q$
Output Format
Print $Q$ lines, each containing the answer.
Samples
Sample Input
|
|
Sample Output
|
|
Explanation:
- Query 1: Nodes 4,5,7.
- Query 2: Node 7.
- Queries 3-4: No valid nodes.
Analysis
Preprocess each node’s entry/exit timestamps via DFS. For each depth $d$, collect all entry times. For a query $(U_i,D_i)$, count nodes at depth $D_i$ whose entry time is between $U_i$’s entry and exit times using binary search.
Code
|
|