#!/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 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 add(i1: list[str], i2: list[str]) -> list[str]: while len(i2) < len(i1): # Zarovnáme, pokud dvě čísla jsou různá 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] def solve( finish: int, negate: bool = False, ) -> None: values = [[f"in{i}"] for i in range(n)] while len(values) > 1: values = [ add(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] finish_number = list("0"*int(log2(n)+1) + bin(finish)[2:])[::-1] for i in range(len(value)): if finish_number[i] == "0": value[i] = gate_not(value[i]) res = gate_and(res, value[i]) if negate: print(f"result: {res} 0 1") else: print(f"result: {res} 1 0") subtask, n = map(int, input().split()) result = [None, n, 0, 1, n//2] solve(result[subtask], subtask == 2)