Abwords
Time limit1sMemory limit128 MB
Given N, find the minimum word length over A/B words (starting with A, length at least 2) that admits an N-step cycle of the two given transformations.
- Level
Hard8 of 10
- Topics
- Math, Combinatorics, Brute force, Implementation
- Solved
- No attempts yet
Problem
A word is a string of two or more letters, each of them A or B, that starts with A. Two actions can be applied to a word, and each one gives another word.
- R1: change only the last letter. A becomes B and B becomes A. Every other letter stays as it is.
- R2: build a new word from the word . The first letter of is A. At a position , the letter is B when and are equal, and A when they differ. The new word then replaces .
Start from a word and apply actions of type R1 and R2 in any order. The sequence of actions is an -transformation of when both of these hold.
- The word after the -th action equals .
- The words produced along the way differ from one another and from .
An integer greater than 1 is given. Find the smallest number of letters a word can have if it starts an -transformation.
Input
The first line contains the integer .
Output
Print on one line the smallest number of letters of a word that can start an -transformation. If no such word exists, print -1.
Constraints
Hint
No word of fewer than 4 letters starts a sequence of 6 actions that comes back to it without any word appearing twice along the way. The four-letter word AABB does have such a sequence. Applying R2 to AABB gives ABAB, another R2 gives AAAA, R1 gives AAAB, R2 gives ABBA, R1 gives ABBB, and a last R2 comes back to AABB. So the answer for is 4.