Time Limit: 1000MS
Memory Limit: 10000K
A D J P T B E K Q U C G L R V F I N S W H M O X YA valid word in the 25-language has its letters in each row as well as in each column written in ascending order. Thus, the word ADJPTBEKQUCGLRVFINSWHMOXY is a valid word, in contrast to the word ADJPTBEGQUCKLRVFINSWHMOXY (the ascending order is violated in the second column, and in the third column, too). Santa Claus has a lexicon. His lexicon is the list of all valid 25-language words in ascending order (lexicographically) along with their ordinal numbers starting from 1. For example, in the lexicon ABCDEFGHIJKLMNOPQRSTUVWXY is the word number 1 and ABCDEFGHIJKLMNOPQRSUTVWXY is the word number 2. In word number 2, U and T are interchanged from their order in word number 1. Unfortunately, this lexicon is huge. Write a program that determines the ordinal number of an arbitrary given word, and also the word corresponding to a given ordinal number. There are no more than 2^31 words in the lexicon.