Osmosmjerka
Time limit4sMemory limit256 MB
Given a letter block tiled infinitely in all directions, pick a random start square and one of 8 directions twice, and report the probability that the two read words of length K match, as a reduced fraction.
- Level
Hard8 of 10
- Topics
- Math, String matching, Hash map, Combinatorics
- Solved
- No attempts yet
Problem
Repeating a block of letters with rows and columns in every direction gives an infinite eight-direction word search. For example, this block
honi
hsin
builds the crossword
...honihonihonihoni...
...hsinhsinhsinhsin...
...honihonihonihoni...
...hsinhsinhsinhsin...
which runs on forever left, right, up and down.
Pick one square of the crossword at random, and one of the eight directions at random. Start at that square, step one square at a time in that direction, and read letters. That gives a word of length . Since the crossword is one block repeated, every square of the crossword matches some square of the block, so picking the starting square is the same as drawing one of the squares of the block uniformly. The direction is drawn uniformly from the eight as well.
Run this trial twice, independently. Compute the probability that the two words are equal.
Input
The first line contains the integers , , (, ).
Each of the next lines contains lowercase letters of the English alphabet and describes one row of the block. The block contains at least two different letters.
Output
Print the probability as a reduced fraction p/q, with no spaces.