In computer science, a hash function is a function
which maps data of arbitrary size (called keys) into data of
fixed size called hashes; typically integers within a
certain range, say for some constant . It is highly desireable that
different keys yields different hashes, but since there are
usually more possible keys than available hashes, collisions
are inevitable.
When our set of possible keys is bounded, however, we can
create what is known as a perfect hash function. Given
a finite set of keys ,
a perfect hash function maps distinct elements in to a set of non-negative integers
less than , with no
collisions. In mathematical terms, it is an injective function
, for some constant .
We are aiming to implement a perfect hash function for a set
of strings, such that you can choose the constant as small as possible. As as
starting point, consider the hash function for strings below
(which, with the except of the modulo operation, is stolen from
the built-in hashcode function for strings in the Java
language):
public int hash(String s) {
int hash = 0;
for (int i = 0; i < s.length(); i++) {
hash = (31 * hash + s.charAt(i)) % M;
}
return hash;
}
The above code corresponds to the function
with and
. It is known as the Horner’s
method for computing a polynomial.
Your task is to find a value for the constant (possibly different from 31) and a
value for such that
is a perfect hash
function for the set of strings , and is as small as possible.
Explantion of sample
For example, consider the set . The smallest
we can hope for is 2, since . Sadly, the two strings
‘Hi’ and ‘ha’ will yield the same hash for regardless of our value of
. For , however, we can pick
, which maps the two
strings to different values (even if does not).
Input
The first line of input contains an integer (), the size of the set . Then the elements of follows on the next lines, each consisting of a unique
string. No string has more than 32 characters, and all strings
are made entirely of alphabetic ASCII characters (that is,
65 <= s.charAt(i) <= 90 || 97 <=
s.charAt(i) <= 122 for every and ).
You may assume that for all input files, .
Output
Output a single line containing two space-separated
non-negative integers
and , such that
is as small as
possible and there are no collisions using as the constant of . If multiple choices of
are possible, output
your favourite among those less than .
Sample Input 1 |
Sample Output 1 |
2
Hi
ha
|
2 3
|