现充|junyu33

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 n snakes, numbered 1,,n. Their initial powers are positive integers ai, satisfying

a1a2an.

At any moment, snakes are compared by (power,index): 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 (x,i) and the weakest snake is (y,j), then after eating, the weakest snake dies and the strongest snake becomes

(xy,i).

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. n106.

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,

a1<a2<<an,

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:

Let us focus on the sentence “determined by the parity in Phase 2.” First consider a dangerous step:

Thus, by the reasoning above, when n is odd the strongest snake will refuse to eat; when n is even it will eat, turning the game into the odd-n case, and then the game immediately stops. The final number of snakes is odd, exactly what we want to prove.

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 ui=(bi,id(ui)), corresponding to power and index. Sort the current state from weakest to strongest:

u1<u2<<um1<um

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 1, which satisfies the claim. After um eats u1, the updated snake is um=(bmb1,id(um)), and um is the weakest snake.

By induction, we need to prove that the next step is still dangerous. More concretely, the current strongest snake um1 will eat the weakest snake um, and its new power becomes bm1(bmb1)=bm1bm+b1, with index id(um1). We need to ensure that (bm1bm+b1,id(um1))<u2, which is equivalent to proving:

bm1bm+b1<b2(bm1bm+b1=b2id(um1)<id(u2))

Since u1<u2<<um1<um guarantees bm1bm, the first branch is immediate if the inequality is strict. If equality holds, meaning (b1=b2)(bm1=bm), we need to prove id(um1)<id(u2). This is the core and difficult part of the problem.

Main Invariants and Brief Intuition

To prove this inequality, the following five invariants are used:

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 A,B,C,D,E hold, and then prove that after the strongest snake eats, the next state still satisfies A,B,C,D,E. The dependencies are shown in the table below:

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 ιr=id(ur).

A'

If three snakes with the same power appear, then the newly inserted snake must have power Δ=bmb1 and coincide with two old snakes that also have power Δ. This contradicts the previous assumption B. Therefore A holds.

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 um eats u1, its power is d=Δ=bmb1, while the new width satisfies Δbmb2. If b1b2, then we immediately get Δ<Δ=d. If b1=b2, meaning the two weakest snakes have the same power, property B already says Δ<b1. Therefore, after um eats u1, there is actually an old snake whose power is also bmb1<b1 in order to pair with the new snake, which contradicts the assumption that u1,u2 are the two weakest snakes.

If the pair already existed before, then the previous property B already gives d>Δ. If the new snake is not the weakest snake, then ΔΔ<d directly. Now consider the case where the new snake, which is also the weakest snake, has power Δ. Then Δ=bm1Δ. We want to prove d>Δ. Since Δ=bm1ΔbmΔ=b1, we have Δb1. On the other hand, if d=b1, then the original state would already have a pair of snakes with power b1, and together with the eaten snake u1 there would be three snakes with the same power, contradicting property A. Hence db1. And since this pair is not the weakest pair, otherwise u1 would not have been the eaten snake, we get d>b1. Combining this with Δb1 gives Δ<d.

C'

C is easy to understand intuitively: the new snake will not be inserted to the left of the low-power region. Even if the powers are equal, its larger index will place it farther to the right.

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 D, the rightmost snake um has an index larger than the old equal pair, so no matter where this snake is inserted, the conclusion is unaffected.

For the case where a new equal pair appears, suppose the new equal pair is formed when v=(Δ,ιm) collides with an old snake uk satisfying bk=Δ. We only need to consider whether ιm>ιk holds. Since we are discussing a “non-bottom equal pair,” if k=2, then after deleting u1, u2 and v would form the bottom equal pair, a contradiction. Therefore k3. This old snake uk lies in the low-power region, and um is to the right of uk in the original state. Therefore, by property C, the index of um is greater than the index of uk.

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 E by induction. The fourth case cannot happen by A. Property B says that the power Δ of the new equal pair must be smaller than the power d of any existing old equal pair, so the second case, old-new, is also impossible.

Only the new-old case remains. Again suppose the new equal pair is formed when v=(Δ,ιm) collides with an old snake uk satisfying bk=Δ. Let the two snakes in the higher old pair be ut,ut+1. We need to prove ιt<max(ιk,ιm). Here we prove the stronger statement ιt<ιm. By property D, before um eats, it must be to the right of ut,ut+1, because the pair is not at the very bottom. Therefore ιt<ιm holds.

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 id(um1)<id(u2);

  • 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

(x,i)

where x is its power and i is its index. Define the strength order

(x,i)(y,j)

if and only if

x<y,

or

x=yandi<j.

Thus, the larger an element is under , the stronger it is.


Theorem

Starting from an initial state

