Hex statistics
Time limit1sMemory limit512 MB
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.
- Level
Medium7 of 10
- Topics
- Math, Combinatorics, Sorting
- Solved
- No attempts yet
Problem
A sequence holds integers written in hexadecimal. Write for the sum of all elements of .
Let be a permutation of the 16 hexadecimal digits. Deleting a digit means erasing every occurrence of from every element of . 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 and record the sequence after each deletion. That gives 16 sequences. Writing for the result of deleting the digits through from , the 16 sequences are , , , , and every element of the last one is 0. Define
For example, take and a permutation that starts with , , . Deleting the digit gives , and deleting the digit after that gives .
The value of depends on which permutation is used. Over all 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 in decimal. The first two are printed exactly, with no modulo taken.
Input
The first line contains the length of the sequence. Each of the next lines contains one element of the sequence , in input order. Every number in the input, including , is written in hexadecimal with lowercase letters.
Constraints
Output
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 permutations modulo 3b9aca07.