description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
a, b, k = map(int, input().split()) if a == 0 or b == 1: if k != 0: print("No") elif a == 0 and b == 1: print("Yes\n1 1") elif a == 0: print(f"Yes\n{'1' * b} {'1' * b}") elif b == 1: print("Yes\n1" + "0" * a + " " + "1" + "0" * a) elif a + b - 2 < k: print("No") else: x = "1" * b + "0" * a if k <= a: print("Yes\n" + x + " " + "1" * (b - 1) + "0" * k + "1" + "0" * (a - k)) else: y = "1" * (b - (k - a) - 1) + "0" + "1" * (k - a) + "0" * (a - 1) + "1" print(f"Yes\n{x} {y}")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING BIN_OP STRING VAR STRING BIN_OP STRING VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING BIN_OP STRING VAR STRING STRING BIN_OP STRING VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING VAR STRING BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING VAR STRING BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER STRING BIN_OP STRING BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING VAR STRING VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
a, b, k = map(int, input().split()) if k == 0: print("Yes") print("1" * b + "0" * a) print("1" * b + "0" * a) quit() if k >= a + b - 1: print("No") else: if b == 1 or a == 0: print("No") quit() first = "1" * b + "0" * a second = [c for c in first] take = min(a, k) k -= take second[b - 1 + take] = "1" second[b - k - 1] = "0" print("Yes") print(first) print("".join(second))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
a, b, k = map(int, input().split()) if b == 1 or a == 0: if k == 0: print("YES") y = [1] * b + [0] * a print(*y, sep="") print(*y, sep="") else: print("NO") elif k > a: if b >= 3 and k <= a + b - 2: x = [1] * b + [0] * a v = a + b - 2 - k + 1 y1 = [1] + [1] * (b - 2) + [0] * (a - 1) + [1] y = [] for i in range(a + b - 1): if i == v: y.append(0) y.append(y1[i]) print("YES") print(*x, sep="") print(*y, sep="") else: print("NO") else: print("YES") x = [] y = [1] * (b - 1) + [0] * a + [1] for i in range(a + b - k - 1): if b > 1: x.append(1) b = b - 1 else: x.append(0) x.append(1) for i in range(k): x.append(0) print(*x, sep="") print(*y, sep="")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING IF VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
a, b, k = map(int, input().split()) if a == 0 or b == 1: if k == 0: print("Yes") print("{}{}".format("1" * b, "0" * a)) print("{}{}".format("1" * b, "0" * a)) else: print("No") elif k > a + b - 2: print("No") elif k <= a: print("Yes") print("{}{}".format("1" * b, "0" * a)) print("{}{}{}{}".format("1" * (b - 1), "0" * k, "1", "0" * (a - k))) else: print("Yes") print("{}{}".format("1" * b, "0" * a)) print( "{}{}{}{}{}".format( "1" * (b - (k - a) - 1), "0", "1" * (k - a), "0" * (a - 1), "1" ) )
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING VAR STRING BIN_OP STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER STRING BIN_OP STRING BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR NUMBER STRING
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
a, b, k = map(int, input().split()) if a == 0: if k == 0: s = b * "1" print("Yes") print(s) print(s) else: print("No") exit() if b == 1: if k == 0: s = "1" + a * "0" print("Yes") print(s) print(s) else: print("No") exit() if k > a + b - 2: print("No") exit() s = "1" * b + "0" * a if k <= a: t = "1" * (b - 1) + "0" * k + "1" + "0" * (a - k) print("Yes") print(s) print(t) exit() left = k - a t = (b - left - 1) * "1" + "0" + left * "1" + (a - 1) * "0" + "1" print("Yes") print(s) print(t)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING VAR STRING BIN_OP STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER STRING STRING BIN_OP VAR STRING BIN_OP BIN_OP VAR NUMBER STRING STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") z1, o1, o2 = map(int, input().split()) z2 = z1 + o1 - o2 f = "1" * o1 + "0" * z1 ans = "YES" if not z1 or o1 == 1: if o2: ans = "NO" else: s = "1" * o1 + "0" * z1 elif o2 <= z1: s = "1" * (o1 - 1) + "0" * o2 + "1" + "0" * (z1 - o2) elif o2 < o1 + z1 - 1: s = "1" * (z1 + o1 - o2 - 1) + "0" + "1" * (o2 - z1) + "0" * (z1 - 1) + "1" else: ans = "NO" if ans == "NO": print(ans) else: print(ans) print(f) print(s)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR STRING IF VAR VAR NUMBER IF VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING VAR STRING BIN_OP STRING BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER STRING BIN_OP STRING BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR NUMBER STRING ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
import sys input = sys.stdin.readline a, b, k = map(int, input().split()) if b == 1: if k == 0: print("Yes") x = "1" + "0" * a y = "1" + "0" * a print(x) print(y) else: print("No") elif k > a: if a == 0: print("No") elif k > a + b - 2: print("No") else: x = "11" + "0" * (a - 1) + "1" * (k - a) + "0" + "1" * (b - 2 - k + a) y = "1" + "0" * a + "1" * (b - 1) print("Yes") print(x) print(y) else: x = "11" + "0" * k + "1" * (b - 2) + "0" * (a - k) y = "1" + "0" * k + "1" * (b - 1) + "0" * (a - k) print("Yes") print(x) print(y)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP STRING BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR VAR STRING BIN_OP STRING BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP STRING VAR BIN_OP STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP STRING VAR BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP STRING VAR BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
from sys import stdin input = stdin.readline def main(): a, b, k = map(int, input().split()) if b == 1 or a == 0: if k != 0: print("No") return else: x = ["1"] * b + ["0"] * a y = x.copy() elif k > a + b - 2: print("No") return elif k <= a: x = ["1"] * (b - 1) + ["0"] * (a - k) y = x.copy() x = x + ["1"] + ["0"] * k y = y + ["0"] * k + ["1"] else: x = ["1"] * b + ["0"] * a y = x.copy() y[-k - 1] = "0" y[-1] = "1" x = "".join(x) y = "".join(y) print("Yes") print(x) print(y) main()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP BIN_OP LIST STRING VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR NUMBER BIN_OP LIST STRING BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR LIST STRING BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP LIST STRING VAR LIST STRING ASSIGN VAR BIN_OP BIN_OP LIST STRING VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
from sys import stdin, stdout input = stdin.readline a, b, k = map(int, input().split()) if k == 0: s1 = "1" * b + "0" * a s2 = "1" * b + "0" * a a = 0 b = 0 else: s1 = "1" s2 = "1" b -= 1 if a < k and a > 0: s = "1" * (k - a) + "0" * (a - 1) s1 += "1" + s + "0" s2 += "0" + s + "1" b -= k - a + 1 a = 0 else: s1 += "1" + "0" * k s2 += "0" * k + "1" b -= 1 a -= k if b > 0: s1 += "1" * b s2 += "1" * b if a > 0: s1 += "0" * a s2 += "0" * a if a < 0 or b < 0: print("NO") else: print("YES") print(s1) print(s2)
ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR NUMBER VAR BIN_OP BIN_OP STRING VAR STRING VAR BIN_OP BIN_OP STRING VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP STRING BIN_OP STRING VAR VAR BIN_OP BIN_OP STRING VAR STRING VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR IF VAR NUMBER VAR BIN_OP STRING VAR VAR BIN_OP STRING VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
a, b, k = map(int, input().split()) if a == k == 0 and b == 1: print("Yes") print(1) print(1) elif k > a + b - 2 or b == 1 and k > 0 or a == 0 and k > 0: print("No") elif k == 0: sa = "1" sb = "1" b -= 1 while b: sa += "1" sb += "1" b -= 1 while a: sa += "0" sb += "0" a -= 1 print("Yes") print(sa) print(sb) else: sa = "1" sb = "1" b -= 1 sa += "1" sb += "0" k -= 1 b -= 1 a -= 1 ss = "0" * a + "1" * b sa += ss[:k] + "0" + ss[k:] sb += ss[:k] + "1" + ss[k:] print("Yes") print(sa) print(sb)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING VAR NUMBER WHILE VAR VAR STRING VAR STRING VAR NUMBER WHILE VAR VAR STRING VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
import sys input = sys.stdin.readline def get_ret_1(a, b, k): if b == 1 and k == 0: x = y = "1" + "0" * a return "Yes\n" + x + "\n" + y x = "1" * (b - 1) + "0" * (a + b - k - (b - 1) - 1) + "1" + "0" * k y = "1" * (b - 1) + "0" * (a + b - (b - 1) - 1) + "1" return "Yes\n" + x + "\n" + y def get_ret_2(a, b, k): x = "1" * b + "0" * a y = "1" * (a + b - k - 1) + "0" + "1" * (k - a) + "0" * (a - 1) + "1" return "Yes\n" + x + "\n" + y def main(): a, b, k = map(int, input().split()) if a == 0 and k != 0: print("No") return if b == 1 and k > 0: print("No") return if a >= k: print(get_ret_1(a, b, k)) elif a + b - 2 >= k: print(get_ret_2(a, b, k)) else: print("No") main()
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP STRING BIN_OP STRING VAR RETURN BIN_OP BIN_OP BIN_OP STRING VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER STRING BIN_OP STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP STRING BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER STRING RETURN BIN_OP BIN_OP BIN_OP STRING VAR STRING VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER STRING BIN_OP STRING BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR NUMBER STRING RETURN BIN_OP BIN_OP BIN_OP STRING VAR STRING VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
a, b, k = map(int, input().split()) a, b = b, a if k == 0: print("Yes") print("1" * a + "0" * b) print("1" * a + "0" * b) exit() if a <= 1 or b == 0: print("No") exit() if a + b - 1 <= k: print("No") exit() zero = b - 1 xl = [0] yl = [1] for i in range(k - 1): if zero > 0: xl.append(0) yl.append(0) zero -= 1 else: xl.append(1) yl.append(1) xl.append(1) yl.append(0) for i in range(a + b - k - 2): if zero > 0: xl.append(0) yl.append(0) zero -= 1 else: xl.append(1) yl.append(1) xl.append(1) yl.append(1) xl = [str(x) for x in xl] yl = [str(y) for y in yl] print("Yes") print("".join(xl[::-1])) print("".join(yl[::-1]))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
from sys import stdin, stdout def yes(x, y): stdout.write("Yes\n") stdout.write("".join(map(str, x))) stdout.write("\n") stdout.write("".join(map(str, y))) stdout.write("\n") def no(): stdout.write("No\n") def check(x, y): x = int(f"0b{''.join(map(str, x))}", base=2) y = int(f"0b{''.join(map(str, y))}", base=2) print(bin(x - y)) a, b, k = map(int, stdin.readline().split()) if k == 0: lst = [1] * b + [0] * a yes(lst, lst) elif b == 1 or k > a + b - 2: no() else: x = [1] + [0] * (a + b - 1) y = x.copy() on_x = 1 on_y = 1 + k x[on_x] = 1 y[on_y] = 1 ones_left = b - 2 counter = 2 try: while ones_left > 0: if counter != on_y: ones_left -= 1 x[counter] = 1 y[counter] = 1 counter += 1 yes(x, y) except: no()
FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
import sys from sys import stdin tt = 1 for loop in range(tt): a, b, k = map(int, stdin.readline().split()) if k == 0: print("Yes") print("1" * b + "0" * a) print("1" * b + "0" * a) elif a == 0: if k > 0: print("No") else: print("Yes") print("1" * b) print("1" * b) elif a + b - 1 <= k: print("No") else: x = [None] * (a + b) y = [None] * (a + b) x[k] = "1" y[k] = "0" x[0] = "0" y[0] = "1" a -= 1 b -= 1 for i in range(len(x)): if x[i] != None: continue if a > 0: x[i] = "0" y[i] = "0" a -= 1 else: x[i] = "1" y[i] = "1" x.reverse() y.reverse() x = "".join(x) y = "".join(y) if x[0] == y[0] == "1": print("Yes") print(x) print(y) else: print("No")
IMPORT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NONE IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
import sys input = sys.stdin.readline Z, O, k = map(int, input().split()) if O == 1: if k == 0: print("Yes") print("1" + "0" * Z) print("1" + "0" * Z) else: print("No") sys.exit() if Z == 0: if k == 0: print("Yes") print("1" * O) print("1" * O) else: print("No") sys.exit() if k == 0: print("Yes") print("1" * O + "0" * Z) print("1" * O + "0" * Z) sys.exit() MAX = Z + O - 2 if k > MAX: print("No") sys.exit() if k <= Z: A = "1" * O + "0" * Z B = ["0"] * (Z + O) for i in range(O - 1): B[i] = "1" B[O + k - 1] = "1" print("Yes") print(A) print("".join(B)) sys.exit() SA = MAX - k A = "1" * O + "0" * Z B = ["0"] * (Z + O) B[0] = "1" for i in range(1, 1 + SA): B[i] = "1" B[-1] = "1" for i in range(O - SA - 2): B[SA + i + 2] = "1" print("Yes") print(A) print("".join(B))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR VAR ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR STRING ASSIGN VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given three integers a, b, k. Find two binary integers x and y (x β‰₯ y) such that 1. both x and y consist of a zeroes and b ones; 2. x - y (also written in binary form) has exactly k ones. You are not allowed to use leading zeros for x and y. Input The only line contains three integers a, b, and k (0 ≀ a; 1 ≀ b; 0 ≀ k ≀ a + b ≀ 2 β‹… 10^5) β€” the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by x and y in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, x = 101000_2 = 2^5 + 2^3 = 40_{10}, y = 100001_2 = 2^5 + 2^0 = 33_{10}, 40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}. Hence x-y has 3 ones in base-2. In the second example, x = 10100_2 = 2^4 + 2^2 = 20_{10}, y = 10010_2 = 2^4 + 2^1 = 18, x - y = 20 - 18 = 2_{10} = 10_{2}. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
a, b, k = map(int, input().split()) if a >= k and b > 1: print("YES") for i in range(b - 1): print(1, end="") for i in range(a - k): print(0, end="") print(1, end="") for i in range(k): print(0, end="") print() for i in range(b - 1): print(1, end="") for i in range(a): print(0, end="") print(1, end="") elif a + b - 2 >= k and a > 0 and b > 1: print("YES") for i in range(b): print(1, end="") for i in range(a): print(0, end="") print() print(1, end="") for i in range(b - 2 - (k - a)): print(1, end="") print(0, end="") for i in range(k - a): print(1, end="") for i in range(a - 1): print(0, end="") print(1, end="") elif k == 0: print("YES") for i in range(b): print(1, end="") for i in range(a): print(0, end="") print() for i in range(b): print(1, end="") for i in range(a): print(0, end="") else: print("NO")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR STRING
Harasees has an array A of size N containing non-negative integers. He starts at the index 1 of A and wants to reach the index N. In one move, he can do the following: If he is currently at index i, then he can move to any index j such that i ≀ j ≀ \min(i + A_{i}, N). The cost of a path is defined as the [bitwise OR] of all the array elements visited by Harasees during his journey from index 1 to N. Formally if he visited the indices 1, X_{1}, X_{2}, \ldots, X_{K}, N, then the cost of this path = (A_{1} \ | \ A_{X_{1}} \ | \ A_{X_{2}} \ | \ \ldots \ | \ A_{X_{K}} \ | \ A_{N}). Determine the minimum possible cost to reach index N from index 1. If it is not possible to do so, output -1. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains a single integer N - the length of the array A. - The next line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum cost to go from index 1 to index N using the given moves. If it is not possible to do so, output -1. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $0 ≀ A_{i} ≀ 5 \cdot 10^{5}$ - The sum of $N$ over all test cases won't exceed $5 \cdot 10^{5}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≀ M ≀ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 5 1 1 1 1 1 4 2 4 1 3 ----- Sample Output 1 ------ 1 3 ----- explanation 1 ------ - Test Case 1: Harasees has no choice but to keep going from his current position to the position that follows it immediately till he reaches the $5$th index. So the minimum cost (in this case, the only possible cost) is $1\ |\ 1\ |\ 1\ |\ 1\ |\ 1\ =\ 1$. - Test Case 2: It is optimal for Harasees to make the following moves: $(i = 1) \rightarrow (i = 3) \rightarrow (i = 4)$. The cost of this path is: $A_{1} \ | \ A_{3} \ | \ A_{4} = 2\ |\ 1\ |\ 3\ =\ 3$. We can prove that no path exists with a cost $< 3$. ----- Sample Input 2 ------ 1 5 1 0 2 3 3 ----- Sample Output 2 ------ -1 ----- explanation 2 ------ - Test Case 1: In this case Harasees will get stuck at index $2$ and there is no way to reach the last index.
def canJump(nums): n = len(nums) i = 0 while i < n: p = i if p == n - 1: return True if nums[p] == 0 and p != n - 1: return False i += 1 nxt = i m = nums[i] while i <= p + nums[p]: if i >= n: return True if m <= nums[i] + (i - p - 1): m = nums[i] + (i - p - 1) nxt = i i += 1 if nxt == p: return False i = nxt return False for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) a = arr[0] | arr[-1] p = a nums = [] for j in range(n): if a | arr[j] == a: nums.append(arr[j]) else: nums.append(0) if canJump(nums): print(a) continue f = False for i in range(20): if a >> i & 1 == 0: a += 1 << i nums = [] for j in range(n): if a | arr[j] == a: nums.append(arr[j]) else: nums.append(0) if canJump(nums): f = True idx = i break if f: arr = nums[:] for i in range(idx - 1, -1, -1): if p >> i & 1 == 0 and a >> i & 1 == 1: nums = [] for j in range(n): if arr[j] >> i & 1 == 0: nums.append(arr[j]) else: nums.append(0) if canJump(nums): arr = nums a -= 1 << i print(a) else: print(-1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Harasees has an array A of size N containing non-negative integers. He starts at the index 1 of A and wants to reach the index N. In one move, he can do the following: If he is currently at index i, then he can move to any index j such that i ≀ j ≀ \min(i + A_{i}, N). The cost of a path is defined as the [bitwise OR] of all the array elements visited by Harasees during his journey from index 1 to N. Formally if he visited the indices 1, X_{1}, X_{2}, \ldots, X_{K}, N, then the cost of this path = (A_{1} \ | \ A_{X_{1}} \ | \ A_{X_{2}} \ | \ \ldots \ | \ A_{X_{K}} \ | \ A_{N}). Determine the minimum possible cost to reach index N from index 1. If it is not possible to do so, output -1. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains a single integer N - the length of the array A. - The next line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum cost to go from index 1 to index N using the given moves. If it is not possible to do so, output -1. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $0 ≀ A_{i} ≀ 5 \cdot 10^{5}$ - The sum of $N$ over all test cases won't exceed $5 \cdot 10^{5}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≀ M ≀ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 5 1 1 1 1 1 4 2 4 1 3 ----- Sample Output 1 ------ 1 3 ----- explanation 1 ------ - Test Case 1: Harasees has no choice but to keep going from his current position to the position that follows it immediately till he reaches the $5$th index. So the minimum cost (in this case, the only possible cost) is $1\ |\ 1\ |\ 1\ |\ 1\ |\ 1\ =\ 1$. - Test Case 2: It is optimal for Harasees to make the following moves: $(i = 1) \rightarrow (i = 3) \rightarrow (i = 4)$. The cost of this path is: $A_{1} \ | \ A_{3} \ | \ A_{4} = 2\ |\ 1\ |\ 3\ =\ 3$. We can prove that no path exists with a cost $< 3$. ----- Sample Input 2 ------ 1 5 1 0 2 3 3 ----- Sample Output 2 ------ -1 ----- explanation 2 ------ - Test Case 1: In this case Harasees will get stuck at index $2$ and there is no way to reach the last index.
def check(arr, mask): if arr[0] & mask != arr[0]: return False if arr[-1] & mask != arr[-1]: return False i = 1 max_reach = arr[0] while i < len(arr): if i > max_reach: return False if arr[i] & mask == arr[i]: max_reach = max(max_reach, i + arr[i]) i += 1 return True t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split(" "))) ans = 0 for bit in reversed(range(31)): if check(arr, ans + (1 << bit) - 1): continue else: ans += 1 << bit if check(arr, ans): print(ans) else: print(-1)
FUNC_DEF IF BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN 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 VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Harasees has an array A of size N containing non-negative integers. He starts at the index 1 of A and wants to reach the index N. In one move, he can do the following: If he is currently at index i, then he can move to any index j such that i ≀ j ≀ \min(i + A_{i}, N). The cost of a path is defined as the [bitwise OR] of all the array elements visited by Harasees during his journey from index 1 to N. Formally if he visited the indices 1, X_{1}, X_{2}, \ldots, X_{K}, N, then the cost of this path = (A_{1} \ | \ A_{X_{1}} \ | \ A_{X_{2}} \ | \ \ldots \ | \ A_{X_{K}} \ | \ A_{N}). Determine the minimum possible cost to reach index N from index 1. If it is not possible to do so, output -1. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains a single integer N - the length of the array A. - The next line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum cost to go from index 1 to index N using the given moves. If it is not possible to do so, output -1. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $0 ≀ A_{i} ≀ 5 \cdot 10^{5}$ - The sum of $N$ over all test cases won't exceed $5 \cdot 10^{5}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≀ M ≀ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 5 1 1 1 1 1 4 2 4 1 3 ----- Sample Output 1 ------ 1 3 ----- explanation 1 ------ - Test Case 1: Harasees has no choice but to keep going from his current position to the position that follows it immediately till he reaches the $5$th index. So the minimum cost (in this case, the only possible cost) is $1\ |\ 1\ |\ 1\ |\ 1\ |\ 1\ =\ 1$. - Test Case 2: It is optimal for Harasees to make the following moves: $(i = 1) \rightarrow (i = 3) \rightarrow (i = 4)$. The cost of this path is: $A_{1} \ | \ A_{3} \ | \ A_{4} = 2\ |\ 1\ |\ 3\ =\ 3$. We can prove that no path exists with a cost $< 3$. ----- Sample Input 2 ------ 1 5 1 0 2 3 3 ----- Sample Output 2 ------ -1 ----- explanation 2 ------ - Test Case 1: In this case Harasees will get stuck at index $2$ and there is no way to reach the last index.
def decToBin(num): out = [(0) for _ in range(32)] idx = 31 maxBitPos = 31 while num > 0: out[idx] = num % 2 if num % 2 == 1: maxBitPos = idx num //= 2 idx -= 1 return out, max(0, maxBitPos - 2) def binToDec(arr): num, base = 0, 1 for bit in arr[::-1]: num += bit * base base *= 2 return num def isPossible(n, arr): curr, reach = 0, 0 while curr < n and curr <= reach: reach = max(reach, curr + arr[curr]) curr += 1 return reach >= n - 1 def helper(n, arr, data, ans, k, maxBitPos): curr, reach = 0, 0 while curr < n and curr <= reach: isIncluded = True for i in range(maxBitPos, k + 1): if data[curr][i] == 1 and ans[i] == 0: isIncluded = False if isIncluded: reach = max(reach, curr + arr[curr]) curr += 1 return reach >= n - 1 def solve(n, arr): if isPossible(n, arr) is False: return -1 data = [None for i in range(n)] maxBitPos = 31 for i in range(n): data[i], bitPos = decToBin(arr[i]) maxBitPos = min(maxBitPos, bitPos) ans = [(0) for _ in range(32)] for i in range(32): if data[0][i] == 1 or data[-1][i] == 1: ans[i] = 1 for i in range(maxBitPos, 32): if ans[i] == 0: if helper(n, arr, data, ans, i, maxBitPos) is False: ans[i] = 1 return binToDec(ans) tc = int(input()) for _ in range(tc): n = int(input()) arr = list(map(int, input().split())) print(solve(n, arr))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Harasees has an array A of size N containing non-negative integers. He starts at the index 1 of A and wants to reach the index N. In one move, he can do the following: If he is currently at index i, then he can move to any index j such that i ≀ j ≀ \min(i + A_{i}, N). The cost of a path is defined as the [bitwise OR] of all the array elements visited by Harasees during his journey from index 1 to N. Formally if he visited the indices 1, X_{1}, X_{2}, \ldots, X_{K}, N, then the cost of this path = (A_{1} \ | \ A_{X_{1}} \ | \ A_{X_{2}} \ | \ \ldots \ | \ A_{X_{K}} \ | \ A_{N}). Determine the minimum possible cost to reach index N from index 1. If it is not possible to do so, output -1. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains a single integer N - the length of the array A. - The next line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum cost to go from index 1 to index N using the given moves. If it is not possible to do so, output -1. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $0 ≀ A_{i} ≀ 5 \cdot 10^{5}$ - The sum of $N$ over all test cases won't exceed $5 \cdot 10^{5}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≀ M ≀ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 5 1 1 1 1 1 4 2 4 1 3 ----- Sample Output 1 ------ 1 3 ----- explanation 1 ------ - Test Case 1: Harasees has no choice but to keep going from his current position to the position that follows it immediately till he reaches the $5$th index. So the minimum cost (in this case, the only possible cost) is $1\ |\ 1\ |\ 1\ |\ 1\ |\ 1\ =\ 1$. - Test Case 2: It is optimal for Harasees to make the following moves: $(i = 1) \rightarrow (i = 3) \rightarrow (i = 4)$. The cost of this path is: $A_{1} \ | \ A_{3} \ | \ A_{4} = 2\ |\ 1\ |\ 3\ =\ 3$. We can prove that no path exists with a cost $< 3$. ----- Sample Input 2 ------ 1 5 1 0 2 3 3 ----- Sample Output 2 ------ -1 ----- explanation 2 ------ - Test Case 1: In this case Harasees will get stuck at index $2$ and there is no way to reach the last index.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) def check(mask): if arr[0] | mask != mask: return False if arr[-1] | mask != mask: return False dp = [False] * n dp[0] = True j = 0 for i in range(n): if arr[i] | mask != mask: continue if not dp[i]: continue r = min(n - 1, i + arr[i]) while j <= r: if arr[j] | mask == mask: dp[j] = True j += 1 return dp[-1] mask = (1 << 19) - 1 if not check(mask): print(-1) else: for i in range(18, -1, -1): mask ^= 1 << i if not check(mask): mask |= 1 << i print(mask)
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 VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Harasees has an array A of size N containing non-negative integers. He starts at the index 1 of A and wants to reach the index N. In one move, he can do the following: If he is currently at index i, then he can move to any index j such that i ≀ j ≀ \min(i + A_{i}, N). The cost of a path is defined as the [bitwise OR] of all the array elements visited by Harasees during his journey from index 1 to N. Formally if he visited the indices 1, X_{1}, X_{2}, \ldots, X_{K}, N, then the cost of this path = (A_{1} \ | \ A_{X_{1}} \ | \ A_{X_{2}} \ | \ \ldots \ | \ A_{X_{K}} \ | \ A_{N}). Determine the minimum possible cost to reach index N from index 1. If it is not possible to do so, output -1. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains a single integer N - the length of the array A. - The next line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum cost to go from index 1 to index N using the given moves. If it is not possible to do so, output -1. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $0 ≀ A_{i} ≀ 5 \cdot 10^{5}$ - The sum of $N$ over all test cases won't exceed $5 \cdot 10^{5}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≀ M ≀ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 5 1 1 1 1 1 4 2 4 1 3 ----- Sample Output 1 ------ 1 3 ----- explanation 1 ------ - Test Case 1: Harasees has no choice but to keep going from his current position to the position that follows it immediately till he reaches the $5$th index. So the minimum cost (in this case, the only possible cost) is $1\ |\ 1\ |\ 1\ |\ 1\ |\ 1\ =\ 1$. - Test Case 2: It is optimal for Harasees to make the following moves: $(i = 1) \rightarrow (i = 3) \rightarrow (i = 4)$. The cost of this path is: $A_{1} \ | \ A_{3} \ | \ A_{4} = 2\ |\ 1\ |\ 3\ =\ 3$. We can prove that no path exists with a cost $< 3$. ----- Sample Input 2 ------ 1 5 1 0 2 3 3 ----- Sample Output 2 ------ -1 ----- explanation 2 ------ - Test Case 1: In this case Harasees will get stuck at index $2$ and there is no way to reach the last index.
def works(vec, mask): if vec[-1] & mask != vec[-1]: return False mx = 0 for i, v in zip(range(len(vec)), vec): if i > mx: break if v & mask == v and v + i > mx: mx = v + i return mx >= len(vec) - 1 t = int(input()) for _ in range(t): __ = input() vec = [int(x) for x in input().split()] if not works(vec, (1 << 20) - 1): print(-1) else: ans = 0 for bit in range(19, -1, -1): if not works(vec, ans | (1 << bit) - 1): ans |= 1 << bit print(ans)
FUNC_DEF IF BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Harasees has an array A of size N containing non-negative integers. He starts at the index 1 of A and wants to reach the index N. In one move, he can do the following: If he is currently at index i, then he can move to any index j such that i ≀ j ≀ \min(i + A_{i}, N). The cost of a path is defined as the [bitwise OR] of all the array elements visited by Harasees during his journey from index 1 to N. Formally if he visited the indices 1, X_{1}, X_{2}, \ldots, X_{K}, N, then the cost of this path = (A_{1} \ | \ A_{X_{1}} \ | \ A_{X_{2}} \ | \ \ldots \ | \ A_{X_{K}} \ | \ A_{N}). Determine the minimum possible cost to reach index N from index 1. If it is not possible to do so, output -1. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains a single integer N - the length of the array A. - The next line contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum cost to go from index 1 to index N using the given moves. If it is not possible to do so, output -1. ------ Constraints ------ $1 ≀ T ≀ 1000$ $1 ≀ N ≀ 5 \cdot 10^{5}$ $0 ≀ A_{i} ≀ 5 \cdot 10^{5}$ - The sum of $N$ over all test cases won't exceed $5 \cdot 10^{5}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≀ M ≀ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 5 1 1 1 1 1 4 2 4 1 3 ----- Sample Output 1 ------ 1 3 ----- explanation 1 ------ - Test Case 1: Harasees has no choice but to keep going from his current position to the position that follows it immediately till he reaches the $5$th index. So the minimum cost (in this case, the only possible cost) is $1\ |\ 1\ |\ 1\ |\ 1\ |\ 1\ =\ 1$. - Test Case 2: It is optimal for Harasees to make the following moves: $(i = 1) \rightarrow (i = 3) \rightarrow (i = 4)$. The cost of this path is: $A_{1} \ | \ A_{3} \ | \ A_{4} = 2\ |\ 1\ |\ 3\ =\ 3$. We can prove that no path exists with a cost $< 3$. ----- Sample Input 2 ------ 1 5 1 0 2 3 3 ----- Sample Output 2 ------ -1 ----- explanation 2 ------ - Test Case 1: In this case Harasees will get stuck at index $2$ and there is no way to reach the last index.
from sys import stdin input = stdin.readline BIT = 20 def solve(N, A): if N == 1: return A[0] maximum = 1 i = 1 while i <= maximum <= N: maximum = max(maximum, i + A[i]) i += 1 if maximum < N: return -1 answer = 0 for b in reversed(range(BIT + 1)): mask = answer + (1 << b) - 1 maximum = 1 if A[1] & mask == A[1] and A[N] & mask == A[N]: i = 1 while i <= maximum and i <= N: if A[i] & mask == A[i]: maximum = max(maximum, i + A[i]) i += 1 if maximum < N: answer += 1 << b return answer T = int(input().strip()) for problem in range(1, T + 1): N = int(input().strip()) A = [None] + [int(x) for x in input().strip().split()] print(solve(N, A))
ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
def sz(n): if n == 0: return -1 else: count = 0 while n != 1: count += 1 n = int(n / 2) return count for _ in range(int(input())): A, B, N = [int(x) for x in input().split()] if A == 0 and B == 0: print(0) continue sza = sz(A) + 1 szb = sz(B) + 1 mx = max(sza, szb) a = [(0) for _ in range(mx)] for i in reversed(range(sza)): if A - pow(2, i) >= 0: a[i] = 1 A -= pow(2, i) b = [(0) for _ in range(mx)] for i in reversed(range(szb)): if B - pow(2, i) >= 0: b[i] = 1 B -= pow(2, i) mx1, sm = -1, 0 for i in range(mx): if a[i] != b[i]: sm += pow(2, i) mx1 = i if mx1 == -1: print(0) elif N <= pow(2, mx1): print(-1) elif N > pow(2, mx1) and N <= sm: print(2) else: print(1)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
t = int(input()) for _ in range(t): a, b, n = map(int, input().split()) if a == b: print(0) else: z = 1 while z < n: z *= 2 if a ^ b < n: print(1) elif a ^ b < z: print(2) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
for _ in range(int(input())): a, b, n = (int(i) for i in input().split()) c = a ^ b if a == b: print(0) continue if c < n: print(1) elif 2 ** (len(bin(c)) - 3) < n: print(2) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
t = int(input()) def fun(a, b, n): diff = bin(b ^ a)[2:] pr = pow(2, len(diff) - 1) if n >= pr: vl = a ^ b if vl <= n: return 1 else: return 2 else: return -1 for _ in range(t): a, b, n = map(int, input().split()) dif = b - a if dif == 0: print(0) else: x = fun(a, b, n - 1) print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
ans = [] for _ in range(int(input())): A, B, N = [int(x) for x in input().split()] AexorB = A ^ B binAexorB, binN = bin(AexorB)[2:], bin(N - 1)[2:] if A == B: an = 0 elif AexorB < N: an = 1 elif N != 1 and len(binAexorB) == len(binN): an = 2 else: an = -1 ans.append(an) print(*ans, sep="\n")
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
def answer(a, b, n): if a == b: return 0 xor = a ^ b if xor < n: return 1 z = 1 while z < n: z *= 2 if xor < z: return 2 return -1 for T in range(int(input())): a, b, n = map(int, input().split()) print(answer(a, b, n))
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
T = int(input()) def algo(): x, y, N = map(int, input().split()) d = x ^ y if d == 0: print(0) elif d in range(1, N): print(1) elif 2 ** (len(bin(d)) - 3) >= N: print(-1) else: print(2) for i in range(T): algo()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
for _ in range(int(input())): a, b, n = list(map(int, input().split())) s = 1 while s < n: s *= 2 if a == b: print(0) elif a ^ b < n: print(1) elif a ^ b < s: print(2) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
t = int(input()) for i in range(0, t): abn = input() abn = abn.split(" ") a = int(abn[0]) b = int(abn[1]) n = int(abn[2]) ai = bin(a) ai = ai.replace("0b", "") bi = bin(b) bi = bi.replace("0b", "") mi = bin(n - 1) mi = mi.replace("0b", "") l = max(len(ai), len(bi), len(mi)) ai = (l - len(ai)) * "0" + ai bi = (l - len(bi)) * "0" + bi mi = (l - len(mi)) * "0" + mi x = "" for i2 in range(0, l): k = int(str(ai)[i2]) ^ int(str(bi)[i2]) x = x + str(k) tr = 0 for i3 in range(0, l): k2 = mi[i3], x[i3] if a == b: tr = 0 break if k2 == ("0", "0"): continue if k2 == ("1", "0"): tr = 1 break if k2 == ("1", "1"): tr = 1 continue if k2 == ("0", "1"): if tr == 0: tr = -1 break else: tr = 2 break print(tr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING 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 FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR STRING STRING IF VAR STRING STRING ASSIGN VAR NUMBER IF VAR STRING STRING ASSIGN VAR NUMBER IF VAR STRING STRING IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
for _ in range(int(input())): a, b, n = map(int, input().split()) if a == b: print(0) continue val = a ^ b if val < n: print(1) continue while val >= n: val = val - (val & -val) if val: print(2) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
for _ in range(int(input())): A, B, N = map(int, input().split()) X = A ^ B if X == 0: print(0) elif 1 <= X < N: print(1) elif N != 1 and len(str(bin(X))) == len(str(bin(N - 1))): print(2) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
def solve(a, b, n): if a == b: print(0) return x = a ^ b if x < n: print(1) elif 2 ** (len(bin(x)) - 3) < n: print(2) else: print(-1) t = int(input()) for _ in range(t): a, b, n = list(map(int, input().split())) solve(a, b, n)
FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
n = int(input()) while n: n -= 1 a, b, N = map(int, input().split()) if a == b: print(0) continue x = a ^ b b = len(bin(x)[2:]) - 1 if x < N: print(1) elif N <= x and N >= 2**b + 1: print(2) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
for t in range(int(input())): a, b, n = map(int, input().split()) if a == b: print(0) continue bina, binb = bin(a)[:1:-1], bin(b)[:1:-1] la, lb = len(bina), len(binb) diff = "" for i in range(min(la, lb)): if bina[i] == binb[i]: diff += "0" else: diff += "1" if la > lb: diff += bina[lb:] if la < lb: diff += binb[la:] diff = diff[::-1].lstrip("0") if int(diff, 2) < n: print(1) continue newdiff = diff[0] + "0" * (len(diff) - 1) if int(newdiff, 2) < n: print(2) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR STRING VAR STRING IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
T = int(input()) def check(N): l = len(bin(N)) - 2 temp = 2 ** (l - 1) if temp == N: return False return True for i in range(0, T): A, B, N = map(int, input().split()) C = A ^ B if A == B: print(0) elif C < N: print(1) elif C > N and len(bin(C)) == len(bin(N)) and check(N): print(2) elif C == N: if check(N): print(2) else: print(-1) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
def function(A, B, N): if A == B: return 0 val = A ^ B if val < N: return 1 else: c1 = 0 c2 = 0 N = N - 1 while val: if N: c1 += 1 N = N >> 1 c2 += 1 val = val >> 1 if c1 == c2: return 2 else: return -1 def main(): T = int(input()) for _ in range(T): A, B, N = list(map(int, input().split(" "))) val = function(A, B, N) print(val) main()
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
def op(a, b, n): arra = [(0) for i in range(30)] arrb = [(0) for i in range(30)] j = 29 while a > 0 or b > 0: arra[j] = a & 1 arrb[j] = b & 1 a >>= 1 b >>= 1 j -= 1 j = 29 val = 0 res = 0 countbits = 0 while j >= 0: if arra[j] == arrb[j]: val += pow(2, 29 - j) * 0 else: val += pow(2, 29 - j) * 1 countbits += 1 if val >= n: if countbits > 1: res += 1 countbits = 0 val = 0 continue else: return -1 j -= 1 if countbits > 0 and val < n: res += 1 return res case = int(input()) for i in range(case): a, b, n = map(int, input().split(" ")) ans = op(a, b, n) print(ans)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
def countbit(n): count = 0 while n > 0: n >>= 1 count += 1 return count T = int(input()) while T > 0: a, b, n = map(int, input().split()) t = a ^ b if t == 0: print(0) elif 0 < t < n: print(1) elif countbit(t) == countbit(n - 1): print(2) else: print(-1) T -= 1
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
ans = "" for _ in range(int(input())): a, b, n = list(map(int, input().split())) count = 0 poss = True if a == b: count = 0 elif a ^ b < n: count = 1 else: for i in range(31, -1, -1): if (a ^ b) & 1 << i >= n: poss = False break if not poss: ans += "-1\n" continue else: count = 2 ans += str(count) + "\n" print(ans)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
t = int(input()) def fun(a, b, n): if a == b: return 0 x = a ^ b if x < n: return 1 c = 0 while x > 0: x = x >> 1 c = c + 1 if 2 ** (c - 1) < n: return 2 return -1 while t > 0: a, b, n = [int(i) for i in input().split()] print(fun(a, b, n)) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
t = int(input()) for i in range(t): li = list(map(int, input().split())) a = li[0] b = li[1] n = li[2] n -= 1 if a == b: print(0) continue q = a ^ b temp = q res = 0 while temp: if res: res *= 2 else: res = 1 temp = temp >> 1 if n < res: print(-1) elif n >= q: print(1) else: print(2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
t = int(input()) while t: a, b, n = map(int, input().split(" ")) num = 0 count = 0 flag = False for i in range(29, -1, -1): val = 1 << i if a & val and not b & val: if val >= n: print(-1) break else: if num + val >= n: count += 1 a ^= num num = val else: num += val flag = True else: for i in range(30): val = 1 << i if b & val and not a & val: if val >= n: print(-1) break elif num + val >= n: count += 1 a ^= num num = val else: num += val flag = True else: if flag: count += 1 print(count) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
JJ has three integers A, B, and N. He can apply the following operation on A: Select an integer X such that 1 ≀ X < N and set A := A \oplus X. (Here, \oplus denotes the [bitwise XOR operation].) JJ wants to make A equal to B. Determine the minimum number of operations required to do so. Print -1 if it is not possible. ------ Input Format ------ - The first line contains a single integer T β€” the number of test cases. Then the test cases follow. - The first and only line of each test case contains three integers A, B, and N β€” the parameters mentioned in the statement. ------ Output Format ------ For each test case, output the minimum number of operations required to make A equal to B. Output -1 if it is not possible to do so. ------ Constraints ------ $1 ≀ T ≀ 1000$ $0 ≀ A, B < 2^{30}$ $1 ≀ N ≀2^{30}$ ------ subtasks ------ Subtask 1 (30 points): $N$ is a power of $2$. Subtask 2 (70 points): Original Constraints. ----- Sample Input 1 ------ 3 5 5 2 3 7 8 8 11 1 ----- Sample Output 1 ------ 0 1 -1 ----- explanation 1 ------ - Test Case $1$: $A$ is already equal to $B$. Hence we do not need to perform any operation. - Test Case $2$: We can perform the operation with $X = 4$ which is $< 8$. Therefore $A = 3 \oplus 4 = 7$. Thus, only one operation is required. - Test Case $3$: We can show that it is not possible to make $A$ equal to $B$. ----- Sample Input 2 ------ 2 24 27 3 4 5 1000 ----- Sample Output 2 ------ 2 1 ----- explanation 2 ------ Note that the above sample case belongs to subtask $2$. - Test Case 1: We can first perform the operation with $X = 1$ which is $< 3$. Therefore $A = 24 \oplus 1 = 25$. Then we can perform the operation with $X = 2$ which is $< 3$. Therefore $A = 25 \oplus 2 = 27$. Therefore we can make $A$ equal to $B$ in $2$ operations. It can be shown that it is not possible to do this in less than $2$ operations.
t = int(input()) while t: li = [int(x) for x in input().split()] a = li[0] b = li[1] n = li[2] c = a ^ b z = 1 while z < n: z *= 2 if c == 0: print(0) elif c < n: print(1) elif c < z: print(2) else: print(-1) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
n = int(input()) E = [[(int(x) - 1) for x in input().split()] for i in range(n - 1)] G = [[] for i in range(n)] for u, v in E: G[u].append(v) G[v].append(u) root = next(i for i in range(n) if len(G[i]) > 1) pa = [None] * n d = [None] * n useless = [0] * n q = [(root, 0, root)] while q: parent, depth, i = q.pop() pa[i] = parent d[i] = depth G[i].sort(key=lambda j: len(G[j])) for j in G[i]: if pa[j] == None: q.append((i, depth + 1, j)) if len(G[j]) == 1 and j != G[i][0]: useless[j] = 1 leaves = [i for i in range(n) if len(G[i]) == 1] ans_min = 1 if len(set(d[i] % 2 for i in leaves)) == 1 else 3 ans_max = n - 1 - sum(useless) print(ans_min, ans_max)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR NUMBER VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
n = int(input()) graph = [[] for i in range(n + 1)] deg = [0] * (n + 1) for i in range(n - 1): x, y = map(int, input().split()) graph[x].append(y) graph[y].append(x) deg[x] += 1 deg[y] += 1 root = -1 leavs = [] for i in range(n + 1): if deg[i] == 1: root = i break for i in range(n + 1): if deg[i] == 1: leavs.append(i) stack = [] depth = [0] * (n + 1) stack.append(root) visi = set() visi.add(root) cnt = 0 gg = 0 nod = graph[root][0] while len(stack) != 0: x = stack.pop() fn = 0 for nodes in graph[x]: if nodes not in visi: stack.append(nodes) visi.add(nodes) depth[nodes] = depth[x] + 1 if deg[nodes] == 1: fn = 1 if fn: cnt += 1 if x == nod and fn == 1: gg = 1 fl = 0 for i in leavs: if depth[i] % 2: fl = 1 if fl: print(3, end=" ") else: print(1, end=" ") if gg == 0: cnt += 1 print(n + cnt - 1 - len(leavs))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
n = int(input()) g = [[] for i in range(n)] for i in range(n - 1): a, b = [(int(s) - 1) for s in input().split()] g[a].append(b) g[b].append(a) ls = [] for i in range(n): if len(g[i]) == 1: ls.append(i) maxans = n - 1 ls_set = set(ls) for i in range(n): ints = len(ls_set.intersection(g[i])) maxans -= max(ints - 1, 0) minans = 1 vd = set() st = [(ls[0], 0)] while len(st) > 0: v, d = st.pop() vd.add(v) if len(g[v]) == 1 and d % 2 == 1: minans = 3 break for w in g[v]: if w not in vd: st.append((w, d + 1)) print(minans, maxans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys n = int(sys.stdin.readline().strip()) N = [[] for i in range(0, n)] C = [(-1) for i in range(0, n)] for i in range(0, n - 1): a, b = list(map(int, sys.stdin.readline().strip().split())) a, b = a - 1, b - 1 N[a].append(b) N[b].append(a) C[0] = 0 L0 = [0] L1 = [] while len(L0) + len(L1) > 0: for i in L0: for j in N[i]: if C[j] == -1: C[j] = 1 L1.append(j) L0 = [] for i in L1: for j in N[i]: if C[j] == -1: C[j] = 0 L0.append(j) L1 = [] f = 1 F = n - 1 L = [] M = [] for i in range(0, n): if len(N[i]) == 1: L.append(N[i]) M.append(i) L.sort() for i in range(0, len(L) - 1): if L[i] == L[i + 1]: F = F - 1 if C[M[i]] != C[M[i + 1]]: f = 3 print(f, F)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST WHILE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys readline = sys.stdin.readline def parorder(Edge, p): N = len(Edge) par = [0] * N par[p] = -1 stack = [p] order = [] visited = set([p]) ast = stack.append apo = order.append while stack: vn = stack.pop() apo(vn) for vf in Edge[vn]: if vf in visited: continue visited.add(vf) par[vf] = vn ast(vf) return par, order def getcld(p): res = [[] for _ in range(len(p))] for i, v in enumerate(p[1:], 1): res[v].append(i) return res N = int(readline()) Edge = [[] for _ in range(N)] for _ in range(N - 1): a, b = map(int, readline().split()) a -= 1 b -= 1 Edge[a].append(b) Edge[b].append(a) Leaf = [i for i in range(N) if len(Edge[i]) == 1] sLeaf = set(Leaf) for i in range(N): if i not in sLeaf: root = i break P, L = parorder(Edge, root) dp = [0] * N used = set([root]) stack = [root] while stack: vn = stack.pop() for vf in Edge[vn]: if vf not in used: used.add(vf) stack.append(vf) dp[vf] = 1 - dp[vn] if len(set([dp[i] for i in Leaf])) == 1: mini = 1 else: mini = 3 k = set() for l in Leaf: k.add(P[l]) maxi = N - 1 - len(Leaf) + len(k) print(mini, maxi)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys n = int(input()) adj = [[] for _ in range(n)] for _ in range(n - 1): a, b = [int(x) for x in sys.stdin.readline().split()] a -= 1 b -= 1 adj[a].append(b) adj[b].append(a) mx = n - 1 for i in range(n): num_adj_leaves = sum([(1) for b in adj[i] if len(adj[b]) == 1]) if num_adj_leaves: mx -= num_adj_leaves - 1 root = 0 parent = [None] * n parent[0] = -1 stack = [0] toposort = [] while len(stack): cur = stack.pop() toposort.append(cur) for nb in adj[cur]: if parent[nb] == None: parent[nb] = cur stack.append(nb) toposort = list(reversed(toposort)) has_even_from_leaf = [False] * n has_odd_from_leaf = [False] * n for v in toposort: if len(adj[v]) == 1: has_even_from_leaf[v] = True if parent[v] != -1: has_even_from_leaf[parent[v]] |= has_odd_from_leaf[v] has_odd_from_leaf[parent[v]] |= has_even_from_leaf[v] mn = 1 if any(has_even_from_leaf[v] and has_odd_from_leaf[v] for v in range(n)): mn += 2 print(mn, mx)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
n = int(input()) G = {} for i in range(n - 1): a, b = map(int, input().split()) try: G[a].append(b) except: G[a] = [b] try: G[b].append(a) except: G[b] = [a] leaves = [] for i in G.keys(): if len(G[i]) == 1: leaves.append(i) else: sv = i val = [(-1) for i in range(n + 1)] Q = [sv] val[sv] = 0 tp = 0 while len(Q) != 0: a = Q.pop() iv = val[a] tc = -1 for i in G[a]: if val[i] == -1: val[i] = iv + 1 if len(G[i]) == 1: tc = tc + 1 else: Q.append(i) tp = tp + max(tc, 0) v = val[leaves[0]] flag = 0 for i in range(1, len(leaves)): if abs(val[leaves[i]] - v) % 2 != 0: flag = 1 break if flag == 0: print(1, end=" ") else: print(3, end=" ") print(n - tp - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
n = int(input()) g = [[] for i in range(n)] for i in range(n - 1): u, v = [(int(i) - 1) for i in input().split()] g[u].append(v) g[v].append(u) leaf = [(len(i) == 1) for i in g] root = -1 mx = n - 1 for i in range(n): if leaf[i]: root = i leafs = 0 for j in g[i]: if leaf[j]: leafs += 1 if leafs > 1: mx -= leafs - 1 stack = [(root, -1, 0)] even = True while len(stack) > 0: i, j, d = stack.pop() if leaf[i] and d % 2 == 1: even = False break for k in g[i]: if k != j: stack.append((k, i, d + 1)) mn = 1 if even else 3 print(mn, mx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
n = int(input()) l = [[] for i in range(n)] for i in range(n - 1): a, b = map(int, input().split()) a -= 1 b -= 1 l[a].append(b) l[b].append(a) maxans = n - 1 v = [0] * n leaf = [] for i in range(len(l)): k = l[i] if len(k) == 1: if v[k[0]] == 1: maxans -= 1 else: v[k[0]] = 1 leaf.append(i) q = [0] visited = [-1] * n visited[0] = 0 while q: t = q.pop() for i in l[t]: if visited[i] == -1: q.append(i) visited[i] = 1 - visited[t] k = visited[leaf[0]] for i in leaf: if visited[i] != k: minans = 3 break else: minans = 1 print(minans, maxans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys sys.setrecursionlimit(10**6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def MI1(): return map(int1, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): def findmin(): p = dep[leaf[0]] & 1 for u in leaf[1:]: if dep[u] & 1 != p: return 3 return 1 n = II() to = [[] for _ in range(n)] for _ in range(n - 1): u, v = MI1() to[u].append(v) to[v].append(u) dep = [-1] * n dep[0] = 0 pp = [-1] * n stack = [0] while stack: u = stack.pop() for v in to[u]: if v == pp[u]: continue pp[v] = u dep[v] = dep[u] + 1 stack.append(v) leaf = [] for u in range(n): if len(to[u]) == 1: leaf.append(u) mn = findmin() st = set() cnt = 0 for u in leaf: p = to[u][0] if p in st: cnt += 1 else: st.add(p) mx = n - 1 - cnt print(mn, mx) main()
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
n = int(input()) gs = [[] for _ in range(n)] for _ in range(n - 1): u, v = map(int, input().split()) u -= 1 v -= 1 gs[u].append(v) gs[v].append(u) for root, u in enumerate(gs): if len(u) > 1: break leaves = [] q = [root] ds = [None] * n ds[root] = 0 par = [None] * n while q: u = q.pop() if len(gs[u]) == 1: leaves.append(u) for v in gs[u]: if ds[v] is not None: continue q.append(v) ds[v] = ds[u] + 1 par[v] = u dodd = False deven = False pars = [0] * n for leave in leaves: if ds[leave] % 2: dodd = True else: deven = True pars[par[leave]] += 1 samepar = 0 for p in pars: if not p: continue samepar += p - 1 ansmin = 3 if dodd and deven else 1 ansmax = n - 1 - samepar print(ansmin, ansmax)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys input = sys.stdin.readline n = int(input()) E = [[] for i in range(n + 1)] for i in range(n - 1): a, b = list(map(int, input().split())) E[a].append(b) E[b].append(a) LEAF = [] for i in range(n + 1): if len(E[i]) == 1: LEAF.append(i) Q = [1] USE = [-1] * (n + 1) USE[1] = 0 while Q: x = Q.pop() for to in E[x]: if USE[to] == -1: USE[to] = 1 - USE[x] Q.append(to) f = USE[LEAF[0]] for l in LEAF: if f != USE[l]: MIN = 3 break else: MIN = 1 MAX = n - 1 FP = [0] * (n + 1) for l in LEAF: for to in E[l]: if FP[to] == 1: MAX -= 1 else: FP[to] = 1 print(MIN, MAX)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
n = int(input()) dic = {} dicle = {} for i in range(n): dic[i + 1] = [] dicle[i + 1] = 0 for i in range(n - 1): x, y = map(int, input().split()) dic[x].append(y) dic[y].append(x) dicle[x] += 1 dicle[y] += 1 root = 0 leafcount = 0 se = set({}) for i in dicle: if dicle[i] == 1: leafcount += 1 root = i se.add(dic[i][0]) maxans = n - 1 - leafcount + len(se) flag = True se = {root} tem = {root} dis = [0] * (n + 1) level = 0 while tem: level += 1 tem1 = set({}) for i in tem: xx = dic[i] for j in xx: if not j in se: if level % 2 != 0 and dicle[j] == 1: flag = False se.add(j) tem1.add(j) dis[j] = level tem = tem1.copy() if flag: print(1, maxans) else: print(3, maxans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR DICT FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
I = lambda: list(map(int, input().split())) (n,) = I() g = [[] for i in range(n + 1)] for i in range(n - 1): x, y = I() g[x].append(y) g[y].append(x) leaf = [] for i in range(1, n + 1): if len(g[i]) == 1: leaf.append(i) st = [1] visi = [-1] * (n + 1) visi[1] = 0 while st: x = st.pop() for y in g[x]: if visi[y] == -1: visi[y] = 1 - visi[x] st.append(y) ch = visi[leaf[0]] fl = 1 temp = 0 for i in leaf: if visi[i] != ch: fl = 0 mi = 1 if not fl: mi = 3 an = [0] * (n + 1) ma = n - 1 for i in leaf: for j in g[i]: if an[j] == 1: ma -= 1 else: an[j] = 1 print(mi, ma)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
def solveAll(): case = readCase() print(*solve(case)) def readCase(): treeSize = int(input()) graph = [[] for i in range(treeSize)] for _ in range(1, treeSize): a, b = (int(x) for x in input().split()) a -= 1 b -= 1 graph[a] += [b] graph[b] += [a] return graph def solve(graph): leafs = computeLeafs(graph) return minF(graph, leafs), maxF(graph, leafs) def computeLeafs(graph): leafs = [node for node in range(0, len(graph)) if len(graph[node]) == 1] return leafs def minF(graph, leafs): color = [None] * len(graph) queue = [0] color[0] = "a" head = 0 while head < len(queue): currentNode = queue[head] head += 1 for neighbor in graph[currentNode]: if color[neighbor] != None: continue color[neighbor] = "b" if color[currentNode] == "a" else "a" queue += [neighbor] if all(color[leafs[0]] == color[leaf] for leaf in leafs): return 1 return 3 def maxF(graph, leafs): group = [0] * len(graph) for leaf in leafs: parent = graph[leaf][0] group[parent] += 1 ans = len(graph) - 1 for size in group: if size >= 2: ans -= size - 1 return ans solveAll()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR LIST VAR VAR VAR LIST VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR VAR STRING STRING STRING VAR LIST VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys n, *ab = map(int, sys.stdin.read().split()) graph = [[] for _ in range(n)] for a, b in zip(*([iter(ab)] * 2)): a -= 1 b -= 1 graph[a].append(b) graph[b].append(a) def main(): rank = [None] * n rank[0] = 0 parent = [None] * n leaves = [] stack = [0] while stack: u = stack.pop() if not u == 0 and len(graph[u]) == 1: leaves.append(u) continue for v in graph[u]: if v == parent[u]: continue parent[v] = u rank[v] = rank[u] + 1 stack.append(v) if len(graph[0]) == 1: leaves.append(0) parent[0] = graph[0][0] parent[graph[0][0]] = None pars = set() for leaf in leaves: pars.add(parent[leaf]) ma = n - 1 - (len(leaves) - len(pars)) rs = set([(rank[leaf] & 1) for leaf in leaves]) mi = 1 if len(rs) == 1 else 3 print(mi, ma) main()
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR BIN_OP LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NONE ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
from sys import stdin input = stdin.readline class N: def __init__(self, v) -> None: self.v = v self.c = set() self.l = False n = int(input()) arr = [N(i) for i in range(n)] es = set() for _ in range(n - 1): x, y = map(int, input().split()) x -= 1 y -= 1 x, y = min(x, y), max(x, y) es.add((x, y)) arr[x].c.add(arr[y]) arr[y].c.add(arr[x]) ps = set() for x in arr: if len(x.c) == 1: x.l = True ps.add(list(x.c)[0]) for p in ps: kept = False rs = [] for c in p.c: if c.l: if kept: x = p.v y = c.v x, y = min(x, y), max(x, y) es.discard((x, y)) rs.append(c) else: kept = True for c in rs: p.c.discard(c) root = ps.pop() stk = [(root, 0)] viz = set() ls = [] while stk: root, l = stk.pop() viz.add(root) for c in root.c: if c not in viz: if c.l: ls.append(l + 1) else: stk.append((c, l + 1)) if len(es) > 1: print(1 + 2 * int(len(set([(l % 2) for l in ls])) == 2), len(es)) else: print(1, 1)
ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys sys.setrecursionlimit(10**6) n = int(input().strip()) conns = [set() for i in range(n + 1)] for i in range(n - 1): a, b = [int(d) for d in input().strip().split()] conns[a].add(b) conns[b].add(a) even = False odd = False for i in range(1, n + 1): if len(conns[i]) > 1: first = i break q = [(first, -1, 0)] index = 0 while index < len(q): node, parent, dist = q[index] index += 1 if len(conns[node]) == 1: if dist % 2 == 0: even = True else: odd = True for c in conns[node]: if c != parent: q += [(c, node, dist + 1)] num_leaves = [(0) for i in range(n + 1)] for i in range(1, n + 1): if len(conns[i]) == 1: for c in conns[i]: num_leaves[c] += 1 ma = n - 1 for val in num_leaves: if val > 0: ma -= val - 1 if even and odd: mi = 3 else: mi = 1 print(mi, ma)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
n = int(input()) g = [[] for i in range(n)] L = [-1] * n for i in range(n - 1): a, b = map(int, input().split(" ")) a -= 1 b -= 1 g[a] += [b] g[b] += [a] L[0] = 0 q = [0] i = 0 odd = 0 even = 0 x = 0 while i < len(q): a = q[i] if len(g[a]) == 1: if L[a]: odd += 1 else: even += 1 i += 1 for b in g[a]: if L[b] != -1: continue L[b] = 1 - L[a] q += [b] for a in range(n): s = 0 for b in g[a]: if len(g[b]) == 1: s += 1 if s: x += s - 1 print(1 + 2 * (odd * even > 0), n - x - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys sys.setrecursionlimit(11000) _INPUT_LINES = sys.stdin.read().splitlines() input = iter(_INPUT_LINES).__next__ def go(): n = int(input()) if n == 2: return "1 1" e = {i: set() for i in range(1, n + 1)} rank = [0] * (n + 1) root = -1 for _ in range(n - 1): a, b = map(int, input().split()) e[a].add(b) e[b].add(a) rank[a] += 1 rank[b] += 1 if rank[a] > 1: root = a if rank[b] > 1: root = b mx = n - 1 for a in range(1, n + 1): cnt = sum(rank[b] == 1 for b in e[a]) mx -= max(0, cnt - 1) ev, od = False, False que = [root] i = 0 dep = [0] * (n + 1) done = {root} while i < len(que): v = que[i] if len(e[v]) == 1: if dep[v] % 2 == 0: ev = True if dep[v] % 2 == 1: od = True for w in e[v]: if w not in done: que.append(w) dep[w] = dep[v] + 1 done.add(w) i += 1 if ev and od: mn = 3 else: mn = 1 return f"{mn} {mx}" t = 1 ans = [] for _ in range(t): ans.append(str(go())) print("\n".join(ans))
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys lines = sys.stdin.readlines() n = int(lines[0].strip()) edges = {} for i in range(1, n): u, v = map(int, lines[i].strip().split(" ")) if u not in edges: edges[u] = [] if v not in edges: edges[v] = [] edges[u].append(v) edges[v].append(u) leaves = [] parLeave = {} for u in edges.keys(): if len(edges[u]) == 1: leaves.append(u) parLeave[u] = edges[u][0] maxF = n - (len(leaves) - len(set(parLeave.values()))) - 1 parity = {leaves[0]: 0} stack = [leaves[0]] seen = set(stack) while stack: node = stack.pop() for adj in edges[node]: if adj not in seen: parity[adj] = 1 - parity[node] stack.append(adj) seen.add(adj) parityOfLeaves = list(map(lambda x: parity[x], leaves)) if min(parityOfLeaves) == max(parityOfLeaves): minF = 1 else: minF = 3 print("{} {}".format(minF, maxF))
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
from sys import stderr, stdin def rl(): return [int(w) for w in stdin.readline().split()] (n,) = rl() a = [[] for _ in range(n)] for _ in range(n - 1): ai, bi = rl() a[ai - 1].append(bi - 1) a[bi - 1].append(ai - 1) for v in range(n): if len(a[v]) == 1: f = [(v, a[v][0])] break d = 2 f0 = 1 f1 = 0 while f: nf = [] for u, v in f: df1 = 1 for w in a[v]: if w != u: if len(a[w]) > 1: nf.append((v, w)) elif d > 2: df1 = 2 if d % 2: f0 = 3 f1 += df1 f = nf d += 1 print(f0, f1)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys input = sys.stdin.readline def DFS(i): visited = {i: True} stack = [(i, 0)] while len(stack) != 0: tail, depth = stack.pop(-1) flag = True for each in neigh[tail]: if each not in visited: visited[each] = True flag = False stack.append((each, depth + 1)) if flag: leafDepth.append(depth) n = int(input()) neigh = [[] for i in range(n)] l = [] for i in range(n - 1): a, b = map(int, input().split()) a -= 1 b -= 1 neigh[a].append(b) neigh[b].append(a) l.append((a, b)) edges = set() for a, b in l: if len(neigh[a]) == 1: a = -1 if len(neigh[b]) == 1: b = -1 if a > b: a, b = b, a edges.add((a, b)) MAX = len(edges) leafDepth = [] DFS(0) if len(neigh[0]) == 1: MIN = 1 if all([(True if i % 2 == 0 else False) for i in leafDepth]) else 3 else: MIN = ( 1 if all([(True if i % 2 == 0 else False) for i in leafDepth]) or all([(True if i % 2 == 1 else False) for i in leafDepth]) else 3 ) print(MIN, MAX)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR DICT VAR NUMBER ASSIGN VAR LIST VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
size = int(input()) graph = [[] for i in range(size)] for _ in range(1, size): a, b = (int(x) for x in input().split()) a -= 1 b -= 1 graph[a] += [b] graph[b] += [a] leafs = [node for node in range(0, len(graph)) if len(graph[node]) == 1] color = [None] * len(graph) queue = [0] color[0] = "a" head = 0 while head < len(queue): currentNode = queue[head] head += 1 for neighbor in graph[currentNode]: if color[neighbor] != None: continue color[neighbor] = "b" if color[currentNode] == "a" else "a" queue += [neighbor] if all(color[leafs[0]] == color[leaf] for leaf in leafs): minF = 1 else: minF = 3 group = [0] * len(graph) for leaf in leafs: parent = graph[leaf][0] group[parent] += 1 maxF = len(graph) - 1 for size in group: if size >= 2: maxF -= size - 1 print(str(minF) + " " + str(maxF))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR VAR STRING STRING STRING VAR LIST VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
from sys import stdin, stdout def main(): n = int(stdin.readline()) g = [[] for _ in range(n + 1)] k = [[] for _ in range(n + 1)] already = [0] * (n + 1) already1 = [0] * (n + 1) even = [False] * (n + 1) odd = [False] * (n + 1) for _ in range(n - 1): a, b = list(map(int, stdin.readline().split())) g[a].append(b) g[b].append(a) k[a].append(b) k[b].append(a) start = -1 for i in range(1, n + 1): if len(g[i]) > 1: start = i break stack = [start] mn = 1 mx = n - 1 while stack: x = stack[-1] if already[x] == 0: already[x] = -1 still_deep = False if len(g[x]) > 0: y = g[x][-1] if already[y] == 0: stack.append(y) g[x].pop() still_deep = True continue else: g[x].pop() continue if still_deep is True: continue stack.pop() if already[x] == -1 and len(k[x]) == 1: already[x] = 1 already1[stack[-1]] += 1 odd[stack[-1]] = True continue if odd[x] == even[x]: mn = 3 if already1[x] > 0: mx -= already1[x] - 1 already[x] = 1 if stack: y = stack[-1] if odd[x] is True: even[y] = True if even[x] is True: odd[y] = True stdout.write(str(mn) + " " + str(mx)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR 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 NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You have unweighted tree of $n$ vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves $v_{1}$ and $v_{2}$ of this tree, bitwise XOR of weights of all edges on the simple path between $v_{1}$ and $v_{2}$ has to be equal to $0$. Note that you can put very large positive integers (like $10^{(10^{10})}$). It's guaranteed that such assignment always exists under given constraints. Now let's define $f$ as the number of distinct weights in assignment. [Image] In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $0$. $f$ value is $2$ here, because there are $2$ distinct edge weights($4$ and $5$). [Image] In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $1$ and vertex $6$ ($3, 4, 5, 4$) is not $0$. What are the minimum and the maximum possible values of $f$ for the given tree? Find and print both. -----Input----- The first line contains integer $n$ ($3 \le n \le 10^{5}$)Β β€” the number of vertices in given tree. The $i$-th of the next $n-1$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le n$)Β β€” it means there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that given graph forms tree of $n$ vertices. -----Output----- Print two integersΒ β€” the minimum and maximum possible value of $f$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints. -----Examples----- Input 6 1 3 2 3 3 4 4 5 5 6 Output 1 4 Input 6 1 3 2 3 3 4 4 5 4 6 Output 3 3 Input 7 1 2 2 7 3 4 4 7 5 6 6 7 Output 1 6 -----Note----- In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image] In the second example, possible assignments for each minimum and maximum are described in picture below. The $f$ value of valid assignment of this tree is always $3$. [Image] In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. [Image]
import sys input = sys.stdin.readline n = int(input()) edge = [[] for _ in range(n)] for _ in range(n - 1): u, v = map(int, input().split()) u -= 1 v -= 1 edge[u].append(v) edge[v].append(u) is_leaf = [False] * n for i in range(n): if len(edge[i]) == 1: is_leaf[i] = True else: start = i oe = [0, 0] def dfs(d, p, v): queue = [(d, p, v)] while queue: d, p, v = queue.pop() if is_leaf[v]: oe[d % 2] += 1 continue for nv in edge[v]: if nv == p: continue queue.append((d + 1, v, nv)) dfs(0, -1, start) if 0 in oe: minima = 1 else: minima = 3 dim = 0 for i in range(n): cnt = 0 for v in edge[i]: cnt += is_leaf[v] if cnt > 1: dim += cnt - 1 print(minima, n - 1 - dim)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST VAR VAR VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) state = [-1] * n bit = 1 << 29 while bit > 0: poss = True for j in range(n): if state[j] == 0 and not a[j] & bit: poss = False elif state[j] == 1 and not b[j] & bit: poss = False elif not a[j] & bit and not b[j] & bit: poss = False temp = bit bit >>= 1 if not poss: continue for i in range(n): if state[i] != -1: continue if not a[i] & temp: state[i] = 1 elif not b[i] & temp: state[i] = 0 ans = (1 << 30) - 1 flips = 0 for i in range(n): if state[i] == 1: flips += 1 ans &= b[i] else: ans &= a[i] print(str(ans) + " " + str(flips))
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
import sys input = sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = inp() while t: t -= 1 n = inp() a = lis() b = lis() bits = [0] * 30 flip = [0] * n fl = [[] for i in range(30)] for i in range(29, -1, -1): can = [] can1 = [] flag = 0 for j in range(n): if a[j] & 1 << i: if b[j] & 1 << i == 0: can1.append(j) continue if b[j] & 1 << i and flip[j] == 0: can.append(j) else: bits[i] = 1 flag = 1 break if flag == 0: for j in can1: flip[j] = 1 for j in can: flip[j] = 1 a[j], b[j] = b[j], a[j] fl[i].append(j) s = 0 vis = set() for i in range(30): if bits[i]: continue s += 2**i for j in fl[i]: vis.add(j) print(s, len(vis))
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
T = int(input()) def check(bit, setBits, array): for i in range(N): here = 0 if front[i] == float("inf") or front[i] & 1 << bit == 0: here = here + 1 if back[i] == float("inf") or back[i] & 1 << bit == 0: here = here + 1 if here == 2: return False for i in range(N): if front[i] != float("inf") and front[i] & 1 << bit == 0: front[i] = float("inf") if back[i] != float("inf") and back[i] & 1 << bit == 0: back[i] = float("inf") return True for _ in range(T): N = int(input()) front = list(map(int, input().split())) back = list(map(int, input().split())) array = [] bit = 32 ans = 0 setBits = set() flips = 0 array = [(0) for i in range(N)] while bit >= 0: value = check(bit, setBits, array) if value == True: ans = ans + 2**bit bit = bit - 1 count = 0 for i in range(len(front)): if front[i] == float("inf"): count = count + 1 print(ans, end=" ") print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING IF VAR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) st = [-1] * n ans = 0 for i in range(29, -1, -1): ps = 1 bit = 1 << i for i in range(n): if st[i] == 0 and a[i] & bit == 0: ps = 0 break elif st[i] == 1 and b[i] & bit == 0: ps = 0 break elif st[i] == -1 and b[i] & bit == 0 and a[i] & bit == 0: ps = 0 break if ps: ans += bit for i in range(n): if st[i] == 1 or st[i] == 0: continue elif a[i] & bit and b[i] & bit: continue elif a[i] & bit: st[i] = 0 else: st[i] = 1 print(ans, st.count(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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for _ in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] res = [(-1) for i in range(n)] bcount = 0 abin = [bin(i) for i in a] bbin = [bin(i) for i in b] for i in reversed(range(30)): currentrow = [(-1) for k in range(n)] usable = True for j in range(n): if res[j] == -1: aval = a[j] >> i & 1 bval = b[j] >> i & 1 if aval == 0 and bval == 0: usable = False break if aval == 1 and bval == 1: continue elif aval == 1: currentrow[j] = 0 else: currentrow[j] = 1 else: aval = a[j] >> i & 1 bval = b[j] >> i & 1 if res[j] == 0: if aval == 0: usable = False elif res[j] == 1: if bval == 0: usable = False if usable: for l in range(n): if res[l] == -1: res[l] = currentrow[l] start = 2**30 - 1 for i in range(n): if res[i] == 0 or res[i] == -1: start &= a[i] else: start &= b[i] bcount += 1 print(start, bcount)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
t = int(input()) while t > 0: n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 state = [-1] * n for i in range(30, -1, -1): bit = 2**i is_bit_set = True for j in range(n): if a[j] & bit == 0 and b[j] & bit == 0: is_bit_set = False if a[j] & bit == 0 and state[j] == 0: is_bit_set = False if b[j] & bit == 0 and state[j] == 1: is_bit_set = False if is_bit_set: ans += bit for j in range(n): if a[j] & bit == 0: state[j] = 1 if b[j] & bit == 0: state[j] = 0 flip = 0 for i in range(n): if state[i] == 1: flip += 1 print(ans, flip) t = t - 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
t = int(input()) for xyz in range(t): n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) m = max(max(A), max(B)) b = 0 while m > 1: m = m // 2 b += 1 ans = 0 flips = 0 while b >= 0: f = True for i in range(n): if (A[i] >> b & 1 == 0 or ans & A[i] != ans) and ( B[i] == None or B[i] >> b & 1 == 0 or ans & B[i] != ans ): f = False break if f: cur = 0 for i in range(n): if A[i] >> b & 1 == 0 and B[i] != None: A[i] = B[i] B[i] = None cur += 1 flips += cur ans += 1 << b b -= 1 print(ans, flips)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NONE BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NONE VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for tc in range(int(input())): n = int(input()) ls = list(map(int, input().split())) lk = list(map(int, input().split())) comb = [-1] * len(ls) res = [] for r in range(30, -1, -1): pos = True for x in range(len(ls)): if comb[x] == 0 and ls[x] >> r & 1 == 0: pos = False elif comb[x] == 1 and lk[x] >> r & 1 == 0: pos = False elif ls[x] >> r & 1 == 0 and lk[x] >> r & 1 == 0: pos = False if pos == False: continue for x in range(len(ls)): if comb[x] != -1: pass elif ls[x] >> r & 1 == 0: comb[x] = 1 elif lk[x] >> r & 1 == 0: comb[x] = 0 flips = 0 an = -1 for x in range(len(ls)): if comb[x] == 1: flips += 1 if an == -1: an = lk[x] else: an &= lk[x] elif an == -1: an = ls[x] else: an &= ls[x] print(an, flips)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) totaland = 2**32 flag = 0 overallflip = [] maxi = 0 while totaland > 0: localflip = [] j = 0 while j < n: x = a[j] & totaland y = b[j] & totaland if x == 0 and y == 0: break elif x == 0: localflip.append(1) elif y == 0: localflip.append(0) else: localflip.append(2) j += 1 if j == n: if overallflip == []: overallflip = localflip maxi = totaland else: k = 0 temp = [] while k < n: if localflip[k] == 2: temp.append(overallflip[k]) elif overallflip[k] != localflip[k] and overallflip[k] == 2: temp.append(localflip[k]) elif overallflip[k] == localflip[k]: temp.append(localflip[k]) k += 1 continue else: break k += 1 if k == n: overallflip = temp maxi += totaland totaland >>= 1 if overallflip == []: print(0, 0) else: print(maxi, overallflip.count(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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR IF VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
t = int(input()) def log(n): x = str(bin(n))[2:] return len(x) - 1 def check(n, m): x = str(bin(n))[2:] if m < len(x): return x[-m - 1] == "1" return False for _ in range(t): n = int(input()) f = list(map(int, input().split())) b = list(map(int, input().split())) mix = [[f[i], b[i]] for i in range(n)] m = log(max(f + b)) val = 0 flips = 0 ind = [] while m >= 0: av = True tempMix = mix[:] tempFlips = flips for i, val in enumerate(mix): if len(val) == 2: x = check(val[0], m) y = check(val[1], m) if not (x or y): av = False break elif x and not y: tempMix[i] = [val[0]] elif y and not x: tempFlips += 1 tempMix[i] = [val[1]] elif not check(val[0], m): av = False break if av: mix = tempMix flips = tempFlips ind.append(m) m -= 1 s = 0 for i in ind: s += 2**i print(s, flips)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER STRING RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
def gcd(a, b): if b == 0: return a return gcd(b, a % b) def check(arr, lex, size): j = 0 ans = 0 k = 0 rem = 0 while j < len(arr): if k == 0: rem = j if k < size: ans = gcd(ans, arr[j]) if ans < lex: j = rem + 1 ans = 0 k = 0 continue j += 1 k += 1 if k == size: return True else: if k == size and ans >= lex: return True return False def bsearch(a): k = 2 left = 1 right = len(a) ans = 0 while left <= right: mid = left + (right - left) // 2 if check(a, k, mid): ans = mid left = mid + 1 else: right = mid - 1 return ans mod = 10**9 + 7 for ii in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = max(max(a), max(b)) x = bin(c)[2:] m = len(x) arr = [2] * n ans = [0] * m count = 0 for j in range(m - 1, -1, -1): bit = pow(2, j) flag = True for i in range(n): if arr[i] == 2 and bit & (a[i] | b[i]) == 0: flag = False break if arr[i] == 1 and bit & a[i] == 0: flag = False break if arr[i] == 0 and bit & b[i] == 0: flag = False break if flag: count += bit for i in range(n): x = a[i] & bit y = b[i] & bit if arr[i] == 2 and x and y: continue if arr[i] == 2 and x: arr[i] = 1 if arr[i] == 2 and y: arr[i] = 0 c = 0 for i in range(n): if arr[i] == 0: c += 1 print(count, c)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 st = [-1] * n for k in range(29, -1, -1): poss = 1 for i in range(n): if st[i] == 0: if not 1 << k & a[i]: poss = 0 break elif st[i] == 1: if not 1 << k & b[i]: poss = 0 break elif not 1 << k & a[i] and not 1 << k & b[i]: poss = 0 break if not poss: continue else: ans += 1 << k for i in range(n): if st[i] != -1: continue elif not (a[i] & 1 << k and b[i] & 1 << k): if a[i] & 1 << k: st[i] = 0 else: st[i] = 1 print(ans, st.count(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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for _ in range(int(input())): N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) flip = [-1] * N for i in range(30, -1, -1): pos = True for j in range(N): if flip[j] == 0 and not A[j] & 1 << i: pos = False break elif flip[j] == 1 and not B[j] & 1 << i: pos = False break elif flip[j] == -1 and (not A[j] & 1 << i and not B[j] & 1 << i): pos = False break if not pos: continue for j in range(N): if flip[j] == -1: if A[j] & 1 << i and B[j] & 1 << i: continue elif A[j] & 1 << i: flip[j] = 0 elif B[j] & 1 << i: flip[j] = 1 count = 0 if flip[0] == 0 or flip[0] == -1: bit = A[0] else: bit = B[0] count += 1 for i in range(1, N): if flip[i] == 0 or flip[i] == -1: bit = A[i] & bit else: bit = B[i] & bit count += 1 print(bit, count)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for _ in range(int(input())): n = int(input()) ai = list(map(int, input().split())) bi = list(map(int, input().split())) fi = [1] * n for i in range(n): x = str(bin(ai[i])[2:]) ai[i] = "0" * (32 - len(x)) + x x = str(bin(bi[i])[2:]) bi[i] = "0" * (32 - len(x)) + x fl = 0 for i in range(32): f = 1 for j in range(n): if ai[j][i] != "1": if fi[j] == 1: if bi[j][i] != "1": f = 0 break else: f = 0 break if f == 1: for j in range(n): if ai[j][i] != "1": ai[j], bi[j] = bi[j], ai[j] fi[j] = 0 fl = fl + 1 elif bi[j][i] != "1": fi[j] = -1 an = int(ai[0], 2) for ele in ai: an = int(ele, 2) & an print(an, fl)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING IF VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) mx = max(max(a), max(b)) l = len(bin(mx).replace("0b", "")) count = [3] * n flag = False for i in range(l, -1, -1): if not flag: count = [3] * n temp = count[:] cnt = 0 for j in range(n): if count[j] == 1: if a[j] & 1 << i == 0: count[j] = 0 elif count[j] == 2: if b[j] & 1 << i == 0: count[j] = 0 elif count[j] == 3: if a[j] & 1 << i == 0 and b[j] & 1 << i == 0: count[j] = 0 elif a[j] & 1 << i == 0: count[j] = 2 elif b[j] & 1 << i == 0: count[j] = 1 if count[j] == 0: cnt += 1 if cnt == 0: flag = True elif flag: count = temp[:] ans = [] cnt = 0 for i in range(n): if count[i] == 1: ans.append(a[i]) elif count[i] == 2: ans.append(b[i]) cnt += 1 elif count[i] == 3: ans.append(a[i]) if len(ans) < n: print(0, 0) else: nd = ans[0] for i in ans: nd = nd & i print(nd, cnt)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
from sys import stdin, stdout mod = 1000000007 def srt(n, l, r): m = (l + r) // 2 if l > r: return m if m * m > n: r = m - 1 return srt(n, l, r) elif m * m < n: l = m + 1 return srt(n, l, r) else: return m for _ in range(int(stdin.readline())): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) siri = [] flips = 0 for i in range(32, -1, -1): shan = 1 << i count1 = 0 count2 = 0 mps = [] for j in range(n): if shan & a[j]: count1 += 1 elif shan & b[j]: count2 += 1 mps.append(j) else: break if count1 == n: siri.append(shan) elif count2 + count1 == n: pos = 1 for e in mps: v = 0 if len(siri) > 0: for q in siri: if q & b[e]: v += 1 if v != len(siri): pos = 0 break if pos: siri.append(shan) for e in mps: flips += 1 a[e], b[e] = b[e], a[e] sreeshanth = a[0] for i in range(1, n): sreeshanth &= a[i] print(sreeshanth, flips)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR RETURN 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) pos = [-1] * n x = 1 << 29 while x: poss = True for y in range(n): if pos[y] == 0 and not a[y] & x: poss = False elif pos[y] == 1 and not b[y] & x: poss = False elif not a[y] & x and not b[y] & x: poss = False for y in range(n): if poss: if not a[y] & x: pos[y] = 1 elif not b[y] & x: pos[y] = 0 x >>= 1 ans = 0 anf = (1 << 30) - 1 for x in range(n): if pos[x] == -1 or pos[x] == 0: anf = anf & a[x] elif pos[x] == 1: anf &= b[x] ans += pos[x] == 1 print(anf, ans)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for _ in range(int(input())): N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) flip = set() band = 0 mbits = 0 for i in range(N): mbits = max(mbits, len(bin(A[i])[2:]), len(bin(B[i])[2:])) for bit in range(mbits, 0, -1): tbt = 2 ** (bit - 1) rflip = set() aflip = set() for i in range(N): if i in flip: if B[i] & tbt == 0: if A[i] & tbt != 0 and (band == 0 or A[i] & band == band): rflip.add(i) else: break elif A[i] & tbt == 0: if B[i] & tbt != 0 and (band == 0 or B[i] & band == band): aflip.add(i) else: break else: band += tbt for i in aflip: flip.add(i) for i in rflip: flip.remove(i) print(str(band) + " " + str(len(flip)))
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
t = int(input()) for z in range(t): n = int(input()) state = [-1] * n a = list(map(int, input().split())) b = list(map(int, input().split())) x = 1 << 29 while x > 0: chng = True x = x // 2 for i in range(n): if state[i] == 0 and not a[i] & x: chng = False elif state[i] == 1 and not b[i] & x: chng = False elif not a[i] & x and not b[i] & x: chng = False if not chng: continue for i in range(n): if state[i] != -1: continue if not a[i] & x: state[i] = 1 elif not b[i] & x: state[i] = 0 ans = (1 << 30) - 1 count = 0 for i in range(n): if state[i] == 1: count += 1 ans &= b[i] else: ans &= a[i] print(ans, count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for tcase in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) p2, flip, canflip = 2**29, [0] * n, [True] * n result = 0 while p2 > 0: i, x = 0, p2 while x == p2 and i < n: if canflip[i]: x = p2 & (a[i] | b[i]) else: x = p2 & a[i] i += 1 if x == p2: result += p2 for i in range(n): canflip[i] = canflip[i] and p2 & b[i] == p2 if canflip[i] and p2 & a[i] == 0: a[i], b[i] = b[i], a[i] flip[i] = 1 canflip[i] = False p2 //= 2 print(result, sum(flip))
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
INF = pow(2, 31) - 1 for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) state = [-1] * n for i in range(31, -1, -1): temp = 1 << i possible = True for i in range(n): if state[i] == 0 and a[i] & temp == 0: possible = False elif state[i] == 1 and b[i] & temp == 0: possible = False elif a[i] & temp == 0 and b[i] & temp == 0: possible = False if possible: for i in range(n): if state[i] == -1: if a[i] & temp == 0: state[i] = 1 elif b[i] & temp == 0: state[i] = 0 ans = INF moves = 0 for i in range(n): if state[i] == 1: ans &= b[i] moves += 1 else: ans &= a[i] print(ans, moves)
ASSIGN VAR BIN_OP FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) vis = set() ans = 0 for i in range(30, -1, -1): f = 1 h = [] for j in range(n): if j in vis: if 1 << i & b[j] == 0: f = 0 elif a[j] & 1 << i == 0: if b[j] & 1 << i and b[j] & ans == ans: h.append(j) else: f = 0 if f: ans += 1 << i for j in h: vis.add(j) print(ans, len(vis))
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
def main(): n = int(input()) a1 = list(map(int, input().split())) b1 = list(map(int, input().split())) m = len(bin(max(max(a1), max(b1)))[2:]) arr = [3] * n ans = 0 for j in range(m - 1, -1, -1): b2 = pow(2, j) f = True for i in range(n): if arr[i] == 3 and b2 & (a1[i] | b1[i]) == 0: f = False break if arr[i] == 1 and b2 & a1[i] == 0: f = False break if arr[i] == 0 and b2 & b1[i] == 0: f = False break if f == True: ans += b2 for i in range(n): first = a1[i] & b2 second = b1[i] & b2 if arr[i] == 3 and first and second: continue if arr[i] == 3 and first: arr[i] = 1 if arr[i] == 3 and second: arr[i] = 0 count = 0 for i in range(n): if arr[i] == 0: count += 1 print(ans, count) t = 1 t = int(input()) for i in range(t): main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 flips = 0 decided = [-1] * n for j in range(29, -1, -1): possible = True make_decided = [-1] * n for i in range(n): if decided[i] != -1: if decided[i] == 0 and a[i] & 1 << j == 1 << j: continue elif decided[i] == 1 and b[i] & 1 << j == 1 << j: continue else: possible = False break elif a[i] & 1 << j != 1 << j and b[i] & 1 << j != 1 << j: possible = False break elif a[i] & 1 << j == 1 << j and b[i] & 1 << j == 1 << j: pass elif b[i] & 1 << j == 1 << j: make_decided[i] = 1 elif a[i] & 1 << j == 1 << j: make_decided[i] = 0 else: possible = False break if possible: for k in range(n): if make_decided[k] != -1: decided[k] = make_decided[k] ans = ans * 10 + 1 else: ans = ans * 10 anss = 0 ans = str(ans) n_ans = len(ans) for i in range(n_ans - 1, -1, -1): if ans[i] == "1": anss += 1 << n_ans - i - 1 print(anss, end=" ") for i in range(n): if decided[i] == 1: flips += 1 print(flips)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
T = int(input()) for t in range(T): n = int(input()) arr1 = [bin(int(i))[2:] for i in input().split()] arr1 = [("0" * (31 - len(i)) + i) for i in arr1] arr2 = [bin(int(i))[2:] for i in input().split()] arr2 = [("0" * (31 - len(i)) + i) for i in arr2] count = [-1] * n answer = [0] * 31 for i in range(31): flip = [-1] * n ans = True for j in range(n): if count[j] == -1: if arr1[j][i] == "1" and arr2[j][i] == "1": pass elif arr1[j][i] == "1" and arr2[j][i] != "1": flip[j] = 0 elif arr2[j][i] == "1": flip[j] = 1 else: ans = False break elif count[j] == 0: if arr1[j][i] == "1": pass else: ans = False break elif count[j] == 1: if arr2[j][i] == "1": pass else: ans = False break if ans: for j in range(n): if flip[j] != -1: count[j] = flip[j] answer[i] = 1 pow = 30 tot = 0 for i in range(31): if answer[i] == 1: tot += 2**pow pow -= 1 flips = 0 for i in count: if i == 1: flips += 1 print(tot, flips)
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 VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR STRING VAR VAR VAR STRING IF VAR VAR VAR STRING VAR VAR VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
def intinp(): return int(input()) def mapinp(): return map(int, list(input().split())) T = intinp() for _ in range(T): N = intinp() A = list(mapinp()) B = list(mapinp()) state = [0] * N flipped = [0] * N for i in range(29, -1, -1): flip = True for j in range(N): if A[j] & 1 << i == 0: if B[j] & 1 << i != 0 and state[j] == 0: state[j] = 2 else: flip = False break elif B[j] & 1 << i == 0 and state[j] == 0: state[j] = 3 for k in range(N): if flip and state[k] == 2: temp = A[k] A[k] = B[k] B[k] = temp flipped[k] = 1 state[k] = 1 if flip and state[k] == 3: state[k] = 1 if not flip and (state[k] == 2 or state[k] == 3): state[k] = 0 bitwiseAnd = A[0] numFlip = 0 for i in range(N): bitwiseAnd = bitwiseAnd & A[i] if flipped[i] == 1: numFlip += 1 print(bitwiseAnd, end=" ") print(numFlip)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
def func(): turn = [(-1) for x in range(n)] for req in range(31, -1, -1): flag = False changed = [(False) for x in range(n)] swapped = [(False) for x in range(n)] curr = 1 << req for j in range(n): if turn[j] != -1: if curr & a[j] == 0: flag = True break continue if curr & a[j] and curr & b[j]: continue elif curr & a[j] and curr & b[j] == 0: turn[j] = 0 changed[j] = True elif curr & a[j] == 0 and curr & b[j]: turn[j] = 1 a[j], b[j] = b[j], a[j] swapped[j] = True changed[j] = True else: flag = True break if flag: for j in range(n): if changed[j]: turn[j] = -1 if swapped[j]: a[j], b[j] = b[j], a[j] calc = (1 << 31) - 1 ans = 0 for j in range(n): if turn[j] == 1: ans += 1 calc = calc & a[j] print(calc, ans) for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) func()
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
Chef found N magical cards in his drawer. Each card has a number written on each of its faces. He places all the cards on the table in a front face-up manner. Chef denotes the numbers on the front face by A_{1}, A_{2},..., A_{N} and on the back face by B_{1}, B_{2},..., B_{N}. Chef can choose some (possibly zero or all) cards and flip them, then he will calculate the [bitwise AND] of all the numbers currently in a face-up manner. Now Chef wonders what is the *maximum* bitwise AND that he can achieve and what is the *minimum* number of cards he has to flip to achieve this value. Can you help him find it? ------ Input Format ------ - The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. - Each test case contains three lines of input. - The first line contains a single integer N - the number of cards. - The second line contains N space-separated integers A_{1}, A_{2},..., A_{N} - the numbers on the front face of the cards - The third line contains N space-separated integers B_{1}, B_{2},..., B_{N} - the numbers on the back face of the cards ------ Output Format ------ For each test case, print a single line containing two space-separated integers, first denoting the maximum bitwise AND possible and second denoting the minimum number of flips required to achieve it. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $0 ≀ A_{i} ≀ 2^{30} - 1$ $0 ≀ B_{i} ≀ 2^{30} - 1$ - Sum of $N$ over all testcases does not exceeds $10^{5}$. ----- Sample Input 1 ------ 3 3 4 6 8 2 1 2 3 0 2 0 2 0 8 3 1 2 1 2 3 6 ----- Sample Output 1 ------ 2 2 0 0 2 2 ----- explanation 1 ------ Test case $1$: The maximum AND is obtained after flipping the first and third cards achieving a configuration of $[2, 6, 2]$ which yields a bitwise AND of $2$. Test case $2$: Every possible configuration of the cards yields a bitwise AND equal to $0$. So to ensure the minimum number of operations, Chef performs no flip.
T = int(input()) while T != 0: n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) m = max(max(A), max(B)) index = 0 while m != 1: index += 1 m //= 2 ans = (1 << index + 1) - 1 state = [(-1) for t in range(n)] i = 1 << index while i > 0: poss = True for j in range(n): if state[j] == 1 and not A[j] & i: poss = False break elif state[j] == 0 and not B[j] & i: poss = False break elif not A[j] & i and not B[j] & i: poss = False break if poss: for k in range(n): if state[k] != -1: continue if not A[k] & i: state[k] = 0 elif not B[k] & i: state[k] = 1 i >>= 1 flip = 0 for p in range(n): if state[p] == 0: flip += 1 ans &= B[p] else: ans &= A[p] print(ans, flip, sep=" ") T -= 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR NUMBER
The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the presented tasks are lovely, but don't show the truth about competitors' skills. The boy is now looking at the ratings of consecutive participants written in a binary system. He thinks that the more such ratings differ, the more unfair is that such people are next to each other. He defines the difference between two numbers as the number of bit positions, where one number has zero, and another has one (we suppose that numbers are padded with leading zeros to the same length). For example, the difference of $5 = 101_2$ and $14 = 1110_2$ equals to $3$, since $0101$ and $1110$ differ in $3$ positions. Johnny defines the unfairness of the contest as the sum of such differences counted for neighboring participants. Johnny has just sent you the rating sequence and wants you to find the unfairness of the competition. You have noticed that you've got a sequence of consecutive integers from $0$ to $n$. That's strange, but the boy stubbornly says that everything is right. So help him and find the desired unfairness for received numbers. -----Input----- The input consists of multiple test cases. The first line contains one integer $t$ ($1 \leq t \leq 10\,000$)Β β€” the number of test cases. The following $t$ lines contain a description of test cases. The first and only line in each test case contains a single integer $n$ ($1 \leq n \leq 10^{18})$. -----Output----- Output $t$ lines. For each test case, you should output a single line with one integerΒ β€” the unfairness of the contest if the rating sequence equals to $0$, $1$, ..., $n - 1$, $n$. -----Example----- Input 5 5 7 11 1 2000000000000 Output 8 11 19 1 3999999999987 -----Note----- For $n = 5$ we calculate unfairness of the following sequence (numbers from $0$ to $5$ written in binary with extra leading zeroes, so they all have the same length): $000$ $001$ $010$ $011$ $100$ $101$ The differences are equal to $1$, $2$, $1$, $3$, $1$ respectively, so unfairness is equal to $1 + 2 + 1 + 3 + 1 = 8$.
def recur(n): if n == 0: return 0 return recur(n // 2) + n t = int(input()) for hh in range(0, t): n = int(input()) ans = recur(n) print(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR 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 VAR EXPR FUNC_CALL VAR VAR