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!
cheater
damn the legend is back
Cheater! Cheater! Cheater! ↓↓↓ !!!
UPD: bro it's me swastik_P in the photo, don't you recognize my face?
bro really came to cf after a 4 month break to comment this
Bla bla! Bla Bla! Bla Bla!
idgaf to u bro js stfu at this point
u dont even have any proof that i have cheated in past "1 year or maybe 11 months" and i have left competitive programming "for now" i am preparing for imo so js stfu u r js a random kid who have nothing to do except spamming god knows when will cf ban your id, i mean it already banned your 3 previous one for same reason right? js imagine login into platform after like 7 months js to see a mf using your 7th grade photo and writing cheater on it like it will do anything js fuck off bro no one cares
using words like noob trying to add NN in every sentence to sound intelligent like whoahh a another tier of ragebait!
also wtf did u thought using 3yo photo of a child gonna do to him? wtf do u even want to do at this point? i cheated on cp like a year ago and then i left it, problem fixed right? but u did not u r still continuing spamming u r also a 13yo right? so why the fk r u wasting ur time here go prepare for olympiads!
I am a cheater, and my photo proves it, also what problem do you have with me writing cheater on my photo? Like you can do that too, if you want, there is no rule saying you can't write 'cheater' on your photo.
you all so noob downvoters, you don't even know, that matrix's filled with numbers can be used for cheating, lmao
also, if you don't want to cheat, then you can use them in your code, here is how:
write the problem statement in form of a string vector, at the top of your code
integrate the neural network/machine learning model(trained to solve problems), the result output must be in form of a turing machine
process the turing machine, to get the result of all test cases, and output the answers
even though there will be problems with tle/mle/wa, at least it can work for some problems
Like I was writing a comment asking when is your next useless information blog and saw this lol.
And ClaudeFable5...
By the way I don't know should I click on the spoiler (solution for problem v2) cuz it might be a rickroll...
You have received 1 rickroll immunity powerup
When activated, it prevents you from 1 rickroll (and is used up).
Expires in 67 seconds. Must be used before expiry.
Click here to claim.
And yes I clicked on the link to claim...
and ngl this blog is so easy to understand, especially for a guy with 2 braincells like me
bro the apology.greatericontop.me url is pretty old now (everyone knows it's a rickroll)
I have it until like June 2027
It is. Don't click it
actually new record: greateric wrote a blog without putting rickroll into it
Wait for a revision and he'll pull the link out of nowhere
greateric taking notes
nah this is educational blog we do not rickroll here fr
I recommend you and DuyMinh3005 check the link at the end of this blog
well I clicked on that link before T-T
I mean it's educational, isn't it?
bruh I forgot to glaze greateric.
orz greateric
bruh (till now) there are 16 comments in this blog and 7 of them are mine.
I guess I glaze greateric a little bit too much.
update: 8 comments now
Bruh no way this blog only gets 17 upvotes
Fun problem