Print the polynomial hashes of the K lexicographically smallest non-empty subsequences of the given array.
Hard8HeapSortingCombinatoricsNo attempts yetTime limit1sMemory limit256 MBYou are given an array of N integers. Sort every non-empty subsequence of that array lexicographically and call the result s1,s2,…,sq. 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−1.
Array A is lexicographically smaller than array B if Ai<Bi at the first position i where the two arrays differ, or if A is a strict prefix of B.
The hash of an array with values v1,v2,…,vp is defined as
h(s)=(v1Bp−1+v2Bp−2+⋯+vp−1B+vp)modM
where B and M are given integers. For a given K, compute h(s1),h(s2),…,h(sK).
The first line contains the integers N, K, B, M (1≤N≤100000, 1≤K≤100000, 1≤B,M≤1000000).
The second line contains the integers a1,a2,…,aN (1≤ai≤100000).
Every input satisfies K≤2N−1.
Print K lines. Line j contains h(sj).
In the first example the sorted subsequences are s1=[1], s2=[1,2], s3=[2], so h(s1)=1mod5=1, h(s2)=(1+2)mod5=3 and h(s3)=2mod5=2.
In the second example they are s1=[1], s2=[1], s3=[1,1], s4=[1,3]. Two elements have the value 1, so [1] appears twice. The hashes are h(s1)=1mod3=1, h(s2)=1mod3=1, h(s3)=(1×2+1)mod3=0 and h(s4)=(1×2+3)mod3=2.