Under construction

Still being built. Things may change

Vault Breaker

Time limit1sMemory limit1024 MB

Summary
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 NN-digit number in base BB, and it can start with 00. The monitor shows the NN-digit base-BB number S=SN−1SN−2⋯S1S0‾(B)S=\overline{S_{N-1}S_{N-2}\cdots S_1S_0}_{(B)}. 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 11 to the displayed number. If the result has more than NN digits, only the last NN digits are shown. In other words, S←(S+1) mod BNS\leftarrow (S+1)\bmod B^N.
  • Button B. Adds 11 to each digit of the displayed number. If a digit becomes BB, it becomes 00. In other words, for every ii, Si←(Si+1) mod BS_i\leftarrow (S_i+1)\bmod B.

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 SS 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 SS again. Both buttons must be pressed at least once.

The NN-digit base-BB number S=SN−1SN−2⋯S1S0‾(B)S=\overline{S_{N-1}S_{N-2}\cdots S_1S_0}_{(B)} has the following value.

S=SN−1SN−2⋯S1S0‾(B)=∑i=0N−1SiBiS=\overline{S_{N-1}S_{N-2}\cdots S_1S_0}_{(B)}=\sum_{i=0}^{N-1}{S_iB^i}

Input

The first line contains two integers, NN (1≤N≤100 0001 \le N \le 100\,000), the number of digits, and BB (2≤B≤100 0002 \le B \le 100\,000), the base, separated by a space.

The second line contains NN integers SN−1,SN−2,⋯ ,S1,S0S_{N-1}, S_{N-2}, \cdots, S_1, S_0 (0≤Si<B0 \le S_i < B), which give each digit of SS, separated by spaces.

Output

On the first line, print the minimum number of button presses VV. If VV is greater than 4 000 0004\,000\,000, print -1 instead.

If VV is at most 4 000 0004\,000\,000, print a string of length VV 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.

Examples1

  1. Example 1

    Input
    3 3
    2 0 1
    
    Expected output
    6
    BBAAAA