COCI '15 Contest 5 #6 Podnizovi

View as PDF

Submit solution


Points: 30 (partial)
Time limit: 0.6s
Memory limit: 256M

Problem types

You are given an array of integers of length N. Let s1,s2,,sq be the lexicographically sorted array of all its non-empty subsequences. A subsequence of the array is an array obtained by removing zero or more elements from the initial array. Notice that some subsequences can be equal and that it holds q=2N1.

An array A is lexicographically smaller than array B if Ai<Bi where i is the first position at which the arrays differ, or if A is a strict prefix of array B.

Let us define the hash of an array that consists of values v1,v2,,vp as:

h(s)=(v1Bp1+v2Bp2++vp1B+vp)modM where B, M are given integers.

Calculate h(s1),h(s2),,h(sK) for a given K.

Input

The first line contains integers N,K,B,M (1N100000,1K100000,1B,M1000000).

The second line contains integers a1,a2,,aN (1ai100000).

In all test cases, it will hold K2N1.

Output

Output K lines, the jth line containing h(sj).

Scoring

In test cases worth 60% of total points, it will additionally hold 1a1,a2,,aN30.

Sample Input 1

Copy
2 3 1 5
1 2

Sample Output 1

Copy
1
3
2

Explanation for Sample 1

It holds: s1=[1], s2=[1,2], s3=[2]. h(s1)=1mod5=1, h(s2)=(1+2)mod5=3, h(s3)=2mod5=2.

Sample Input 2

Copy
3 4 2 3
1 3 1

Sample Output 2

Copy
1
1
0
2

Explanation for Sample 2

It holds: s1=[1], s2=[1], s3=[1,1], s4=[1,3]. h(s1)=1mod3=1, h(s2)=1mod3=1, h(s3)=(12+1)mod3=0, h(s4)=(12+3)mod3=2.

Sample Input 3

Copy
5 6 23 1000
1 2 4 2 3

Sample Output 3

Copy
1
25
25
577
274
578

Comments

There are no comments at the moment.