This page is still under construction.

Parts of this page are still being built. What you see may change.

Crusher's Code

Time limit10sMemory limit128 MB

Summary
Compute the expected number of loop iterations for two randomized swap sorts on arrays of up to 8 values.
Level

Hard8 of 10

Topics
Probability, Dynamic programming, Math
Solved
No attempts yet

Problem

Wesley Crusher is the teaching assistant for Introduction to Algorithms. In the first class the cadets were asked to come up with a sorting algorithm of their own. Monty came up with this code.

while (!sorted(a)) {
    int i = random(n) ;
    int j = random(n) ;
    if (a[min(i,j)] > a[max(i,j)])
        swap(a[i], a[j]) ;
}

Carlos, inspired by it, came up with this code.

while (!sorted(a)) {
    int i = random(n-1) ;
    int j = i + 1 ;
    if (a[i] > a[j])
        swap(a[i], a[j]) ;
}

The array a holds NN elements numbered 0 through N−1N-1, and the n in the code is that NN. Every call to random(k) independently picks one integer from 0 to k−1k-1, all with the same probability. The number of iterations is the number of times the body of the while statement runs. An iteration that only compares two values without swapping them still counts. An array that is already sorted never enters the body, so its number of iterations is 0.

Wesley has to decide which algorithm is better. Given an array of at most 8 values, compute the expected number of iterations each algorithm runs before that array is sorted.

Input

The first line contains the number of test cases TT (2≤T≤1002 \le T \le 100).

Each test case is given on a single line. The first value on the line is the number of array elements NN (2≤N≤82 \le N \le 8), followed by the NN elements of the array separated by spaces. Every element is an integer from 0 to 100, and the same value may appear more than once.

Output

For each test case, print the expected number of iterations for Monty's algorithm and for Carlos's algorithm on one line, in the form Monty <Monty's expectation> Carlos <Carlos's expectation>.

Print both expectations with exactly six digits after the decimal point, rounding at the seventh digit. Put exactly one space between words, and no space at the start or the end of a line. No answer comes within 10−910^{-9} of a rounding boundary, so the digits are the same whichever way a boundary would be resolved.

Examples1

  1. Example 1

    Input
    12
    2 1 2
    2 2 1
    3 1 2 3
    3 3 2 1
    4 1 2 3 4
    4 4 3 2 1
    4 2 1 4 3
    5 1 1 1 1 1
    5 5 4 3 2 1
    8 8 7 6 5 4 3 2 1
    8 3 1 4 1 5 9 2 6
    8 2 7 1 8 2 8 1 8
    
    Expected output
    Monty 0.000000 Carlos 0.000000
    Monty 2.000000 Carlos 1.000000
    Monty 0.000000 Carlos 0.000000
    Monty 6.000000 Carlos 5.000000
    Monty 0.000000 Carlos 0.000000
    Monty 14.666667 Carlos 12.500000
    Monty 12.000000 Carlos 4.500000
    Monty 0.000000 Carlos 0.000000
    Monty 26.382275 Carlos 23.641975
    Monty 89.576273 Carlos 79.496510
    Monty 79.161905 Carlos 33.422840
    Monty 63.815873 Carlos 38.910494