Backstory: "Problem statement v2" was a problem I tried to propose for our round. Unfortunately, a really similar version ("Problem statement v1") has been used before. But then we had the question of if these two problem statements were actually the same or different. Are the answers to both problems always going to be the same, or is there an example out there where they differ?
Problem statement v1
This is a problem you might have seen before.
Eric has $$$n$$$ (where $$$n$$$ is even) poker chips. They are currently in a stack, the bottom one is numbered $$$1$$$, the second-bottom one is numbered $$$2$$$, ..., the top one is numbered $$$n$$$. Eric will shuffle the poker chips by interleaving the bottom half and the top half together. Formally, this permutes the stack from
$$$1, 2, ..., n$$$to
$$$1, \frac{n}{2}+1, 2, \frac{n}{2}+2, 3, \frac{n}{2}+3, ..., \frac{n}{2}, n.$$$How many shuffles must he perform to revert to the starting position? (Find the minimum number)
Constraints: $$$n \le 200{,}000$$$.
Solution v1
SpoilerLet's switch to zero-based indexing: $$$0, 1, 2, ..., n-1$$$, and for convenience let $$$N = n-1$$$. Notice that this operation sends $$$0$$$ and $$$N$$$ back to themselves, so let's ignore them and focus on indices $$$1...N$$$ for now.
Now, we can represent the swap as moving index $$$x$$$ to $$$f(x)$$$:
$$$\displaystyle f(x) = 2x \bmod N.$$$You can convince yourself this works: for the first half, $$$x$$$ maps to $$$2x$$$ directly, while for the upper half, it is correct to doubling it, reduce mod $$$n$$$, and add 1 (which is just mod by $$$N$$$).
Then the answer is just the order $$$k$$$, which is the smallest $$$k$$$ s.t.:
$$$\displaystyle 2^k \equiv 1 \pmod N.$$$You can convince yourself this condition is both necessary and sufficient. Necessary, because for $$$o \lt k$$$, the chip at index $$$1$$$ is routed to $$$2^o$$$ which is not $$$1$$$, and sufficient because with $$$k$$$ operations, every index $$$i$$$ gets mapped back to index $$$i$$$.
Actually finding $$$k$$$ is the discrete logarithm problem; it's famously hard and used in Diffie-Hellman key exchange. For our purposes, we can brute force in $$$O(N)$$$ (we only need to search $$$k = 1...N-1$$$ because remainders $$$\bmod N$$$ cycle every $$$\phi(N)$$$). There are also some algorithms like baby step giant step or Pollard rho that run in $$$O(\sqrt{N})$$$. Nobody knows an algorithm that runs in polynomial of $$$\log N$$$ (number of bits in $$$N$$$, since cryptography-sized $$$N$$$ is usually on the order of $$$2^{2048}$$$) yet.
Problem statement v2
Eric has $$$n$$$ (where $$$n$$$ is even) poker chips once again. The shuffle operation is the same: $$$1, 2, ..., n \rightarrow 1, \frac{n}{2}+1, 2, \frac{n}{2}+2, 3, \frac{n}{2}+3, ..., \frac{n}{2}, n$$$.
But, this time, the bottom $$$m$$$ poker chips are white, and the top $$$n-m$$$ poker chips are green. We are interested in the minimum number of shuffles until all $$$m$$$ white chips are on the bottom and all $$$n-m$$$ green chips are on top again. The stack does not have to be sorted; for example, if $$$n = 6$$$ and the white chips are $$$1, 2, 3$$$ and the green chips are $$$4, 5, 6$$$, then $$$[1, 3, 2, 5, 4, 6]$$$ would count as good.
The problem: Prove that the answer to this problem is always the same as previous (aka if white and green are separated, then the stack must be sorted), or find an example where it is faster to get a white/green stack than a sorted stack.
There is now no constraint on $$$n$$$ other than it has to be even. We avoid the trivial case of $$$m = 1$$$ or $$$n - m = 1$$$, since if there is only 1 chip of either color, then it forever stays on the top/bottom so you get a white/green stack after every operation.
Solution v2
Thanks to Puddles_Penguin, daniel.glabai, and ClaudeFable5 for working through this.
SpoilerIt turns out is impossible to have white at the bottom and green at the top, unless the stack is also sorted.
First, let's note that the stack is symmetric. If we reversed it, then everything is still the same. So if $$$m \gt \frac{n}{2}$$$, then let's flip the stack over so $$$m_{new} = n - m_{old} \le \frac{n}{2}$$$.
Then, let's solve this problem: Does there exist a number $$$c$$$, such that $$$c \cdot { 1...t } \equiv {1...t} \pmod N$$$? In other words, can we find a set $$${1...t}$$$ that is closed under multiplication by $$$c \pmod N$$$? ($$$t \ge 1$$$, $$$c \ge 2$$$, since $$$c=1$$$ just perfectly sorts it)
We show that this is impossible for $$$t \lt \frac{N}{2}$$$ (remember $$$N$$$ is odd).
Suppose such $$$c$$$ and $$$t$$$ did exist. Then let's consider:
- If $$$c \gt \frac{N}{2}$$$, then $$$1$$$ has to be in the set, and thus $$$c \gt \frac{N}{2}$$$ has to be in the set, but that's not possible since $$$t \lt \frac{N}{2} \lt c$$$.
- If $$$c \lt \frac{N}{2}$$$ and $$$ct \lt N$$$, then consider $$$t$$$, which maps to $$$ct$$$, and $$$ct \lt N$$$, so $$$ct \bmod N \gt t$$$, so $$$ct$$$ is not in the set and that's another contradiction.
- If $$$c \lt \frac{N}{2}$$$ and $$$ct \ge N$$$, then consider the sequence $$$c, 2c, 3c, ...$$$. Because $$$c \lt \frac{N}{2}$$$, there will exist some $$$d$$$ where $$$\frac{N}{2} \lt cd \lt N$$$. That's because the window strictly between $$$\frac{N}{2}$$$ to $$$N$$$ has size $$$\left\lfloor \frac{N}{2} \right\rfloor$$$, and if $$$c \le \left\lfloor \frac{N}{2} \right\rfloor$$$, then you can't "skip" over this window. Also the $$$ct \ge N$$$ condition guarantees that $$$d \le t$$$, so $$$d$$$ is in the set. Then, since $$$t \lt \frac{N}{2}$$$, $$$cd$$$ can't be in the set, and we have a contradiction.
To use the lemma to solve the problem, we have $$$m \le \frac{n}{2}$$$ white chips occupying indices $$$0, ..., m-1$$$. This is exactly like our multiplication problem with $$$t = m-1 \lt \frac{N}{2}$$$. And we allow any value $$$c$$$, which is actually stronger than we need (under real operations, $$$c$$$ can only be powers of 2 $$$\bmod N$$$).
Thus, our lemma tells us that no $$$c \ge 2$$$ exists that permutes our white chips $$${1...t}$$$ onto themselves $$${1...t}$$$. Tthe only solution is $$$c=1$$$, which fully sorts them too.
Bonus: if we split the stack into thirds and shuffled them together that way ($$$f(x) = 3x \bmod N$$$), the same reasoning would apply!