D(s) = 26 (26 (26 (a') + b') + c') + d'where a', b', c', and d' are the ASCII values of a, b, c, and d respectively. From this observe that D(s) can be computed in a loop, such that in each iteration the current value of D(s) is multiplied by 26 and the ASCII value of the next letter is added. You can get around the integer overflow problem by taking "mod M" at the end of each iteration rather than after D(s) has been completely computed. It is not hard to show that D(s) computed in this manner will be identical to D(s) computed by taking "mod S" once at the end, after D(s) has been completely computed.