Minterm and maxterm of a row

Every row of a truth table has two names. Its minterm is the AND of all the variables that is 1 only on that row. Its maxterm is the OR of all the variables that is 0 only on that row.

What to build

Print the minterm and the maxterm of a given row.

Requirements

  • The variables are A, B, C, D with A as the leftmost (most significant) bit.
  • A complemented variable is written with ', for example A'.
  • The minterm joins its literals with no sign, like A'BC. The maxterm joins them with + inside brackets, like (A + B' + C').

Input

One line with n (1 to 4) and the row number k (0 to 2ⁿ − 1).

Output

Two lines: the minterm, then the maxterm.

Constraints

1 ≤ n ≤ 4.

Example

Input:

3 5

Output:

AB'C
(A' + B + C')

Why it works

A minterm must be 1 on its own row only, so each variable must show up as 1 there: the variable itself when its bit is 1, the primed variable when it is 0. A maxterm is the opposite, 0 only on its row, so a bit 1 gives a primed variable (that literal is 0) and a bit 0 gives the plain one. The maxterm is always the complement of the minterm of the same row.

Examples

Example 1 · Input
3 5
Expected output
AB'C
(A' + B + C')
Example 2 · Input
2 0
Expected output
A'B'
(A + B)

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Write the row number as `n` binary digits: format(k, '0' + str(n) + 'b').

Hint 2

In a minterm a 1 bit is the variable and a 0 bit is the variable with a prime. In a maxterm it is the other way round.

Hint 3

Join the maxterm's literals with ` + `, and put it in brackets.

Solution

Show one correct solution (try it yourself first)
n, k = map(int, input().split())
bits = format(k, "0" + str(n) + "b")
names = "ABCD"
minterm = "".join(names[i] if b == "1" else names[i] + "'" for i, b in enumerate(bits))
maxterm = " + ".join(names[i] + "'" if b == "1" else names[i] for i, b in enumerate(bits))
print(minterm)
print("(" + maxterm + ")")

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Minterms, maxterms and canonical forms →