Problem LinkAuthor: Alexandru Valeanu DifficultyEASY PrerequisitesObservations ProblemYou are given a pack of $2^N$ cards and you shuffle it in N steps. At step $k$ $(0 ≤ k < N)$ you divide the deck into $2^k$ equalsized decks. Each one of those decks is reordered by having all the cards that lie on even positions first, followed by all cards that lie on odd positions (the order is preserved in each one of the two subsequences). Then all decks are put back together (again, the order of decks is preserved). Given an unordered deck of size $2^N$, find the position of the card labelled $K$ in the final, shuffled deck. Quick ExplanationCheck the binary representation of $K$ and the final answer. Try to spot some observations. ExplanationSubtask  1Since, $N <= 10$, we can simply simulate the whole process and find the exact order of the cards after all the $N$ shuffles are done. The simply output the $K^{th}$ index in the final array. The complexity of the above solution would be $O(N*{2}^{N})$ because there are total $N$ steps of shuffling and each shuffles uses all the $2^N$ cards. This solution is thus too slow for the remaining subtasks. Subtask  2, 3 (Author's Solution)Let us try to find the binary representation of $K$ and the final answer and try to spot some observations based on it. (Assume below that all binary representations are $N$ bit long). Let $N = 3$ Below is the table : K ANS 000 000 001 100 010 010 011 110 100 001 101 101 110 011 111 111 Do you spot the relationship between the two of them? Yes, the answer is simply the reverse of $K$ in binary representation. Thus, we can simply find the binary representation of $K$. Then try to construct the answer from the reverse of the binary representation found. Subtask  2, 3 (Editorialist's Solution)Since, $K$ is of the order od $2^N$, we would like to come up with an logarithmic approach in $K$ or linear in $N$. According to the way operations are performed, it is easy to see that once, 2 cards are separated into different decks, they can never appear in the same deck again. Thus we try to find how the cards of one deck are related to the other as same kind of operations (separating into decks and then reassembing based on even/odd combinations) is done of both of them. Consider the operations one by one. Let $K = 0$, Here all the even numbers appear first in increasing order and then the odd numbers appear first. Thus, if we divide the array into 2 equal halves, we can see that numbers in right half are simply $1 (2^0)$, plus the numbers of the left side. $K = 0$ > $ 0 2 4 6  1 3 5 7$ Now, let $K = 1$. The deck is divided into 4 halves and as per the first observation, cards in left deck of last operation cannot go to right deck of last operation. Thus, we would see how the leftmost 2 decks are related and the rightmost 2 decks are related. One can again see that for both halves, numbers of right deck are simply $2 (2^1)$, plus the numbers of the left side. $K = 1$ > $ 0 4  2 6  1 5  3 7$ Thus, if we continue this procedure, we see that number of rigth side after $i$ operations are simply $2^i$ plus the number of left side. Thus, we now layout our algorithm, which is similar to binary search, as we would reduce our search space to half on each iteration and try to find the contribution of each step to the answer. A similar problem using the above technique can be found out here Pseudo Code
Some Silly ErrorsMost of the participants, although found the correct logic but could not get full score. This is because of overflow in the answer. This is mostly the case in C++/Java users. If we use "long long", the maximum limit is 63 bits on positive side and 63 bits on the negative side. To get full score, one had to use 64bit representation i.e. "unsigned long long". This should have been a good experience for some to learn about overflow cases in languages. Although python users had an advantage here as there is no overflow there. Time Complexity$O(n)$ or $O(\log{K})$ Space Complexity$O(1)$ Solution Linksasked 24 Aug '17, 16:10

The editorial does not explain why the binary representation of the answer is the reverse binary representation of $K$. What happens at every step $k$ is that the rightmost $N  k$ bits in the binary representation are cyclically shifted by one position to the right, so that the bit that was originally at position $k$ from the right ends up at position $k$ from the left. Here is the implementation of this algorithm. answered 27 Aug '17, 04:26

Another $O(N)$ approach: The $2^n$ numbers can be divided into $2^{n1}$ pairs $mod$ $2^{n1}$. Observe that these modular residues pairs are arranged in the same order as the final ordering $N = n1$. For example  The final ordering for $N=3$ is $\{0, 4, 2, 6, 1, 5, 3, 7\}$ which is equivalent to $\{0,0,2,2,1,1,3,3\}$ $(mod$ $4)$. This is the same as the final ordering for $N = 2$ i.e. $\{0,2,1,3\}$ With a recursive approach, we use the ordering for $N=n1$ to obtain the ordering of the modulo pairs for $N=n$. Within the pair, the smaller number (which is strictly less than $2^{n1}$) comes first.
answered 27 Aug '17, 00:48

answered 27 Aug '17, 01:11