a1<a2<<an,

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 m snakes, ordered by strength as

u1u2um.

Write

ur=(br,ιr),

where br is the power and ιr=id(ur).

Then

b1b2bm.

If br=br+1, then since the snake with larger index is stronger, we have

ιr<ιr+1.

After the current strongest snake um eats the current weakest snake u1, the new snake is

v=(bmb1,ιm).

Define the width of the current state as

Δ=bmb1.

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.

b1=b2,bm1=bm,

then

ιm1<ι2.

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

br=br+1=d,

then

d>Δ=bmb1.

Invariant C: Index Monotonicity in the Low-Power Region

If

r3,brΔ,

then for every s>r,

ιr<ιs.

Invariant D: For a Non-Bottom Equal Pair, the Left Endpoint Has Smaller Index Than Every Snake to Its Right

If

r2,br=br+1,

then for every s>r+1,

ιr<ιs.

Invariant E: Crossing Index Relation Between Two Equal Adjacent Pairs

If

br=br+1,bs=bs+1,r<s,

then

ιs<ιr+1.

We prove that A–E hold for all forced-reachable states.


The Initial State Satisfies the Invariants

In the initial state,

a1<a2<<an.

Thus there are no equal-power pairs, so A, B, D, and E are all vacuously true.

Moreover, the initial ordering is exactly

(a1,1)(a2,2)(an,n),

so if r3, then for every s>r,

ιr=r<s=ιs.

Therefore C also holds.


Induction Step

Assume a forced-reachable state

u1u2um

satisfies A–E.

Write

ur=(br,ιr),Δ=bmb1.

In the next forced step, the new snake is

v=(Δ,ιm).

The next state is obtained by reordering

u2,u3,,um1,v,

and we denote it by

w1w2wm1.

Let their powers be

c1c2cm1,

and let the width of the next state be

Δ=cm1c1.

The multiset of powers in the next state is

{b2,b3,,bm1,Δ}.

Thus

c1=min(b2,Δ),cm1=max(bm1,Δ).

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 d.

There are two cases.


Case 1: It Is an Old Equal Pair

That is, this equal pair does not contain the new snake v, but consists of two old snakes already present in the previous state.

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,

d>Δ.

We need to prove

d>Δ.

Since d>Δ and dbm1, we have

cm1=max(bm1,Δ)=bm1.

If

c1=b2,

then

Δ=bm1b2bmb2=(bmb1)(b2b1)Δ.

Hence

d>ΔΔ.

If

c1=Δ,

then

Δ=bm1ΔbmΔ=b1.

This old equal pair does not contain u1. If d=b1, then the previous state would contain at least the three snakes u1 and these two old snakes, all with power b1, contradicting A. Therefore

d>b1.

Hence

d>b1Δ.

Thus any old equal pair satisfies B.


Case 2: It Is a Newly Created Equal Pair

This means the power of the new snake v,

Δ,

is exactly the power of some old snake uk:

bk=Δ,2km1.

Thus the power of the new equal pair is

d=Δ.

Since bk=Δb2, we have

c1=b2.

We need to prove

Δ>Δ.

If

b1=b2,

then the previous state has a bottom equal pair u1,u2. By the induction hypothesis B,

b1>Δ.

On the other hand,

Δ=bkb2=b1,

a contradiction.

Therefore

b1<b2.

Also,

cm1bm=b1+Δ.

Hence

cm1<b2+Δ.

Therefore

Δ=cm1b2<Δ.

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

v=(Δ,ιm).

If v joins an already existing old equal pair, then three snakes of the same power would be created.

But if the previous state already had two old snakes of power Δ, then it would contain an equal adjacent pair of power Δ. This contradicts the induction hypothesis B, because B requires every equal adjacent pair of power d to satisfy

d>Δ.

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

r3,crΔ,

then for every s>r,

id(wr)<id(ws).

First, wr cannot be the new snake v.

If wr=v, then

cr=Δ.

Since r3, there are at least two snakes before v in the next state. Hence

b2Δ.

Thus

c1=b2.

If b1=b2, then the previous state has a bottom equal pair, and by B,

b1>Δ,

contradicting b2Δ.

Therefore

b1<b2.

Then

cm1bm=b1+Δ<b2+Δ,

so

Δ=cm1b2<Δ.

But if wr=v, then cr=Δ, contradicting crΔ.

Therefore wr must be an old snake. Let

wr=uk.

After deleting u1, the old snake u2 can be at most the second snake in the next state; since r3, we have

k3.

We now prove

bkΔ.

Assume the contrary:

bk>Δ.

If

c1=b2,

then

Δ=cm1b2bmb2=(bmb1)(b2b1)Δ.

Thus

bk>ΔΔ,

contradicting

bk=crΔ.

