COCI '15 Contest 5 #6 Podnizovi
View as PDFYou are given an array of integers of length . Let 
 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 
.
An array  is lexicographically smaller than array 
 if 
 where 
 is the first position at which the arrays differ, or if 
 is a strict prefix of array 
.
Let us define the hash of an array that consists of values  as:
where 
, 
 are given integers.
Calculate  for a given 
.
Input
The first line contains integers  
.
The second line contains integers  
.
In all test cases, it will hold .
Output
Output  lines, the 
 line containing 
.
Scoring
In test cases worth 60% of total points, it will additionally hold .
Sample Input 1
2 3 1 5
1 2
Sample Output 1
1
3
2
Explanation for Sample 1
It holds: , 
, 
. 
, 
, 
.
Sample Input 2
3 4 2 3
1 3 1
Sample Output 2
1
1
0
2
Explanation for Sample 2
It holds: , 
, 
, 
. 
, 
, 
, 
.
Sample Input 3
5 6 23 1000
1 2 4 2 3
Sample Output 3
1
25
25
577
274
578
Comments