algorithmtreesegment-tree

SPOJ "Card Trick": unable to understand how to apply binary index tree


Card Trick is a problem on Sphere online judge.
It states that

The magician shuffles a small pack of cards, holds it face down and performs the following procedure:

  1. The top card is moved to the bottom of the pack. The new top card is dealt face up onto the table. It is the Ace of Spades.

  2. Two cards are moved one at a time from the top to the bottom. The next card is dealt face up onto the table. It is the Two of Spades.

  3. Three cards are moved one at a time…

  4. This goes on until the nth and last card turns out to be the n of Spades.

This impressive trick works if the magician knows how to arrange the cards beforehand (and knows how to give a false shuffle). Your program has to determine the initial order of the cards for a given number of cards, 1 ≤ n ≤ 20000.

Input

On the first line of the input is a single positive integer, telling the number of test cases to follow. Each case consists of one line containing the integer n.

Output

For each test case, output a line with the correct permutation of the values 1 to n, space separated. The first number showing the top card of the pack, etc…

Example

Input:
2
4
5

Output:
2 1 4 3
3 1 4 5 2

Now the only solution I can think of is to use a queue and simulate the process.
But that would be O(n^2). I read the comments and they suggested using segment tree of BIT.
I know both segment tree and BIT but am unable to understand how to implement them in this question.
Please suggest some way to do this.


Solution

  • I have no idea why this problem should be linked with BIT or segment tree, but I solved the problem using simple "O(N^2)" simulation.

    First the time limit for this problem is 11s, and N == 20000. This indicates that a O(kN) solution may pass the problem. I believe you think this k should be N, because simple simulation requires this, but somehow it can be optimized.

    Let's see how we can construct the sequence when N == 5:

    Round 1, count 1 space starting from first space after last position:        _ 1 _ _ _
    Round 2, count 2 spaces starting from first space after last position:       _ 1 _ _ 2
    Round 3, count 3 spaces starting from first space after last position:       3 1 _ _ 2
    Round 4, count 4 spaces starting from first space after last position:       3 1 4 _ 2
    Round 5, count 5 spaces starting from first space after last position:       3 1 4 5 2
    

    We can see a nice pattern: for round i, we should count i space starting from the first space after last position, and warp back when necessary.

    However, a crucial step is: after some rounds, the spaces left will be smaller than the space to count. In this case, we can take a mod to save time!

    For example, in Round 4 of the previous example, we have only 2 spaces left but 4 spaces to count. If we count 4, it's a waste of time. Counting 4 steps is equivalent to count 4 % 2 == 0 space starting from the first space after last position. You can verify this point by yourself :)

    Therefore, we can simulate this process using the code:

    memset(ans, 255, sizeof(ans));
    while (cur <= n)
    {
        int i, cnt;
        int left = n - cur + 1; // count how many spaces left
        left = cur % left + 1; // this line is critical, mod to save time!
    
        for (i = pos, cnt = 0; ; ++i) // simulate the process
        {
            if (i > n) i = 1;
            if (ans[i] == -1) ++cnt;
            if (cnt == left) break;
        }
    
        ans[i] = cur;
        pos = i;
    
        ++cur;
    }