HomeVolumesContestsSectionsForumsUsersPrintHelpAbout

Volumes > Problems from everywhere > problem:


TopCoder-1. 50761 - Magic Words

Guest
• Discussion of problem (1)

Volume problems

• 50642 - Draft board
• 50643 - Stepeny couples
• 50644 - Capital
• 50645 - Line
• 50665 - Intersecting rectangles
• 50762 - Rare name
• KBH-1012. 50617 - KBH
• KBH-1013. 50618 - Code Correction
• TopCoder-1. 50761 - Magic Words
• TopCoder-2. 50246 - Making Potions
• TopCoder-3. 50245 - Santa Gifts
• TopCoder-4. 50701 - Cell Removal
• TopCoder-5. 50760 - Hexatridecimal...
• TopCoder-6. 50759 - Text Statistics
• TopCoder-7. 50244 - Chessboard P...
• TopCoder-8. 50641 - Strong Prime ...
• VologdaInterCity-A. 50614 - Numbe...

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb. Difficulty Gamma

Consider a string T containing exactly L characters. The string Ti is a cyclic shift of T starting from position i (0 ≤ i < L). Ti contains exactly the same number of characters as T. For each j between 0 and L-1, inclusive, character j of Ti is equal to character (i+j)%L of T. Let's call T a magic word if there are exactly K positions i such that Ti = T.

You are given a set of N words S = {S[0], ..., S[N-1]}. For each permutation p = {p[0], ..., p[N-1]} of integers between 0 and N-1, inclusive, we can define a string generated by this permutation as a concatenation S[p[0]] + ... + S[p[N-1]]. Return the number of permutations that generate magic words. All indices in this problem as 0-based.

Input
The first line contains two numbers N and K (1 ≤ N ≤ 8, 1 ≤ K ≤ 200). Next N lines will contain strings S[i]. Each line will contain only uppercase Latin letters ('A' - 'Z'). All lines will contain between 1 and 20 characters, inclusive.
Output
Output single number - the number of permutations that generate magic words.

Input 1 Input 2 Input 3 Input 4
3 1
CAD
ABRA
ABRA
3 2
AB
RAAB
RA
4 1
AA
AA
AAA
A
6 15
AA
AA
AAA
A
AAA
AAAA
Output 1 Output 2 Output 3 Output 4
6
3
0
720

Для отправки решений необходимо выполнить вход.

www.contester.ru