If

c1=Δ<b2,

then

cm1=bm1,

so

Δ=bm1Δ.

From bkΔ, we get

bk+Δbm1bm=b1+Δ.

Therefore

bkb1.

But k3, so

bkb2b1.

Hence

b1=b2=bk,

which gives three snakes of the same power, contradicting A.

Therefore

bkΔ.

By C in the previous state, since

k3,bkΔ,

we know that for every t>k,

ιk<ιt.

Every snake to the right of wr=uk in the next state is either an old snake ut with t>k, or the new snake v, whose index is ιm, with m>k. Therefore

ιk<id(ws)

for every s>r.

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:

wr,wr+1,r2,

with

cr=cr+1.

We need to prove that for every s>r+1,

id(wr)<id(ws).
Case 1: It Is an Old Equal Pair

Suppose this pair was

uk,uk+1

in the previous state.

Since it does not contain the deleted snake u1, we have

k2.

By D in the previous state,

ιk<ιt

for every

t>k+1.

Every snake to the right of this old equal pair in the next state is either an old snake ut with t>k+1, or the new snake v, whose index is ιm, with m>k+1. Therefore the conclusion holds.


Case 2: It Is a Newly Created Equal Pair

This means that for some 2km1,

bk=Δ,

and the new equal pair consists of the old snake uk and the new snake v.

Since this equal pair is not a bottom equal pair in the next state, i.e. r2, after deleting u1 there is still at least one snake before them. Therefore

k3.

By C in the previous state, since

k3,bk=Δ,

we know that for every t>k,

ιk<ιt.

In particular,

ιk<ιm.

Therefore, within this new equal pair, the left endpoint is the old snake uk, not the new snake v.

Every snake to the right of this new equal pair in the next state is an old snake ut with t>k. By the inequality above,

ιk<ιt.

Thus D holds in the next state.


5. Proof of E in the Next State

Take two equal adjacent pairs in the next state:

wr,wr+1

and

ws,ws+1,

where

r<s.

We need to prove

id(ws)<id(wr+1).
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

id(ws)<id(wr+1).
Case 2: The Lower Equal Pair Is New, and the Higher Equal Pair Is Old

The new equal pair consists of

v=(Δ,ιm)

and some old snake uk.

Thus the right endpoint index of this new equal pair is

max(ιk,ιm).

Suppose the higher old equal pair was

ut,ut+1

in the previous state.

This old equal pair does not contain u1, so

t2.

By D in the previous state,

ιt<ιm.

In the next state, the left endpoint index of this higher old equal pair is still ιt. Therefore

id(ws)=ιt<ιmmax(ιk,ιm)=id(wr+1).

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 Δ. Therefore the new equal pair is weaker than every old equal pair, and it cannot be the higher equal pair.


Case 4: Both Equal Pairs Are New

This case is also impossible, because one step inserts only one new snake v, so it can participate in at most one equal adjacent pair; moreover, A guarantees that no three snakes have the same power.

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

b1=b2,bm1=bm,

then take

r=1,s=m1

in E, and get

ιm1<ι2.

That is,

id(um1)<id(u2).

3. Safe Steps and Dangerous Steps

Now return to the forced process.

In the state

u1u2um,

we force the strongest snake um to eat the weakest snake u1, obtaining the new snake

v=(bmb1,ιm).

If v is not the weakest snake in the next state, we call this step a safe step.

If m3 and v becomes the weakest snake in the next state, we call this step a dangerous step.

When m=2, after eating there is only one snake left, so the eater necessarily survives; therefore the step with m=2 is regarded as a safe step.


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

u1um,

and the current strongest snake is um. After eating, it becomes

p=(Δ,ιm),Δ=bmb1.

Since this is a safe step, p is not the weakest snake in the next state.

We first prove a protection property:

In the subsequent evolution of the forced process, before p next becomes the strongest snake, p will not become the weakest snake.

Proof.

First, a safe step implies

b1<b2.

Otherwise, if

b1=b2,

then there is a bottom equal pair. By invariant B,

b1>Δ.

Hence

b2>Δ,

which means the new snake p is weaker than u2, so p would become the weakest snake, contradicting that the step is safe.

Therefore

b1<b2.

Now consider the state after the eating. Partition the snakes other than p into two classes:

L={x:xp},

the snakes weaker than p, and

H={x:px},

the snakes stronger than p.

Since this step is safe,

L.

If

H=,

then p is already the strongest snake, and the conclusion is immediate.

Now assume H.

We prove that the following property is preserved:

For any

h=(X,α)H,=(Y,β)L,

we have

(XY,α)p.

Initially, both h and are old snakes from the previous state. Hence

Xbm,Yb2.

Therefore

XYbmb2<bmb1=Δ.