Edit: Fixed by Vijju123 Corrected code https://www.codechef.com/viewsolution/15138756 Original: What's wrong with my code/logic It fails on subtask 3 and runs on 1&2 (Lang Python 3) https://www.codechef.com/viewsolution/15128686 My logic: Let N1=2^(N1){length of subarrays} If the K is even then it will go in the left part of the array and its position in the left array will be K/2 If the K is odd then it will go in the right part of array and its position in the right subarray will still be K/2 but it will have whole left subarray before it, hence add length of left subarray in it(store length of left subarray in variable pre) Now in both the cases divide k by 2 (k/=2) and in odd case add N1 in the pre variable, divide N1=N1/2 because length of subarray will decrease by 2 in each run. Repeat this N times and final answer is pre+K answered 27 Aug '17, 11:44
Try changing this
(27 Aug '17, 12:04)
Didn't work with that too. Still AC on 1&2 subtask and WA on 3rd
(27 Aug '17, 12:20)
2
Suffering from floating errors. If you use "K>>=1" and "N!>>=1" instead of dividing K by 2, you will get AC. Still looking why your K behaves weirdly, but I am no python user :( EDIT: Refer to "//" and "/" operator, and their difference. Floating point divison has an error of about ${10}^{9}$ ,and that matters a LOT here :(
(27 Aug '17, 13:19)
Got another reason to hate python :( c++ is my regular one. Thanks Vijju123 Got AC
(27 Aug '17, 15:29)

how can i improve my solution : https://www.codechef.com/viewsolution/15131329 answered 26 Aug '17, 23:19

@alexvaleanu I used unsigned long long int in C++14 but it still couldn't store values as large as 2^64. Had to resort to BigInteger in Java. Any ideas why ? Please have a look solution link (I used the Binary Search approach) answered 26 Aug '17, 23:39
You only needed till 2^63, no? And even after summing, its 2^641. 2^64 is out of limits, but 2^641 is in limit
(26 Aug '17, 23:41)
My point is the solution should've been AC then !! But it threw a RE (SIGFPE) error. WHY ??
(26 Aug '17, 23:45)
It means you are doing infinite loop somewhere. And getting a RE due to this. Like when an array gets involved in a infinite loop.
(26 Aug '17, 23:57)
2
Try this test case 1 64 18446744073709551615 Second number is 2^641. Actually problem is value of total become 2^64 which cannot fit in unsigned long long.
(27 Aug '17, 00:01)
1
With that attitude of yours, I'd rather not :) . Go debug yourself, your code is none of my concern.
(27 Aug '17, 00:07)
THANKS @dushsingh1995
(27 Aug '17, 00:10)
showing 5 of 6
show all

I used unsigned long long int...but then also got wrong answer...can someone please help https://www.codechef.com/viewsolution/15129654 answered 26 Aug '17, 23:51

I solved it using simple recursion in python
answered 27 Aug '17, 00:22
My code was a simply while loop :p , but our ideas are same . First time solved a Q in less than 15 lines XD
(27 Aug '17, 00:23)
There is a shorted Python solution :) Check mine!
(27 Aug '17, 00:25)
There is even a shorter way to do this. :) https://www.codechef.com/viewsolution/15156758
(29 Aug '17, 06:25)

I am a newbie here, As my answer my wrong, but I want to know, what was wrong with this: Initial array:{0,1,2,3,4,5,6,7} n=3 for any k, which is greater than or equal to n, the least size of the block will be 2. Now after all the rearrangements the final array is {0,4,2,6,1,5,3,7} Now my point is to swap the arr[1] with arr[1+pow(2,n1)1] and similarly with arr[3] until 1+pow(2,n1)1 < n. Can anyone help?? answered 27 Aug '17, 02:27

It was a great contest! One hardly learns much from easier contests, but tough contests give you an opportunity to learn a lot. Kudos to you problem setter! answered 27 Aug '17, 09:12

Here, have a look at my solution. My observation was that once a number gets in a range whic was changed earlier, it never gets out of the range. And the numbers in the new range are always in a sorted order. Therefore, you can just check the index of number in that range, find the new index and reduce the range and repeat it n1 times. answered 27 Aug '17, 10:09

Answer is hidden as author is suspended. Click here to view.
answered 27 Aug '17, 10:20

Whats wrong with my code? Getting wrong answer on test 3 subtask 3. answered 27 Aug '17, 20:33

Is n't the Pseudo Code given wrong? answered 27 Aug '17, 23:35
@ayushgoyal1703, No. You can check my solution(editorialist) as well.
(28 Aug '17, 03:00)

Let us try to find the binary representation of K and the final answer and try to spot some observations based on it. (Assume below that all binary representations are N bit long). Let N=3 Below is the table :
Do you spot the relationship between the two of them? Yes, the answer is simply the reverse of K in binary representation. Thus, we can simply find the binary representation of K . Then try to construct the answer from the reverse of the binary representation found. Can someone provide me an implementation of this approach ?? Thanks in advance :) Is this the implementation of the above approach ?? answered 28 Aug '17, 13:13

Can someone explain the author's solution in details ?How he arrived at it. Thanks in Advance answered 22 Sep '17, 20:53

BTW idk why nobody said this, but this is seriously a great editorial @likecs !!
why we need to move the number to the left if it is on right?
@anno, we are now actually moving number from left to right. It is just that calculating the number of left side is easier. So, we the number lies on right, we know that in $i$ step it is $2^i$ plus that on left side. Thus, we add this contribution. I would recommend you to try some more examples on paper and also try to solve the problem mentioned in the link.
Nice question! Inspired by FFT if I'm not wrong? :)
@meooow Thank you! You are the first one to notice the FFT part. Yes, sorting by reverse_bits is the first step in the iterative implementation of FFT.