Vault Breaker
Time limit1sMemory limit1024 MB
Find the fewest presses of A (add 1 to the whole number mod B^N) and B (add 1 to each digit mod B) that return the display to S, using both at least once.
- Level
Medium7 of 10
- Topics
- Math, Number theory
- Solved
- No attempts yet
Problem
Util received an invitation from Sejun, a MatKor executive, and was allowed into the MatKor executive room. Sejun's Codeforces handle is SafeSpot, so the executive room has a vault.
The vault's password is an -digit number in base , and it can start with . The monitor shows the -digit base- number . The number on the monitor can be changed with two kinds of buttons. Only one button can be pressed at a time. Each button does the following when pressed.
- Button
A. Adds to the displayed number. If the result has more than digits, only the last digits are shown. In other words, . - Button
B. Adds to each digit of the displayed number. If a digit becomes , it becomes . In other words, for every , .
While Sejun is away, the curious Util decides to press the buttons on the vault. Pressing only one button over and over is boring, so Util will press both A and B at least once. However, if the value is different from its starting state, Sejun may notice that Util tampered with the vault. So Util wants the number on the monitor to be again when the operations finish. Since Util does not know when Sejun will return, Util wants to minimize the number of button presses.
Help Util press the vault buttons as few times as possible so that the number becomes again. Both buttons must be pressed at least once.
The -digit base- number has the following value.
Input
The first line contains two integers, (), the number of digits, and (), the base, separated by a space.
The second line contains integers (), which give each digit of , separated by spaces.
Output
On the first line, print the minimum number of button presses . If is greater than , print -1 instead.
If is at most , print a string of length on the second line. Each character is A or B, and the string gives the order in which the buttons are pressed.
If several sequences have the minimum length, print any one of them.
Hint
Sejun's Codeforces handle is SafeSpot.