So

(XY,α)p.

Now assume this property holds at some moment, and that p is not yet the strongest snake.

Since H, the current strongest snake lies in H; since L, the current weakest snake lies in L.

Let the current strongest snake be

h=(X,α),

and the current weakest snake be

=(Y,β).

The next forced step produces

z=(XY,α).

By the preservation property,

zp.

Thus z enters L and does not move to the right of p.

It remains only to check that the preservation property still holds.

For old hH{h} and old L{}, the property already held.

We only need to check the new weaker snake z. Take any old

h=(X,α)H{h}.

Since h is the current strongest snake,

hh,

so

XX.

Thus

X(XY)=XX+YY.

If

Y<Δ,

then

X(XY)<Δ,

so

(X(XY),α)p.

If

Y=Δ,

then and p have the same power, and p.

At this moment and p are the two weakest snakes in the current state. If equality occurs in the expression above, i.e.

X(XY)=Δ,

then necessarily

X=X.

Thus h and h have the same power and are the two strongest snakes in the current state.

By the core invariant,

id(h)<id(p).

Therefore even if the powers are the same,

(X(XY),α)p.

Hence the preservation property holds.

Therefore, as long as p has not become the strongest snake, each forced step moves one snake from H into L, and the new snake never moves to the right of p. The snake p itself neither becomes the weakest snake nor gets eaten.

Thus p either eventually becomes the strongest snake, or the game stops before that; in either case, it does not die before then.

Now return to the rational game.

If, starting from the state after the eating, the game stops before p becomes the strongest snake, then p clearly survives.

If the game reaches a state where p becomes the strongest snake, then it is p's turn to decide. By the game rule, p eats only if it will eventually survive; otherwise it refuses and survives immediately.

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 3 snakes is also dangerous.

Consider a forced state

u1u2um,m4.

Suppose this step is dangerous. That is, after the strongest snake um eats the weakest snake u1, the new snake

v=(bmb1,ιm)

becomes the weakest snake in the next state.

Thus

vu2.

In the next state, the strongest snake is the old um1, and the weakest snake is v.

The next forced step makes um1 eat v, producing the new snake

q=(bm1(bmb1),ιm1).

Its power is

bm1bm+b1.

Since

bm1bm,b1b2,

we have

bm1bm+b1b2.

If the inequality is strict, then q is clearly the new weakest snake, so the next step is still dangerous.

The only case that requires attention is equality.

Equality holds if and only if

bm1=bm,b1=b2.

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,

ιm1<ι2.

The power of q is equal to b2, and its index is ιm1. Therefore

q=(b2,ιm1)(b2,ι2)=u2.

Thus even when the powers are equal, q is still weaker than u2, and it again becomes the weakest snake in the next state.

Therefore the next step is also dangerous.

Hence, once a step with m4 is dangerous, the step with m1 snakes is also dangerous.

Thus, if dangerous steps occur at all, they form a consecutive suffix:

ShSh1S3S2,

where

Sk

denotes the forced-process state with k snakes.

Note that in the step S2S1, after eating there is only one snake left, so the eater necessarily survives; we regard this step as safe.


6. Decisions in the Rational Game

Now consider the actual rational game.

The forced process gives a sequence of states

Sn,Sn1,,S1.

The actual game can only follow this forced path until the current strongest snake refuses at some step.

Let

Em

denote the assertion: in state Sm, the current strongest snake will eat.

We have already proved:

If SmSm1 is a safe step, then

Em=true.

In particular,

E2=true,

because after eating only the eater remains.

Now consider a dangerous step.

If SmSm1 is dangerous and m3, then after eating, the current strongest snake becomes the weakest snake in Sm1.

Therefore whether it survives depends exactly on whether the strongest snake in Sm1 eats:

Thus, for a dangerous step,

Em=¬Em1.

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

1,

which is odd.


Case 2: The Forced Process Has Dangerous Steps

Let the first state in the dangerous suffix be

Sh.

That is:

ShSh1,Sh1Sh2,,S3S2

are all dangerous steps, while every

SmSm1(m>h)

is a safe step.

Therefore the actual game will certainly first reach Sh.

In the dangerous suffix, we have

E2=true,

and for 3mh,

Em=¬Em1.

Hence

E3=false,E4=true,E5=false,E6=true,

and so on.

That is,

Em=truem is even,Em=falsem is odd.

Therefore:

If h is odd, then in Sh the strongest snake refuses, and the game stops with h snakes, an odd number.

If h is even, then in Sh the strongest snake eats, and the game enters Sh1. Since h1 is odd, the strongest snake in Sh1 refuses, and the game stops with h1 snakes, also an odd number.


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:

The number of surviving snakes at the end is always odd.