Im trying to solve this problem on hackerrank. I am unable to understand the editorial. If someone could explain the editorial part with proof it would be of great help. Click for problem
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 151 |
Im trying to solve this problem on hackerrank. I am unable to understand the editorial. If someone could explain the editorial part with proof it would be of great help. Click for problem
Name |
---|
The original problem states, that you have coins on the squares [0, 1, ...], and in each turn you move a coin from a square to a square with a lower number.
Now we interpret the statement differently: Instead of coin on a square with number x, think of a piles (heaps) with x stones.
For example: if you have two coins on square 1, two coins on square 2 and one coin on square 4, then you can think of two piles with each 1 stone, two piles with each 2 stones and one pile with 4 stones.
Now a move is defined of putting a coin from square x to square y (x>y). In our stone-pile analogy: you take a pile with x stones and transform it into a pile with y stones. So you are basically removing stones.
Now you can see, why this is equivalent to the nim-game. You have piles of stone, and in each step you remove a few stones from one pile.
The classical nim-solution is: if the xor of all piles is != 0, then the first player wins, otherwise the second. So in the example the xor of all piles would be: 1 xor 1 xor 2 xor 2 xor 4 = 4. Therefore the first player wins.
And at the end you can do one final optimization, because there will be a lot of piles with equal sizes: for each number z the following equation holds: z xor z = 0. Therefore if you have even many piles of size z, you already know the answer: z xor z xor z xor ... xor z = (z xor z) ... xor (z xor z) = 0 xor ... xor 0 = 0. And if you have odd number of piles you get: z xor z xor z xor ... xor z = (z xor z) ... xor (z xor z) xor z = 0 xor ... xor 0 xor z = z.
So we only have to xor the sizes, of which there are an odd number of piles. Or translated back to the original statement. Xor all square indices i, where ci is odd.
Hey thanks for replying , I had one doubt. "Now a move is defined of putting a coin from square x to square y (x>y). In our stone-pile analogy: you take a pile with x stones and transform it into a pile with y stones. So you are basically removing stones." Here you take a pile with X stones and transform it into a pile with Y stones.In this the pile with X stones gets decreased to a pile of Y stones.But those stones may increase the value of the previous index piles right. How are you accounting for that? I hope you understand my doubt.
No the stones don't increase any other piles.
Lets say you have the following squares: [0, 2, 2, 0, 1] (same board as before), than this is equalent to the piles 1, 1, 2, 2, 4.
Lets take the coin from square 4 and move it to square 1, this results in the square configuration [0, 3, 2, 0, 0], or equivalent in the piles 1, 1, 1, 2, 2.
Here you can see, that moving one coin only affects the one pile (4 -> 1). Nothing else happens. Putting the coin from square 4 to square 1 is the same thing as removing 3 stones from the pile with 4 stones.