Tales from DeCrypt

Time limit1sMemory limit128 MB

Problem

In newsgroups, mailing lists, and other public forums, a popular way to obscure text without truly hiding it is the ROT13 cipher: every alphabetic character is rotated by 13 positions (modulo 26), so the very same routine both encrypts and decrypts. Material that some readers might find offensive is deliberately posted in ROT13 form, on the theory that a reader who chooses to rotate it back into clear text is then responsible for what they read.

A rotational cipher can also genuinely hide information, not merely obscure it. Your job is to write the decryption program for the encryption scheme described below.

The scheme operates only on 7-bit printable ASCII: the 95 characters from 0x20 (space) through 0x7e (~) inclusive. Any byte outside this range is copied to the output untouched, so the ciphertext stays ordinary text that can be stored and transmitted as such.

Random number generator. Three integers drive a linear congruential generator: the multiplier a, the modulus m, and a mutable seed s.

double r(int a, int m, int &s):   // s is carried over between calls
    double val = (s mod m) / double(m)
    s = (a * s + 1) mod m
    return val

The multiplier, modulus, and initial seed appear on the first line as three whitespace-separated integers in the order a m s, each satisfying $2 \le a, m, s \le 65536$. For instance, a first line of 12343 65536 11 means a = 12343, m = 65536, and s = 11.

Encryption. The ciphertext is produced one character at a time:

for each character c in the input stream:
    if c is not in the range 0x20 .. 0x7e:
        output c unchanged
    else:
        c = ((c - 32) + ceil(95 - r(a, m, s) * 95)) mod 95 + 32
        output c

Every printable character maps to another printable character, and the generator advances only when a printable character is processed. Recover the original text.

Input

The input is exactly what the encryption program produced. The first line holds the three whitespace-separated integers a, m, and s. The encrypted text begins on the next line and runs to the end of the file; any non-printable bytes it contains (such as newlines) were passed straight through by the encryption step.

Output

Print the decryption of the encrypted text given in the input.