This page is still under construction.

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

Program Optimization

Time limit2sMemory limit256 MB

Summary
Simulate the given C++ program exactly, including its mt19937 random sequence, while answering range mex queries over a permuted array fast enough for q up to 10^7.
Level

Hard8 of 10

Topics
Simulation, Segment tree, Implementation
Solved
No attempts yet

Problem

Consider the following C++ code:

#include <algorithm>
#include <random>

int query_mex(const int *a, int l, int r);

int simulate(int n, int *a, int q, int k, int s) {
  std::mt19937 gen;
  gen.seed(s);
  int last = 0;
  while (q--) {
    int op = gen() % k;
    int i = (gen() + last) % n;
    if (!op && i) {
      std::swap(a[i - 1], a[i]);
    } else {
      int j = gen() % n;
      last ^= query_mex(a, std::min(i, j), std::max(i, j));
    }
  }
  return last;
}

In this program, query_mex(a, l, r) returns the smallest non-negative integer that does not occur among al,al+1,…,ara_l, a_{l + 1}, \dots, a_r.

Given the values of nn, aa, qq, kk, and ss, find the value the function returns.

Input

The first line contains four integers nn, qq, kk, and ss (1≤n≤2×1051 \leq n \leq 2\times 10^5, 1≤q≤1071 \leq q \leq 10^7, 1≤k≤1091 \leq k \leq 10^9, 0≤s≤1090 \leq s \leq 10^9). The second line contains nn distinct integers a0,a1,…,an−1a_0, a_1, \dots, a_{n - 1} (0≤ai<n0 \leq a_i < n).

Output

Output an integer, the value the function returns.

Examples1

  1. Example 1

    Input
    3 5 1 0
    0 1 2
    
    Expected output
    3