Finite automaton

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

Today Vasya learned what a <<deterministic finite automaton>> (DFA) is, and he's aching to tell everyone about it.

As it turns out, there are NN states in a DFA. The automaton can be in any one of these state at any given moment during its work. The input of the automaton is an arbitrary string, and after its work, the automaton tells whether the string is acceptable.

The automaton works in the following manner:

  1. In the beginning of its work, the automaton is in the start state, which is always marked as such.
  2. The automaton reads all symbols of the string one by one from left to right. After reading each symbol, the automaton can switch to a different state (described in detail below).
  3. After the string is read completely, the automaton defines the answer based on the state in which it ended up.

For each state uu of the automaton and each possible symbol cc, automaton defines in which state will it be afer reading the symbol cc, if it was in the state uu beforehand. This new state can either be the same state uu or be a different state. Moreover, for each state the automaton defines the answer it will give (whether the string is accepted or not) if it finished in that state.

In the first seminar on the subject, Vasya constructed all sorts of DFA's, and he was given the following problem for <<homework>>. Build a DFA which, given a non-negative integer written in the BB-ary numeral system, accepts those and only those integers that are divisible by the given module MM.

To simplify, Vasya assumes that the input number:

  • begins from high-order digits (they're written on the left, big-endian);
  • can have leading zeroes;
  • can be empty: in this case it is equal to zero and is definitely divisible by MM.

Vasya is a born perfectionist, and he wants to learn how to build DFA's that meet the problem requirements with the smallest possible number of states, He asked you to help him.

입력

The only line of the input file contains two integers: BB --- the base of the positional numeral system in which the input number is given and MM --- the module which all acceptable and only acceptable numbers must be divisible by (2B16,2M1052 \le B \le 16, 2 \le M \le 10^5).

출력

Print the description of the smallest DFA meeting the problem requirements to the output file.

The first line of the output file must contain two integers: NN --- the count of states in the automaton (N2N \ge 2) and SS --- the start state number (0S<N0 \le S < N). All states are numbered successively beginning from zero.

The second line must contain NN space-separated symbols. The kk-th of these symbols defines the answer the automaton gives if it ends up in the kk-th state upon the completion of its work (0k<N0 \le k < N). A symbol equals 'G' if the string should be deemed acceptable, and 'B' otherwise.

There must be NN line following, each containing BB integers. The kk-th number in the ii-th of these lines contains the state number in which the automaton ends up after reading digit kk, if before that it was in the state ii (0i<N0 \le i < N, 0k<B0 \le k < B). This number can be any integer between 00 and N1N-1 inclusively.

힌트

Shown below is the DFA used in the sample. The letter "S" and the number are shown near each state. Bold arrow points to the start state. Each normal arrow describes the state the DFA goes to after reading the digit written near the arrow.