MATDYS - Editorial

Another O(N) approach:

The 2^n numbers can be divided into 2^{n-1} pairs mod 2^{n-1}. Observe that these modular residues pairs are arranged in the same order as the final ordering N = n-1.

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=n-1 to obtain the ordering of the modulo pairs for N=n. Within the pair, the smaller number (which is strictly less than 2^{n-1}) comes first.

define f(n, k):
    if n==2:
        //Base case. Return the answer using the final ordering {0, 2, 1, 3}
    else:
        power = 2^(n-1) //use (1LL)<<(n-1)
        position = f(n-1, k % power) //recursion
        return 2*position + k/power //to handle ordering in the modulo pair
//n = 1 is handled separately

C++ code can be found here

1 Like

Iterate from the least significant bit to the most significant one,and use binary search to find position of number

1 Like

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,n-1)-1] and similarly with arr[3] until 1+pow(2,n-1)-1 < n.
Can anyone help??

Here is a simple O(logK) approach. It uses binary representation of K.

    #define ull unsigned long long

    int i=1;
    ull ans=0;
    while(k){
        if(k&1){
            ans+=(ull)pow(2,n-i);
        }
        k>>=1;
        i++;
    }
    cout<<ans;

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.

3 Likes

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!

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 n-1 times.

Solution.

What does the statement “numbers in right half are simply 1(2^0), plus the numbers of the left side.” mean exactly? Could you please provide examples? Thank you.

reading the editorial, and realizing i was so close ,seriously hurts .already solved k-char problem during the LOC, but when u fail to solve the similar problems just by some distance simple hurts , this teaches me that try till the last minute.

Edit: Fixed by Vijju123 Corrected code CodeChef: Practical coding for everyone

Original:

What’s wrong with my code/logic It fails on subtask 3 and runs on 1&2 (Lang Python 3) CodeChef: Practical coding for everyone

My logic:
Let N1=2^(N-1){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

1 Like

Whats wrong with my code? Getting wrong answer on test 3 subtask 3.

https://www.codechef.com/viewsolution/15133990

Is n’t the Pseudo Code given wrong?

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.

Can someone provide me an implementation of this approach ?? Thanks in advance :slight_smile:

Is this the implementation of the above approach ??

Can someone explain the author’s solution in details ?How he arrived at it.

Thanks in Advance

The idea was to give a small advantage to careful contestants and Python coders.

That was graceful. The concept was thoroughly tested. I like it now…although it made me pull my hair during contest cause i JUST couldnt get it right in c++ hahahahah. Should had been careful :slight_smile:

I am sorry this was the case for you!

Thanks for such a great problem! Such problems are the cause I give priority to contests over dinner

1 Like

Thank you for your feedback!

2 Likes

You only needed till 2^63, no? And even after summing, its 2^64-1. 2^64 is out of limits, but 2^64-1 is in limit