This page is still under construction.

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

Subsequence Hashes

Time limit1sMemory limit256 MB

Summary
Print the polynomial hashes of the K lexicographically smallest non-empty subsequences of the given array.
Level

Hard8 of 10

Topics
Heap, Sorting, Combinatorics
Solved
No attempts yet

Problem

You are given an array of NN integers. Sort every non-empty subsequence of that array lexicographically and call the result s1,s2,…,sqs_1, s_2, \dots, s_q. A subsequence is an array obtained by deleting zero or more elements from the original array. Several subsequences can be equal to one another, and q=2N−1q = 2^N - 1.

Array AA is lexicographically smaller than array BB if Ai<BiA_i < B_i at the first position ii where the two arrays differ, or if AA is a strict prefix of BB.

The hash of an array with values v1,v2,…,vpv_1, v_2, \dots, v_p is defined as

h(s)=(v1Bp−1+v2Bp−2+⋯+vp−1B+vp) mod Mh(s) = (v_1 B^{p-1} + v_2 B^{p-2} + \dots + v_{p-1} B + v_p) \bmod M

where BB and MM are given integers. For a given KK, compute h(s1),h(s2),…,h(sK)h(s_1), h(s_2), \dots, h(s_K).

Input

The first line contains the integers NN, KK, BB, MM (1≤N≤100 0001 \le N \le 100\,000, 1≤K≤100 0001 \le K \le 100\,000, 1≤B,M≤1 000 0001 \le B, M \le 1\,000\,000).

The second line contains the integers a1,a2,…,aNa_1, a_2, \dots, a_N (1≤ai≤100 0001 \le a_i \le 100\,000).

Every input satisfies K≤2N−1K \le 2^N - 1.

Output

Print KK lines. Line jj contains h(sj)h(s_j).

Note

In the first example the sorted subsequences are s1=[1]s_1 = [1], s2=[1,2]s_2 = [1, 2], s3=[2]s_3 = [2], so h(s1)=1 mod 5=1h(s_1) = 1 \bmod 5 = 1, h(s2)=(1+2) mod 5=3h(s_2) = (1 + 2) \bmod 5 = 3 and h(s3)=2 mod 5=2h(s_3) = 2 \bmod 5 = 2.

In the second example they are s1=[1]s_1 = [1], s2=[1]s_2 = [1], s3=[1,1]s_3 = [1, 1], s4=[1,3]s_4 = [1, 3]. Two elements have the value 1, so [1][1] appears twice. The hashes are h(s1)=1 mod 3=1h(s_1) = 1 \bmod 3 = 1, h(s2)=1 mod 3=1h(s_2) = 1 \bmod 3 = 1, h(s3)=(1×2+1) mod 3=0h(s_3) = (1 \times 2 + 1) \bmod 3 = 0 and h(s4)=(1×2+3) mod 3=2h(s_4) = (1 \times 2 + 3) \bmod 3 = 2.

Examples3

  1. Example 1

    Input
    2 3 1 5
    1 2
    
    Expected output
    1
    3
    2
    
  2. Example 2

    Input
    3 4 2 3
    1 3 1
    
    Expected output
    1
    1
    0
    2
    
  3. Example 3

    Input
    5 6 23 1000
    1 2 4 2 3
    
    Expected output
    1
    25
    25
    577
    274
    578