#P6728. [COCI 2015/2016 #5] PODNIZOVI

[COCI 2015/2016 #5] PODNIZOVI

Description

Given an array a1,a2,,aNa_1, a_2, \dots, a_N of length NN. Let s1,s2,,sqs_1, s_2, \dots, s_q be all non-empty subsequences of this array, where q=2n1q = 2^n - 1.

About lexicographical order: if the first position where two arrays AA and BB differ is ii, and Ai<BiA_i < B_i; or AA is a strict prefix of BB (i.e., they cannot be equal), then AA is lexicographically smaller than BB.

We define the hash value of an array consisting of v1,v2,,vpv_1, v_2, \dots, v_p as:

$$h(s)=(v_1\times B^{p-1}+v_2\times B^{p-2}+\dots +v_{p-1}\times B+v_p) \bmod M$$

where B,MB, M are given integers.

For a given integer KK, find the values of h(s1),h(s2),,h(sK)h(s_1), h(s_2), \dots, h(s_K).

Input Format

The first line contains four integers N,K,B,MN, K, B, M.

The second line contains NN integers a1,a2,,aNa_1, a_2, \dots, a_N.

Output Format

Output a total of KK lines. The ii-th line should be the value of h(si)h(s_i).

2 3 1 5
1 2
1
3
2
3 4 2 3
1 3 1
1
1
0
2
5 6 23 1000
1 2 4 2 3
1
25
25
577
274
578

Hint

Sample Explanation

Sample 11

s1=[1],s2=[1,2],s3=[2]s_1 = [1], s_2 = [1, 2], s_3 = [2].

$h(s_1) = 1 \bmod 5 = 1, h(s_2) =(1 + 2) \bmod 5 = 3, h(s_3) = 2 \bmod 5 = 2$.

Sample 22

s1=[1],s2=[1],s3=[1,1],s4=[1,3]s_1 = [1], s_2 = [1], s_3 = [1, 1], s_4 = [1, 3].

$h(s1) = 1 \bmod 3 = 1, h(s_2) = 1 \bmod 3 = 1, h(s_3) = (1 \times 2 + 1) \bmod 3 = 0, h(s_4) = (1 \times 2 + 3) \bmod 3 = 2$.

Constraints

For 60%60\% of the testdata, 1ai301 \le a_i \le 30.
For 100%100\% of the testdata, 1N,K,ai1051 \le N, K, a_i \le 10^5, 1B,M1061 \le B, M \le 10^6, and K2N1K \le 2^N - 1.

Notes

Translated from COCI2015-2016 CONTEST #5 T6 PODNIZOVI

Translated by ChatGPT 5