description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
k = int(input()) s = str(input()) t = str(input()) a, b, m = [], [], [] p, d = 0, 0 for i in range(k - 1, -1, -1): a.append(ord(s[i]) - ord("a")) b.append(ord(t[i]) - ord("a")) a[k - 1 - i] += b[k - 1 - i] + d d = a[k - 1 - i] // 26 if i != 0: a[k - 1 - i] %= 26 carry = 0 for i in range(k - 1, -1, -1): current = a[i] + carry * 26 a[i] = current // 2 carry = current % 2 print(chr(a[i] + 97), end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
a = int(input()) b = [(ord(i) - 97) for i in input()[::-1]] c = [(ord(i) - 97) for i in input()[::-1]] it = b[0] + c[0] s = [] for i in range(1, a): it += 26 * (b[i] + c[i]) s.append(chr(it // 2 % 26 + 97)) it //= 26 s.append(chr(it // 2 % 26 + 97)) s = "".join(s[::-1]) print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
def main(): n = int(input()) s = [(ord(c) - ord("a")) for c in input()] t = [(ord(c) - ord("a")) for c in input()] r = [(s[i] + t[i]) for i in range(n)] for i in range(n - 1, 0, -1): if r[i] >= 26: r[i] -= 26 r[i - 1] += 1 k = 0 rs = [0] * n for i in range(n): b = r[i] + k rs[i] = chr(b // 2 + ord("a")) if b % 2: k = 26 else: k = 0 return "".join(rs) print(main())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
k = int(input()) s = input().strip() t = input().strip() num_2 = list(map(ord, s)) num_1 = list(map(ord, t)) for i in range(k): num_1[i] -= 97 num_2[i] -= 97 def plus(num_1, num_2): res = [0] * (len(num_1) + 1) memory = 0 num_1.reverse() num_2.reverse() for i in range(len(num_1)): add = num_1[i] + num_2[i] + memory res[i] = add % 26 memory = add // 26 res.reverse() res[0] = memory return res def div_2(num): memory = 0 res = [0] * len(num) for i in range(len(num)): res[i] = (num[i] + memory * 26) // 2 memory = (num[i] + memory * 26) % 2 return res summ = plus(num_1, num_2) res = div_2(summ) print("".join(chr(i + 97) for i in res[1:]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
N = int(input()) s1 = [(ord(i) - ord("a")) for i in input()[::-1]] s2 = [(ord(i) - ord("a")) for i in input()[::-1]] res = [] temp = s1[0] + s2[0] for i in range(1, N): temp += (s1[i] + s2[i]) * 26 res.append(chr(temp // 2 % 26 + ord("a"))) temp //= 26 res.append(chr(temp // 2 % 26 + ord("a"))) print("".join(res[::-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
alphabet = "abcdefghijklmnopqrstuvwxyz" alphabet_d = {} for i, c in enumerate(alphabet): alphabet_d[c] = i def transform_string(s): n = len(s) res = [0] for i in range(n): res.append(alphabet_d[s[i]]) return res k = int(input()) s = input() t = input() s = transform_string(s) t = transform_string(t) for i in range(k, -1, -1): s[i] += t[i] if i: s[i - 1] += s[i] // 26 s[i] = s[i] % 26 for i in range(k + 1): rem = s[i] % 2 s[i] = s[i] // 2 if i + 1 <= k: s[i + 1] += rem * 26 else: assert rem == 0 s = list(map(lambda x: alphabet[x], s[1:])) print("".join(s))
ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
indexes = {} alpha = "abcdefghijklmnopqrstuvwxyz" for i in alpha: indexes[i] = alpha.index(i) n = int(input()) first = [indexes[x] for x in input()] second = [indexes[x] for x in input()] third = [0] * n carry = 0 for i in range(n - 1, -1, -1): tempResp = first[i] + second[i] + carry if i != 0 and tempResp >= 26: carry = 1 tempResp = tempResp - 26 else: carry = 0 third[i] = tempResp carry = 0 for i in range(n): comCarry = third[i] + carry * 26 third[i] = comCarry // 2 if comCarry % 2 != 0: carry = 1 else: carry = 0 print("".join(alpha[k] for k in third))
ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
def next_int(): return int(input()) def next_int_list(): return [int(i) for i in input().split(" ")] def next_float(): return float(input()) def next_float_list(): return [int(i) for i in input().split(" ")] def next_string(): return input() def print_int_list(l): print(" ".join([str(i) for i in l])) n = next_int() s = next_string() t = next_string() orda = ord("a") base = 26 s = [(ord(c) - orda) for c in s] t = [(ord(c) - orda) for c in t] num = [(0) for i in range(n + 1)] acc = 0 for i in range(n - 1, -1, -1): summ = s[i] + t[i] + acc if summ >= base: summ -= base acc = 1 else: acc = 0 num[i + 1] = summ num[0] = acc acc = 0 for i in range(n + 1): d = num[i] + acc if d % 2 == 1: acc = base else: acc = 0 num[i] = d // 2 num = "".join([chr(i + orda) for i in num[1:]]) print(num)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
k = int(input()) s1 = input() s2 = input() a1 = [] a2 = [] delta = [] for i in range(k): a1.append(ord(s1[i]) - 97) a2.append(ord(s2[i]) - 97) delta.append(a2[i] + a1[i]) for i in range(k - 1): delta[i + 1] += 26 * (delta[i] % 2) delta[i] = delta[i] // 2 delta[k - 1] = delta[k - 1] // 2 i = k - 1 while i > 0: if delta[i] >= 26: delta[i - 1] += 1 delta[i] -= 26 else: i -= 1 for i in range(k): print(chr(delta[i] + 97), end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
def stringToBase(string, K): base = [] for i in range(0, K): base += [ord(string[i]) - ord("a")] return base def getMedian(stringA, stringB, K): baseA = stringToBase(stringA, K) baseB = stringToBase(stringB, K) baseResult = [] for i in range(0, K): baseResult += [0] for i in range(0, K): index = K - i - 1 baseResult[index] += baseA[index] + baseB[index] if baseResult[index] >= 26 and index > 0: baseResult[index] -= 26 baseResult[index - 1] += 1 for i in range(0, K): if i != K - 1: baseResult[i + 1] += 26 * (baseResult[i] % 2) baseResult[i] = int(baseResult[i] / 2) stringMedian = "" for i in range(0, K): stringMedian += chr(ord("a") + baseResult[i]) return stringMedian K = int(input()) S = input() T = input() print(getMedian(S, T, K))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR LIST BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
k = int(input()) s = input() t = input() s = [(ord(s[i]) - ord("a")) for i in range(k)] t = [(ord(t[i]) - ord("a")) for i in range(k)] res = [0] * (k + 1) add = 0 for i in range(k - 1, -1, -1): sum = t[i] + s[i] + add add = sum // 26 res[i + 1] = sum % 26 res[0] = add add = 0 ans = [] for i in range(k + 1): d = add * 26 + res[i] add = d % 2 ans.append(d // 2) print("".join(map(lambda x: chr(x + ord("a")), ans[1:])))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING VAR NUMBER
You are given two strings $s$ and $t$, both consisting of exactly $k$ lowercase Latin letters, $s$ is lexicographically less than $t$. Let's consider list of all strings consisting of exactly $k$ lowercase Latin letters, lexicographically not less than $s$ and not greater than $t$ (including $s$ and $t$) in lexicographical order. For example, for $k=2$, $s=$"az" and $t=$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"]. Your task is to print the median (the middle element) of this list. For the example above this will be "bc". It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Input----- The first line of the input contains one integer $k$ ($1 \le k \le 2 \cdot 10^5$) β€” the length of strings. The second line of the input contains one string $s$ consisting of exactly $k$ lowercase Latin letters. The third line of the input contains one string $t$ consisting of exactly $k$ lowercase Latin letters. It is guaranteed that $s$ is lexicographically less than $t$. It is guaranteed that there is an odd number of strings lexicographically not less than $s$ and not greater than $t$. -----Output----- Print one string consisting exactly of $k$ lowercase Latin letters β€” the median (the middle element) of list of strings of length $k$ lexicographically not less than $s$ and not greater than $t$. -----Examples----- Input 2 az bf Output bc Input 5 afogk asdji Output alvuw Input 6 nijfvj tvqhwp Output qoztvz
n = int(input()) a = input() b = input() ch_a = [] ch_b = [] ch_re = [0] * n for i in range(n): ch_a.append(int(ord(a[i]) - ord("a"))) ch_b.append(int(ord(b[i]) - ord("a"))) flag = 0 for i in range(n - 1, -1, -1): temp = ch_a[i] + ch_b[i] + flag ch_re[i] = temp % 26 flag = temp // 26 if flag == 1: ch_re[0] += 26 flag = 0 for i in range(n): if flag: ch_re[i] += 26 flag = int(ch_re[i] % 2) ch_re[i] = int(ch_re[i] // 2) ch_re[i] = chr(ch_re[i] + ord("a")) print("".join(ch_re))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def solve(K, A, B, C): a = list(bin(A)[2:]) b = list(bin(B)[2:]) c = list(bin(C)[2:]) l = max(map(len, (a, b, c))) a = ["0"] * (l - len(a)) + a b = ["0"] * (l - len(b)) + b c = ["0"] * (l - len(c)) + c for i in range(len(c)): if c[i] == "0": if a[i] != "0": K -= 1 a[i] = "0" if b[i] != "0": K -= 1 b[i] = "0" elif a[i] == "0" and b[i] == "0": K -= 1 b[i] = "1" if K < 0: return (-1,) i = 0 while K > 0 and i < len(a): if a[i] == "1": if b[i] == "0": if K >= 2: a[i] = "0" b[i] = "1" K -= 2 else: a[i] = "0" K -= 1 i += 1 return hex(int("".join(a), 2))[2:].upper(), hex(int("".join(b), 2))[2:].upper() Q = int(input()) for _ in range(Q): K = int(input()) A = int(input(), 16) B = int(input(), 16) C = int(input(), 16) print("\n".join(map(str, solve(K, A, B, C))))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
import sys from itertools import chain input = sys.stdin.readline def fit(l): return lambda x: x + ["0"] * (l - len(x)) def hex_to_list(x): return list( chain.from_iterable( [bool(n & 1 << i) for i in range(4)] for n in [int(xx, 16) for xx in reversed(x)] ) ) def list_to_hex(l): return ( "".join( hex(sum(1 << i for i, b in enumerate(l[i : i + 4]) if b))[2:].upper() for i in range(0, len(l), 4) )[::-1].lstrip("0") or "0" ) def main(k, a, b, c): a, b, c = map(hex_to_list, (a, b, c)) n = max(len(a), len(b), len(c)) a, b, c = map(fit(n), (a, b, c)) for i in range(n): if c[i]: if not a[i] and not b[i]: k -= 1 b[i] = True else: if a[i]: a[i] = False k -= 1 if b[i]: b[i] = False k -= 1 if k < 0: return (-1,) for i in reversed(range(n)): if not k: break if a[i]: if b[i]: a[i] = False k -= 1 elif k >= 2: a[i] = False b[i] = True k -= 2 return map(list_to_hex, (a, b)) for _ in range(int(input())): print(*main(*([int(input())] + [input().strip() for __ in range(3)])), sep="\n")
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL STRING FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER STRING STRING FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER STRING
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def adjust(a, b, c, k): for i in range(4): if c & 1 << i and not (a | b) & 1 << i: b |= 1 << i k -= 1 elif not c & 1 << i: if a & 1 << i: a ^= 1 << i k -= 1 if b & 1 << i: b ^= 1 << i k -= 1 return (a, b, k) if k >= 0 else None def make_small(a, b, k): for i in range(3, -1, -1): if (a | b) & 1 << i: if k >= 1 and a & b & 1 << i: a ^= 1 << i k -= 1 elif k >= 2 and a & 1 << i: a ^= 1 << i b ^= 1 << i k -= 2 return a, b, k def fh(c): return int(c, 16) def th(v): if 0 <= v <= 9: return chr(v + ord("0")) if v == 10: return "A" if v == 11: return "B" if v == 12: return "C" if v == 13: return "D" if v == 14: return "E" if v == 15: return "F" q = int(input().strip()) for _ in range(q): k = int(input().strip()) a = input().strip() b = input().strip() c = input().strip() max_len = max(len(a), len(b), len(c)) a = "0" * (max_len - len(a)) + a b = "0" * (max_len - len(b)) + b c = "0" * (max_len - len(c)) + c new_a = [(0) for _ in range(max_len)] new_b = [(0) for _ in range(max_len)] for i in range(max_len): ret = adjust(fh(a[i]), fh(b[i]), fh(c[i]), k) if ret is None: print(-1) break new_a[i], new_b[i], k = ret else: i = 0 while k and i < max_len: new_a[i], new_b[i], k = make_small(new_a[i], new_b[i], k) i += 1 print("".join(map(th, new_a)).lstrip("0") or "0") print("".join(map(th, new_b)).lstrip("0") or "0")
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR NUMBER VAR VAR VAR NONE FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF IF NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING STRING
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input()) for _ in range(Q): k = int(input()) A = bin(int(input(), 16))[2:] B = bin(int(input(), 16))[2:] C = bin(int(input(), 16))[2:] l = max(len(A), len(B), len(C)) A = list("0" * (l - len(A)) + A) B = list("0" * (l - len(B)) + B) C = list("0" * (l - len(C)) + C) for i in range(l): if A[i] == B[i] == "0" and C[i] == "1": B[i] = "1" k -= 1 if A[i] > C[i] or B[i] > C[i]: if A[i] == "1": A[i] = "0" k -= 1 if B[i] == "1": B[i] = "0" k -= 1 if k < 0: print(-1) continue for i in range(l): if A[i] == "1" and B[i] == "1" and k > 0: A[i] = "0" k -= 1 if A[i] == "1" and B[i] == "0" and k > 1: A[i] = "0" B[i] = "1" k -= 2 print("%X" % int("".join(A), 2)) print("%X" % int("".join(B), 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input().strip()) for a0 in range(Q): k = int(input()) a, b, c = [input() for _ in range(3)] m = max(len(x) for x in (a, b, c)) a = "0" * (m - len(a)) + a b = "0" * (m - len(b)) + b c = "0" * (m - len(c)) + c ra = "" rb = "" for da, db, dc in zip(a, b, c): da = int(da, 16) db = int(db, 16) dc = int(dc, 16) diff = (da | db) ^ dc d1, d2, d3 = diff & da, diff & db, (diff ^ db) & diff & ~da k -= bin(d1).count("1") k -= bin(d2).count("1") k -= bin(d3).count("1") if k < 0: print(-1) break ra = ra + "%X" % (da ^ d1) rb = rb + "%X" % (db ^ d2 ^ d3) else: ra2 = rb2 = "" for da, db in zip(ra, rb): p = 8 da = int(da, 16) db = int(db, 16) while k and p: if da & p: if db & p: da -= p k -= 1 elif k >= 2: da -= p db += p k -= 2 p >>= 1 if ra2 or da: ra2 = ra2 + "%X" % da if rb2 or db: rb2 = rb2 + "%X" % db print(ra2 or "0") print(rb2 or "0")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP STRING BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP STRING VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input())): K = int(input()) A, B, C = input().strip(), input().strip(), input().strip() a = bin(int(A, 16))[2:] b = bin(int(B, 16))[2:] c = bin(int(C, 16))[2:] mx = max(len(a), len(b), len(c)) k = 0 a = list(map(int, list("0" * (mx - len(a)) + a))) b = list(map(int, list("0" * (mx - len(b)) + b))) c = list(map(int, list("0" * (mx - len(c)) + c))) for i in range(len(a)): if a[i] == 1: if b[i] == 1: if c[i] == 0: a[i] = 0 b[i] = 0 k += 2 elif c[i] == 0: a[i] = 0 k += 1 elif b[i] == 1: if c[i] == 0: b[i] = 0 k += 1 elif c[i] == 1: b[i] = 1 k += 1 if k > K: print(-1) break else: for j in range(len(a)): if k == K: break if a[j] == 1 and b[j] == 1 and c[j] == 1: a[j] = 0 k += 1 if k >= K: break if a[j] == 1 and b[j] == 0 and c[j] == 1 and k < K - 1: a[j] = 0 b[j] = 1 k += 2 if k >= K: break print(str(hex(int("".join(map(str, a)), 2))).upper()[2:]) print(str(hex(int("".join(map(str, b)), 2))).upper()[2:])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
import sys Q = int(input().strip()) def printHex(A, B): print(hex(int("".join(A), 2)).upper()[2:]) print(hex(int("".join(B), 2)).upper()[2:]) for t in range(Q): k = int(input().strip()) A = list(bin(int(input().strip(), 16))[2:]) B = list(bin(int(input().strip(), 16))[2:]) C = list(bin(int(input().strip(), 16))[2:]) longitud = max(len(A), len(B), len(C)) A = ["0" for i in range(longitud - len(A))] + A B = ["0" for i in range(longitud - len(B))] + B C = ["0" for i in range(longitud - len(C))] + C for i in range(longitud): if C[i] == "0": if A[i] != "0": A[i] = "0" k -= 1 if B[i] != "0": B[i] = "0" k -= 1 elif A[i] != "1" and B[i] != "1": B[i] = "1" k -= 1 if k < 0: print(-1) elif k == 0: printHex(A, B) else: for i in range(longitud): if C[i] == "1": if B[i] == "1" and A[i] == "1" and k >= 1: A[i] = "0" k -= 1 elif B[i] == "0" and A[i] == "1" and k >= 2: A[i] = "0" B[i] = "1" k -= 2 printHex(A, B)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input())): K = int(input()) A = int(input(), 16) B = int(input(), 16) C = int(input(), 16) liczba = ( bin((A | B) ^ C).count("1") + bin(A & B).count("1") - bin(A & B & C).count("1") ) if liczba > K: print(-1) else: X = (A | B) ^ C Y = X & C dodaj = Y usun = X ^ Y Bn = B ^ dodaj ldodaj = bin(dodaj).count("1") lusun = 0 usunA = A & usun An = A ^ usunA lusun += bin(usunA).count("1") usunB = B & usun Bn = Bn ^ usunB lusun += bin(usunB).count("1") zmiany = ldodaj + lusun if zmiany < K: x = K - zmiany An = list(bin(An)) An.reverse() Bn = list(bin(Bn)) Bn.reverse() An.pop(-1) An.pop(-1) Bn.pop(-1) Bn.pop(-1) if len(An) < len(Bn): An.extend(["0" for x in range(len(Bn) - len(An))]) elif len(Bn) < len(An): Bn.extend(["0" for x in range(len(An) - len(Bn))]) An.reverse() Bn.reverse() while x: for i in range(len(An)): if An[i] == Bn[i] and An[i] == "1": An[i] = "0" x -= 1 if x == 0: break elif An[i] == "1" and Bn[i] == "0" and x >= 2: An[i] = "0" Bn[i] = "1" x -= 2 if x == 0: break else: x = 0 An = int("".join(An), 2) Bn = int("".join(Bn), 2) print("{0:x}".format(An).upper()) print("{0:x}".format(Bn).upper())
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR STRING FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def solve(a, b, c, k): a = "{0:b}".format(int(a, 16)) b = "{0:b}".format(int(b, 16)) c = "{0:b}".format(int(c, 16)) l = max(len(a), len(b), len(c)) a = "0" * (l - len(a)) + a b = "0" * (l - len(b)) + b c = "0" * (l - len(c)) + c a = [int(x) for x in a] b = [int(x) for x in b] c = [int(x) for x in c] for i in range(l - 1, -1, -1): if a[i] | b[i] != c[i]: if k <= 0: print(-1) return if c[i] == 1: b[i] = 1 k -= 1 continue else: if a[i] == 1: k -= 1 a[i] = 0 if b[i] == 1: k -= 1 b[i] = 0 for i in range(l): if (b[i] == 0) & (a[i] == c[i] == 1): if k >= 2: b[i] = 1 a[i] = 0 k -= 2 if a[i] & b[i] == c[i] == 1: if k >= 1: a[i] = 0 k -= 1 if k <= 0: break a = int("".join(map(str, a)), 2) b = int("".join(map(str, b)), 2) a = hex(a) b = hex(b) print(a[2:].upper()) print(b[2:].upper()) T = int(input()) for t0 in range(T): k = int(input()) a = input() b = input() c = input() solve(a, b, c, k)
FUNC_DEF ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input().strip())): num_changes = 0 K, A, B, C = ( int(input().strip()), list("{0:b}".format(int(input(), 16))), list("{0:b}".format(int(input(), 16))), list("{0:b}".format(int(input(), 16))), ) max_length = max(len(A), len(B), len(C)) A, B, C = ( ["0"] * (max_length - len(A)) + A, ["0"] * (max_length - len(B)) + B, ["0"] * (max_length - len(C)) + C, ) for i in range(max_length): if C[i] == "1": if B[i] == "0" and A[i] == "0": if num_changes + 1 > K: num_changes += 1 break B[i] = "1" num_changes += 1 elif B[i] == "1" and A[i] == "1": if num_changes + 2 > K: num_changes += 2 break A[i] = "0" B[i] = "0" num_changes += 2 elif A[i] == "1": if num_changes + 1 > K: num_changes += 1 break A[i] = "0" num_changes += 1 elif B[i] == "1": if num_changes + 1 > K: num_changes += 1 break B[i] = "0" num_changes += 1 if num_changes > K: print(-1) else: for i in range(max_length): if C[i] == "1": if B[i] == "1" and A[i] == "1": if num_changes + 1 > K: break A[i] = "0" num_changes += 1 elif A[i] == "1": if num_changes + 2 > K: continue A[i] = "0" B[i] = "1" num_changes += 2 print("{0:X}".format(int("".join(A), 2))) print("{0:X}".format(int("".join(B), 2)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR VAR STRING IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR VAR STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def leftpad(s, n): padlen = n - len(s) return "0" * padlen + s def solve(k, a, b, c): changed = 0 ap = "" bp = "" for x, y, z in zip(a, b, c): x = int(x, 16) y = int(y, 16) z = int(z, 16) p = 1 while p < 16: if z & p == 0: if x & p: x ^= p changed += 1 if y & p: y ^= p changed += 1 elif x & p == 0 and y & p == 0: y ^= p changed += 1 p <<= 1 ap += hex(x)[2:] bp += hex(y)[2:] app = "" bpp = "" for x, y, z in zip(ap, bp, c): x = int(x, 16) y = int(y, 16) z = int(z, 16) p = 8 while p > 0: if changed + 2 <= k and x & p and not y & p and z & p: x ^= p y ^= p changed += 2 elif changed + 1 <= k and x & p and y & p and z & p: x ^= p changed += 1 p >>= 1 app += hex(x)[2:] bpp += hex(y)[2:] if changed <= k: app = app.lstrip("0") if not app: app = "0" bpp = bpp.lstrip("0") if not bpp: bpp = "0" print(app.upper()) print(bpp.upper()) else: print(-1) q = int(input()) for _ in range(q): k = int(input()) a = input() b = input() c = input() n = max(len(a), len(b), len(c)) solve(k, leftpad(a, n), leftpad(b, n), leftpad(c, n))
FUNC_DEF ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP STRING VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input()) for __ in range(Q): K = int(input()) a = bin(int(input(), 16))[2:] b = bin(int(input(), 16))[2:] c = bin(int(input(), 16))[2:] maxlength = max(len(a), len(b), len(c)) a = list(map(int, a.rjust(maxlength, "0"))) b = list(map(int, b.rjust(maxlength, "0"))) c = list(map(int, c.rjust(maxlength, "0"))) for i in range(maxlength): if c[i]: if a[i] or b[i]: pass else: b[i] = 1 K -= 1 else: if a[i]: a[i] = 0 K -= 1 if b[i]: b[i] = 0 K -= 1 if K < 0: break if K < 0: print(-1) continue if K == 0: print(hex(int("".join([str(x) for x in a]), 2))[2:].upper()) print(hex(int("".join([str(x) for x in b]), 2))[2:].upper()) continue for i in range(maxlength): if c[i]: if a[i] and b[i] and K > 0: a[i] = 0 K -= 1 elif a[i] and K > 1: a[i] = 0 b[i] = 1 K -= 2 if K == 0: break print(hex(int("".join([str(x) for x in a]), 2))[2:].upper()) print(hex(int("".join([str(x) for x in b]), 2))[2:].upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
import sys Q = int(input().strip()) def boolify(s, length, mlength): res = [False] * (4 * mlength) k = mlength - length for i in range(0, length): x = int(s[i], 16) res[4 * k + 0] = x & 8 != 0 res[4 * k + 1] = x & 4 != 0 res[4 * k + 2] = x & 2 != 0 res[4 * k + 3] = x & 1 != 0 k += 1 return res def minimize(x, y, k, l): i = 0 while k and i < l: if x[i] and not y[i] and k >= 2: x[i] = False y[i] = True k -= 2 if x[i] and y[i]: x[i] = False k -= 1 i += 1 return k def print_res(x, l): hx = "0123456789ABCDEF" i = 0 while i < l - 4 and x[i] == x[i + 1] == x[i + 2] == x[i + 3] == 0: i += 4 while i < l: sys.stdout.write(hx[(x[i] << 3) + (x[i + 1] << 2) + (x[i + 2] << 1) + x[i + 3]]) i += 4 sys.stdout.write("\n") def query(): K = int(input().strip()) sA = input().strip() sB = input().strip() sC = input().strip() lA, lB, lC = len(sA), len(sB), len(sC) length = max(lA, lB, lC) A = boolify(sA, lA, length) B = boolify(sB, lB, length) C = boolify(sC, lC, length) i = 4 * length while i: i -= 1 a, b, c = A[i], B[i], C[i] if (a or b) != c: if c: B[i] = True K -= 1 else: if a: A[i] = False K -= 1 if b: B[i] = False K -= 1 if K < 0: print(-1) return minimize(A, B, K, 4 * length) print_res(A, 4 * length) print_res(B, 4 * length) while Q: query() Q -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
import sys def solve(a, b, c, k, debug=0): if debug > 0: print("a=%s b=%s c=%s k=%d" % (a, b, c, k), file=sys.stderr) mx = max(len(a), len(b), len(c)) a = "0" * (mx - len(a)) + a b = "0" * (mx - len(b)) + b c = "0" * (mx - len(c)) + c mp = { ("0", "0", "0"): (0, "0", "0"), ("0", "0", "1"): (1, "0", "1"), ("0", "1", "0"): (1, "0", "0"), ("0", "1", "1"): (0, "0", "1"), ("1", "0", "0"): (1, "0", "0"), ("1", "0", "1"): (0, "1", "0"), ("1", "1", "0"): (2, "0", "0"), ("1", "1", "1"): (0, "1", "1"), } cnt = 0 AA, BB, C = [], [], [] for v1, v2, v3 in zip(a, b, c): cc, ba, bb = mp[v1, v2, v3] cnt += cc AA.append(ba) BB.append(bb) C.append(v3) if debug > 0: print( "v1=%s v2=%s v3=%s cc=%d ba=%s bb=%s" % (v1, v2, v3, cc, ba, bb), file=sys.stderr, ) if cnt > k: return None mp = { ("0", "0", "0"): (0, "0", "0"), ("0", "1", "1"): (0, "0", "1"), ("1", "0", "1"): (2, "0", "1"), ("1", "1", "1"): (1, "0", "1"), } for i, t in enumerate(zip(AA, BB, C)): if cnt == k: break cc, nba, nbb = mp[t] if cnt + cc <= k: cnt += cc AA[i], BB[i] = nba, nbb AA, BB = int("".join(AA), 2), int("".join(BB), 2) return hex(AA)[2:], hex(BB)[2:] t = int(input()) def hextobin(s): mp = { "0": "0000", "1": "0001", "2": "0010", "3": "0011", "4": "0100", "5": "0101", "6": "0110", "7": "0111", "8": "1000", "9": "1001", "A": "1010", "B": "1011", "C": "1100", "D": "1101", "E": "1110", "F": "1111", } t = [] for c in s: t.append(mp[c]) return "".join(t) for _ in range(t): k = int(input()) a = hextobin(input()) b = hextobin(input()) c = hextobin(input()) t = solve(a, b, c, k, 0) if t is None: print(-1) else: A, B = t print(A.upper()) print(B.upper())
IMPORT FUNC_DEF NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER STRING STRING NUMBER STRING STRING NUMBER STRING STRING NUMBER STRING STRING NUMBER STRING STRING NUMBER STRING STRING NUMBER STRING STRING NUMBER STRING STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN NONE ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER STRING STRING NUMBER STRING STRING NUMBER STRING STRING NUMBER STRING STRING FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input()) for a in range(Q): k = int(input()) A = "{0:b}".format(int(input(), 16)) B = "{0:b}".format(int(input(), 16)) C = "{0:b}".format(int(input(), 16)) len_A = len(A) len_B = len(B) len_C = len(C) max_len = max(len_A, len_B, len_C) A = list("0" * (max_len - len_A) + A) B = list("0" * (max_len - len_B) + B) C = list("0" * (max_len - len_C) + C) found = True AChanged = [False] * max_len BChanged = [False] * max_len for ind in range(max_len): if C[ind] == "0": if A[ind] == "1": AChanged[ind] = True A[ind] = "0" k -= 1 if B[ind] == "1": BChanged[ind] = True B[ind] = "0" k -= 1 if C[ind] == "1": if A[ind] == "0" and B[ind] == "0": B[ind] = "1" BChanged[ind] = True k -= 1 if k < 0: found = False break if k > 0: for ind in range(max_len): if A[ind] == "1" and B[ind] == "1": A[ind] = "0" if not AChanged[ind]: k -= 1 if A[ind] == "1" and B[ind] == "0": if k > 1: A[ind] = "0" B[ind] = "1" if not AChanged[ind]: k -= 1 if not BChanged[ind]: k -= 1 if k < 1: break A = int("".join(A), 2) B = int("".join(B), 2) C = int("".join(C), 2) if found: print(format(A, "X")) print(format(B, "X")) else: print("-1")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input()) for _ in range(Q): K = int(input()) A = bin(int(input(), 16))[2:] B = bin(int(input(), 16))[2:] C = bin(int(input(), 16))[2:] max_lth = max(len(A), len(B), len(C)) max_lth += max_lth % 8 A = list(A.rjust(max_lth, "0")) B = list(B.rjust(max_lth, "0")) C = list(C.rjust(max_lth, "0")) extras = [] ops = 0 for bit_pos in range(max_lth): if C[bit_pos] == "0": if A[bit_pos] == "1": ops += 1 A[bit_pos] = "0" if B[bit_pos] == "1": ops += 1 B[bit_pos] = "0" elif A[bit_pos] == "0" and B[bit_pos] == "0": ops += 1 B[bit_pos] = "1" elif A[bit_pos] == "1": extras.append(bit_pos) if ops > K: ops = -1 break if ops == -1: print(-1) else: for bit in extras: if ops < K: if B[bit] == "1": A[bit] = "0" ops += 1 elif ops < K - 1: A[bit] = "0" B[bit] = "1" ops += 2 else: break print(hex(int("".join(A), 2))[2:].upper()) print(hex(int("".join(B), 2))[2:].upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def first_pass(A, B, C, k): Ad = "" Bd = "" cnt = 0 for i, (a, b, c) in enumerate(zip(A, B, C)): if int(a) | int(b) != int(c): if c == "0": Ad += "0" Bd += "0" if a == b == "1": cnt += 2 else: cnt += 1 else: Ad += "0" Bd += "1" cnt += 1 if cnt > k: return -1, -1, -1 else: Ad += a Bd += b return Ad, Bd, cnt def second_pass(A, B, C, cnt): Ad, Bd = list(A), list(B) for i, (a, b, c) in enumerate(zip(A, B, C)): if c == "1": if a == b == "1": Ad[i] = "0" cnt += -1 elif a == "1" and b == "0" and cnt > 1: Ad[i] = "0" Bd[i] = "1" cnt += -2 if cnt == 0: break elif cnt < 0: print("Ahhh Poop") return "".join(Ad), "".join(Bd) for _ in range(int(input())): k = int(input()) A = bin(int(input(), 16))[2:] B = bin(int(input(), 16))[2:] C = bin(int(input(), 16))[2:] maxl = max(len(A), len(B), len(C)) A = A.rjust(maxl, "0") B = B.rjust(maxl, "0") C = C.rjust(maxl, "0") cnt = 0 Ad, Bd, cnt = first_pass(A, B, C, k) if Ad == Bd == -1: print(-1) else: cnt = k - cnt if cnt: Ad, Bd = second_pass(Ad, Bd, C, cnt) print(hex(int(Ad, 2))[2:].upper()) print(hex(int(Bd, 2))[2:].upper())
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING VAR STRING IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR STRING VAR STRING VAR NUMBER IF VAR VAR RETURN NUMBER NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR STRING VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN FUNC_CALL STRING VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def char_to_binary(ch): val = ord(ch) if val <= ord("9"): val -= ord("0") else: val = val - ord("A") + 10 ret = [0] * 4 for i in range(3, -1, -1): ret[i] = val & 1 val >>= 1 return ret def binary_to_char(ar): first_not_found = True ll = len(ar) ll //= 4 u = 0 for i in range(ll): val = 0 for j in range(4): val = val * 2 + ar[u] u += 1 if val != 0 or first_not_found == False: first_not_found = False print(chr(val + ord("0")) if val < 10 else chr(val + ord("A") - 10), end="") print(0 if first_not_found else "") def str_to_binary(a, l): ret = [(0) for i in range(4 * l)] for ch in a: l -= 1 tmp = char_to_binary(a[l]) for i in range(4): ret[4 * l + i] = tmp[i] return ret q = int(input()) while q > 0: q -= 1 k = int(input()) a = input() b = input() c = input() l = max([len(a), len(b), len(c)]) x = str_to_binary(a, l) y = str_to_binary(b, l) z = str_to_binary(c, l) ans_x = [(0) for i in range(len(x))] ans_y = [(0) for i in range(len(x))] for i in range(4 * l): if z[i] == 0: ans_x[i] = 0 ans_y[i] = 0 if x[i] == 1: k -= 1 if y[i] == 1: k -= 1 else: if x[i] == 0 and y[i] == 0: ans_y[i] = 1 k -= 1 else: ans_y[i] = y[i] ans_x[i] = x[i] if k < 0: break i = 0 while i < 4 * l and k > 0: if ans_x[i] == 1: if ans_y[i] == 1: ans_x[i] = 0 k -= 1 elif k >= 2: ans_x[i] = 0 ans_y[i] = 1 k -= 2 i += 1 if k < 0: print(-1) else: binary_to_char(ans_x) binary_to_char(ans_y)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER STRING FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
from sys import stderr Q = int(input()) for _ in range(Q): K = int(input()) A = int(input(), 16) B = int(input(), 16) C = int(input(), 16) badbits = (A | B) ^ C badonesC = C & badbits badonesA = A & (badbits ^ badonesC) badonesB = B & (badbits ^ badonesC) if badonesC: n = bin(badonesC).count("1") K -= n B |= badonesC if badonesB: n = bin(badonesB).count("1") K -= n B ^= badonesB if badonesA: n = bin(badonesA).count("1") K -= n A ^= badonesA while K > 0: superfluous = (A & B).bit_length() swappable = (A & (A ^ B)).bit_length() if swappable > superfluous and K >= 2: bit = 1 << swappable >> 1 A ^= bit B ^= bit K -= 2 elif superfluous > 0: bit = 1 << superfluous >> 1 A ^= bit K -= 1 else: break print(-1 if K < 0 else "%X\n%X" % (A, B))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL BIN_OP VAR VAR ASSIGN VAR FUNC_CALL BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP STRING VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def read_int(): return int(input().strip()) def read_string(): return str(input().strip()) HEX_BIN = { "0": (0, 0, 0, 0), "1": (0, 0, 0, 1), "2": (0, 0, 1, 0), "3": (0, 0, 1, 1), "4": (0, 1, 0, 0), "5": (0, 1, 0, 1), "6": (0, 1, 1, 0), "7": (0, 1, 1, 1), "8": (1, 0, 0, 0), "9": (1, 0, 0, 1), "A": (1, 0, 1, 0), "B": (1, 0, 1, 1), "C": (1, 1, 0, 0), "D": (1, 1, 0, 1), "E": (1, 1, 1, 0), "F": (1, 1, 1, 1), } BIN_HEX = {v: k for k, v in HEX_BIN.items()} def get_binary_list(x): return list(HEX_BIN[x]) def compute_single_change(a, b, c): ab = get_binary_list(a) bb = get_binary_list(b) cb = get_binary_list(c) k = 0 for i in range(4): if cb[i] == 0: if ab[i] == 1: ab[i] = 0 k += 1 if bb[i] == 1: bb[i] = 0 k += 1 if cb[i] == 1: if ab[i] == bb[i] == 0: bb[i] = 1 k += 1 ap = BIN_HEX[tuple(ab)] bp = BIN_HEX[tuple(bb)] return k, ap, bp def compute_minimization(K, m, y, c): mb = get_binary_list(m) bb = get_binary_list(y) cb = get_binary_list(c) for i in range(4): if K == 0: break if cb[i] == 1: if mb[i] == bb[i] == 1: mb[i] = 0 K -= 1 if mb[i] == 1 and K >= 2: mb[i], bb[i] = 0, 1 K -= 2 ap = BIN_HEX[tuple(mb)] bp = BIN_HEX[tuple(bb)] return K, ap, bp def minimize_values(K, A, B, C): minimized = "" other = "" for m, y, c in zip(A, B, C): if K > 0: K, mp, bp = compute_minimization(K, m, y, c) minimized += mp other += bp else: minimized += m other += y return minimized, other def compute_solution(K, A, B, C): Ap = Bp = "" for a, b, c in zip(A, B, C): k, ap, bp = compute_single_change(a, b, c) K -= k if K < 0: return [-1] Ap += ap Bp += bp if K > 0: Ap, Bp = minimize_values(K, Ap, Bp, C) return Ap, Bp def normalize_lengths(S1, S2, S3): def pad(m, s): return "0" * (m - len(s)) + s ML = max(len(S1), len(S2), len(S3)) return pad(ML, S1), pad(ML, S2), pad(ML, S3) def remove_trailing_zeroes(n): m = n.lstrip("0") if m == "": return "0" else: return m Q = read_int() for q in range(Q): K = read_int() A = read_string() B = read_string() C = read_string() A, B, C = normalize_lengths(A, B, C) sol = compute_solution(K, A, B, C) if len(sol) == 1: print(-1) else: Ap, Bp = sol Ap = remove_trailing_zeroes(Ap) Bp = remove_trailing_zeroes(Bp) print(Ap) print(Bp)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN LIST NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF FUNC_DEF RETURN BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF VAR STRING RETURN STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def transform(a, b, c): changes = 0 if c == "0": if a == "1": changes += 1 if b == "1": changes += 1 return "0", "0", changes if a == "1" or b == "1": return a, b, 0 return "0", "1", 1 for _ in range(int(input())): k = int(input()) A, B, C = [bin(int(input(), 16))[2:] for _ in range(3)] n = max(len(A), len(B), len(C)) A, B, C = A.zfill(n), B.zfill(n), C.zfill(n) P, Q = [], [] for i in range(n): p, q, x = transform(A[i], B[i], C[i]) P.append(p) Q.append(q) k -= x if k < 0: break if k < 0: print(-1) continue i = 0 while k > 0 and i < n: if P[i] == "1" and Q[i] == "1": P[i] = "0" k -= 1 elif P[i] == "1" and Q[i] == "0" and k > 1: P[i], Q[i] = "0", "1" k -= 2 i += 1 print(hex(int("".join(P), 2))[2:].upper()) print(hex(int("".join(Q), 2))[2:].upper())
FUNC_DEF ASSIGN VAR NUMBER IF VAR STRING IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER RETURN STRING STRING VAR IF VAR STRING VAR STRING RETURN VAR VAR NUMBER RETURN STRING STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR STRING STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for x in range(int(input())): k = int(input()) a = bin(int(input(), 16))[2:] b = bin(int(input(), 16))[2:] c = bin(int(input(), 16))[2:] mv = max(len(a), len(b), len(c)) while len(a) < mv: a = "0" + a while len(b) < mv: b = "0" + b while len(c) < mv: c = "0" + c a = list(a) b = list(b) c = list(c) for x in range(mv): if c[x] == "0": if a[x] != "0": a[x] = "0" k -= 1 if b[x] != "0": b[x] = "0" k -= 1 if c[x] == "1" and a[x] == "0" and b[x] == "0": b[x] = "1" k -= 1 if k < 0: print(-1) else: for x in range(mv): if a[x] == "1" and b[x] == "1" and k > 0: a[x] = "0" k -= 1 if a[x] == "1" and b[x] == "0" and k > 1: a[x] = "0" b[x] = "1" k -= 2 print(hex(int("".join(a), 2)).upper()[2:]) print(hex(int("".join(b), 2)).upper()[2:])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input())): K = int(input()) A = bin(int(input(), 16))[2:] B = bin(int(input(), 16))[2:] C = bin(int(input(), 16))[2:] A1 = "" B1 = "" k = 0 l = max(len(A), len(B), len(C)) A = "0" * (l - len(A)) + A B = "0" * (l - len(B)) + B C = "0" * (l - len(C)) + C flag = [0] * l for i in range(l): if C[i] == "0": if A[i] == "1": A1 += "0" k += 1 else: A1 += "0" if B[i] == "1": B1 += "0" k += 1 else: B1 += "0" elif A[i] == "0": A1 += "0" if B[i] == "1": B1 += "1" else: B1 += "1" k += 1 else: A1 += "1" B1 += B[i] if B[i] == "1": flag[i] = 1 else: flag[i] = 2 if k > K: print(-1) elif k == K: print(hex(int(A1, 2))[2:].upper()) print(hex(int(B1, 2))[2:].upper()) else: A2 = "" B2 = "" d = K - k for i in range(l): if flag[i]: if int(d / 2) > 0: A2 += "0" d -= 1 if B[i] == "0": B2 += "1" d -= 1 else: B2 += "1" elif B1[i] == "1": A2 += "0" d -= 1 B2 += "1" else: A2 += A1[i] B2 += B1[i] if d <= 0: A2 += A1[i + 1 :] B2 += B1[i + 1 :] break else: A2 += A1[i] B2 += B1[i] print(hex(int(A2, 2))[2:].upper()) print(hex(int(B2, 2))[2:].upper())
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR STRING VAR NUMBER VAR STRING IF VAR VAR STRING VAR STRING VAR NUMBER VAR STRING IF VAR VAR STRING VAR STRING IF VAR VAR STRING VAR STRING VAR STRING VAR NUMBER VAR STRING VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER IF VAR VAR STRING VAR STRING VAR NUMBER VAR STRING IF VAR VAR STRING VAR STRING VAR NUMBER VAR STRING VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for t in range(int(input())): k = int(input()) a = bin(int(input(), 16))[2:] b = bin(int(input(), 16))[2:] c = bin(int(input(), 16))[2:] if len(a) > len(b): b = "0" * (len(a) - len(b)) + b else: a = "0" * (len(b) - len(a)) + a if len(a) > len(c): c = "0" * (len(a) - len(c)) + c else: a = "0" * (len(c) - len(a)) + a b = "0" * (len(c) - len(b)) + b a = list(a) b = list(b) c = list(c) cantBeDone = False i = 0 while k > 0 and i < len(c): if c[i] == "0": if a[i] == "1" and b[i] == "1": if k - 2 >= 0: a[i] = b[i] = "0" k -= 2 else: cantBeDone = True break elif not (a[i] == b[i] and a[i] == "0"): a[i] = b[i] = "0" k -= 1 elif a[i] == "0" and b[i] == "0": b[i] = "1" k -= 1 i += 1 i = 0 while k > 0 and i < len(a): if c[i] == "1": if a[i] == "1" and b[i] == "1": a[i] = "0" k -= 1 elif a[i] == "1" and b[i] == "0": if k - 2 >= 0: a[i] = "0" b[i] = "1" k -= 2 i += 1 if int("".join(a), 2) | int("".join(b), 2) != int("".join(c), 2): print("-1") else: print(hex(int("".join(a), 2))[2:].upper()) print(hex(int("".join(b), 2))[2:].upper())
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for T in range(int(input())): k = int(input()) a = input() b = input() c = input() A = [x for x in bin(int(a, 16))[2:]] B = [x for x in bin(int(b, 16))[2:]] C = [x for x in bin(int(c, 16))[2:]] length = [len(A), len(B), len(C)] m = max(length) A = ["0" for i in range(m - length[0])] + A B = ["0" for i in range(m - length[1])] + B C = ["0" for i in range(m - length[2])] + C ch = 0 bah = 0 d = {} n = 0 for i in range(m): if C[i] == "0": if A[i] != "0": if ch + 1 <= k: ch += 1 A[i] = "0" else: bah = 1 break if B[i] != "0": if ch + 1 <= k: ch += 1 B[i] = "0" else: bah = 1 break if C[i] == "1": if A[i] == "0" and B[i] == "0": B[i] = "1" ch += 1 if C[i] == "1" and A[i] == "1" and B[i] == "1": n += 1 d[i] = 1 elif C[i] == "1" and A[i] == "1" and B[i] == "0": n += 1 d[i] = 2 if bah == 1: print(-1) continue i = 0 l = sorted(d) while k - ch > 0 and i < n: if k - ch >= 2 and d[l[i]] == 2: A[l[i]] = "0" B[l[i]] = "1" ch += 2 elif k - ch >= 1 and d[l[i]] == 1: A[l[i]] = "0" ch += 1 i += 1 a = "" for i in A: a += i print(hex(int(a, 2))[2:].upper()) b = "" for i in B: b += i print(hex(int(b, 2))[2:].upper())
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP STRING VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP STRING VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input()) for _ in range(Q): K = int(input()) A = int(input(), 16) B = int(input(), 16) C = int(input(), 16) if K == 0: if A | B == C: print("{:X}".format(A)) print("{:X}".format(B)) else: print(-1) continue M = max(A, B, C) L = len("{0:b}".format(M)) a = "{:0{}b}".format(A, L) b = "{:0{}b}".format(B, L) c = "{:0{}b}".format(C, L) a = list(a) b = list(b) b2 = list(b) for i, v in enumerate(c): if v == "0": if a[i] == "1": a[i] = "0" K -= 1 if b[i] == "1": b[i] = "0" K -= 1 elif a[i] == "0" and b[i] == "0": b[i] = "1" K -= 1 if K < 0: print(-1) continue for i, v in enumerate(c): if v == "1": if a[i] == "1": if b[i] == "1": a[i] = "0" K -= 1 elif K > 1: a[i] = "0" b[i] = "1" K -= 2 assert b2[i] == "0" if K == 0: break print("{:X}".format(int("".join(a), 2))) print("{:X}".format(int("".join(b), 2)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
bin_hex = { "0000": "0", "0001": "1", "0010": "2", "0011": "3", "0100": "4", "0101": "5", "0110": "6", "0111": "7", "1000": "8", "1001": "9", "1010": "A", "1011": "B", "1100": "C", "1101": "D", "1110": "E", "1111": "F", } hex_bin = { "0": "0000", "1": "0001", "2": "0010", "3": "0011", "4": "0100", "5": "0101", "6": "0110", "7": "0111", "8": "1000", "9": "1001", "A": "1010", "B": "1011", "C": "1100", "D": "1101", "E": "1110", "F": "1111", } q = int(input()) for qi in range(q): K = int(input()) A = input() B = input() C = input() na = len(A) nb = len(B) nc = len(C) n = max(na, nb, nc) A = list("0" * (n - na) + A) B = list("0" * (n - nb) + B) C = list("0" * (n - nc) + C) k = 0 for i in range(n): a = list(hex_bin[A[i]]) b = list(hex_bin[B[i]]) c = list(hex_bin[C[i]]) for j in range(4): if c[j] == "1" and b[j] == "0" and a[j] == "0": b[j] = "1" k += 1 elif c[j] == "0" and b[j] == "1" and a[j] == "0": b[j] = "0" k += 1 elif c[j] == "0" and b[j] == "0" and a[j] == "1": a[j] = "0" k += 1 elif c[j] == "0" and b[j] == "1" and a[j] == "1": a[j] = "0" b[j] = "0" k += 2 A[i] = bin_hex["".join(a)] B[i] = bin_hex["".join(b)] if k > K: print(-1) continue for i in range(n): if k == K: break a = list(hex_bin[A[i]]) b = list(hex_bin[B[i]]) c = list(hex_bin[C[i]]) for j in range(4): if c[j] == "1" and b[j] == "1" and a[j] == "1" and K > k: a[j] = "0" k += 1 elif c[j] == "1" and b[j] == "0" and a[j] == "1" and K > k + 1: b[j] = "1" a[j] = "0" k += 2 A[i] = bin_hex["".join(a)] B[i] = bin_hex["".join(b)] all_0 = True for i in range(n): if A[i] != "0": A = A[i:] all_0 = False break if all_0: A = ["0"] print("".join(A)) all_0 = True for i in range(n): if B[i] != "0": B = B[i:] all_0 = False break if all_0: B = ["0"] print("".join(B))
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL STRING VAR ASSIGN VAR VAR VAR FUNC_CALL STRING VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL STRING VAR ASSIGN VAR VAR VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR LIST STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR LIST STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input()) for _ in range(q): k = int(input()) a = list(bin(int(input(), 16))[2:]) b = list(bin(int(input(), 16))[2:]) c = list(bin(int(input(), 16))[2:]) max_length = max(len(a), len(b), len(c)) a = ["0"] * (max_length - len(a)) + a b = ["0"] * (max_length - len(b)) + b c = ["0"] * (max_length - len(c)) + c count = 0 for i, (a0, b0, c0) in enumerate(zip(a, b, c)): if c0 == "0": if a0 == "1": a[i] = "0" count += 1 if b0 == "1": b[i] = "0" count += 1 elif a0 == "0" and b0 == "0": b[i] = "1" count += 1 if count > k: print(-1) continue for i, (a0, b0, c0) in enumerate(zip(a, b, c)): if count == k: break if a0 == "0": continue if b0 == "1" and count < k: a[i] = "0" count += 1 elif count + 1 < k: a[i] = "0" b[i] = "1" count += 2 for i, (a0, b0, c0) in enumerate(zip(a, b, c)): if count == k: break if a0 == "1" and b0 == "1": b[i] = "0" count += 1 print(hex(int("".join(a), 2))[2:].upper()) print(hex(int("".join(b), 2))[2:].upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR STRING IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR STRING IF VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR STRING VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
n = int(input().strip()) def numbits(i): return bin(i).count("1") for _ in range(n): k = int(input().strip()) a = int(input().strip(), 16) b = int(input().strip(), 16) c = int(input().strip(), 16) wrongbits = c ^ (a | b) turnoff_a = wrongbits & a turnoff_b = wrongbits & b turnon = wrongbits & c k -= numbits(turnoff_a) + numbits(turnoff_b) + numbits(turnon) if k >= 0: a -= turnoff_a b -= turnoff_b b += turnon tmp = 0 while k > 0 and a > 0: highest_a = 1 << a.bit_length() - 1 if b & highest_a: a -= highest_a k -= 1 elif k >= 2: a -= highest_a b += highest_a k -= 2 else: a -= highest_a tmp += highest_a a += tmp print("%X" % a) print("%X" % b) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def eat(): ret = bin(int("A" + input(), 16))[2:] ret = [int(_) for _ in ret] return ret[4:] def prt(a): b = "".join([str(_) for _ in a]) print("%X" % int(b, 2)) def app(a, n): if len(a) == n: return a.reverse() while len(a) < n: a.append(0) a.reverse() q = int(input()) for cc in range(q): k = int(input()) a = eat() b = eat() c = eat() n = max(len(a), len(b), len(c)) app(a, n) app(b, n) app(c, n) for i in range(len(a)): if a[i] | b[i] == c[i]: continue if c[i]: b[i] = 1 k -= 1 else: if a[i]: a[i] = 0 k -= 1 if b[i]: b[i] = 0 k -= 1 if k < 0: print(-1) continue for i in range(len(a)): if k == 0: break if c[i]: if a[i] and b[i]: a[i] = 0 k -= 1 if a[i] and not b[i] and k >= 2: a[i] = 0 b[i] = 1 k -= 2 prt(a) prt(b)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
c = { "0": "0000", "1": "0001", "2": "0010", "3": "0011", "4": "0100", "5": "0101", "6": "0110", "7": "0111", "8": "1000", "9": "1001", "A": "1010", "B": "1011", "C": "1100", "D": "1101", "E": "1110", "F": "1111", } d = {} for i in c: d[c[i]] = i for i in range(int(input())): k = int(input()) s1 = "" for j in input(): s1 += c[j] s2 = "" for j in input(): s2 += c[j] s3 = "" for j in input(): s3 += c[j] m = max(len(s1), len(s2), len(s3)) s1 = list("0" * (m - len(s1)) + s1) s2 = list("0" * (m - len(s2)) + s2) s3 = list("0" * (m - len(s3)) + s3) for i in range(m): if s3[i] == "0": if s1[i] == "1": k -= 1 s1[i] = "0" if s2[i] == "1": k -= 1 s2[i] = "0" elif s1[i] == "0" and s2[i] == "0": k -= 1 s2[i] = "1" for i in range(m): if k == 0: break if s3[i] == "1" and s1[i] == "1" and s2[i] == "1": s1[i] = "0" k -= 1 if s3[i] == "1" and s1[i] == "1" and s2[i] == "0" and k >= 2: k -= 2 s1[i] = "0" s2[i] = "1" if k < 0: print(-1) else: s = "" k = -1 for i in range(len(s1) // 4): s += d["".join(s1[4 * i : 4 * i + 4])] for i in range(len(s)): if s[i] != "0": k = 0 print(s[i:]) break if k == -1: print(0) k = -1 s = "" for i in range(len(s2) // 4): s += d["".join(s2[4 * i : 4 * i + 4])] for i in range(len(s)): if s[i] != "0": k = 0 print(s[i:]) break if k == -1: print(0)
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL STRING VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL STRING VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
import sys Q = int(sys.stdin.readline()) while Q: K = int(sys.stdin.readline()) A = int(sys.stdin.readline(), 16) B = int(sys.stdin.readline(), 16) C = int(sys.stdin.readline(), 16) A2 = A & C B2 = B & C B3 = B2 | C & ~A2 K -= bin(A ^ A2).count("1") + bin(B ^ B3).count("1") A3 = bin(A2)[2:][::-1] B4 = bin(B3)[2:][::-1] As, Bs = 0, 0 j = max(len(A3), len(B4)) while j >= 0: if j < len(A3) and A3[j] == "1": if j < len(B4) and B4[j] == "1": if K > 0: Bs += 1 << j K -= 1 else: As += 1 << j Bs += 1 << j elif K > 1: Bs += 1 << j K -= 2 else: As += 1 << j elif j < len(B4) and B4[j] == "1": Bs += 1 << j j -= 1 if K >= 0: print(hex(As)[2:].upper()) print(hex(Bs)[2:].upper()) else: print(-1) Q -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR VAR STRING VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input())): k = int(input()) a = bin(int(input(), 16))[2:] b = bin(int(input(), 16))[2:] c = bin(int(input(), 16))[2:] la = len(a) lb = len(b) lc = len(c) lm = max(la, lb, lc) a = "0" * (lm - la) + a b = "0" * (lm - lb) + b c = "0" * (lm - lc) + c a = [(ord(a[i]) - ord("0")) for i in range(lm)] b = [(ord(b[i]) - ord("0")) for i in range(lm)] c = [(ord(c[i]) - ord("0")) for i in range(lm)] o = 0 for i in range(0, lm): if o > k: break if a[i] | b[i] != c[i]: if c[i] == 0: if a[i] == 1: a[i] = 0 o += 1 if b[i] == 1: b[i] = 0 o += 1 else: b[i] = 1 o += 1 if o > k: print(-1) else: for i in range(lm): if o >= k: break if a[i] == 1 and b[i] == 1: a[i] = 0 o += 1 elif a[i] == 1 and b[i] == 0 and k - o >= 2: a[i] = 0 b[i] = 1 o += 2 p = 0 while p + 1 < lm and a[p] == 0: p += 1 q = 0 while q + 1 < lm and b[q] == 0: q += 1 for i in range(lm): a[i] = chr(a[i] + 48) b[i] = chr(b[i] + 48) a = hex(int("".join(a[p:]), 2))[2:] b = hex(int("".join(b[q:]), 2))[2:] a = a.upper() b = b.upper() print(a) print(b)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def one_count(s): return bin(s).count("1") nq = int(input().strip()) for i in range(nq): k = int(input().strip()) a, b, c = [int(input().strip(), 16) for i in range(3)] t1_mask = (a ^ b) & ~c t2_mask = a & b & ~c t3_mask = ~(a | b) & c c = [one_count(x) for x in [t1_mask, t2_mask, t3_mask]] c[1] *= 2 nb = sum(c) if nb > k: print(-1) continue t1_mask |= t2_mask a &= ~t1_mask b &= ~t1_mask b |= t3_mask rb = k - nb al = list(bin(a))[2:] bl = list(bin(b))[2:] diff = len(al) - len(bl) if diff > 0: bl = ["0"] * diff + bl else: al = ["0"] * -diff + al for i in range(len(al)): if rb >= 1 and al[i] == "1" and bl[i] == "1": al[i] = "0" rb -= 1 elif rb >= 2 and al[i] == "1" and bl[i] == "0": al[i] = "0" bl[i] = "1" rb -= 2 print(hex(int("".join(al), 2))[2:].upper()) print(hex(int("".join(bl), 2))[2:].upper())
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input()) for i in range(0, Q): K = int(input()) A = int(input(), 16) B = int(input(), 16) C = int(input(), 16) length = max(len(bin(A)), len(bin(B)), len(bin(C))) - 2 diff = C ^ (A | B) basic = bin(diff).count("1") + bin(A & B & ~C).count("1") if basic > K: print(-1) else: nB = B - (B & diff) + (~A & ~B & C) nA = A - (A & diff) e = K - basic inds = [i for i, x in enumerate(bin(nA & C)[2:].zfill(length)) if x == "1"] bA = bin(nA)[2:].zfill(length) bB = bin(nB)[2:].zfill(length) for i in inds: if bB[i] == "0": if e >= 2: bA = bA[:i] + "0" + bA[i + 1 :] bB = bB[:i] + "1" + bB[i + 1 :] e = e - 2 elif e >= 1: bA = bA[:i] + "0" + bA[i + 1 :] e = e - 1 nA = int(bA, 2) nB = int(bB, 2) print(hex(nA)[2:].upper()) print(hex(nB)[2:].upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL FUNC_CALL VAR VAR STRING FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input()) for t in range(q): k = int(input()) a = bin(int(input(), 16))[2:] b = bin(int(input(), 16))[2:] c = bin(int(input(), 16))[2:] l = max(len(a), len(b), len(c)) a = (l - len(a)) * "0" + a b = (l - len(b)) * "0" + b c = (l - len(c)) * "0" + c a_new = "" b_new = "" for i in range(l): if c[i] == "0": if a[i] == "1": if k >= 1: k -= 1 else: print(-1) break a_new += "0" if b[i] == "1": if k >= 1: k -= 1 else: print(-1) break b_new += "0" elif a[i] == "0" and b[i] == "0": if k >= 1: k -= 1 else: print(-1) break a_new += "0" b_new += "1" else: a_new += a[i] b_new += b[i] else: for i in range(l): if k == 0: break if (a[i], b[i], c[i]) == ("1", "0", "1"): if k >= 2: k -= 2 if i != l - 1: a_new = a_new[:i] + "0" + a_new[i + 1 :] b_new = b_new[:i] + "1" + b_new[i + 1 :] else: a_new = a_new[:i] + "0" b_new = b_new[:i] + "1" elif (a[i], b[i], c[i]) == ("1", "1", "1"): if k >= 1: k -= 1 if i != l - 1: a_new = a_new[:i] + "0" + a_new[i + 1 :] else: a_new = a_new[:i] + "0" print(hex(int(a_new, 2))[2:].upper()) print(hex(int(b_new, 2))[2:].upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR STRING IF VAR VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR STRING IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR STRING VAR STRING VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR STRING STRING STRING IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING IF VAR VAR VAR VAR VAR VAR STRING STRING STRING IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input().strip())): K = int(input().strip()) A = int(input().strip(), 16) B = int(input().strip(), 16) C = int(input().strip(), 16) biggest = max(max(A, B), C) fill_len = len(bin(biggest)) - 2 __A = list(bin(A)[2:].zfill(fill_len)) __B = list(bin(B)[2:].zfill(fill_len)) __C = list(bin(C)[2:].zfill(fill_len)) num_changes = 0 extra_A = [] for i in range(fill_len): if __C[i] == "0": if __A[i] == "1": __A[i] = "0" num_changes += 1 if __B[i] == "1": __B[i] = "0" num_changes += 1 elif __B[i] == "0" and __A[i] == "0": __B[i] = "1" num_changes += 1 elif __B[i] == "1" and __A[i] == "1": extra_A.append((i, False)) elif __B[i] == "0" and __A[i] == "1": extra_A.append((i, True)) if num_changes > K: print(-1) else: if num_changes != K: for j in range(len(extra_A)): if extra_A[j][1]: if num_changes + 2 <= K: __A[extra_A[j][0]] = "0" __B[extra_A[j][0]] = "1" num_changes += 2 else: __A[extra_A[j][0]] = "0" num_changes += 1 if num_changes == K: break print(hex(int("".join(__A), 2))[2:].upper()) print(hex(int("".join(__B), 2))[2:].upper())
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER STRING ASSIGN VAR VAR VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR VAR NUMBER STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input()) for _ in range(q): k = int(input()) a = input() b = input() c = input() m = max(len(a), len(b), len(c)) a = list(a.zfill(m)) b = list(b.zfill(m)) oa = a.copy() ob = b.copy() c = list(c.zfill(m)) runningTotalChanges = 0 changesA = [0] * m changesB = [0] * m for i in range(m): partA = int(a[i], 16) partB = int(b[i], 16) partC = int(c[i], 16) newPartA = (partA ^ partC) & partA changesA[i] = bin(newPartA).count("1") newPartA ^= partA a[i] = hex(newPartA)[2:] newPartB = (newPartA | partB) ^ partC ^ partB changesB[i] = bin(partB ^ newPartB).count("1") b[i] = hex(newPartB)[2:] runningTotalChanges = sum(changesB) + sum(changesA) i = 0 while runningTotalChanges < k and i < m: ca = bin(int(oa[i], 16)).count("1") cb = bin(int(ob[i], 16) ^ int(c[i], 16)).count("1") if runningTotalChanges + ca + cb - changesA[i] - changesB[i] <= k: a[i] = "0" b[i] = c[i] runningTotalChanges += ca + cb - changesA[i] - changesB[i] else: _a = int(a[i], 16) _b = int(b[i], 16) _c = int(c[i], 16) for j in range(4, -1, -1): if runningTotalChanges == k: break if _a >> j & 1 == 1: if _b >> j & 1 == 1: if _c >> j & 1 == 1: _a = _a ^ 1 << j runningTotalChanges += 1 elif runningTotalChanges + 2 <= k: _a = _a ^ 1 << j _b = _b ^ 1 << j runningTotalChanges += 2 elif _c >> j & 1 == 1: if runningTotalChanges + 2 <= k: _a = _a ^ 1 << j _b = _b ^ 1 << j runningTotalChanges += 2 else: _a = _a ^ 1 << j runningTotalChanges += 1 elif _b >> j & 1 == 1: if _c >> j & 1 == 0: _b = _b ^ 1 << j runningTotalChanges += 1 elif _c >> j & 1 == 1: _b = _b ^ 1 << j runningTotalChanges += 1 a[i] = hex(_a)[2:] b[i] = hex(_b)[2:] i += 1 if runningTotalChanges > k: print("-1") else: print("".join(a).lstrip("0").upper() or "0") print("".join(b).lstrip("0").upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER STRING IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL STRING VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL STRING VAR STRING
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input())): k = int(input()) a = bin(int(input(), 16))[2:] b = bin(int(input(), 16))[2:] c = bin(int(input(), 16))[2:] nbits = max(len(a), len(b), len(c)) a = "0" * (nbits - len(a)) + a b = "0" * (nbits - len(b)) + b c = "0" * (nbits - len(c)) + c a = [(ord(x) - 48) for x in a] b = [(ord(x) - 48) for x in b] c = [(ord(x) - 48) for x in c] req = 0 for i in range(nbits): if a[i] == 1 and c[i] == 0: req += 1 a[i] = 0 if b[i] == 1 and c[i] == 0: req += 1 b[i] = 0 if a[i] == 0 and b[i] == 0 and c[i] == 1: req += 1 b[i] = 1 if req <= k: rem = k - req i = 0 p = 0 while i < nbits and p < rem: if a[i] == 1 and b[i] == 1: a[i] = 0 p += 1 elif a[i] == 1 and b[i] == 0 and p + 2 <= rem: a[i] = 0 b[i] = 1 p += 2 i += 1 s1 = "".join([str(x) for x in a]) s2 = "".join([str(x) for x in b]) s1 = hex(int(s1, 2))[2:].upper() s2 = hex(int(s2, 2))[2:].upper() print(s1) print(s2) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input())): k = int(input()) a, b, c = (list(input()) for _ in range(3)) max_len = max(len(num) for num in (a, b, c)) a, b, c = (["0"] * (max_len - len(num)) + num for num in (a, b, c)) for i in range(max_len): a_i, b_i, c_i = (int(num, 16) for num in (a[i], b[i], c[i])) d_i = "{:04b}".format((a_i | b_i) ^ c_i) a_i, b_i, c_i = (list("{:04b}".format(num)) for num in (a_i, b_i, c_i)) for j in range(4): if d_i[j] == "1": if c_i[j] == "1": b_i[j] = "1" k -= 1 else: if a_i[j] == "1": a_i[j] = "0" k -= 1 if b_i[j] == "1": b_i[j] = "0" k -= 1 if k < 0: break if k < 0: break else: a[i], b[i] = ("{:X}".format(int("".join(num), 2)) for num in (a_i, b_i)) if k < 0: print(-1) else: for i in range(max_len): a_i, b_i = (list("{:04b}".format(int(num, 16))) for num in (a[i], b[i])) for j in range(4): if a_i[j] == b_i[j] == "1": if k >= 1: a_i[j] = "0" k -= 1 else: break elif a_i[j] == "1" and b_i[j] == "0": if k >= 2: a_i[j] = "0" b_i[j] = "1" k -= 2 else: continue a[i], b[i] = ("{:X}".format(int("".join(num), 2)) for num in (a_i, b_i)) if k == 0: break string = "".join(a).lstrip("0") print(string if string else "0") string = "".join(b).lstrip("0") print(string if string else "0")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
total = 0 Q = int(input()) for iets in range(Q): K = int(input()) A = list(format(int(input(), 16), "0>200006b")) B = list(format(int(input(), 16), "0>200006b")) C = format(int(input(), 16), "0>200006b") for index in range(200006): if C[index] == "0": if A[index] == "1": A[index] = "0" K -= 1 if B[index] == "1": B[index] = "0" K -= 1 elif A[index] == "0" and B[index] == "0": B[index] = "1" K -= 1 if K < 0: print(-1) else: for index in range(200006): if K == 0: break if C[index] == "1": if A[index] == "1" and B[index] == "1": A[index] = "0" K -= 1 elif A[index] == "1" and B[index] == "0" and K != 1: A[index] = "0" B[index] = "1" K -= 2 print(str(hex(int("".join(A), 2))[2:].upper())) print(str(hex(int("".join(B), 2))[2:].upper()))
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def fun(K, ABC): abc = [] l = max([len(i) for i in ABC]) for i in ABC: abc.append(list(bin(int(i, 16))[2:].zfill(4 * l))) for i in range(4 * l): if abc[0][i] == "0" and abc[1][i] == "0" and abc[2][i] == "1": abc[1][i] = "1" K -= 1 if abc[0][i] == "0" and abc[1][i] == "1" and abc[2][i] == "0": abc[1][i] = "0" K -= 1 if abc[0][i] == "1" and abc[1][i] == "0" and abc[2][i] == "0": abc[0][i] = "0" K -= 1 if abc[0][i] == "1" and abc[1][i] == "1" and abc[2][i] == "0": abc[0][i] = "0" abc[1][i] = "0" K -= 2 if K < 0: return -1 if K >= 1: for i in range(4 * l): if abc[0][i] == "1" and abc[1][i] == "1" and abc[2][i] == "1" and K >= 1: abc[0][i] = "0" K -= 1 if abc[0][i] == "1" and abc[1][i] == "0" and abc[2][i] == "1" and K >= 2: abc[0][i] = "0" abc[1][i] = "1" K -= 2 if K < 1: break return [ hex(int("".join(abc[0]), 2))[2:].upper(), hex(int("".join(abc[1]), 2))[2:].upper(), ] for _ in range(int(input())): K = int(input()) A = input() B = input() C = input() res = fun(K, [A, B, C]) if res == -1: print(-1) else: for i in res: print(i)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR STRING ASSIGN VAR NUMBER VAR STRING VAR NUMBER IF VAR NUMBER RETURN LIST FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def change_the_bits_and_print(A, B, C, k): L = max(A, B, C).bit_length() one = "1" zero = "0" bA = "0" * (L - A.bit_length()) + bin(A)[2:] bB = "0" * (L - B.bit_length()) + bin(B)[2:] bC = "0" * (L - C.bit_length()) + bin(C)[2:] A_p = [] B_p = [] bits_changed_successfully = True for i in range(len(bC)): if bC[i] == one: if bA[i] == one or bB[i] == one: A_p.append(bA[i]) B_p.append(bB[i]) else: B_p.append(one) A_p.append(zero) k -= 1 else: A_p.append(zero) k -= bA[i] != zero B_p.append(zero) k -= bB[i] != zero if k < 0: bits_changed_successfully = False break if bits_changed_successfully: if k > 0: for i in range(len(bC)): if bC[i] == one: if A_p[i] == B_p[i]: A_p[i] = zero k -= 1 elif A_p[i] > B_p[i] and k > 1: A_p[i], B_p[i] = B_p[i], A_p[i] k -= 2 if k == 0: break print(hex(int("".join(A_p), 2))[2:].upper()) print(hex(int("".join(B_p), 2))[2:].upper()) else: print(-1) queries = int(input().strip()) for _i in range(queries): k = int(input().strip()) A = int(input().strip(), 16) B = int(input().strip(), 16) C = int(input().strip(), 16) change_the_bits_and_print(A, B, C, k)
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input())): k = int(input()) A = "{0:b}".format(int(input(), 16)) B = "{0:b}".format(int(input(), 16)) C = "{0:b}".format(int(input(), 16)) l = max([len(A), len(B), len(C)]) A = "0" * (l - len(A)) + A B = "0" * (l - len(B)) + B C = "0" * (l - len(C)) + C a = [] b = [] c = 0 for i in range(l): if C[i] == "0": a.append("0") b.append("0") c += A[i] == "1" c += B[i] == "1" elif A[i] == "0": a.append("0") b.append("1") c += B[i] == "0" else: a.append("1") b.append(B[i]) if c > k: print(-1) else: c = k - c for i in range(l): if c <= 0: break if a[i] == "1": if b[i] == "1": a[i] = "0" c -= 1 elif c > 1: c -= 2 a[i] = "0" b[i] = "1" a = "".join(a) b = "".join(b) print(hex(int(a, 2))[2:].upper()) print(hex(int(b, 2))[2:].upper())
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR VAR VAR STRING VAR VAR VAR STRING IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
t = int(input()) for i in range(t): count = 0 k = int(input()) a = bin(int(input(), 16)) b = bin(int(input(), 16)) c = input() num = len(c) * 4 c = bin(int(c, 16))[2:].zfill(num) a = list(a[2:].zfill(num)) b = list(b[2:].zfill(num)) for j in range(num): if c[j] == "0" and (a[j] == "1" and b[j] == "1"): count += 2 a[j] = "0" b[j] = "0" elif c[j] == "0" and not (a[j] == "0" and b[j] == "0"): count += 1 a[j] = "0" b[j] = "0" elif c[j] == "1" and a[j] == "0" and b[j] == "0": count += 1 b[j] = "1" if count == k: print(hex(int("".join(a), 2))[2:].upper()) print(hex(int("".join(b), 2))[2:].upper()) elif count > k: print(-1) else: for j in range(num): if a[j] == "1" and b[j] == "1" and count <= k - 1: count += 1 a[j] = "0" elif a[j] == "1" and b[j] == "0" and count <= k - 2: count += 2 a[j] = "0" b[j] = "1" if count == k: break print(hex(int("".join(a), 2))[2:].upper()) print(hex(int("".join(b), 2))[2:].upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
for _ in range(int(input())): k = int(input()) a = bin(int(input(), 16))[2:] b = bin(int(input(), 16))[2:] c = bin(int(input(), 16))[2:] m = max(len(a), len(b), len(c)) a = list(a.zfill(m)) b = list(b.zfill(m)) c = list(c.zfill(m)) for i in range(len(a)): if c[i] == "0": if a[i] == "1": a[i] = "0" k -= 1 if b[i] == "1": b[i] = "0" k -= 1 elif a[i] == "0" == b[i]: b[i] = "1" k -= 1 for i in range(len(a)): if k > 0 and a[i] == "1" == b[i]: a[i] = "0" k -= 1 if k > 1 and a[i] == "1" and b[i] == "0": a[i] = "0" b[i] = "1" k -= 2 if k < 0: print(-1) else: print(hex(int("".join(a), 2))[2:].upper()) print(hex(int("".join(b), 2))[2:].upper())
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
import sys def first_pass(a, b, c, k): i = 0 while i < len(a): if a[i] is "0" and b[i] is "1" and c[i] is "0": k -= 1 if k < 0: return -1 b[i] = "0" elif a[i] is "1" and b[i] is "0" and c[i] is "0": k -= 1 if k < 0: return -1 a[i] = "0" elif a[i] is "1" and b[i] is "1" and c[i] is "0": k -= 2 if k < 0: return -1 a[i] = "0" b[i] = "0" elif a[i] is "0" and b[i] is "0" and c[i] is "1": k -= 1 if k < 0: return -1 b[i] = "1" i += 1 return k def second_pass(a, b, c, k): i = 0 while i < len(a) and k > 0: if a[i] is "1" and b[i] is "0" and c[i] is "1": if k >= 2: k -= 2 a[i] = "0" b[i] = "1" elif a[i] is "1" and b[i] is "1" and c[i] is "1": k -= 1 a[i] = "0" i += 1 return k lines = [] for line in sys.stdin: lines.append(line) numberOfCase = int(lines.pop(0)) while numberOfCase > 0: numberOfCase -= 1 K = int(lines.pop(0)) A = bin(int(lines.pop(0), 16))[2:] B = bin(int(lines.pop(0), 16))[2:] C = bin(int(lines.pop(0), 16))[2:] max_len = max([len(A), len(B), len(C)]) A = A.zfill(max_len) B = B.zfill(max_len) C = C.zfill(max_len) A = list(A) B = list(B) C = list(C) K = first_pass(A, B, C, K) if K is -1: print("-1") continue K = second_pass(A, B, C, K) print(hex(int("".join(A), 2))[2:].upper()) print(hex(int("".join(B), 2))[2:].upper())
IMPORT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR STRING VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input()) while q > 0: k = int(input()) a = input() b = input() c = input() h_size = max(len(a) * 4, max(len(b) * 4, len(c) * 4)) bin_a = bin(int(a, 16))[2:].zfill(h_size) bin_b = bin(int(b, 16))[2:].zfill(h_size) bin_c = bin(int(c, 16))[2:].zfill(h_size) ans_a = list(bin_a) ans_b = list(bin_b) count = 0 for i in range(0, len(bin_c)): if bin_c[i] == "0": if bin_a[i] != "0": ans_a[i] = "0" count += 1 if bin_b[i] != "0": ans_b[i] = "0" count += 1 elif bin_a[i] == "1": continue elif bin_b[i] != "1": ans_b[i] = "1" count += 1 if count <= k: if count < k: k = k - count for i in range(0, len(bin_a)): if ans_a[i] == "1": if ans_b[i] == "1": if k >= 1: ans_a[i] = "0" k -= 1 elif k >= 2: ans_a[i] = "0" ans_b[i] = "1" k -= 2 ans_a = "".join(ans_a) ans_b = "".join(ans_b) ans1 = "%*X" % ((len(ans_a) + 3) // 4, int(ans_a, 2)) ans2 = "%*X" % ((len(ans_b) + 3) // 4, int(ans_b, 2)) print(str(ans1).upper().strip()) print(str(ans2).upper().strip()) else: print(-1) q -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input()) for qq in range(q): k = int(input()) a = list(bin(int(input(), 16))[2:].zfill(16**5)) b = list(bin(int(input(), 16))[2:].zfill(16**5)) c = list(bin(int(input(), 16))[2:].zfill(16**5)) notPossible = False for i in range(len(a)): if a[i] == "1" and c[i] == "0": if k > 0: a[i] = "0" k -= 1 else: notPossible = True break if notPossible: print(-1) continue for i in range(len(b)): if int(a[i]) | int(b[i]) != int(c[i]): if k > 0: b[i] = str(int(b[i]) ^ 1) k -= 1 else: notPossible = True if notPossible: print(-1) continue for i in range(len(a)): if k > 0: if int(a[i]) & int(b[i]) == 1: a[i] = "0" k -= 1 if k > 1 and (a[i] == "1" and b[i] == "0"): a[i] = str(int(a[i]) ^ 1) b[i] = str(int(b[i]) ^ 1) k -= 2 else: break print(hex(int("".join(a), 2))[2:].upper()) print(hex(int("".join(b), 2))[2:].upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def string_to_bits(s, total_bits): for _ in range(total_bits - 4 * len(s)): yield 0 for c in s: bits = [] number = int(c, base=16) for _ in range(4): bits.append(number % 2) number //= 2 for bit in reversed(bits): yield bit def group(n, seq): items = [] for el in seq: items.append(el) if len(items) == n: yield tuple(items) items.clear() if len(items) > 0: yield items def bits_to_chars(bits): still_zero = True for bit_group in group(4, bits): n = 0 for bit in bit_group: n = n * 2 + bit if still_zero and n == 0: continue still_zero = False yield "%X" % n if still_zero: yield "0" class NotSolvableError(Exception): pass def process(a_bits, b_bits, c_bits, max_corrections, operations): corrections_left = max_corrections for operation in operations: for i in range(len(a_bits)): corrections_left, a_bits[i], b_bits[i] = operation( a_bits[i], b_bits[i], c_bits[i], corrections_left ) def strict_operation(a, b, c, corrections_left): def use_correction(): nonlocal corrections_left if corrections_left == 0: raise NotSolvableError corrections_left -= 1 if c == 0: if a == 1: a = 0 use_correction() if b == 1: b = 0 use_correction() elif (a, b, c) == (0, 0, 1): b = 1 use_correction() return corrections_left, a, b def minimize_operation(a, b, c, corrections_left): if (a, b, c) == (1, 0, 1) and corrections_left >= 2: a, b = 0, 1 corrections_left -= 2 elif (a, b, c) == (1, 1, 1) and corrections_left >= 1: a = 0 corrections_left -= 1 return corrections_left, a, b def main(): Q = int(input().strip()) for _ in range(Q): K = int(input().strip()) strings = [input().strip() for _ in range(3)] max_bits = max(map(len, strings)) * 4 a_bits, b_bits, c_bits = [list(string_to_bits(s, max_bits)) for s in strings] try: process(a_bits, b_bits, c_bits, K, [strict_operation, minimize_operation]) except NotSolvableError: print("-1") else: print("".join(bits_to_chars(a_bits))) print("".join(bits_to_chars(b_bits))) main()
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR NUMBER FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER EXPR BIN_OP STRING VAR IF VAR EXPR STRING CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FUNC_DEF IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
BITS = { "0": [0, 0, 0, 0], "1": [1, 0, 0, 0], "2": [0, 1, 0, 0], "3": [1, 1, 0, 0], "4": [0, 0, 1, 0], "5": [1, 0, 1, 0], "6": [0, 1, 1, 0], "7": [1, 1, 1, 0], "8": [0, 0, 0, 1], "9": [1, 0, 0, 1], "A": [0, 1, 0, 1], "B": [1, 1, 0, 1], "C": [0, 0, 1, 1], "D": [1, 0, 1, 1], "E": [0, 1, 1, 1], "F": [1, 1, 1, 1], } def bitsof(x): B = [] for c in x: B.append(BITS[c]) C = [] for D in reversed(B): C.extend(D) return C def frombits(B): letters = [] for i in range(len(B) // 4): letters.append( "{:x}".format( B[4 * i] + 2 * (B[4 * i + 1] + 2 * (B[4 * i + 2] + 2 * B[4 * i + 3])) ) ) res = "".join(reversed(letters)) if res.count("0") != len(res): return res.upper().lstrip("0") else: return "0" def atbit(s, *n): return [i[s] for i in n] def force(A, B, C, s): a, b, c = atbit(s, A, B, C) if c == 0: A[s] = 0 B[s] = 0 return a + b elif c == 1 and a == b == 0: B[s] = 1 return 1 else: return 0 def aelim(A, B, C, s, l): a, b, c = atbit(s, A, B, C) if a == b == c == 1: if l >= 1: A[s] = 0 return 1 else: return 0 elif a == c == 1: if l >= 2: B[s] = 1 A[s] = 0 return 2 else: return 0 else: return 0 def tosize(A, bits): while len(A) < bits: A.append(0) Q = int(input()) for _ in range(Q): K = int(input()) A = bitsof(input()) B = bitsof(input()) C = bitsof(input()) bits = max(len(A), len(B), len(C)) tosize(A, bits) tosize(B, bits) tosize(C, bits) r = 0 for b in range(bits): r += force(A, B, C, b) if r > K: print(-1) continue left = K - r for b in range(bits - 1, -1, -1): left -= aelim(A, B, C, b, left) print(frombits(A)) print(frombits(B))
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR RETURN FUNC_CALL FUNC_CALL VAR STRING RETURN STRING FUNC_DEF RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
dtoh = { (0): "0", (1): "1", (2): "2", (3): "3", (4): "4", (5): "5", (6): "6", (7): "7", (8): "8", (9): "9", (10): "A", (11): "B", (12): "C", (13): "D", (14): "E", (15): "F", } T = int(input()) for t in range(T): K = int(input()) A = list(input()) B = list(input()) C = list(input()) lenA = len(A) lenB = len(B) lenC = len(C) length = max(lenA, lenB, lenC) A = ["0"] * (length - lenA) + A B = ["0"] * (length - lenB) + B C = ["0"] * (length - lenC) + C count = 0 bits = [8, 4, 2, 1] for i in range(length): a = int(A[i], 16) b = int(B[i], 16) c = int(C[i], 16) an = 0 bn = 0 for j in range(4): aj = 1 if a & bits[j] else 0 bj = 1 if b & bits[j] else 0 cj = 1 if c & bits[j] else 0 if cj == 0: if aj == 1: aj = 0 K -= 1 if bj == 1: bj = 0 K -= 1 if cj == 1: if aj == 0 and bj == 0: bj = 1 K -= 1 an |= bits[j] if aj else 0 bn |= bits[j] if bj else 0 A[i] = dtoh[an] B[i] = dtoh[bn] for i in range(lenC): a = int(A[i], 16) b = int(B[i], 16) c = int(C[i], 16) an = 0 bn = 0 for j in range(4): aj = 1 if a & bits[j] else 0 bj = 1 if b & bits[j] else 0 cj = 1 if c & bits[j] else 0 if cj == 1: if aj == 1 and bj == 1 and K > 0: aj = 0 K -= 1 elif aj == 1 and bj == 0 and K > 1: aj = 0 bj = 1 K -= 2 an |= bits[j] if aj else 0 bn |= bits[j] if bj else 0 A[i] = dtoh[an] B[i] = dtoh[bn] if K < 0: print("-1") else: i = 0 while i < len(A) - 1 and A[0] == "0": del A[i] while i < len(B) - 1 and B[0] == "0": del B[i] print("".join(A)) print("".join(B))
ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input().strip()) def bump_len(s, l): return "0" * (l - len(s)) + s count = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4] def bits_for_digit(a, b, c): ab = a | b drop_a = a & ~c drop_b = b & ~c needed = ~ab & c return count[needed] + count[drop_a] + count[drop_b] def doit(a, b, c, extra): a &= c b &= c b |= c & ~a for i in [8, 4, 2, 1]: if extra == 0: break if a & i != 0: if b & i != 0: a ^= i extra -= 1 elif extra > 1: a ^= i b ^= i extra -= 2 return a, b, extra def hexzip(ABC): for abc in zip(*ABC): yield [int(x, 16) for x in abc] for _ in range(Q): K = int(input().strip()) ABC = [input().strip() for _ in range(3)] length = max(len(s) for s in ABC) ABC = [bump_len(s, length) for s in ABC] need = 0 for a, b, c in hexzip(ABC): need += bits_for_digit(a, b, c) if need > K: print(-1) continue extra = K - need out_a = out_b = "" na = nb = 0 for a, b, c in hexzip(ABC): oa, ob, extra = doit(a, b, c, extra) na += count[a ^ oa] nb += count[b ^ ob] if out_a == "0": out_a = "" if out_b == "0": out_b = "" out_a += "%X" % (oa,) out_b += "%X" % (ob,) assert na + nb <= K print(out_a) print(out_b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR LIST NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input().strip()) for i in range(Q): K = int(input().strip()) K_orig = K A_temp = input().strip() B_temp = input().strip() C_temp = input().strip() z = len(C_temp) * 4 A = bin(int(A_temp, 16))[2:].zfill(z) B = bin(int(B_temp, 16))[2:].zfill(z) C = bin(int(C_temp, 16))[2:].zfill(z) a = list(A) b = list(B) c = list(C) n = len(c) p_1 = [] p_2 = [] for i in range(n): if c[i] == "0": if a[i] != "0": a[i] = "0" K -= 1 if b[i] != "0": b[i] = "0" K -= 1 elif a[i] == "0": if b[i] == "0": b[i] = "1" K -= 1 elif b[i] == "1": p_1.append(i) else: p_2.append(i) if K < 0: print(-1) else: while K > 0 and not (len(p_2) == 0 and len(p_1) == 0): if ( len(p_2) != 0 and K >= 2 and (len(p_1) == 0 or len(p_1) != 0 and p_2[0] < p_1[0]) ): a[p_2[0]] = "0" b[p_2[0]] = "1" K -= 2 p_2.pop(0) elif len(p_1) != 0: a[p_1[0]] = "0" K -= 1 p_1.pop(0) else: K = -1 Ap = "".join(a) Bp = "".join(b) Ap = hex(int(Ap, 2))[2:].upper() Bp = hex(int(Bp, 2))[2:].upper() print(Ap) print(Bp)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input()) class Enum: skip = 0 flip_a = 1 flip_b = 2 flip_both = 3 potential_flip_both = 4 potential_flip_a = 5 def binaryToHex(x): return hex(int(x, 2))[2:].upper() def hexToBinary(x, numBits): scale = 16 return bin(int(x, scale))[2:].zfill(numBits) def getAction(a, b, c): zeroesOrOnes = a == b == c eithers = (int(a) ^ int(b)) & int(c) if zeroesOrOnes or eithers: return Enum.skip if a == "0" and int(b) ^ int(c): return Enum.flip_b if a == "1" and b == "0" and c == "0": return Enum.flip_a else: return Enum.flip_both def solve(k, a, b, c): numBits = 4 * max(len(x) for x in [a, b, c]) a = hexToBinary(a, numBits) b = hexToBinary(b, numBits) c = hexToBinary(c, numBits) flipsRemaining = k newA = [] newB = [] for i in range(len(a)): action = getAction(a[i], b[i], c[i]) if action == Enum.flip_both: flipsRemaining -= 2 a_char = "0" b_char = "0" elif action == Enum.flip_a: flipsRemaining -= 1 a_char = "0" b_char = b[i] elif action == Enum.flip_b: flipsRemaining -= 1 a_char = a[i] if b[i] == "0": b_char = "1" else: b_char = "0" elif action == Enum.skip: a_char = a[i] b_char = b[i] newA.append(a_char) newB.append(b_char) if flipsRemaining < 0: return "-1" a = "".join(newA) b = "".join(newB) newA = [] newB = [] for i in range(len(a)): if flipsRemaining == 0: newA.append(a[i:]) newB.append(b[i:]) break if a[i] == "1" and b[i] == "0" and c[i] == "1": if flipsRemaining > 1: flipsRemaining -= 2 a_char = "0" b_char = "1" else: a_char = a[i] b_char = b[i] elif a[i] == b[i] == c[i] == "1": flipsRemaining -= 1 a_char = "0" b_char = b[i] else: a_char = a[i] b_char = b[i] newA.append(a_char) newB.append(b_char) a = "".join(newA) b = "".join(newB) return "{}\n{}".format(binaryToHex(a), binaryToHex(b)) for i in range(q): k = int(input()) a = input() b = input() c = input() print(solve(k, a, b, c))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR IF VAR STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING VAR STRING VAR STRING RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def hex_to_bin(st): res = "" for i in st: if i == "0": res += "0000" elif i == "1": res += "0001" elif i == "2": res += "0010" elif i == "3": res += "0011" elif i == "4": res += "0100" elif i == "5": res += "0101" elif i == "6": res += "0110" elif i == "7": res += "0111" elif i == "8": res += "1000" elif i == "9": res += "1001" elif i == "A": res += "1010" elif i == "B": res += "1011" elif i == "C": res += "1100" elif i == "D": res += "1101" elif i == "E": res += "1110" else: res += "1111" return res def bin_to_hex(st): res = "" for i in range(0, len(st), 4): if st[i : i + 4] == "0000": res += "0" elif st[i : i + 4] == "0001": res += "1" elif st[i : i + 4] == "0010": res += "2" elif st[i : i + 4] == "0011": res += "3" elif st[i : i + 4] == "0100": res += "4" elif st[i : i + 4] == "0101": res += "5" elif st[i : i + 4] == "0110": res += "6" elif st[i : i + 4] == "0111": res += "7" elif st[i : i + 4] == "1000": res += "8" elif st[i : i + 4] == "1001": res += "9" elif st[i : i + 4] == "1010": res += "A" elif st[i : i + 4] == "1011": res += "B" elif st[i : i + 4] == "1100": res += "C" elif st[i : i + 4] == "1101": res += "D" elif st[i : i + 4] == "1110": res += "E" else: res += "F" i = 0 while res[i] == "0": i += 1 if i == len(res): return "0" return res[i:] def fun(a, b, c, n, maximum): required = 0 a_copy = [i for i in a] b_copy = [i for i in b] for i in range(n): if c[i] == "0" and a[i] == "1" and b[i] == "1": required += 2 a_copy[i] = "0" b_copy[i] = "0" elif c[i] == "0" and (a[i] == "1" or b[i] == "1"): required += 1 if a[i] == "1": a_copy[i] = "0" else: b_copy[i] = "0" elif c[i] == "1" and a[i] == "0" and b[i] == "0": required += 1 b_copy[i] = "1" if required > maximum: break if required <= maximum: remaining = maximum - required for i in range(n): if remaining == 0: break if c[i] == "1" and a[i] == "1": if b[i] == "1": a_copy[i] = "0" remaining -= 1 elif b[i] == "0" and remaining >= 2: a_copy[i] = "0" b_copy[i] = "1" remaining -= 2 return [True, "".join(map(str, a_copy)), "".join(map(str, b_copy))] else: return [False] q = int(input()) while q != 0: q -= 1 k = int(input()) a = input() b = input() c = input() a_bin = hex_to_bin(a) b_bin = hex_to_bin(b) c_bin = hex_to_bin(c) max_length = max([len(a_bin), len(b_bin), len(c_bin)]) a_bin = "0" * (max_length - len(a_bin)) + a_bin b_bin = "0" * (max_length - len(b_bin)) + b_bin c_bin = "0" * (max_length - len(c_bin)) + c_bin res = fun(a_bin, b_bin, c_bin, max_length, k) if res[0] == False: print("-1") else: print(bin_to_hex(res[1])) print(bin_to_hex(res[2]))
FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING IF VAR STRING VAR STRING VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR VAR BIN_OP VAR NUMBER STRING VAR STRING VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN STRING RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER RETURN LIST NUMBER FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def c2i(c): n = ord(c) - 48 return (n, n - 7)[n > 9] def i2c(n): code = (n + 55, n + 48)[n < 10] return chr(code) def getbit(s, ibyte, ibit): return ibyte < len(s) and s[ibyte] & ibit or 0 Q = int(input()) for _ in range(Q): K = int(input()) sa = input() sb = input() sc = input() slen = max(len(sa), len(sb), len(sc)) A = list(map(c2i, sa.zfill(slen))) B = list(map(c2i, sb.zfill(slen))) C = list(map(c2i, sc.zfill(slen))) mlen = max(len(A), len(B), len(C)) ibyte, ibit = mlen - 1, 1 for b in range(mlen * 4): bA = getbit(A, ibyte, ibit) bB = getbit(B, ibyte, ibit) bC = getbit(C, ibyte, ibit) if not bC: if bA: A[ibyte] &= ~ibit K -= 1 if bB: B[ibyte] &= ~ibit K -= 1 elif not bA and not bB: B[ibyte] |= ibit K -= 1 if K < 0: break ibit <<= 1 if ibit > 8: ibyte -= 1 ibit = 1 ibyte, ibit = 0, 8 while K > 0 and ibit and ibyte < mlen: bA = getbit(A, ibyte, ibit) bB = getbit(B, ibyte, ibit) bC = getbit(C, ibyte, ibit) if bC: if bA and bB: A[ibyte] &= ~ibit K -= 1 elif bA and not bB and K > 1: A[ibyte] &= ~ibit B[ibyte] |= ibit K -= 2 ibit >>= 1 if not ibit: ibyte += 1 ibit = 8 if K < 0: print(-1) else: sa = "".join(map(i2c, A)).lstrip("0") sb = "".join(map(i2c, B)).lstrip("0") if len(sa) == 0: sa = "0" if len(sb) == 0: sb = "0" print(sa) print(sb)
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def oor(a, b): if str(a) == "1" or str(b) == "1": return 1 else: return 0 for _ in range(int(input())): k = int(input()) a = str(bin(int(input(), 16)))[2:] b = str(bin(int(input(), 16)))[2:] c = str(bin(int(input(), 16)))[2:] le = max([len(i) for i in [a, b, c]]) a = list("".join(["0"] * abs(le - len(a))) + a) b = list("".join(["0"] * abs(le - len(b))) + b) c = list("".join(["0"] * abs(le - len(c))) + c) no = True for i in range(le): if k < 0: print("-1") no = False break if not oor(a[i], b[i]) == int(c[i]): if c[i] == "0": if a[i] == "0": b[i] = "0" k -= 1 elif b[i] == "0": a[i] = "0" k -= 1 else: if k < 2: no = False print("-1") break a[i] = "0" b[i] = "0" k -= 2 else: b[i] = "1" k -= 1 if k > 0: for i in range(le): if k <= 0: break if a[i] == "1" and b[i] == "0": if k >= 2: a[i] = "0" b[i] = "1" k -= 2 if a[i] == "1" and a[i] == b[i]: a[i] = "0" k -= 1 if no: print(str(hex(int("".join(a), 2)))[2:].upper()) print(str(hex(int("".join(b), 2)))[2:].upper())
FUNC_DEF IF FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL STRING BIN_OP LIST STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL STRING BIN_OP LIST STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL STRING BIN_OP LIST STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
bits = [ "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111", ] def hex2bits(h): s = "" for d in h: if d.isnumeric(): s += bits[int(d)] else: s += bits[10 + ord(d) - 65] return s def bits2hex(b): return hex(int(b, 2))[2:].upper() for q in range(0, int(input().strip())): K = int(input().strip()) A = hex2bits(input().strip()) B = hex2bits(input().strip()) C = hex2bits(input().strip()) mx = max(len(A), len(B), len(C)) A = A.rjust(mx, "0") B = B.rjust(mx, "0") C = C.rjust(mx, "0") noAnswer = False D, E = "", "" for i in range(0, mx): a, b, c = A[i], B[i], C[i] if (a, b, c) == ("0", "0", "1"): if K < 1: noAnswer = True break D += a E += "1" K -= 1 elif (a, b, c) == ("0", "1", "0"): if K < 1: noAnswer = True break D += a E += "0" K -= 1 elif (a, b, c) == ("1", "0", "0"): if K < 1: noAnswer = True break D += "0" E += b K -= 1 elif (a, b, c) == ("1", "1", "0"): if K < 2: noAnswer = True break D += "0" E += "0" K -= 2 else: D += a E += b if noAnswer: print(-1) else: for i in range(0, mx): if K < 1: break a, b, c = A[i], B[i], C[i] if (a, b, c) == ("1", "0", "1"): if K >= 2: D = D[0:i] + "0" + D[i + 1 :] E = E[0:i] + "1" + E[i + 1 :] K -= 2 elif (a, b, c) == ("1", "1", "1"): if K >= 1: D = D[0:i] + "0" + D[i + 1 :] K -= 1 else: continue print(bits2hex(D)) print(bits2hex(E))
ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FUNC_DEF ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR STRING STRING STRING IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING STRING STRING IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING STRING STRING IF VAR NUMBER ASSIGN VAR NUMBER VAR STRING VAR VAR VAR NUMBER IF VAR VAR VAR STRING STRING STRING IF VAR NUMBER ASSIGN VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR STRING STRING STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR STRING STRING STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input()) d = { "0": "0000", "1": "0001", "2": "0010", "3": "0011", "4": "0100", "5": "0101", "6": "0110", "7": "0111", "8": "1000", "9": "1001", "A": "1010", "B": "1011", "C": "1100", "D": "1101", "E": "1110", "F": "1111", } d2 = { "0000": "0", "0001": "1", "0010": "2", "0011": "3", "0100": "4", "0101": "5", "0110": "6", "0111": "7", "1000": "8", "1001": "9", "1010": "A", "1011": "B", "1100": "C", "1101": "D", "1110": "E", "1111": "F", } while Q > 0: Q -= 1 k = int(input()) A = input() B = input() C = input() mx = max([len(A), len(B), len(C)]) A = "0" * (mx - len(A)) + A B = "0" * (mx - len(B)) + B C = "0" * (mx - len(C)) + C aa = "" bb = "" cc = "" for a in A: aa += d[a] for b in B: bb += d[b] for c in C: cc += d[c] A = list(map(int, aa)) B = list(map(int, bb)) C = list(map(int, cc)) mx = len(A) ans = 0 for i in range(mx): if A[i] | B[i] == C[i]: continue if C[i] == 0: if A[i]: A[i] = 0 k -= 1 if B[i]: B[i] = 0 k -= 1 else: B[i] = 1 k -= 1 if k < 0: print(-1) continue for i in range(mx): if k <= 0: break if A[i] == 1 and B[i] == 1: A[i] = 0 k -= 1 if k >= 2: if A[i] == 1 and B[i] == 0: A[i] = 0 B[i] = 1 k -= 2 AA = "" BB = "" for i in range(0, mx, 4): a = "" b = "" for j in range(4): a = a + str(A[i + j]) b = b + str(B[i + j]) a = d2[a] b = d2[b] if a == "0" and AA == "": a = "" if b == "0" and BB == "": b = "" AA += a BB += b if AA == "": AA = "0" if BB == "": BB = "0" print(AA) print(BB)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR STRING IF VAR STRING VAR STRING ASSIGN VAR STRING VAR VAR VAR VAR IF VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input()) for _ in range(0, Q): num_swaps = int(input()) a = int(input(), 16) b = int(input(), 16) c = int(input(), 16) a = [int(d) for d in bin(a)[2:]] b = [int(d) for d in bin(b)[2:]] c = [int(d) for d in bin(c)[2:]] ideal_length = max(map(len, (a, b, c))) a = [0] * (ideal_length - len(a)) + a b = [0] * (ideal_length - len(b)) + b c = [0] * (ideal_length - len(c)) + c for i in range(0, ideal_length): if c[i] == 0: if a[i] == 1: a[i] = 0 num_swaps -= 1 if b[i] == 1: b[i] = 0 num_swaps -= 1 elif c[i] == 1: if a[i] == 0 and b[i] == 0: b[i] = 1 num_swaps -= 1 else: print(c[i]) raise Exception() for i in range(0, ideal_length): if c[i] == 0: if a[i] == 1 or b[i] == 1: raise Exception() if c[i] == 1: if a[i] == 0 and b[i] == 0: raise Exception() if num_swaps < 0: print(-1) continue for i in range(0, ideal_length): if num_swaps <= 0: break if a[i] == 1: if b[i] == 1: a[i] = 0 num_swaps -= 1 elif num_swaps >= 2: a[i] = 0 b[i] = 1 num_swaps -= 2 a_int = int("".join("01"[i] for i in a), 2) b_int = int("".join("01"[i] for i in b), 2) print("{:X}".format(a_int)) print("{:X}".format(b_int))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR IF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING STRING VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
SOLUTION_PER_BIT = [ [[[True, 0, [0, 0]], [True, 1, [0, 1]]], [[True, 1, [0, 0]], [True, 0, [0, 1]]]], [ [[True, 1, [0, 0]], [False, 2, [0, 1], [1, 0]]], [[True, 2, [0, 0]], [False, 1, [0, 1], [1, 1]]], ], ] def solve(K, A, B, C): tentative_sol = [] tentative_K = 0 mandatory_K = 0 for na, nb, nc in zip(A, B, C): for mask in [8, 4, 2, 1]: ba = int(not not int(na, 16) & mask) bb = int(not not int(nb, 16) & mask) bc = int(not not int(nc, 16) & mask) s = SOLUTION_PER_BIT[ba][bb][bc] tentative_sol.append(s) tentative_K += s[1] if s[0]: mandatory_K += s[1] if mandatory_K > K: print(-1) return i = len(tentative_sol) - 1 while i >= 0 and tentative_K > K: s = tentative_sol[i] if not s[0]: tentative_K -= s[1] s = list(s) del s[2] tentative_sol[i] = s i -= 1 if tentative_K < K: i += 2 for x in tentative_sol[i:]: if x[2] == [1, 1]: x[2] = [0, 1] break Ap = 0 Bp = 0 for x in tentative_sol: Ap = Ap << 1 | x[2][0] Bp = Bp << 1 | x[2][1] print("{0:X}\n{1:X}".format(Ap, Bp)) nTestCases = int(input().strip()) for _ in range(nTestCases): K = int(input().strip()) A, B, C = (input() for i in [1, 2, 3]) if len(A) != len(B) or len(B) != len(C): raise ValueError("A, B, C must have the same length") solve(K, A, B, C)
ASSIGN VAR LIST LIST LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR LIST NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input()) for _ in range(q): k = int(input()) a = bin(int(input(), 16))[2:] b = bin(int(input(), 16))[2:] c = bin(int(input(), 16))[2:] maxlen = max(map(len, (a, b, c))) a = a.zfill(maxlen) b = b.zfill(maxlen) c = c.zfill(maxlen) best_a = "" best_b = "" changes = 0 for a_bit, b_bit, c_bit in zip(a, b, c): if c_bit == "0": best_a += "0" best_b += "0" changes += 1 if a_bit != "0" else 0 changes += 1 if b_bit != "0" else 0 elif a_bit == "1" or b_bit == "1": best_a += a_bit best_b += b_bit else: best_a += "0" best_b += "1" changes += 1 if changes > k: print(-1) else: for i, (a_bit, b_bit, c_bit) in enumerate(zip(a, b, c)): if c_bit == "1": if changes < k and a_bit == "1" and b_bit == "1": best_a = best_a[:i] + "0" + best_a[i + 1 :] best_b = best_b[:i] + "1" + best_b[i + 1 :] changes += 1 elif changes < k - 1 and a_bit == "1" and b_bit == "0": best_a = best_a[:i] + "0" + best_a[i + 1 :] best_b = best_b[:i] + "1" + best_b[i + 1 :] changes += 2 real_changes = sum(1 if a != b else 0 for a, b in zip(a, best_a)) real_changes += sum(1 if a != b else 0 for a, b in zip(b, best_b)) assert changes == real_changes print(hex(int(best_a, 2))[2:].upper()) print(hex(int(best_b, 2))[2:].upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR VAR STRING NUMBER NUMBER VAR VAR STRING NUMBER NUMBER IF VAR STRING VAR STRING VAR VAR VAR VAR VAR STRING VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR STRING IF VAR VAR VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def to_bits(S, bits): A = [0] * bits i = bits - 4 * len(S) for c in S: j = int(c, 16) if j & 8: A[i] = 1 if j & 4: A[i + 1] = 1 if j & 2: A[i + 2] = 1 if j & 1: A[i + 3] = 1 i += 4 return A def print_hex(A): S = [""] * (len(A) // 4) S[-1] = "0" nonzero = 0 for i in range(0, len(A), 4): d = A[i] << 3 | A[i + 1] << 2 | A[i + 2] << 1 | A[i + 3] if d or nonzero: S[i >> 2] = "%X" % d nonzero = 1 print("".join(S)) Q = int(input()) for q in range(Q): K = int(input()) A = input().strip() B = input().strip() C = input().strip() bits = 4 * max(len(A), len(B), len(C)) A = to_bits(A, bits) B = to_bits(B, bits) C = to_bits(C, bits) for i in range(bits): if C[i]: if A[i] == 0 and B[i] == 0: B[i] = 1 K -= 1 else: if A[i]: A[i] = 0 K -= 1 if B[i]: B[i] = 0 K -= 1 if K < 0: print(-1) else: for i in range(bits): if A[i]: if B[i]: if K >= 1: A[i] = 0 K -= 1 elif K >= 2: A[i] = 0 B[i] = 1 K -= 2 print_hex(A) print_hex(B)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP STRING VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input()) def swap(x): x1 = x & 1 x2 = (x & 2) >> 1 x3 = (x & 4) >> 2 x4 = (x & 8) >> 3 return x1 << 3 | x2 << 2 | x3 << 1 | x4 for _ in range(q): k = int(input()) a = input() b = input() c = input() max_len = max(len(a), len(b), len(c)) if len(a) < max_len: x = "0" * (max_len - len(a)) a = x + a if len(b) < max_len: x = "0" * (max_len - len(b)) b = x + b if len(c) < max_len: x = "0" * (max_len - len(c)) c = x + c a_ans = "" b_ans = "" for i in range(max_len): a_i = int(a[i], 16) b_i = int(b[i], 16) c_i = int(c[i], 16) o_a_i = a_i o_b_i = b_i o_c_i = c_i for j in range(3, -1, -1): a_x = a_i >> j & 1 b_x = b_i >> j & 1 c_x = c_i >> j & 1 if a_x | b_x != c_x: if c_x == 0: if a_x == 1: o_a_i = o_a_i ^ 1 << j k -= 1 if b_x == 1: o_b_i = o_b_i ^ 1 << j k -= 1 else: o_b_i = o_b_i | 1 << j k -= 1 a_ans += hex(o_a_i)[2:] b_ans += hex(o_b_i)[2:] if k > 0: a = a_ans[:] b = b_ans[:] a_ans = "" b_ans = "" for i in range(max_len): a_i = int(a[i], 16) b_i = int(b[i], 16) c_i = int(c[i], 16) o_a_i = a_i o_b_i = b_i o_c_i = c_i for j in range(3, -1, -1): a_x = a_i >> j & 1 b_x = b_i >> j & 1 c_x = c_i >> j & 1 if c_x == 1: if a_x == 1 and b_x == 1: if k > 0: o_a_i = o_a_i ^ 1 << j k -= 1 elif b_x == 0: if k > 1: o_a_i = o_a_i ^ 1 << j o_b_i = o_b_i | 1 << j k -= 2 a_ans += hex(o_a_i)[2:] b_ans += hex(o_b_i)[2:] if k < 0: print(-1) else: a_ans = a_ans.lstrip("0") if a_ans == "": a_ans = "0" b_ans = b_ans.lstrip("0") if b_ans == "": b_ans = "0" print(a_ans.upper()) print(b_ans.upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
queries = int(input()) def convert_hex_to_bin(hexstring): return bin(int(hexstring, 16))[2:] def convert_bin_to_hex(binstring): return hex(int(binstring, 2))[2:] def solve(k, a, b, c): edits = 0 a = list(a) b = list(b) c = list(c) for i, (ab, bb, cb) in enumerate(zip(a, b, c)): if cb == "1": if ab == "0" and bb == "0": b[i] = "1" edits += 1 else: if ab == "1": a[i] = "0" edits += 1 if bb == "1": b[i] = "0" edits += 1 possible = edits <= k if edits < k: for i, (ab, bb, cb) in enumerate(zip(a, b, c)): if cb == "1" and ab == "1" and bb == "1": a[i] = "0" edits += 1 elif cb == "1" and ab == "1" and bb == "0": if k - edits >= 2: a[i] = "0" b[i] = "1" edits += 2 if edits == k: break a = "".join(a) b = "".join(b) c = "".join(c) return possible, a, b for q in range(queries): k = int(input()) ah = input() bh = input() ch = input() a = convert_hex_to_bin(ah) b = convert_hex_to_bin(bh) c = convert_hex_to_bin(ch) bits = max(len(a), len(b), len(c)) a = a.zfill(bits) b = b.zfill(bits) c = c.zfill(bits) possible, ap, bp = solve(k, a, b, c) if not possible: print("-1") else: print(convert_bin_to_hex(ap).upper()) print(convert_bin_to_hex(bp).upper())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR STRING IF VAR STRING VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR STRING VAR STRING VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR STRING VAR STRING VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
class Solution: def __init__(self): self.K = int(input()) self.A = int(input(), 16) self.B = int(input(), 16) self.C = int(input(), 16) def calculate(self): changes = bin((self.A | self.B) ^ self.C).count("1") if changes > self.K: print(-1) return max_len = max(self.A.bit_length(), self.B.bit_length(), self.C.bit_length()) a_str = "0" * (max_len - self.A.bit_length()) + "{:b}".format(self.A) b_str = "0" * (max_len - self.B.bit_length()) + "{:b}".format(self.B) c_str = "0" * (max_len - self.C.bit_length()) + "{:b}".format(self.C) a = [int(i) for i in a_str] b = [int(i) for i in b_str] c = [int(i) for i in c_str] remaining_swaps = self.K for i in range(max_len): if a[i] | b[i] != c[i]: if c[i] == 1: b[i] = 1 remaining_swaps -= 1 else: if b[i] == 1: b[i] = 0 remaining_swaps -= 1 if a[i] == 1: a[i] = 0 remaining_swaps -= 1 if remaining_swaps < 0: print(-1) return if remaining_swaps == 0: print_hex(a, b) return for i in range(max_len): if a[i] == 1: if b[i] == 1: a[i] = 0 remaining_swaps -= 1 elif remaining_swaps >= 2: b[i] = 1 a[i] = 0 remaining_swaps -= 2 if remaining_swaps == 0: break print_hex(a, b) return def print_hex(a, b): print( "{0:X}\n{1:X}".format( int("".join([str(i) for i in a]), 2), int("".join([str(i) for i in b]), 2) ) ) def main(): cases = int(input()) for i in range(cases): my_object = Solution() my_object.calculate() def get_int_list(in_str): return [int(i) for i in in_str.strip().split()] main()
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def hex_to_bin(hex_str): hi = int(hex_str, 16) return map(int, bin(hi)[2:].zfill(4)) def bin_to_hex(bin_): bi = int("".join(map(str, bin_)), 2) return hex(bi)[2:].upper() def edit_distance(A, B, C): edit_scheme = { (0, 0, 0): 0, (0, 0, 1): 1, (0, 1, 0): 1, (0, 1, 1): 0, (1, 0, 0): 1, (1, 0, 1): 0, (1, 1, 0): 2, (1, 1, 1): 0, } edits = 0 for hex_words in zip(A, B, C): for a, b, c in zip(*map(hex_to_bin, hex_words)): edits += edit_scheme[a, b, c] return edits def aorbify(A, B, C, K): A = A.zfill(len(C)) B = B.zfill(len(C)) opt_edit_scheme = {(1, 1, 1): 1, (1, 0, 1): 2} opt_edits = K - edit_distance(A, B, C) if opt_edits < 0: print(-1) return edA, edB = [], [] for i, hex_chars in enumerate(zip(A, B, C)): binA, binB = [], [] for a, b, c in zip(*map(hex_to_bin, hex_chars)): opt_cost = opt_edit_scheme.get((a, b, c), 0) edit_flag = opt_edits >= opt_cost > 0 opt_edits -= edit_flag and opt_cost binA.append((edit_flag ^ 1) & a & c) binB.append(c if edit_flag else (a ^ 1) & c | a & b & c) edA.append(bin_to_hex(binA)) edB.append(bin_to_hex(binB)) outA = "".join(edA).lstrip("0") outB = "".join(edB).lstrip("0") print(outA if outA else 0) print(outB if outB else 0) test_cases = int(input()) for i in range(test_cases): K, A, B, C = int(input()), input(), input(), input() aorbify(A, B, C, K)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
import sys def debug(*msg): print(*msg, file=sys.stderr) for each_test in range(int(input())): k = int(input()) a = list(input()) b = list(input()) c = list(input()) for i in range(len(c)): ai = int(a[i], base=16) bi = int(b[i], base=16) ci = int(c[i], base=16) oi = ai | bi for j in range(4): mask = 2**j if oi & mask > ci & mask: if ai & mask > 0: ai -= mask k -= 1 if bi & mask > 0: bi -= mask k -= 1 elif oi & mask < ci & mask: bi += mask k -= 1 if k < 0: break if k < 0: break a[i] = hex(ai).upper()[2:] b[i] = hex(bi).upper()[2:] for i in range(len(c)): if k <= 0: break ai = int(a[i], base=16) bi = int(b[i], base=16) ci = int(c[i], base=16) for j in reversed(range(4)): if k == 0: break mask = 2**j if ai & mask == bi & mask and ai & mask > 0: ai -= mask k -= 1 elif ai & mask == ci & mask and ai & mask > 0 and k > 1: ai -= mask bi += mask k -= 2 a[i] = hex(ai).upper()[2:] b[i] = hex(bi).upper()[2:] if k < 0: print(-1) else: print(hex(int("".join(a), base=16)).upper()[2:]) print(hex(int("".join(b), base=16)).upper()[2:])
IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def hex_to_bin(hex_str): return bin(int(hex_str, 16))[2:] def hex_to_bool_array(hex_str): bin_str = hex_to_bin(hex_str) return [(c == "1") for c in bin_str] def bool_array_to_hex(arr): arr = "".join([("1" if b else "0") for b in arr]) return hex(int(arr, 2)).upper()[2:] def pad_bool_arrays(arrays): max_len = max(map(len, arrays)) padded = [] for i, arr in enumerate(arrays): padded.append([False] * (max_len - len(arr)) + arr) return padded def read_input(): K = int(input()) hex_nums = [input() for i in range(3)] A, B, C = pad_bool_arrays([hex_to_bool_array(x) for x in hex_nums]) return K, A, B, C def first_pass(K, A, B, C): assert len(A) == len(B) == len(C) remaining = K for i in range(len(A)): if C[i]: if not (A[i] or B[i]): B[i] = True remaining -= 1 else: if A[i]: A[i] = False remaining -= 1 if B[i]: B[i] = False remaining -= 1 if remaining < 0: return None return remaining, A, B def solve(K, A, B, C): ans = first_pass(K, A, B, C) if ans is None: return None remaining, A, B = ans for i in range(len(A)): assert remaining >= 0 if remaining == 0: break if C[i] and A[i]: if remaining >= 2 or B[i]: A[i] = False remaining -= 1 if not B[i]: B[i] = True remaining -= 1 return A, B def main(): for _ in range(int(input())): ans = solve(*read_input()) if ans is None: print(-1) else: Aprime, Bprime = map(bool_array_to_hex, ans) print(Aprime) print(Bprime) main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR STRING VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL STRING VAR STRING STRING VAR VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NONE RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE RETURN NONE ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def bin_to_hex(bin): return hex(int(bin, 2)).upper()[2:] def determine_answer(k, bin_a, bin_b, bin_c): for i in range(len(bin_a)): if bin_a[i] == "0" and bin_b[i] == "0" and bin_c[i] == "1": bin_b[i] = "1" k -= 1 elif bin_a[i] == "0" and bin_b[i] == "1" and bin_c[i] == "0": bin_b[i] = "0" k -= 1 elif bin_a[i] == "1" and bin_b[i] == "0" and bin_c[i] == "0": bin_a[i] = "0" k -= 1 elif bin_a[i] == "1" and bin_b[i] == "1" and bin_c[i] == "0": bin_a[i] = "0" bin_b[i] = "0" k -= 2 if k < 0: return False for i in range(len(bin_a)): if k == 0: break if k > 1 and bin_a[i] == "1" and bin_b[i] == "0" and bin_c[i] == "1": bin_a[i] = "0" bin_b[i] = "1" k -= 2 elif bin_a[i] == "1" and bin_b[i] == "1" and bin_c[i] == "1": bin_a[i] = "0" k -= 1 return True test_cases = int(input()) num_of_bits = 400000 scale = 16 for i in range(test_cases): k = int(input()) hex_a = input() hex_b = input() hex_c = input() binary_a = list(bin(int(hex_a, scale))[2:].zfill(num_of_bits)) binary_b = list(bin(int(hex_b, scale))[2:].zfill(num_of_bits)) binary_c = list(bin(int(hex_c, scale))[2:].zfill(num_of_bits)) if determine_answer(k, binary_a, binary_b, binary_c): print(bin_to_hex("".join(binary_a))) print(bin_to_hex("".join(binary_b))) else: print(-1)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = input() for i in range(int(q)): k = int(input()) A = "" for e in input(): A += bin(int(e, 16))[2:].zfill(4) B = "" for e in input(): B += bin(int(e, 16))[2:].zfill(4) C = "" for e in input(): C += bin(int(e, 16))[2:].zfill(4) biggest = max(len(A), len(B), len(C)) A = list(A.zfill(biggest)) B = list(B.zfill(biggest)) C = list(C.zfill(biggest)) assert len(A) == len(B) == len(C) for i in range(len(A)): if (A[i] == "1" and B[i] == "0") and C[i] == "0": A[i] = "0" k -= 1 elif (A[i] == "0" and B[i] == "1") and C[i] == "0": B[i] = "0" k -= 1 elif (A[i] == "1" and B[i] == "1") and C[i] == "0": A[i] = "0" B[i] = "0" k -= 2 elif (A[i] == "0" and B[i] == "0") and C[i] == "1": B[i] = "1" k -= 1 if k < 0: print(-1) continue if k > 0: for i in range(len(A)): if C[i] == "1" and B[i] == "1" and A[i] == "1" and k > 0: A[i] = "0" k -= 1 elif C[i] == "1" and B[i] == "0" and A[i] == "1" and k > 1: B[i] = "1" A[i] = "0" k -= 2 if k == 0: break at = int("".join(A), 2) bt = int("".join(B), 2) ct = int("".join(B), 2) assert at or bt == ct print("%X" % int("".join(A), 2)) print("%X" % int("".join(B), 2))
ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
maxm = 10**9 def distance(x, y, z, K): count = 0 x, y, z = list(x), list(y), list(z) for i in range(len(x)): a, b, c = x[i], y[i], z[i] if c == "1": if a == "0" and b == "0": count, y[i] = count + 1, "1" elif c == "0": if a == "1": x[i], count = "0", count + 1 if b == "1": y[i], count = "0", count + 1 if count > K: print(-1) else: count = K - count for i in range(len(x)): a, b, c = x[i], y[i], z[i] if count < 1: break if c == "1": if a == "1" and b == "1": count, x[i] = count - 1, "0" elif a == "1" and b == "0" and count >= 2: x[i], y[i], count = "0", "1", count - 2 nA = "".join([str(ll) for ll in x]) mA = "".join([str(mm) for mm in y]) nB = hex(int(nA, 2))[2:].upper() mB = hex(int(mA, 2))[2:].upper() print(nB) print(mB) for _ in range(int(input())): K = int(input()) A = "0x" + input().strip().upper() B = "0x" + input().strip().upper() C = "0x" + input().strip().upper() x, y, z = bin(int(A, 16))[2:], bin(int(B, 16))[2:], bin(int(C, 16))[2:] maxPad = max(len(x), len(y), len(z)) x = x.rjust(maxPad, "0") y = y.rjust(maxPad, "0") z = z.rjust(maxPad, "0") distance(x, y, z, K)
ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR STRING IF VAR STRING VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER STRING IF VAR STRING IF VAR STRING ASSIGN VAR VAR VAR STRING BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR VAR STRING BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR STRING IF VAR STRING VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER STRING IF VAR STRING VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR VAR STRING STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
Q = int(input().strip()) for q in range(Q): K = int(input().strip()) A = int(input().strip(), base=16) B = int(input().strip(), base=16) C = int(input().strip(), base=16) bina = [int(s) for s in bin(A).split("b")[1]] binb = [int(s) for s in bin(B).split("b")[1]] binc = [int(s) for s in bin(C).split("b")[1]] lena = len(bina) lenb = len(binb) lenc = len(binc) maxl = max(lena, lenb, lenc) if lena < maxl: for index in range(lena, maxl): bina.insert(0, 0) if lenb < maxl: for index in range(lenb, maxl): binb.insert(0, 0) if lenc < maxl: for index in range(lenc, maxl): binc.insert(0, 0) count = 0 for index in range(maxl): if binc[index] == 0: if bina[index] == 1: bina[index] = 0 count += 1 if binb[index] == 1: binb[index] = 0 count += 1 elif bina[index] == 0 and binb[index] == 0: binb[index] = 1 count += 1 if count > K: failed = True else: failed = False index = 0 while count < K and index < maxl: if bina[index] == 1: if binb[index] == 1: bina[index] = 0 count += 1 elif K - count > 1: bina[index] = 0 binb[index] = 1 count += 2 index += 1 A_prime = int("".join(str(b) for b in bina), base=2) B_prime = int("".join(str(b) for b in binb), base=2) if failed: print(-1) else: print(hex(A_prime).upper().split("X")[1]) print(hex(B_prime).upper().split("X")[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
testCases = int(input()) for i in range(testCases): maxChange = int(input()) hexA = input() hexB = input() hexC = input() binA = "" binB = "" binC = "" binaryLength = len(hexA) * 4 binA = "{0:0{width}b}".format(int(hexA, 16), width=binaryLength) binA = list(binA) binB = "{0:0{width}b}".format(int(hexB, 16), width=binaryLength) binB = list(binB) binC = "{0:0{width}b}".format(int(hexC, 16), width=binaryLength) binC = list(binC) optionalBits = list() changes = 0 for i in range(len(binC)): if binC[i] == "1": if binA[i] == "1": optionalBits.append(i) elif binB[i] == "0": binB[i] = "1" changes += 1 if binC[i] == "0": if binA[i] == "1": binA[i] = "0" changes += 1 if binB[i] == "1": binB[i] = "0" changes += 1 if changes < maxChange: for i in optionalBits: if binB[i] == "0" and changes + 2 <= maxChange: binA[i] = "0" binB[i] = "1" changes += 2 elif binB[i] == "1" and changes + 1 <= maxChange: binA[i] = "0" changes += 1 if changes <= maxChange: print(format(int("".join(binA), 2), "x").upper()) print(format(int("".join(binB), 2), "x").upper()) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR FOR VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input().strip()) for qq in range(q): k = int(input().strip()) a = input().strip() b = input().strip() c = input().strip() max_len = max(len(a), max(len(b), len(c))) a_bin = "" b_bin = "" c_bin = "" for ch in a: a_bin += bin(int(ch, 16))[2:].zfill(4) for ch in b: b_bin += bin(int(ch, 16))[2:].zfill(4) for ch in c: c_bin += bin(int(ch, 16))[2:].zfill(4) a_bin = list(a_bin.zfill(max_len * 4)) b_bin = list(b_bin.zfill(max_len * 4)) c_bin = list(c_bin.zfill(max_len * 4)) change = 0 pos_changes = [] for i in range(max_len * 4): if a_bin[i] == "0" and b_bin[i] == "0" and c_bin[i] == "1": change += 1 b_bin[i] = "1" elif a_bin[i] == "0" and b_bin[i] == "1" and c_bin[i] == "0": change += 1 b_bin[i] = "0" elif a_bin[i] == "1" and b_bin[i] == "0" and c_bin[i] == "0": change += 1 a_bin[i] = "0" elif a_bin[i] == "1" and b_bin[i] == "0" and c_bin[i] == "1": pos_changes.append(i) elif a_bin[i] == "1" and b_bin[i] == "1" and c_bin[i] == "0": change += 2 a_bin[i] = "0" b_bin[i] = "0" elif a_bin[i] == "1" and b_bin[i] == "1" and c_bin[i] == "1": pos_changes.append(i) if change > k: print(-1) else: i = 0 while i < len(pos_changes) and change < k: ind = pos_changes[i] if b_bin[ind] == "0" and change + 2 <= k: a_bin[ind] = "0" b_bin[ind] = "1" change += 2 elif b_bin[ind] == "1" and change + 1 <= k: a_bin[ind] = "0" change += 1 i += 1 a_bin = "".join(a_bin) b_bin = "".join(b_bin) a_hex = "" b_hex = "" for i in range(0, len(a_bin), 4): a_hex += hex(int(a_bin[i : i + 4], 2))[2:] for i in range(0, len(b_bin), 4): b_hex += hex(int(b_bin[i : i + 4], 2))[2:] a_hex = a_hex.lstrip("0") b_hex = b_hex.lstrip("0") a_hex = "0" if a_hex == "" else a_hex.upper() b_hex = "0" if b_hex == "" else b_hex.upper() print(a_hex) print(b_hex)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR STRING STRING FUNC_CALL VAR ASSIGN VAR VAR STRING STRING FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def reduceab(k, a, b, c): bitca = bitc(a) reserved = 0 while bitca > 0 and k > 0: mod = 1 << a.bit_length() - 1 a ^= mod k -= 1 bitca -= 1 if mod & b == 0: if k > 0: b |= mod k -= 1 else: reserved |= mod k += 1 a |= reserved return a, b def do_the_shit(k, a, b, c): killa = ~c & a killb = ~c & b addb = c & ~(a | b) k -= sum(map(bitc, (killa, killb, addb))) a ^= killa b ^= killb b |= addb if k < 0: return 0, 0 else: a, b = reduceab(k, a, b, c) return a, b def func(): return input() def q(x): return "{0:X}".format(x) def bitc(x): return bin(x)[2:].count("1") tc = range(int(func().strip())) for _ in tc: kk = int(func().strip()) aao = int(func().strip(), 16) bbo = int(func().strip(), 16) cc = int(func().strip(), 16) aa, bb = do_the_shit(kk, aao, bbo, cc) if aa or bb: print(q(aa)) print(q(bb)) else: print(-1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL STRING VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def HEX(A): A = "".join(list(map(str, A))) A = int(A, 2) A = hex(A).replace("0x", "") return A q = int(input()) for qq in range(q): k = int(input()) a = int(input(), 16) b = int(input(), 16) c = int(input(), 16) A = list(bin(a).replace("0b", "")) B = list(bin(b).replace("0b", "")) C = list(bin(c).replace("0b", "")) m = max(len(A), len(B), len(C)) A = list("0" * (m - len(A))) + A B = list("0" * (m - len(B))) + B C = list("0" * (m - len(C))) + C for i in range(m): A[i] = int(A[i]) B[i] = int(B[i]) C[i] = int(C[i]) for i in range(m): s = "{}{}{}".format(A[i], B[i], C[i]) if s == "000": continue if s == "001": B[i] = 1 k -= 1 if s == "010": B[i] = 0 k -= 1 if s == "011": continue if s == "100": A[i] = 0 k -= 1 if s == "101": continue if s == "110": A[i] = 0 B[i] = 0 k -= 2 if s == "111": continue if k < 0: print("-1") if k == 0: print(HEX(A).upper()) print(HEX(B).upper()) if k > 0: for i in range(m): s = "{}{}{}".format(A[i], B[i], C[i]) if s == "000": continue if s == "011": continue if s == "101": if k > 1: A[i] = 0 B[i] = 1 k -= 2 if s == "111": if k > 0: A[i] = 0 k -= 1 print(HEX(A).upper()) print(HEX(B).upper())
FUNC_DEF ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR VAR VAR IF VAR STRING IF VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR STRING IF VAR STRING ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR STRING IF VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR VAR VAR IF VAR STRING IF VAR STRING IF VAR STRING IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR STRING IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input().strip()) for q0 in range(q): k = int(input().strip()) a = int(input().strip(), 16) b = int(input().strip(), 16) c = int(input().strip(), 16) a1 = c & a b1 = c & b | c & ~a diff = bin(a1 ^ a).count("1") + bin(b1 ^ b).count("1") if diff > k: print(-1) else: flips_left = k - diff bina1 = bin(a1)[2:] binb1 = bin(b1)[2:] maxlen = max(len(bina1), len(binb1)) bina1 = bina1.zfill(maxlen) binb1 = binb1.zfill(maxlen) bina1 = [(1 if digit == "1" else 0) for digit in bina1] binb1 = [(1 if digit == "1" else 0) for digit in binb1] for i in range(0, maxlen): if flips_left == 0: break if bina1[i] == 1 and (flips_left > 1 or flips_left == 1 and binb1[i] == 1): bina1[i] = 0 flips_left -= 1 if binb1[i] == 0: binb1[i] = 1 flips_left -= 1 a2 = int("".join(str(d) for d in bina1), 2) b2 = int("".join(str(d) for d in binb1), 2) print("{:X}".format(a2)) print("{:X}".format(b2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
bn = { "0": "0000", "1": "0001", "2": "0010", "3": "0011", "4": "0100", "5": "0101", "6": "0110", "7": "0111", "8": "1000", "9": "1001", "A": "1010", "B": "1011", "C": "1100", "D": "1101", "E": "1110", "F": "1111", } hx = { "0000": "0", "0001": "1", "0010": "2", "0011": "3", "0100": "4", "0101": "5", "0110": "6", "0111": "7", "1000": "8", "1001": "9", "1010": "A", "1011": "B", "1100": "C", "1101": "D", "1110": "E", "1111": "F", } for each in range(int(input().strip())): k = int(input().strip()) a = list(input().strip()) b = list(input().strip()) c = list(input().strip()) ab = "" bb = "" cb = "" for i in range(max(len(c), len(b), len(a)) - 1, -1, -1): if len(a) > 0: A = a.pop() else: A = "0" if len(b) > 0: B = b.pop() else: B = "0" if len(c) > 0: C = c.pop() else: C = "0" ab = bn[A] + ab bb = bn[B] + bb cb = bn[C] + cb ab = list(ab) bb = list(bb) cb = list(cb) t = 0 i = len(cb) - 1 while i >= 0 and t <= k: if max(ab[i], bb[i]) != cb[i]: if cb[i] == "0": if ab[i] == "1": ab[i] = "0" t = t + 1 if bb[i] == "1": bb[i] = "0" t = t + 1 else: bb[i] = "1" t = t + 1 i = i - 1 if t > k: print(-1) continue l = k - t if l > 0: i = 0 while l > 0 and i < len(cb): if cb[i] == "1": if bb[i] == "1": if ab[i] == "1": ab[i] = "0" l = l - 1 elif l > 1: ab[i] = "0" bb[i] = "1" l = l - 2 i = i + 1 ax = "" bx = "" for i in range(len(ab) // 4): A = "".join(ab[i * 4 : (i + 1) * 4]) B = "".join(bb[i * 4 : (i + 1) * 4]) if hx[A] != "0" or len(ax) != 0: ax = ax + hx[A] if hx[B] != "0" or len(bx) != 0: bx = bx + hx[B] if len(ax) == 0: ax = "0" if len(bx) == 0: bx = "0" print(ax) print(bx)
ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
def calculate(A, B, C, k): n = max(len(A), len(B), len(C)) A, B, C = ([0] * (n - len(x)) + x for x in (A, B, C)) for i, a, b, c in zip(range(n), A, B, C): if a | b < c: B[i] = 1 k -= 1 elif a | b > c: k -= a + b A[i] = B[i] = 0 if k < 0: return [-1] for i, a, b in zip(range(n), A, B): if a > b and k >= 2: A[i], B[i] = 0, 1 k -= 2 elif a >= b >= 1 and k: A[i] = 0 k -= 1 A, B = (hex(int("".join(map(str, x)), 2))[2:].upper() for x in (A, B)) return A, B q = int(input()) for i in range(q): k = int(input()) a, b, c = (list(map(int, bin(int(input(), 16))[2:])) for i in range(3)) print(*calculate(a, b, c, k), sep="\n")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN LIST NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR STRING
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
import sys def parse_input(n): return list("{0:0b}".format(int(n, 16))) def lpad(x, size): return ["0"] * (size - len(x)) + x def normalize(A, B, C): size = max(len(A), len(B), len(C)) return lpad(A, size), lpad(B, size), lpad(C, size) def solve(A, B, C, K): changes = 0 index = 0 while index < len(C): if C[index] == "0": if A[index] == "1": A[index] = "0" changes = changes + 1 if B[index] == "1": B[index] = "0" changes = changes + 1 elif A[index] == "0" and B[index] == "0": B[index] = "1" changes = changes + 1 index += 1 index = 0 while index < len(A) and changes < K: if A[index] == "1" and B[index] == "1": A[index] = "0" changes = changes + 1 elif A[index] == "1" and B[index] == "0" and changes <= K - 2: A[index] = "0" B[index] = "1" changes = changes + 2 index += 1 if changes <= K: print("{0:X}".format(int("".join(A), 2))) print("{0:X}".format(int("".join(B), 2))) else: print("-1") cases = int(input()) for case in range(cases): K = int(input().strip()) A = parse_input(input().strip()) B = parse_input(input().strip()) C = parse_input(input().strip()) A, B, C = normalize(A, B, C) solve(A, B, C, K)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP LIST STRING BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Consider four numbers: $\mbox{A}$, $\mbox{B}$, $\mbox{C}$, and $\mbox{K}$. You must change at most $\mbox{K}$ bits in $\mbox{A}$ and $\mbox{B}$ to form the numbers $\mbox{A'}$ and $B^{\prime}$ satisfying the equation $A'\mid B'=C$. Here, the | symbol denotes the bitwise OR operation. Given $Q$ sets of the numbers defined above, find and print the respective values of $\mbox{A'}$ and $B^{\prime}$ on new lines; if no such value exists, print $-1$ instead. If there are multiple solutions, make $\mbox{A'}$ as small as possible; if there are still multiple solutions, make $B^{\prime}$ as small as possible. Notes: $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$ are given in Hexadecimal (base 16), and $\mbox{K}$ is given in decimal (base 10). If the number of bits changed in $\mbox{A}$ is $k_a$ and the number of bits changed in B is $k_b$, then $k_a+k_b$ must be $\leq K$. Input Format The first line contains an integer, $Q$, denoting the number of queries. The subsequent lines describe each respective query as follows: The first line contains a single integer denoting the value of $\mbox{K}$. Each of the next $3$ lines contains a Hexadecimal (base 16) number describing the respective values of $\mbox{A}$, $\mbox{B}$, and $\mbox{C}$. Constraints $1\leq Q\leq5$ $0\leq K\leq5\times10^5$ $0<A,B,C<16^{5\times10^4}$ Output Format Print two lines of output for each query: The first line should contain a Hexadecimal (base 16) number denoting the value of $\mbox{A'}$. The second line must contain a Hexadecimal (base 16) number denoting the value of $B^{\prime}$. If no valid answer exists, you must instead print one line of output with the integer $-1$. Note: The letters in Hexadecimal numbers must be in uppercase. Sample Input 3 8 2B 9F 58 5 B9 40 5A 2 91 BE A8 Sample Output 8 58 18 42 -1 Explanation Query 0: In this query, $K=8$. Change $A=(2B)_{16}$ to $A'=(8)_{16}$. $3$ bits are changed. Change B = $(9F)_{16}$ to $B'=(58)_{16}$. $5$ bits are changed. $A'\mid B'=(8)_{16}\mid(58)_{16}=(58)_{16}=C$ Query 1: In this query, $K=5$. Change $A=(B9)_{16}$ to $A'=(18)_{16}$. $3$ bits are changed. Change $B=(40)_{16}$ to $B'=(42)_{16}$. Only $1$ bit is changed. $A'\mid B'=(18)_{16}\mid(42)_{16}=(5A)_{16}=C$ Query 2: There is no valid answer, so we print $-1$.
q = int(input()) for repetition in range(q): k = int(input()) a = bin(int(input(), 16))[2:] b = bin(int(input(), 16))[2:] c = bin(int(input(), 16))[2:] l = max(map(len, (a, b, c))) a = list("0" * (l - len(a)) + a) b = list("0" * (l - len(b)) + b) c = list("0" * (l - len(c)) + c) i = 0 while i < l: if c[i] == "0": if a[i] == "1": k -= 1 a[i] = "0" if b[i] == "1": k -= 1 b[i] = "0" elif a[i] == "0" and b[i] == "0": b[i] = "1" k -= 1 if k < 0: print(-1) break i += 1 else: i = 0 while i < l and k > 0: if a[i] == "1": if b[i] == "1": k -= 1 a[i] = "0" elif k > 1: k -= 2 a[i] = "0" b[i] = "1" i += 1 print(format(int("".join(a), 2), "X")) print(format(int("".join(b), 2), "X"))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER STRING
You have received data from a Bubble bot. You know your task is to make factory facilities, but before you even start, you need to know how big the factory is and how many rooms it has. When you look at the data you see that you have the dimensions of the construction, which is in rectangle shape: N x M. Then in the next N lines you have M numbers. These numbers represent factory tiles and they can go from 0 to 15. Each of these numbers should be looked in its binary form. Because from each number you know on which side the tile has walls. For example number 10 in it's binary form is 1010, which means that it has a wall from the North side, it doesn't have a wall from the East, it has a wall on the South side and it doesn't have a wall on the West side. So it goes North, East, South, West. It is guaranteed that the construction always has walls on it's edges. The input will be correct. Your task is to print the size of the rooms from biggest to smallest. Input The first line has two numbers which are N and M, the size of the construction. Both are integers: n (1 ≀ n ≀ 10^3) m (1 ≀ m ≀ 10^3) Next N x M numbers represent each tile of construction. Output Once you finish processing the data your output consists of one line sorted from biggest to smallest room sizes. Example Input 4 5 9 14 11 12 13 5 15 11 6 7 5 9 14 9 14 3 2 14 3 14 Output 9 4 4 2 1
def main(): n, m = map(int, input().split()) grid = [list(map(int, input().split())) for _ in range(n)] visited = [([False] * m) for _ in range(n)] rooms = [] for i in range(n): for j in range(m): if visited[i][j]: continue room_size = 0 stack = [(i, j)] while stack: x, y = stack.pop() if visited[x][y]: continue visited[x][y] = True room_size += 1 if x > 0 and not (grid[x][y] & 8 or grid[x - 1][y] & 2): stack.append((x - 1, y)) if y < m and not (grid[x][y] & 4 or grid[x][y + 1] & 1): stack.append((x, y + 1)) if x < n and not (grid[x][y] & 2 or grid[x + 1][y] & 8): stack.append((x + 1, y)) if y > 0 and not (grid[x][y] & 1 or grid[x][y - 1] & 4): stack.append((x, y - 1)) rooms.append(room_size) rooms.sort(reverse=True) print(" ".join(map(str, rooms)) + " ") main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
You have received data from a Bubble bot. You know your task is to make factory facilities, but before you even start, you need to know how big the factory is and how many rooms it has. When you look at the data you see that you have the dimensions of the construction, which is in rectangle shape: N x M. Then in the next N lines you have M numbers. These numbers represent factory tiles and they can go from 0 to 15. Each of these numbers should be looked in its binary form. Because from each number you know on which side the tile has walls. For example number 10 in it's binary form is 1010, which means that it has a wall from the North side, it doesn't have a wall from the East, it has a wall on the South side and it doesn't have a wall on the West side. So it goes North, East, South, West. It is guaranteed that the construction always has walls on it's edges. The input will be correct. Your task is to print the size of the rooms from biggest to smallest. Input The first line has two numbers which are N and M, the size of the construction. Both are integers: n (1 ≀ n ≀ 10^3) m (1 ≀ m ≀ 10^3) Next N x M numbers represent each tile of construction. Output Once you finish processing the data your output consists of one line sorted from biggest to smallest room sizes. Example Input 4 5 9 14 11 12 13 5 15 11 6 7 5 9 14 9 14 3 2 14 3 14 Output 9 4 4 2 1
def dfs(i, j): vis[i][j] = True global cells cells += 1 adj = [(i - 1, j), (i, j + 1), (i + 1, j), (i, j - 1)] adj.reverse() c = box[i][j] for x, y in adj: if 0 <= x < n and 0 <= y < m and not vis[x][y] and not c % 2: dfs(x, y) c //= 2 n, m = (int(x) for x in input().split()) box = [[int(x) for x in input().split()] for _ in range(n)] vis = [([False] * m) for _ in range(n)] sizes = [] for i in range(n): for j in range(m): if not vis[i][j]: cells = 0 dfs(i, j) sizes.append(cells) sizes.sort(reverse=True) for x in sizes: print(x, end=" ")
FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
You have received data from a Bubble bot. You know your task is to make factory facilities, but before you even start, you need to know how big the factory is and how many rooms it has. When you look at the data you see that you have the dimensions of the construction, which is in rectangle shape: N x M. Then in the next N lines you have M numbers. These numbers represent factory tiles and they can go from 0 to 15. Each of these numbers should be looked in its binary form. Because from each number you know on which side the tile has walls. For example number 10 in it's binary form is 1010, which means that it has a wall from the North side, it doesn't have a wall from the East, it has a wall on the South side and it doesn't have a wall on the West side. So it goes North, East, South, West. It is guaranteed that the construction always has walls on it's edges. The input will be correct. Your task is to print the size of the rooms from biggest to smallest. Input The first line has two numbers which are N and M, the size of the construction. Both are integers: n (1 ≀ n ≀ 10^3) m (1 ≀ m ≀ 10^3) Next N x M numbers represent each tile of construction. Output Once you finish processing the data your output consists of one line sorted from biggest to smallest room sizes. Example Input 4 5 9 14 11 12 13 5 15 11 6 7 5 9 14 9 14 3 2 14 3 14 Output 9 4 4 2 1
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] i_cant_do_bitwise_operations = { (0): (False, False, False, False), (1): (False, False, False, True), (2): (False, False, True, False), (3): (False, False, True, True), (4): (False, True, False, False), (5): (False, True, False, True), (6): (False, True, True, False), (7): (False, True, True, True), (8): (True, False, False, False), (9): (True, False, False, True), (10): (True, False, True, False), (11): (True, False, True, True), (12): (True, True, False, False), (13): (True, True, False, True), (14): (True, True, True, False), (15): (True, True, True, True), } vis = [] def dfs(tiles, i, j): vis[i][j] = True tot = 1 for k in range(4): if not tiles[i][j][k] and not vis[i + directions[k][0]][j + directions[k][1]]: tot += dfs(tiles, i + directions[k][0], j + directions[k][1]) return tot n, m = list(map(int, input().split())) tiles = [] for i in range(n): line = input().split() tile_line = [] vis_line = [] for x in line: tile_line.append(i_cant_do_bitwise_operations[int(x)]) vis_line.append(False) tiles.append(tile_line) vis.append(vis_line) rooms = [] for i in range(n): for j in range(m): if not vis[i][j]: rooms.append(dfs(tiles, i, j)) rooms = sorted(rooms, reverse=True) for x in rooms: print(x, end=" ") print()
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You have received data from a Bubble bot. You know your task is to make factory facilities, but before you even start, you need to know how big the factory is and how many rooms it has. When you look at the data you see that you have the dimensions of the construction, which is in rectangle shape: N x M. Then in the next N lines you have M numbers. These numbers represent factory tiles and they can go from 0 to 15. Each of these numbers should be looked in its binary form. Because from each number you know on which side the tile has walls. For example number 10 in it's binary form is 1010, which means that it has a wall from the North side, it doesn't have a wall from the East, it has a wall on the South side and it doesn't have a wall on the West side. So it goes North, East, South, West. It is guaranteed that the construction always has walls on it's edges. The input will be correct. Your task is to print the size of the rooms from biggest to smallest. Input The first line has two numbers which are N and M, the size of the construction. Both are integers: n (1 ≀ n ≀ 10^3) m (1 ≀ m ≀ 10^3) Next N x M numbers represent each tile of construction. Output Once you finish processing the data your output consists of one line sorted from biggest to smallest room sizes. Example Input 4 5 9 14 11 12 13 5 15 11 6 7 5 9 14 9 14 3 2 14 3 14 Output 9 4 4 2 1
import sys fast = sys.stdin.readline n, m = map(int, fast().split()) grid = [] for _ in range(n): grid.append(list(map(int, fast().split()))) def dfs(i, j): if i >= 0 and j >= 0 and i < n and j < m and grid[i][j] != -1: t = grid[i][j] grid[i][j] = -1 ans = 0 for v in [[1, [0, -1]], [2, [1, 0]], [4, [0, 1]], [8, [-1, -0]]]: if v[0] & t == 0: ans += dfs(i + v[1][0], j + v[1][1]) return ans + 1 else: return 0 rs = [] for i in range(n): for j in range(m): if grid[i][j] != -1: rs.append(dfs(i, j)) rs.sort(reverse=True) for r in rs: print(r, end=" ")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR LIST LIST NUMBER LIST NUMBER NUMBER LIST NUMBER LIST NUMBER NUMBER LIST NUMBER LIST NUMBER NUMBER LIST NUMBER LIST NUMBER NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
You have received data from a Bubble bot. You know your task is to make factory facilities, but before you even start, you need to know how big the factory is and how many rooms it has. When you look at the data you see that you have the dimensions of the construction, which is in rectangle shape: N x M. Then in the next N lines you have M numbers. These numbers represent factory tiles and they can go from 0 to 15. Each of these numbers should be looked in its binary form. Because from each number you know on which side the tile has walls. For example number 10 in it's binary form is 1010, which means that it has a wall from the North side, it doesn't have a wall from the East, it has a wall on the South side and it doesn't have a wall on the West side. So it goes North, East, South, West. It is guaranteed that the construction always has walls on it's edges. The input will be correct. Your task is to print the size of the rooms from biggest to smallest. Input The first line has two numbers which are N and M, the size of the construction. Both are integers: n (1 ≀ n ≀ 10^3) m (1 ≀ m ≀ 10^3) Next N x M numbers represent each tile of construction. Output Once you finish processing the data your output consists of one line sorted from biggest to smallest room sizes. Example Input 4 5 9 14 11 12 13 5 15 11 6 7 5 9 14 9 14 3 2 14 3 14 Output 9 4 4 2 1
import sys sys.setrecursionlimit(100000) def _r(): return sys.stdin.buffer.readline() def rs(): return _r().decode("ascii").strip() def rn(): return int(_r()) def rnt(): return map(int, _r().split()) def rnl(): return list(rnt()) def solve(n, m, mat): dp, color = [([0] * m) for _ in range(n)], 1 def _color(r, c, color): stack, colored = [(r, c)], 1 dp[r][c] = color while stack: r, c = stack.pop() for dr, dc, mask in ((-1, 0, 8), (0, 1, 4), (1, 0, 2), (0, -1, 1)): nr, nc = r + dr, c + dc if mat[r][c] & mask == 0 and dp[nr][nc] == 0: dp[nr][nc] = color colored += 1 stack.append((nr, nc)) return colored sol = [] for r in range(n): for c in range(m): if dp[r][c] == 0: sol.append(_color(r, c, color)) color += 1 return sorted(sol, reverse=True) n, m = rnt() mat = [] for _ in range(n): mat.append(rnl()) print(" ".join(map(str, solve(n, m, mat))), "")
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR LIST VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR STRING