eolymp
bolt
Try our new interface for solving problems
Problems

Key Insight

Key Insight

Alice and Bob love to send each other messages, but they don’t like it when other people read their messages. Your friend Charles is very interested in what Alice and Bob send to each other, but since Alice and Bob are encrypting their messages he is unable to read them, even though he is able to intercept the encrypted messages (called "ciphertext"). \includegraphics{https://static.e-olymp.com/content/cb/cb548ed02715dc9f1a1ad56b846bc022b5e15e2a.jpg} \textit{\textbf{Figure 1}}: Example of transposition. Recently, Charles has not only intercepted an encrypted message, he also knows the original content of this message (which is called "plaintext"). To help him decrypt future messages between Alice and Bob, you are asked to write a program that will help him look for the encryption key. Charles has informed you that he knows that Alice and Bob are using a transposition block cipher. This means that for each block of \textbf{k} characters in the message, the characters within the block are re-ordered into one of \textbf{k!} possible permutations during encryption. Each permutation is determined by its unique corresponding encryption key. The key corresponding to the permutation shown in \textit{\textbf{Figure 1}} would be some representation of (\textbf{123456}) → (\textbf{514362}). Since your only task is counting (possible) keys, the actual representation is not relevant. Fortunately, Charles does know the block size \textbf{k}, and he knows that the plaintext and ciphertext that he intercepted consist of one or more full blocks of length \textbf{k} (i.e., no incomplete blocks) that have each been encrypted with the same key. Given the plaintext \textbf{M} and ciphertext \textbf{C} that Charles has intercepted, your program will compute the number of possible encryption keys. \InputFile For each test case, the input contains three lines: \begin{itemize} \item One line containing a positive integer \textbf{k}, the block size (\textbf{k} ≥ \textbf{1}). \item One line containing \textbf{M}, the plaintext (\textbf{1} ≤ \textbf{|M|} ≤ \textbf{100}, \textbf{|M|} is a multiple of \textbf{k}). \item One line containing \textbf{C}, the ciphertext (\textbf{|C| = |M|}). \end{itemize} Both plaintext and ciphertext consist only of lower-case letters. \OutputFile For each test case, print one line containing the number of possible encryption keys of size \textbf{k}. This number will not exceed \textbf{2^63-1}. If it is impossible to obtain \textbf{M} from \textbf{C} by a transposition cipher of block size \textbf{k}, print '\textbf{0}' (the number zero).
Time limit 1 second
Memory limit 64 MiB
Input example #1
4
treewood
ertedowo
1
nwerc
ncrew
6
secret
etrcse
1
impossibru
youdontsay
Output example #1
1
0
2
0
Source NWERC 2012 - NorthWestern European Regional Championship 2012