Time Limit: Java: 2000 ms / Others: 2000 ms
Memory Limit: Java: 65536 KB / Others: 65536 KB
Cryptography deals with methods of secret communication thattransform a message (the plaintext) into a disguised form (theciphertext) so that no one seeing the ciphertext will be ableto figure out the plaintext except the intended recipient.Transforming the plaintext to the ciphertext is encryption;transforming the ciphertext to the plaintext is decryption.Twisting is a simple encryption method that requires that thesender and recipient both agree on a secret key k, which is apositive integer.
The twisting method uses four arrays: plaintext and ciphertextare arrays of characters, and plaincode and ciphercode arearrays of integers. All arrays are of length n, where n is thelength of the message to be encrypted. Arrays are origin zero, so theelements are numbered from 0 to n - 1. For this problem allmessages will contain only lowercase letters, the period, and theunderscore (representing a space).
The message to be encrypted is stored in plaintext. Given a keyk, the encryption method works as follows. First convert theletters in plaintext to integer codes in plaincode according tothe following rule: '_' = 0, 'a' = 1, 'b' = 2, ..., 'z' = 26,and '.' = 27. Next, convert each code in plaincode to anencrypted code in ciphercode according to the following formula: for alli from 0 to n - 1,ciphercode[i] = (plaincode[ki mod n] - i) mod 28.
(Here x mod y is the positive remainder when x is divided by y.For example, 3 mod 7 = 3, 22 mod 8 = 6, and -1 mod 28 = 27. Youcan use the C '%' operator or Pascal 'mod' operator to computethis as long as you add y if the result is negative.)Finally, convert the codes in ciphercode back to letters inciphertext according to the rule listed above. The final twistedmessage is in ciphertext. Twisting the message cat using the key 5yields the following:
Your task is to write a program that can untwist messages,i.e., convert the ciphertext back to the originalplaintext given the key k. For example, given the key 5 andciphertext 'cs.', your program must output the plaintext 'cat'.
The input file contains one or more test cases, followed by a linecontaining only the number 0 that signals the end of the file.Each test case is on a line by itself and consists of the key k, aspace, and then a twisted message containing at least one and at most 70characters. The key k will be a positive integer not greater than300. For each test case, output the untwisted message on a lineby itself.
Note: you can assume that untwisting a message always yieldsa unique result. (For those of you with some knowledge of basic numbertheory or abstract algebra, this will be the case provided that thegreatest common divisor of the key k and length n is 1, which it will be for all test cases.)