ZOJ1006
Cryptography deals with methods of secret communication that transform a message (the plaintext) into a disguised form (the ciphertext) so that no one seeing the ciphertext will be able to 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 the sender and recipient both agree on a secret key k, which is a positive integer.
密码学方法秘密通信,将消息(明文)为一种变相(密文),其他人看了密文就能找出除了收件人明文。将明文加密,密文解密。
The twisting method uses four arrays: plaintext and ciphertext are arrays of characters, and plaincode and ciphercode are arrays of integers. All arrays are of length n, where n is the length of the message to be encrypted. Arrays are origin zero, so the elements are numbered from 0 to n - 1. For this problem all messages will contain only lowercase letters, the period, and the underscore (representing a space).
扭曲是一种简单的加密方法,要求发送方和接收方都用密钥k,这是一个正整数。扭曲方法是由四种数组组成,明文、密文是一组字符数组,plaincode 和 ciphercode是一组整形数组。所有数组长度为n,n 就是要加密的长度。数组从零开始,元素的编号从0到n-1。对于这个问题,所有的消息只包含小写字母、周期和下划线(表示空格)。
The message to be encrypted is stored in plaintext. Given a key k, the encryption method works as follows. First convert the letters in plaintext to integer codes in plaincode according to the following rule: '_' = 0, 'a' = 1, 'b' = 2, ..., 'z' = 26, and '.' = 27. Next, convert each code in plaincode to an encrypted code in ciphercode according to the following formula: for all i from 0 to n - 1,
要加密的信息用明文存储。给一般密钥K,加密方法如下。首先,先将明文字母在整数代码plaincode按下列规则:“_ ' = 0,A = 1,B = 2,…,Z = 26,= 27和’。”。下一步,将每个代码plaincode到一个加密的代码ciphercode根据以下公式:我从0到n - 1, ciphercode [我] =(plaincode [ KI mod n ] -我)mod 28。
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. You can use the C '%' operator or Pascal 'mod' operator to compute this as long as you add y if the result is negative.) Finally, convert the codes in ciphercode back to letters in ciphertext according to the rule listed above. The final twisted message is in ciphertext. Twisting the message cat using the key 5 yields the following:
| Array | 0 | 1 | 2 |
| plaintext | 'c' | 'a' | 't' |
| plaincode | 3 | 1 | 20 |
| ciphercode | 3 | 19 | 27 |
| ciphertext | 'c' | 's' | '.' |
Your task is to write a program that can untwist messages, i.e., convert the ciphertext back to the original plaintext given the key k. For example, given the key 5 and ciphertext 'cs.', your program must output the plaintext 'cat'.
X mod Y是积极的其余部分时,X是由Y除以例如,3 mod 7 = 3,22 mod 8 = 6,和- 1 mod 28 = 27。你可以使用C“%”运算符或Pascal mod运算符计算这个只要你添加Y如果结果是阴性的。)最后,将码在ciphercode回信件密文按照上面列出的。最终扭曲的消息在密文。使用密钥5将消息猫扭曲如下:
The input file contains one or more test cases, followed by a line containing 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, a space, and then a twisted message containing at least one and at most 70 characters. The key k will be a positive integer not greater than 300. For each test case, output the untwisted message on a line by itself.
输入列中可以输入一个或多个,输入列中有一个0,程序结束。每个测试用例本身都是一行的,由键k、空格和一个包含至少一个至多70个字符的扭曲的消息组成。键k将是一个正整数,不大于300。对于每一个测试案例,输出一行解密的明文。
Note: you can assume that untwisting a message always yields a unique result. (For those of you with some knowledge of basic number theory or abstract algebra, this will be the case provided that the greatest common divisor of the key k and length n is 1, which it will be for all test cases.)
Example input:
5 cs. 101 thqqxw.lui.qswer 3 b_ylxmhzjsys.virpbkr 0
Example output:
cat this_is_a_secret beware._dogs_barking
Source: Zhejiang University Local Contest 2001

浙公网安备 33010602011771号