123. Affine Cipher

Codeforces
IDCF10269123
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
You're trying to come up with a good Codeforces username. You want to use something similar to your real name, but not exactly your real name, to not release too much info about yourself online. Thus, you decide to encode your username using an Affine cipher. An affine cipher uses a key of two values: $a$ and $b$. Given a letter $l$, you first convert it into it's "character code", for example, "a" would become $0$, "b" would become $1$, etc. We'll call this $x$. Then, you calculate the value of $a$$x$ + $b$ mod $26$, and reverse the character code mapping that you used previously, to convert the answer back into a letter. The affine cipher runs this process on all letters (not spaces) in a given string. For example, let's use the letter $c$, and a key of $a$ = $15$, and $b$ = $5$. $c$ has a character code of $2$. Then, the encoded character has a character code of $15$*$2$ + $5$ mod $26$ = $35$ mod $26$ = $9$. Thus, the encoded character is $j$. The first line of input contains two space-separated integers $a$ and $b$: the affine cipher's key, as described above. The second line of input consists of a string consisting only of lowercase characters and spaces. Output the input string, encoded using the affine cipher as described above. ## Input The first line of input contains two space-separated integers $a$ and $b$: the affine cipher's key, as described above. The second line of input consists of a string consisting only of lowercase characters and spaces. ## Output Output the input string, encoded using the affine cipher as described above. [samples]
**Definitions** Let $ a, b \in \mathbb{Z} $ be the affine cipher key, with $ \gcd(a, 26) = 1 $. Let $ S $ be the input string over the alphabet $ \Sigma = \{ \text{a}, \dots, \text{z}, \text{ } \} $. Define the character code mapping $ c: \{\text{a}, \dots, \text{z}\} \to \{0, \dots, 25\} $ by $ c(\text{a}) = 0, c(\text{b}) = 1, \dots, c(\text{z}) = 25 $. Let $ c^{-1}: \{0, \dots, 25\} \to \{\text{a}, \dots, \text{z}\} $ be its inverse. **Constraints** 1. $ 1 \leq a \leq 25 $, $ 0 \leq b \leq 25 $, $ \gcd(a, 26) = 1 $ 2. $ S $ contains only lowercase English letters and spaces. **Objective** For each character $ l \in S $: - If $ l $ is a space, output $ l $. - If $ l $ is a letter, compute: $$ y = (a \cdot c(l) + b) \mod 26 $$ and output $ c^{-1}(y) $. Output the resulting encoded string.
API Response (JSON)
{
  "problem": {
    "name": "123. Affine Cipher",
    "description": {
      "content": "You're trying to come up with a good Codeforces username. You want to use something similar to your real name, but not exactly your real name, to not release too much info about yourself online. Thus,",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10269123"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You're trying to come up with a good Codeforces username. You want to use something similar to your real name, but not exactly your real name, to not release too much info about yourself online. Thus,...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ a, b \\in \\mathbb{Z} $ be the affine cipher key, with $ \\gcd(a, 26) = 1 $.  \nLet $ S $ be the input string over the alphabet $ \\Sigma = \\{ \\text{a}, \\dots, \\text{z}, \\text{ } \\}...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments