For every digit of a hexadecimal string S, find the smallest, largest, and sum over all 16! deletion orders of the total of the 16 prefix sums.
Medium7MathCombinatoricsSortingNo attempts yetTime limit1sMemory limit512 MBA sequence S holds N integers written in hexadecimal. Write sum(S) for the sum of all elements of S.
Let p be a permutation of the 16 hexadecimal digits. Deleting a digit d means erasing every occurrence of d from every element of S. The digits that remain keep their order and are read as a hexadecimal number again, and an element that loses all of its digits becomes 0. A deletion can leave a 0 in front, which does not change the value.
Delete the digits one at a time in the order given by p and record the sequence after each deletion. That gives 16 sequences. Writing S[d1,…,dk] for the result of deleting the digits d1 through dk from S, the 16 sequences are S[p1], S[p1,p2], …, S[p1,…,p16], and every element of the last one is 0. Define
total(S,p)=sum(S[p1])+sum(S[p1,p2])+⋯+sum(S[p1,…,p16])
For example, take S=[9af47c0b,2545557,ff6447979] and a permutation p that starts with 4, 9, 5. Deleting the digit 4 gives S[4]=[9af7c0b,255557,ff67979], and deleting the digit 9 after that gives S[4,9]=[af7c0b,255557,ff677].
The value of total(S,p) depends on which permutation p is used. Over all 16! permutations of the hexadecimal digits, compute three values: the smallest total, the largest total, and the sum of the totals of every permutation. Print the third value modulo 3b9aca07, which is 109+7 in decimal. The first two are printed exactly, with no modulo taken.
The first line contains the length N of the sequence. Each of the next N lines contains one element P of the sequence S, in input order. Every number in the input, including N, is written in hexadecimal with lowercase letters.
Constraints
Print one line with three integers separated by single spaces, all in hexadecimal with lowercase letters: the smallest total, the largest total, and the sum of the totals of all 16! permutations modulo 3b9aca07.