#!/usr/bin/python3 from typing import Callable from math import log2 gates = 0 def gate(in1: str, in2: str, in3: str) -> str: global gates gates += 1 print(f"gate{gates}: {in1} {in2} {in3}") return f"gate{gates}" def gate_not(in1: str) -> str: return gate(in1, "0", "1") def gate_and(in1: str, in2: str) -> str: return gate(in1, in2, "0") def gate_or(in1: str, in2: str) -> str: return gate(in1, "1", in2) def gate_xor(in1: str, in2: str) -> str: return gate(gate_and(in1, in2), "0", gate_or(in1, in2)) def solve( values: list[list[str]], combine: Callable[[list[str], list[str]], list[str]], finish: list[str] ) -> None: while len(values) > 1: values = [ combine(values[i], values[i+1]) if i < len(values) - 1 else values[i] for i in range(0, len(values), 2) ] res = "1" value = values[0] for i in range(len(finish)): if finish[i] == "0": value[i] = gate_not(value[i]) res = gate_and(res, value[i]) print(f"result: {res} 1 0") def subtask1(i1: list[str], i2: list[str]) -> list[str]: return [gate_and(i1[0], i2[0])] def subtask2(i1: list[str], i2: list[str]) -> list[str]: return [gate_or(i1[0], i2[0])] def subtask3(i1: list[str], i2: list[str]) -> list[str]: return [ gate_or(i1[0], i2[0]), gate_or(gate_and(i1[0], i2[0]), gate_or(i1[1], i2[1])) ] def bit_adder(i1: str, i2: str, i3: str) -> tuple[str, str]: x1 = gate_xor(i1, i2) return ( gate_xor(x1, i3), gate_or(gate_and(i1, i2), gate_and(x1, i3)) ) def subtask4(i1: list[str], i2: list[str]) -> list[str]: while len(i2) < len(i1): # Zarovnáme, pokud dvě čísla jsou různě dlouhá i2.append("0") carry = "0" res = [None] * len(i1) for i in range(len(i1)): res[i], carry = bit_adder(i1[i], i2[i], carry) return res + [carry] subtask, n = map(int, input().split()) if subtask == 1: solve([[f"in{i}"] for i in range(n)], subtask1, ["1"]) elif subtask == 2: solve([[f"in{i}"] for i in range(n)], subtask2, ["1"]) elif subtask == 3: solve([[f"in{i}", "0"] for i in range(n)], subtask3, ["1", "0"]) else: solve([ [f"in{i}"] for i in range(n)], subtask4, list(bin(n//2)[2:])[::-1] )