Under construction

Still being built. Things may change

Random Shuffle

Time limit1sMemory limit1024 MB

Summary
Given n and the permutation produced by a xorshift-based Fisher-Yates shuffle, find a 64-bit seed x that generates that exact output.
Level

Hard8 of 10

Topics
Bit manipulation, Math
Solved
No attempts yet

Problem

Prof. Pang is selecting teams that advance to the world final contest. As the regionals are cancelled, he uses random shuffle to rank the teams. There are nn teams in total. His code is as follows:

uint64_t x;//uint64_t represents 64-bit unsigned integer
int n;
uint64_t rand() {//this is a xor-shift random generator
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
    return x;
}
int main() {
    cin >> n;
    cin >> x;
    for (int i = 1; i ≤ n; i++) {//random shuffle [1, 2,..., n]
        a[i] = i;
        swap(a[i], a[rand() % i + 1]);
    }
    for (int i = 1; i ≤ n; i++) {//print the result
        cout << a[i] << (i == n ? '\n' : ' ');
    }
}

He compiled and ran his code and then entered nn and some special nonnegative integer xx. He printed the result on paper.

One day later, Prof. Pang forgot his choice for xx. You are given the result of the code and the integer nn. Please recover the number xx that Prof. Pang had entered.

Input

The first line contains a single integer nn (50≤n≤10000050\le n\le 100000), the number of teams.

The next line contains nn integers, the result printed by Prof. Pang's code. It is guaranteed that the result is correct. That is, there exists an integer xx (0≤x≤264−10\le x\le 2^{64}-1) that leads to the result.

Output

Output the integer xx (0≤x≤264−10\le x\le 2^{64}-1) that Prof. Pang had entered. If there are multiple possible xx values, print any one of them.

Hint

Note that the second line of the sample input is wrapped to fit the width of the page.

Examples1

  1. Example 1

    Input
    50
    36 22 24 21 27 50 28 14 25 34 18 43 47 13 30 7 10 48 20 16 29 9 8 15 3 31 12 38 19 49 37 1 46 32 4 44 11 35 6 33 26 5 45 17 39 40 2 23 42 41
    
    Expected output
    16659580358178468547