Revisiting CSP-S 2020 T4 "Greedy Snake" Six Years Later
Problem Introduction
Six years ago, when I took CSP-S 2020, my coding skills were not particularly polished. After finishing the first two problems, I did not have much time left. So when I looked at T4, Snakes, I only wrote a 20-point special-case solution and left in a hurry. Six years later, I learned from Jiang Yanyan's video that many things had changed again in the OI community. This brought me back to Luogu, where I used to practice problems, and I found that after enough attempts, an LLM could even AC a newly released black problem that only the problem setter had solved.
Of course, that is not the main point today. The point is that I returned to the T4 Snakes problem where I once scraped together 20 points. A simplified statement is:
There are
snakes, numbered . Their initial powers are positive integers , satisfying At any moment, snakes are compared by
: the snake with larger power is stronger; if powers are equal, the snake with larger index is stronger. Therefore the strongest and weakest snakes are uniquely determined. The current strongest snake may choose whether to eat the current weakest snake. If the strongest snake is
and the weakest snake is , then after eating, the weakest snake dies and the strongest snake becomes Every snake is rational: when it becomes the current strongest snake, it eats the current weakest snake if and only if, under the same rules afterward, it will not eventually die. Otherwise, it refuses to eat, and the game ends immediately. The game also ends when only one snake remains.
The task is to compute how many snakes remain at the end.
.
There are already many solutions to this problem. However, while trying to revisit this problem set, I accidentally came up with another conjecture:
If the initial powers are strictly increasing rather than merely nondecreasing, that is,
then is the final number of remaining snakes always odd?
The answer is yes, but the proof is much more tedious than I had expected, even more tedious than the idea and implementation of the original problem itself. More importantly, the complete proof was generated entirely by an LLM, which seems to echo the earlier observation about LLMs ACing black problems.
Brief Idea
Here I will first break down the problem in a more human way of thinking, and in the next section give the AI-generated bottom-up Lemma-Theorem style proof.
Starting from the Original Solution
According to a solution to the original problem, in the general nondecreasing version there are two phases:
- Phase 1, or safe steps: after every strongest snake eats, it does not become the weakest snake, so it can eat without worry.
- Phase 2, or dangerous steps: after every strongest snake eats, it becomes the weakest snake, until some snake can safely eat again, meaning that after eating it either is not the weakest snake or only two snakes remain.
- Once Phase 1 ends, the game is basically over, with whether one more snake can be eaten determined by the parity in Phase 2.
Let us focus on the sentence “determined by the parity in Phase 2.” First consider a dangerous step:
- If there are only two snakes, the strongest snake's choice is obvious: it must eat.
- If there are three snakes, after the strongest snake eats, it becomes the weakest snake, so it will certainly be eaten in the next round. Therefore the strongest snake will refuse to eat.
- If there are four snakes, the strongest snake will certainly eat, because after it becomes the weakest snake, the conclusion from the previous line says that the current strongest snake will refuse to eat it.
- And so on.
Thus, by the reasoning above, when
So we have a very promising picture: if, once dangerous steps begin, the process never switches back to safe steps, then the problem is essentially solved. I tested a large number of random strictly increasing sequences and found no counterexample. In fact, if we weaken “strictly increasing” to “nondecreasing,” counterexamples appear quickly, for example 1 1 1 1, where the process immediately returns to Phase 1 after the first eating. This shows that the condition that the final number is odd is actually quite restrictive, which further strengthens the possibility that strictly increasing sequences really do satisfy “Phase 2 never switches back to Phase 1.”
The Core Inequality
Our approach is induction. First simulate one step where the strongest snake eats the weakest snake. Let the remaining snakes be
Assume the current step is a dangerous step. If this initial condition never holds, then the whole process consists only of safe steps, and the final answer is certainly
By induction, we need to prove that the next step is still dangerous. More concretely, the current strongest snake
Since
Main Invariants and Brief Intuition
To prove this inequality, the following five invariants are used:
: There will never be three snakes with the same power. : If a pair of snakes with the same power appears, then this power must be greater than the current “strongest power minus weakest power,” denoted by . : For a snake in a low-power region, where and its power is at most , if is to the right of , then . : For an equal pair that is not at the very bottom, meaning its position satisfies , the snake on the left has an index smaller than every snake to its right. : Two equal pairs with different powers must have interleaving indices. For example, if and , then . The desired is a direct corollary.
Note that all five properties we want to prove hold in both safe steps and dangerous steps, and they are obviously true in the strictly increasing initial state. So the argument is still a kind of simultaneous induction: assume
| Property to prove in the next state | Main properties used in the current state |
|---|---|
| A' | B |
| B' | A, B |
| C' | A, C |
| D' | C, D |
| E' | A, B, D, E |
Below is the intuition for each property. For convenience, write
A'
If three snakes with the same power appear, then the newly inserted snake must have power
B'
This is probably the hardest property to prove. There are two cases: whether the equal pair is newly created in this round, or already existed before.
If the pair is newly created, then when snake
If the pair already existed before, then the previous property
C'
D'
There are two cases: an old equal pair remains unchanged, or a new equal pair is inserted. For the former, by the previous property
For the case where a new equal pair appears, suppose the new equal pair is formed when
E'
Here there are four cases, ordered from low to high: old-old, old-new, new-old, and new-new. The first case follows directly from
Only the new-old case remains. Again suppose the new equal pair is formed when
Complete Proof
Note that the following content was generated by GPT 5.5-Pro. I used three prompts in total:
The first prompt gave the problem statement;
The second prompt asked GPT to write out the concrete proof of
; The third prompt was: “Please combine the previous outputs and write a complete proof.”
Here I only include the full output from the third prompt.
We now combine the previous conclusions into a complete proof. Represent a snake as
where
if and only if
or
Thus, the larger an element is under
Theorem
Starting from an initial state
if the game is played according to the rational rule in the statement, then the number of surviving snakes at the end is always odd.
1. The Forced Process and Notation
First define an auxiliary process, called the forced process: regardless of whether a snake is rational, at every step we force the current strongest snake to eat the current weakest snake.
Suppose a forced-reachable state contains
Write
where
Then
If
After the current strongest snake
Define the width of the current state as
2. Core Invariants in the Forced Process
We first prove a stronger family of invariants. The key conclusion we will need later is the following:
If in a forced-reachable state the first two snakes have the same power and the last two snakes also have the same power, i.e.
then
To prove this rigorously, we prove the following five properties simultaneously for all forced-reachable states.
Invariant A: No Three Snakes Have the Same Power
In any forced-reachable state, there cannot be three snakes with the same power.
Invariant B: The Power of an Equal Adjacent Pair Is Greater Than the Current Width
If
then
Invariant C: Index Monotonicity in the Low-Power Region
If
then for every
Invariant D: For a Non-Bottom Equal Pair, the Left Endpoint Has Smaller Index Than Every Snake to Its Right
If
then for every
Invariant E: Crossing Index Relation Between Two Equal Adjacent Pairs
If
then
We prove that A–E hold for all forced-reachable states.
The Initial State Satisfies the Invariants
In the initial state,
Thus there are no equal-power pairs, so A, B, D, and E are all vacuously true.
Moreover, the initial ordering is exactly
so if
Therefore C also holds.
Induction Step
Assume a forced-reachable state
satisfies A–E.
Write
In the next forced step, the new snake is
The next state is obtained by reordering
and we denote it by
Let their powers be
and let the width of the next state be
The multiset of powers in the next state is
Thus
We now prove that A–E still hold in the next state.
1. Proof of B in the Next State
Take an equal adjacent pair in the next state, and let its power be
There are two cases.
Case 1: It Is an Old Equal Pair
That is, this equal pair does not contain the new snake
Since the induction hypothesis A holds, the previous state has no three snakes of the same power. Therefore this old equal pair was also an equal adjacent pair in the previous state. By the induction hypothesis B,
We need to prove
Since
If
then
Hence
If
then
This old equal pair does not contain
Hence
Thus any old equal pair satisfies B.
Case 2: It Is a Newly Created Equal Pair
This means the power of the new snake
is exactly the power of some old snake
Thus the power of the new equal pair is
Since
We need to prove
If
then the previous state has a bottom equal pair
On the other hand,
a contradiction.
Therefore
Also,
Hence
Therefore
Thus the newly created equal pair also satisfies B.
Therefore B holds in the next state.
2. Proof of A in the Next State
The previous state contains no three snakes of the same power.
The only possible new power collision in the next state is caused by the new snake
If
But if the previous state already had two old snakes of power
Therefore the new snake can coincide in power with at most one old snake, and it cannot create three snakes of the same power.
Thus A holds in the next state.
3. Proof of C in the Next State
We need to prove: if
then for every
First,
If
Since
Thus
If
contradicting
Therefore
Then
so
But if
Therefore
After deleting
We now prove
Assume the contrary:
If
then
Thus
contradicting
If
then
so
From
Therefore
But
Hence
which gives three snakes of the same power, contradicting A.
Therefore
By C in the previous state, since
we know that for every
Every snake to the right of
for every
Thus C holds in the next state.
4. Proof of D in the Next State
Take a non-bottom equal adjacent pair in the next state:
with
We need to prove that for every
Case 1: It Is an Old Equal Pair
Suppose this pair was
in the previous state.
Since it does not contain the deleted snake
By D in the previous state,
for every
Every snake to the right of this old equal pair in the next state is either an old snake
Case 2: It Is a Newly Created Equal Pair
This means that for some
and the new equal pair consists of the old snake
Since this equal pair is not a bottom equal pair in the next state, i.e.
By C in the previous state, since
we know that for every
In particular,
Therefore, within this new equal pair, the left endpoint is the old snake
Every snake to the right of this new equal pair in the next state is an old snake
Thus D holds in the next state.
5. Proof of E in the Next State
Take two equal adjacent pairs in the next state:
and
where
We need to prove
Case 1: Both Equal Pairs Are Old Equal Pairs
They were also two equal adjacent pairs in the previous state, and their relative order is unchanged.
By E in the previous state, we immediately get
Case 2: The Lower Equal Pair Is New, and the Higher Equal Pair Is Old
The new equal pair consists of
and some old snake
Thus the right endpoint index of this new equal pair is
Suppose the higher old equal pair was
in the previous state.
This old equal pair does not contain
By D in the previous state,
In the next state, the left endpoint index of this higher old equal pair is still
So E holds.
Case 3: The Lower Equal Pair Is Old, and the Higher Equal Pair Is New
This case is impossible.
The power of a new equal pair must be
By B in the previous state, the power of any old equal pair is strictly greater than
Case 4: Both Equal Pairs Are New
This case is also impossible, because one step inserts only one new snake
Therefore E holds in the next state.
Thus A–E hold for every forced-reachable state.
In particular, E gives the core conclusion we need later:
If in a forced-reachable state
then take
in E, and get
That is,
3. Safe Steps and Dangerous Steps
Now return to the forced process.
In the state
we force the strongest snake
If
If
When
4. A Snake in a Safe Step Always Eats
We prove:
If a step is safe, then the current strongest snake will eat in the rational game.
Suppose the current state is
and the current strongest snake is
Since this is a safe step,
We first prove a protection property:
In the subsequent evolution of the forced process, before
next becomes the strongest snake, will not become the weakest snake.
Proof.
First, a safe step implies
Otherwise, if
then there is a bottom equal pair. By invariant B,
Hence
which means the new snake
Therefore
Now consider the state after the eating. Partition the snakes other than
the snakes weaker than
the snakes stronger than
Since this step is safe,
If
then
Now assume
We prove that the following property is preserved:
For any
we have
Initially, both
Therefore
So
Now assume this property holds at some moment, and that
Since
Let the current strongest snake be
and the current weakest snake be
The next forced step produces
By the preservation property,
Thus
It remains only to check that the preservation property still holds.
For old
We only need to check the new weaker snake
Since
so
Thus
If
then
so
If
then
At this moment
then necessarily
Thus
By the core invariant,
Therefore even if the powers are the same,
Hence the preservation property holds.
Therefore, as long as
Thus
Now return to the rational game.
If, starting from the state after the eating, the game stops before
If the game reaches a state where
Therefore, in a safe step, after the current strongest snake eats the weakest snake, it eventually survives.
Thus rationally it will eat.
5. Dangerous Steps Form a Consecutive Suffix
We now prove:
If a step is dangerous, then every subsequent step down to the state with
snakes is also dangerous.
Consider a forced state
Suppose this step is dangerous. That is, after the strongest snake
becomes the weakest snake in the next state.
Thus
In the next state, the strongest snake is the old
The next forced step makes
Its power is
Since
we have
If the inequality is strict, then
The only case that requires attention is equality.
Equality holds if and only if
That is, in the current state the first two snakes have the same power, and the last two snakes also have the same power.
By the core invariant,
The power of
Thus even when the powers are equal,
Therefore the next step is also dangerous.
Hence, once a step with
Thus, if dangerous steps occur at all, they form a consecutive suffix:
where
denotes the forced-process state with
Note that in the step
6. Decisions in the Rational Game
Now consider the actual rational game.
The forced process gives a sequence of states
The actual game can only follow this forced path until the current strongest snake refuses at some step.
Let
denote the assertion: in state
We have already proved:
If
In particular,
because after eating only the eater remains.
Now consider a dangerous step.
If
Therefore whether it survives depends exactly on whether the strongest snake in
- If the strongest snake in
eats, then it is immediately eaten and dies. - If the strongest snake in
refuses, then the game stops immediately and it survives.
Thus, for a dangerous step,
7. The Final Number of Surviving Snakes Is Odd
There are two cases.
Case 1: The Forced Process Has No Dangerous Steps
Then every step is safe.
By the result for safe steps, the current strongest snake eats at every step.
Thus the game continues until only one snake remains.
The final number of surviving snakes is
which is odd.
Case 2: The Forced Process Has Dangerous Steps
Let the first state in the dangerous suffix be
That is:
are all dangerous steps, while every
is a safe step.
Therefore the actual game will certainly first reach
In the dangerous suffix, we have
and for
Hence
and so on.
That is,
Therefore:
If
If
Conclusion
Whether or not the forced process contains dangerous steps, the actual rational game always ends with an odd number of surviving snakes.
Therefore the original claim holds: