description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
from sys import stdin, stdout input = stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = [int(x) for x in input().split()] ans = -float("inf") for i in range(max(n - 400, 0), n): for j in range(i + 1, n): ans = max(ans, (i + 1) * (j + 1) - k * (a[i] | a[j])) print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n, k = map(int, input().split()) arr = [0] + list(map(int, input().split())) r = n maxi = -float("inf") while r >= 1: if r * r < maxi: break l = r - 1 while l >= 1: if r * l < maxi: break maxi = max(maxi, r * l - k * (arr[r] | arr[l])) l -= 1 r -= 1 print(maxi)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
n = int(input()) for i in range(n): l, k = list(map(int, input().split())) finalnum = -1e18 arr = list(map(int, input().split())) for m in range(max(0, l - 101), l): for z in range(m + 1, l): num = (z + 1) * (m + 1) - k * (arr[z] | arr[m]) if num > finalnum: finalnum = num print(finalnum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) a = list(map(int, input().split())) z = n b = 0 while z > 0: b |= z z //= 2 r = int(-1000000000.0) for j in range(n - 1, -1, -1): for i in range(j - 1, -1, -1): if (i + 1) * (j + 1) < r: break r = max(r, (i + 1) * (j + 1) - k * (a[i] | a[j])) print(r) for i in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
def read_ints(): return [int(x) for x in input().split()] outs = [] def prnt(s): outs.append(str(s)) def flush(): print("\n".join(outs)) t = read_ints()[0] while t: t -= 1 n, k = read_ints() x = read_ints() jwb = -1000000000000.0 for i in range(min(n, 2 * k)): for j in range(i + 1, min(n, 2 * k)): a, b = n - i, n - j jwb = max(jwb, a * b - k * (x[a - 1] | x[b - 1])) prnt(jwb) flush()
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for i in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) ans = -(10**10) for i in range(n - 1, max(0, n - 1 - 200), -1): for j in range(i - 1, max(-1, n - 2 - 200), -1): ans = max(ans, (i + 1) * (j + 1) - k * (a[i] | a[j])) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() inf = 10**16 md = 10**9 + 7 def solve(): n, k = LI() aa = LI() si = max(0, n - 1 - 2 * k) ans = -inf for i in range(si, n): for j in range(i + 1, n): cur = (i + 1) * (j + 1) - k * (aa[i] | aa[j]) if cur > ans: ans = cur print(ans) for testcase in range(II()): solve()
IMPORT 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 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 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 FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
t = int(input()) otv = list() for i in range(t): n, k = map(int, input().split()) sp = list(map(int, input().split())) it = -100000000000000000 for j in range(n, 0, -1): pred = sp[j - 1] * k for o in range(j - 1, 0, -1): if j * o - pred < it: break it = max(it, j * o - k * (sp[j - 1] | sp[o - 1])) otv.append(it) for i in otv: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
t = int(input()) for xx in range(t): [n, k] = [int(x) for x in input().split()] a = [int(x) for x in input().split()] start = 0 if n > 100: start = n - k - 4 ans = float("-inf") for i in range(start, len(a)): for j in range(i + 1, len(a)): ans = max(ans, (i + 1) * (j + 1) - k * (a[i] | a[j])) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR 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 IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
t = int(input()) for tc in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) best = -float("inf") i = max(0, n - 203) while i < n: j = i + 1 while j < n: if (i + 1) * (j + 1) - k * (a[i] | a[j]) > best: best = (i + 1) * (j + 1) - k * (a[i] | a[j]) j += 1 i += 1 print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
q = int(input()) for _ in range(q): n = [int(x) for x in input().split()] k = n[1] n = n[0] a = [int(x) for x in input().split()] m = 2 - k * (a[0] | a[1]) for i in range(max(n - 200, 0), n): for j in range(i + 1, n): x = (i + 1) * (j + 1) - k * (a[i] | a[j]) if m < x: m = x print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
t = int(input()) for _ in range(t): n, k = [int(i) for i in input().split()] s = [int(i) for i in input().split()] m = n - 2 * k - 1 _s = s[m:] mat = [] mx = -(10**9) if m >= 0: for i in range(2 * k + 1): for j in range(i + 1, 2 * k + 1): val = (m + i + 1) * (m + j + 1) - k * (_s[i] | _s[j]) if val > mx: mx = val print(mx) else: _mx = -(10**9) for i in range(n): for j in range(i + 1, n): val = i * j + i + j + 1 - k * (s[i] | s[j]) if val > _mx: _mx = val print(_mx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
t = int(input()) while t > 0: n, k = map(int, input().split()) a = list(map(int, input().split())) if n <= 1000: m = -99999999999999999999999 for i in range(n - 1): for j in range(i + 1, n): if (i + 1) * (j + 1) - k * (a[i] | a[j]) > m: m = (i + 1) * (j + 1) - k * (a[i] | a[j]) print(m) else: m = -99999999999999999999999 for i in range(n - 1000 - 1, n - 1): for j in range(i + 1, n): if (i + 1) * (j + 1) - k * (a[i] | a[j]) > m: m = (i + 1) * (j + 1) - k * (a[i] | a[j]) print(m) t -= 1 100000 - 1000
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR BIN_OP NUMBER NUMBER
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
from sys import gettrace, stdin if gettrace(): def inputi(): return input() else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def solve(): n, k = map(int, input().split()) aa = [int(a) for a in input().split()] mnv = n * (n - 1) - 2 * n * k def minj(i): return max(i + 1, mnv // i) res = max( i * j - k * (a | b) for i, a in enumerate(aa[:-1], 1) for j, b in enumerate(aa[minj(i) - 1 :], minj(i)) ) print(res) def main(): t = int(input()) for _ in range(t): solve() main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
from sys import stdin _input = stdin.readline _max, _range, _int, _str = max, range, int, str def solution(): for _ in _range(_int(_input())): n, k = [_int(i) for i in _input().split()] arr = [0] + [_int(i) for i in _input().rstrip("\n").split()] ans = -(n * n) for i in _range(n, 1, -1): for j in _range(i - 1, 0, -1): pr = i * j if pr >= ans: temp = pr - k * (arr[i] | arr[j]) ans = _max(temp, ans) else: break print(ans) solution()
ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
t = int(input()) while t > 0: t -= 1 n, k = map(int, input().split()) a = list(map(int, input().split())) m = -1000000000.0 x = max(0, n - 200) for i in range(x, n): for j in range(i + 1, n): d = (i + 1) * (j + 1) - k * (a[i] | a[j]) m = max(m, d) print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for _ in range(int(input())): n, k = [int(x) for x in input().split()] numbers = [int(x) for x in input().split()] summe = 2 - k * (numbers[0] | numbers[1]) for i in range(n - 1, 0, -1): if (i + 1) * i <= summe: break for i2 in range(i - 1, -1, -1): if (i + 1) * (i2 + 1) <= summe: break if summe < (i + 1) * (i2 + 1) - k * (numbers[i] | numbers[i2]): summe = (i + 1) * (i2 + 1) - k * (numbers[i] | numbers[i2]) print(summe)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys input = sys.stdin.readline (T,) = map(int, input().split()) for _ in range(T): N, k = map(int, input().split()) X = list(map(int, input().split())) c = len(bin(N * (N - 1))) - 2 mm = 1 << len(bin(N)) - 2 R = -(10**18) for i in range(N, max(0, N - 401), -1): for j in range(i - 1, max(0, i - 1 - 401), -1): R = max(R, i * j - k * (X[i - 1] | X[j - 1])) print(R)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) t = float("-inf") i = n - 1 j = 0 mx = max(0, n - 500) while i >= mx: j = i - 1 while j >= mx: t = max(t, (i + 1) * (j + 1) - k * (l[i] | l[j])) j -= 1 i -= 1 print(t)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
p = "" for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) ans = -1e18 for i in range(max(0, n - 101), n): for j in range(i + 1, n): ans = max(ans, (i + 1) * (j + 1) - k * (l[i] | l[j])) p += str(ans) + "\n" print(p)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for _ in range(int(input())): n, k = map(int, input().split()) a = [0] + list(map(int, input().split())) p = n * (n - 1) - k * n * 2 ans = float("-inf") for j in range(n, 0, -1): for i in range(j - 1, 0, -1): if i * j < p: break ans = max(ans, i * j - k * (a[i] | a[j])) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) tempy = arr[:] temparr = tempy[-110:] l = len(temparr) out = -1e20 i = 0 while i < l: j = i + 1 while j < l: flag = (n - l + 1 + i) * (n - l + 1 + j) - k * (temparr[i] | temparr[j]) if flag > out: out = flag j += 1 i += 1 print(out)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
t = int(input()) for tc in range(t): n, k = map(int, input().split()) arr = [int(z) for z in input().split()] left = max(0, n - 2 * k - 1) res = -(10**18) for i in range(left, n): for j in range(i + 1, n): r = (i + 1) * (j + 1) - k * (arr[i] | arr[j]) res = max(res, r) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) maxx = -100000 for i in range(max(1, n - 200), n + 1): for j in range(i + 1, n + 1): maxx = max(maxx, j * i - k * (arr[i - 1] | arr[j - 1])) print(maxx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) def main(): numTestCases = int(input()) for _ in range(numTestCases): _n, k = invr() a = inlt() maxValue = -int(1000000000.0) start = max(0, len(a) - 1000) for i in range(start, len(a)): for j in range(i + 1, len(a)): value = (i + 1) * (j + 1) - k * (a[i] | a[j]) maxValue = max(maxValue, value) print(maxValue) main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
def fun(i, j): global ar, k return (i + 1) * (j + 1) - k * (ar[i] | ar[j]) for _ in range(int(input())): n, k = map(int, input().split()) ar = list(map(int, input().split())) ans = -float("inf") for i in range(n - 1, max(-1, n - 1 - 400), -1): for j in range(i - 1, max(-1, n - 1 - 400), -1): ans = max(ans, fun(i, j)) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
import sys input = sys.stdin.buffer.readline def f(x, y): if y in x: return x if x[0] <= y: return y, x[0] elif x[1] <= y: return x[0], y return x[0], x[1] for t in range(int(input())): N, K = map(int, input().split()) A = list(map(int, input().split())) L = N.bit_length() M = 1 << L DP = [(0, 0) for i in range(M)] for i in range(N): DP[A[i]] = f(DP[A[i]], i + 1) for i in range(M): for j in range(L): if i >> j & 1: continue for k in range(2): DP[i | 1 << j] = f(DP[i | 1 << j], DP[i][k]) ANS = -(10**18) for i in range(M): if DP[i][1] == 0: continue ANS = max(ANS, DP[i][0] * DP[i][1] - i * K) print(ANS)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR IF VAR NUMBER VAR RETURN VAR VAR NUMBER IF VAR NUMBER VAR RETURN VAR NUMBER VAR RETURN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) max_val = -(10**7) for i in range(n, 0, -1): for j in range(i - 1, 0, -1): if i * j < max_val: break elif i * j - k * (a[i - 1] | a[j - 1]) > max_val: max_val = i * j - k * (a[i - 1] | a[j - 1]) print(max_val)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n integers a_1, a_2, …, a_n and an integer k. Find the maximum value of i β‹… j - k β‹… (a_i | a_j) over all pairs (i, j) of integers with 1 ≀ i < j ≀ n. Here, | is the [bitwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operation#OR). Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains two integers n (2 ≀ n ≀ 10^5) and k (1 ≀ k ≀ min(n, 100)). The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print a single integer β€” the maximum possible value of i β‹… j - k β‹… (a_i | a_j). Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Output -1 -4 3 12 Note Let f(i, j) = i β‹… j - k β‹… (a_i | a_j). In the first test case, * f(1, 2) = 1 β‹… 2 - k β‹… (a_1 | a_2) = 2 - 3 β‹… (1 | 1) = -1. * f(1, 3) = 1 β‹… 3 - k β‹… (a_1 | a_3) = 3 - 3 β‹… (1 | 3) = -6. * f(2, 3) = 2 β‹… 3 - k β‹… (a_2 | a_3) = 6 - 3 β‹… (1 | 3) = -3. So the maximum is f(1, 2) = -1. In the fourth test case, the maximum is f(3, 4) = 12.
for _ in range(int(input())): n, k = map(int, input().split(" ")) arr = [int(w) for w in input().split(" ")] ans = -(10**18) temp = [] cnt = 0 for i in range(n - 1, -1, -1): temp.append((arr[i], i)) cnt += 1 if cnt == 300: break for i in range(len(temp)): for j in range(i + 1, len(temp)): u1, v1 = temp[i] u2, v2 = temp[j] ans = max(ans, (v1 + 1) * (v2 + 1) - k * (u1 | u2)) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Petya is a math teacher. n of his students has written a test consisting of m questions. For each student, it is known which questions he has answered correctly and which he has not. If the student answers the j-th question correctly, he gets p_j points (otherwise, he gets 0 points). Moreover, the points for the questions are distributed in such a way that the array p is a permutation of numbers from 1 to m. For the i-th student, Petya knows that he expects to get x_i points for the test. Petya wonders how unexpected the results could be. Petya believes that the surprise value of the results for students is equal to βˆ‘_{i=1}^{n} |x_i - r_i|, where r_i is the number of points that the i-th student has got for the test. Your task is to help Petya find such a permutation p for which the surprise value of the results is maximum possible. If there are multiple answers, print any of them. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. The first line of each test case contains two integers n and m (1 ≀ n ≀ 10; 1 ≀ m ≀ 10^4) β€” the number of students and the number of questions, respectively. The second line contains n integers x_1, x_2, ..., x_n (0 ≀ x_i ≀ (m(m+1))/(2)), where x_i is the number of points that the i-th student expects to get. This is followed by n lines, the i-th line contains the string s_i (|s_i| = m; s_{i, j} ∈ \{0, 1\}), where s_{i, j} is 1 if the i-th student has answered the j-th question correctly, and 0 otherwise. The sum of m for all test cases does not exceed 10^4. Output For each test case, print m integers β€” a permutation p for which the surprise value of the results is maximum possible. If there are multiple answers, print any of them. Example Input 3 4 3 5 1 2 2 110 100 101 100 4 4 6 2 0 10 1001 0010 0110 0101 3 6 20 3 15 010110 000101 111111 Output 3 1 2 2 3 4 1 3 1 4 5 2 6
I = lambda: [*map(int, input().split())] r = range s = sum for _ in [0] * I()[0]: n, m = I() e = I() M = [[*map(int, [*input().strip()])] for i in r(n)] o = [bin(i).count("1") for i in r(1 << n)] q = [s(1 << i for i in r(n) if M[i][j]) for j in r(m)] b = -1 for i in r(1 << n): c = [0] * (n + 1) v = [0] * m d = c[:] P = v[:] for j in r(m): v[j] = o[i ^ q[j]] c[v[j]] += 1 for j in r(n): d[j + 1] = d[j] + c[j] for j in r(m): d[v[j]] += 1 P[j] = d[v[j]] T = ( 2 * s(e[j] for j in r(n) if i >> j & 1) - s(e) + s((o[q[j]] - 2 * o[i & q[j]]) * P[j] for j in r(m)) ) if T > b: b, p = T, P print(*p)
ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR LIST FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
def F1(s, k): siz = len(s) pivot = s[0] index = 1 while index < siz: if s[index] != pivot: break else: index += 1 if index == siz: return s elif pivot > s[index]: res = s[:index] return replaceF(res, pivot, index, siz) else: res = "" ch = str(int(pivot) + 1) return replaceF(res, ch, 0, siz) def replaceF(res, f, start, siz): for i in range(start, siz): res += f return res def F2(s, k): siz = len(s) f1 = s[0] index = 1 while index < siz: if s[index] != f1: break else: index += 1 if index == siz: return s f2 = s[index] pos = index + 1 while pos < siz: if s[pos] != f1 and s[pos] != f2: break else: pos += 1 if pos == siz: return s minF = min(f1, f2) maxF = max(f1, f2) maxInt = int(maxF) minInt = int(minF) if s[pos] < minF: res = s[:pos] return replaceF(res, minF, pos, siz) elif s[pos] < maxF: res = s[:pos] res += maxF return replaceF(res, minF, pos + 1, siz) elif s[pos - 1] == minF: res = s[: pos - 1] if minF not in res: res += str(minInt + 1) if maxInt > minInt + 1: minF = str(minInt + 1) else: minF = str(0) else: res += maxF return replaceF(res, minF, pos, siz) else: stand = pos - 1 while stand >= 0: if s[stand] == minF: break else: stand -= 1 option = False for i in range(stand, -1, -1): if s[i] == maxF: option = True break if option: res = s[:stand] if minF not in res: res += str(minInt + 1) if maxInt > minInt + 1: minF = str(minInt + 1) else: minF = str(0) else: res += maxF return replaceF(res, minF, stand + 1, siz) else: res = s[: stand + 1] res += str(int(maxF) + 1) return replaceF(res, minF, stand + 2, siz) case = int(input()) for t in range(case): s, tmp = input().split() k = int(tmp) if k == 1: print(F1(s, k)) else: print(F2(s, k))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
def get_input(): al = [] for c in range(int(input())): t = input().split(" ") el = [t[0], int(t[1])] al.append(el) return al def find_best(n, j, s): b = ":" m = ":" for k in s: if k < m: m = k if n[j] < k and k < b: b = k if b == ":": return False else: return n[:j] + b + m * (len(n) - j - 1) def solve(n, k): if k == 1: for i in range(1, len(n)): if n[i] > n[0]: return str(int(n[0]) + 1) * len(n) elif n[i] < n[0]: break return n[0] * len(n) s = [] for i in range(len(n)): if n[i] not in s: if len(s) == k: rf = find_best(n, i, s) if rf != False: return rf for j in range(i - 1, -1, -1): if n[j] == "9": continue elif n[j] in n[:j]: rf = find_best(n, j, s) if rf != False: return rf else: c = str(int(n[j]) + 1) cm = c for k in range(j): if n[k] < cm: cm = n[k] d = True for k in range(0, j): if n[k] != c: d = False break if d: return n[:j] + c + "0" * (len(n) - j - 1) return n[:j] + c + min(cm, c) * (len(n) - j - 1) s.append(n[i]) return n def main(): arr_list = get_input() for n in arr_list: r = solve(n[0], n[1]) print(r) main()
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR STRING RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
def recurs(i, n, dist, nw, k, orig): if dist > k: return "" if nw[0:i] < orig[0:i]: return "" if i >= n: return nw c = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] for item in c: nw = nw[:i] + item + nw[i + 1 :] f = False for j in range(i): if nw[j] == item: f = True break cc = recurs(i + 1, n, dist + (f == False), nw, k, orig) if cc != "": return cc return "" t = int(input()) for i in range(t): n, k = map(int, input().split()) n = str(n) nw = "0" * len(n) print(recurs(0, len(n), 0, nw, k, n))
FUNC_DEF IF VAR VAR RETURN STRING IF VAR NUMBER VAR VAR NUMBER VAR RETURN STRING IF VAR VAR RETURN VAR ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR STRING RETURN VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
import sys input = sys.stdin.readline ans1 = set() ans2 = set() for a in range(10): ans = a for i in range(10): ans1.add(ans) ans = ans * 10 + a for a in range(10): for b in range(10): for k in range(2, 11): for bit in range(2**k): x = 0 for i in range(k): bitcnt = 0 if bit >> i & 1: x = x * 10 + a bitcnt += 1 else: x = x * 10 + b if not (bitcnt == 0 or bitcnt == k): ans2.add(x) ans1 = list(ans1) ans2 = list(ans2) ans1.sort() ans2.sort() def bilower(a, x): if len(a) == 0: return -1 mi = 0 ma = len(a) - 1 if a[0] > x: return -1 if a[ma] <= x: return ma while ma - mi > 1: mid = (ma + mi) // 2 if a[mid] <= x: mi = mid else: ma = mid return mi def bihigher(a, x): if len(a) == 0: return 0 mi = 0 ma = len(a) - 1 if a[ma] < x: return ma + 1 if a[0] >= x: return 0 while ma - mi > 1: mid = (ma + mi) // 2 if a[mid] >= x: ma = mid else: mi = mid return ma def birange(a, l, r): left = bihigher(a, l) right = bilower(a, r) return right - left + 1 T = int(input()) for iiii in range(T): n, k = map(int, input().split()) if k == 1: print(ans1[bihigher(ans1, n)]) else: print(ans2[bihigher(ans2, n)])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR RETURN BIN_OP VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
import sys t = int(input()) for i in range(t): n, k = sys.stdin.readline().split() n = n.lstrip("000000000") k = int(k) L = [n[0]] for s in n: if s not in L: L.append(s) if len(L) <= k: print(n) elif k == 1: if L[1] < L[0]: print(L[0] * len(n)) else: print(str(int(L[0]) + 1) * len(n)) else: x, y = int(L[0]), int(L[1]) Num = list(map(int, n)) ind = Num.index(y) bada = False i = 0 while i < len(n): if bada: Num[i] = min(x, y) elif Num[i] > max(x, y): bada = True while Num[i] + 1 > max(x, y) and i > ind: i -= 1 Num[i] += 1 if i == ind: y += 1 if x == y: y = 0 i += 1 bada = False for i in range(len(Num)): if bada == True: Num[i] = min(x, y) else: if Num[i] != x and Num[i] != y: bada = True if Num[i] > min(x, y): Num[i] = max(x, y) else: Num[i] = min(x, y) print(int("".join(list(map(str, Num)))))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
t = int(input()) results = [] for i in range(t): n, k = input().split() k = int(k) if k == 1: digits = len(n) important_num = int(digits * "1") n = int(n) for j in range(1, 10): if important_num * j >= n: results.append(important_num * j) break else: results.append((digits + 1) * "1") elif k == 2: digits_num = len(n) digits = [int(c) for c in n] num_results = [] a = digits[0] char_a = n[0] for b in range(0, 10): char_b = str(b) current_num = "" for digit in digits: if digit > a: if digit > b: break elif digit < b: current_num += char_b + char_a * ( digits_num - len(current_num) - 1 ) num_results.append(current_num) break else: current_num += char_b elif digit < a: if digit > b: current_num += char_a + char_b * ( digits_num - len(current_num) - 1 ) num_results.append(current_num) break elif digit < b: if a < b: current_num += char_a * (digits_num - len(current_num)) num_results.append(current_num) break else: current_num += char_b * (digits_num - len(current_num)) num_results.append(current_num) break else: num_results.append( current_num + char_a + char_b * (digits_num - len(current_num) - 1) ) current_num += char_b elif digit > b: current_num += char_a elif digit < b: num_results.append( current_num + char_b + char_a * (digits_num - len(current_num) - 1) ) current_num += char_a else: current_num += char_a else: num_results.append(current_num) if n[0] == 9: num_results.append("1" + digits_num * "0") else: num_results.append(str(digits[0] + 1) + (digits_num - 1) * "0") results.append(min([int(item) for item in num_results])) for i in range(t): print(results[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
def solve(num, k): count = 0 tab = [(-1) for _ in range(int(k))] idx = 0 rem = -1 rem_idx = -1 for x in num: if int(x) not in tab: if count < int(k): tab[count] = int(x) count += 1 else: rem = int(x) rem_idx = idx break idx += 1 if rem == -1: print(num) return idx_1 = 0 check = False counter = 0 sec_tab = [] new = min(tab) if max(rem, max(tab)) == rem: for x in range(rem_idx, -1, -1): if int(num[x]) < max(tab): idx_1 = x break for x in range(rem_idx + 1): if int(num[x]) not in sec_tab: if counter == count - 1: idx_2 = x break else: counter += 1 sec_tab.append(int(num[x])) if idx_2 >= idx_1: counter = 0 for x in num: if int(x) not in sec_tab and not check: print(int(x) + 1, end="") check = True if int(x) + 1 not in sec_tab: sec_tab.append(int(x) + 1) if len(sec_tab) == count: new = min(sec_tab) else: new = 0 elif not check: print(int(x), end="") else: print(new, end="") print() else: temp = min([i for i in tab if i > int(num[idx_1])]) for j in range(len(num)): if j < idx_1: print(num[j], end="") elif j == idx_1: print(temp, end="") else: print(min(tab), end="") print() else: temp = min([i for i in tab if i > rem]) for x in num: if int(x) not in sec_tab and not check: if counter == count: print(temp, end="") check = True else: sec_tab.append(int(x)) counter += 1 print(int(x), end="") elif not check: print(int(x), end="") else: print(min(tab), end="") print() for i in range(int(input())): num, k = map(str, input().split()) solve(num, k)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
def check(s, k, t, n): z = set() for i in range(len(s)): if i == len(s) - 1: z.add(t) else: z.add(int(s[i])) if len(z) <= k: needed = len(str(n)) - len(s) arr = [] min_val = 9 for i in range(len(s)): if i == len(s) - 1: arr.append(t) if t < min_val: min_val = t else: arr.append(int(s[i])) if int(s[i]) < min_val: min_val = int(s[i]) if len(z) == k: for i in range(needed): arr.append(min_val) else: for i in range(needed): arr.append(0) for i in range(len(arr)): print(arr[i], end="") print() return True return False def func(n, i, k): s = str(n)[0 : i + 1] t = int(s[-1]) + 1 while t < 10: if check(s, k, t, n): return 0 else: t += 1 func(n, i - 1, k) for _ in range(int(input())): n, k = map(int, input().split()) l = len(str(n)) if check(str(n), k, int(str(n)[-1]), n): continue func(n, l - 1, k)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
def nearest_k_number(a, k): c = 0 ta = a while ta != 0: ta = ta // 10 c += 1 numbers = set() counter = 0 while c != 0: c = c - 1 if c != 0: cur = a // 10**c else: cur = a cur = cur % 10 if cur not in numbers: counter += 1 if counter > k: return a // 10 ** (c + 1), a // 10**c numbers.add(cur) return a, a t = int(input()) def check(n, k): c = 0 s = set() while n != 0: t = n % 10 n = n // 10 if t not in s: c += 1 s.add(t) if c > k: return True return False def pad(a, k): t = a m = a % 10 c = 0 while t != 0: if t % 10 < m: m = t % 10 t //= 10 c += 1 while c < k: c += 1 a = a * 10 + m return a def nex(n, k, a0): a1 = a0 nums = set() while a0 != 0: if a0 % 10 not in nums: nums.add(a0 % 10) a0 = a0 // 10 if len(nums) < k: while a1 < n: a1 *= 10 return a1 k = 0 while n != 0: k += 1 n = n // 10 return pad(a1, k) def solve(n, k): if k == 1: tn = n m = -1 for n in range(1, 10): while n < tn: n = n * 10 + n % 10 if m == -1 or m > n: m = n return m return n a = nearest_k_number(n, k) a0 = a[0] a1 = a[1] if a0 == n: return a0 at = a0 pr = False while at != 0: if at % 10 > a1 % 10: pr = True break at = at // 10 if pr: a0 = a1 a0 += 1 while check(a0, k): a0 += 1 a0 = nex(n, k, a0) return a0 tn = [] tk = [] while t > 0: t = t - 1 n, k = list(map(int, input().strip().split())) tn.append(n) tk.append(k) for i in range(len(tk)): print(solve(tn[i], tk[i]))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR RETURN BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
def getBeautiful(num, k): if len(set(str(num))) <= k: return num unstableIdx = k st = str(num) set_ = set(st[: unstableIdx + 1]) while len(set_) <= k: set_.add(st[unstableIdx + 1]) unstableIdx += 1 digits = set(map(int, set(st[:unstableIdx]))) n = int(st[unstableIdx]) while n not in digits: n += 1 if n == 10: break if n in digits: return int( st[:unstableIdx] + str(n) + str(min(digits)) * (len(st) - unstableIdx - 1) ) else: return getBeautiful( (int(st[:unstableIdx]) + 1) * 10 ** (len(st) - unstableIdx), k ) for t in range(int(input())): n, k = map(int, input().split()) print(getBeautiful(n, k))
FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
t = int(input()) k1 = [0] for i in range(1, 10): for j in range(1, 11): e = str(i) * j e = int(e) k1.append(e) k1 = sorted(k1) k2 = [] def ad(i, j, s): if len(s) == 10: if s != s[0] * len(s): k2.append(int(s)) else: if s != "" and s != s[0] * len(s): k2.append(int(s)) if not (i == "0" and s == ""): ad(i, j, s + i) ad(i, j, s + j) for i in range(10): for j in range(i + 1, 10): ad(str(i), str(j), "") k2 += k1 k2 = sorted(k2) def binsearch(l, r, arr, n): while l + 1 < r: mid = (l + r) // 2 if arr[mid] <= n: l = mid else: r = mid if n == arr[l]: return l return r for i in range(t): a, b = map(int, input().split()) if b == 1: ans = binsearch(-1, len(k1), k1, a) print(k1[ans]) else: ans = binsearch(-1, len(k2), k2, a) print(k2[ans])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
def nr_distinct(n): return len(set(list(n))) def get(n, idx, k): if n[idx] == "9": return -1 digs = set(list(n[:idx])) if len(digs) > k: return -1 if len(digs) == k: x = chr(ord("9") + 1) y = x for ch in digs: if ord(ch) > ord(n[idx]) and ord(ch) < ord(x): x = ch if ord(ch) < ord(y): y = ch if ord(x) <= ord("9"): return n[:idx] + x + "".join([y] * (len(n) - idx - 1)) else: return -1 else: x = chr(ord(n[idx]) + 1) digs.add(x) if len(digs) < k: y = "0" else: y = chr(ord("9") + 1) for ch in digs: if ord(ch) < ord(y): y = ch return n[:idx] + x + "".join([y] * (len(n) - idx - 1)) for _ in range(int(input())): n, k = input().split() k = int(k) if nr_distinct(n) <= k: print(n) continue found = False for idx in range(len(n) - 1, -1, -1): ans = get(n, idx, k) if ans != -1: print(ans) found = True break if not found: print("".jioin(["1"] * (len(n) + 1)))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_CALL STRING BIN_OP LIST VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_CALL STRING BIN_OP LIST VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR NUMBER
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
import sys input = sys.stdin.readline bits = [[] for _ in range(11)] for bit in range(1 << 10): se = set() for i in range(10): if bit >> i & 1: se.add(i) bits[len(se)].append(se) def main(): n, k = input().strip().split() k = int(k) l = len(n) times = (10**l - 1) // 9 min_ = 10**20 for se in bits[k]: if max(se) * times < int(n): continue tmp = -1 nex = [-1] * 10 for i in range(9, -1, -1): if i in se: tmp = i nex[i] = tmp ans = [] flg = False for i in range(l): if flg: ans.append(nex[0]) continue s = int(n[i]) t = nex[s] if t == s: ans.append(s) elif t != -1: ans.append(t) flg = True else: cnt = 1 while 1: s = ans[-1] ans.pop() if nex[s + 1] != -1: ans.append(nex[s + 1]) break cnt += 1 for _ in range(cnt): ans.append(nex[0]) flg = True min_ = min(min_, int("".join(map(str, ans)))) print(min_) for _ in range(int(input())): main()
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
for _ in range(int(input())): n, k = map(int, input().split()) l = n while len(set(str(n))) > k: if n % 10 == 0: n = n // 10 else: n += 1 end = str(min(str(n))) * (len(str(l)) - len(str(n))) print(int(str(n) + end))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
a = [] for i in range(10): for j in range(i, 10): for m in range(1, 11): for k in range(2**m): b = bin(k)[2:] b = "0" * (m - len(b)) + b y = "" for l in b: if l == "0": y += str(i) else: y += str(j) if int(y) > 0: a.append(int(y)) a.sort() for i in range(int(input())): n, k = input().split() k = int(k) if k == 1: if int(n[0] * len(n)) >= int(n): print(n[0] * len(n)) elif n[0] == "9": print("1" * (len(n) + 1)) else: print(str(int(n[0]) + 1) * len(n)) else: l = -1 r = len(a) n = int(n) while r - l > 1: m = (l + r) // 2 if a[m] < n: l = m else: r = m print(a[r])
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR IF VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
import sys input = lambda: sys.stdin.readline().strip() def solve(): n, k = map(int, input().split()) s = str(n) if len(set(s)) <= k: return n L = len(s) def helper(a, b): i = 0 while i < len(s) and s[i] in a + b: i += 1 if s[i] > b: j = i - 1 while j >= 0 and s[j] == b: j -= 1 if j >= 0: return s[:j] + b + a * (L - j - 1) else: return "9" * L elif s[i] < a: return s[:i] + a * (L - i) else: return s[:i] + b + a * (L - i - 1) if k == 1: i = 0 while s[i] == s[0]: i += 1 if s[i] < s[0]: res = s[0] * L else: c = str(int(s[0]) + 1) res = c * L else: res = min(helper(str(a), str(b)) for a in range(10) for b in range(a + 1, 10)) return res for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP STRING VAR IF VAR VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
for _ in range(int(input())): n, m = map(int, input().split()) if m == 1: s = str(n) if n <= int(s[0] * len(s)): print(int(s[0] * len(s))) else: print(int(str(int(s[0]) + 1) * len(s))) else: A = list(str(n)) if min(A) == max(A): print(n) else: x = -1 s = str(n) for i in range(len(A) - 1): if A[i] != A[i + 1]: x = i + 1 break if n <= int( s[: x + 1] + str(max(int(s[x]), int(s[x - 1]))) * (len(s) - x - 1) ): Ans = s[: x + 1] mi = str(min(int(s[x]), int(s[x - 1]))) ma = str(max(int(s[x]), int(s[x - 1]))) for i in range(x + 1, len(s)): if int(Ans + mi + ma * (len(s) - i - 1)) >= n: Ans += mi else: Ans += ma print(Ans) elif int(s[x]) + 1 == int(s[0]): print(int(s[:x] + str(int(s[x]) + 1) + "0" * (len(s) - x - 1))) else: print( int( s[:x] + str(int(s[x]) + 1) + str(min(int(s[x]) + 1, int(s[x - 1]))) * (len(s) - x - 1) ) )
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
for _ in range(int(input())): n, k = map(int, input().split()) n = str(n) stack = [["", 0]] l = len(n) store = "" while stack: num, loop = stack.pop() if len(num) == l and len(set(num)) <= k: store = num break flag = 0 for i in range(loop, 10): nn = num + str(i) if int(nn) >= int(n[: len(num) + 1]) and len(set(nn)) <= k: stack.append([nn, 0]) flag = 1 break if flag == 0: stack.append([num[:-1], int(num[-1]) + 1]) print(store)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
t = int(input()) for _ in range(t): n, k = map(int, input().split()) l = [] while n > 0: l.insert(0, n % 10) n = n // 10 digit = set() ans = [] idx = 0 while len(digit) < k - 1: digit.add(l[idx]) ans.append(l[idx]) idx += 1 while idx < len(l) and l[idx] in digit: ans.append(l[idx]) idx += 1 if len(ans) < len(l): tmp = idx digit.add(l[idx]) Ma = max(digit) fix = idx while tmp < len(l) and l[tmp] in digit: if l[tmp] < Ma: fix = tmp tmp += 1 if tmp < len(l) and max(digit) > l[tmp]: for i in range(idx, tmp): ans.append(l[i]) M = Ma for item in digit: if item > l[tmp]: M = min(M, item) ans.append(M) m = min(digit) for _ in range(tmp + 1, len(l)): ans.append(m) elif tmp < len(l) and max(digit) < l[tmp]: if fix != idx: M = Ma for item in digit: if item > l[fix]: M = min(M, item) for i in range(idx, fix): ans.append(l[i]) ans.append(M) m = min(digit) for i in range(fix + 1, len(l)): ans.append(m) elif fix == idx: digit.remove(l[idx]) ans.append(l[idx] + 1) if l[idx] + 1 in digit: for i in range(idx + 1, len(l)): ans.append(0) else: digit.add(l[idx] + 1) m = min(digit) for i in range(idx + 1, len(l)): ans.append(m) else: for i in range(idx, tmp): ans.append(l[i]) tot = ans[0] for i in range(1, len(ans)): tot *= 10 tot += ans[i] print(tot)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
for _ in range(int(input())): n, k = map(int, input().split()) s = str(n) m = int(len(s) * "1") for i in range(1, 10): if i * m >= n: ans = i * m break if k == 2: for i in range(9): for j in range(i + 1, 10): flag = 0 for x in range(len(s)): if int(s[x]) < j: if int(s[x]) < i: ans = min(ans, int(s[:x] + str(i) * (len(s) - x))) ans = min(ans, int(s[:x] + str(j) + str(i) * (len(s) - x - 1))) if s[x] != str(i) and s[x] != str(j): flag = 1 break if not flag: ans = n print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
for _ in range(int(input())): n, k = input().split() k = int(k) if len(n) == 1: print(n) continue if k == 1: f = True ff = True for u in n: if n[0] < u and ff: f = False elif u < n[0]: ff = False if f: print(n[0] * len(n)) else: print(str(int(n[0]) + 1) * len(n)) else: f = True ans = [""] * len(n) i1 = -1 ib = 1 a, b = n[0], n[1] bp1 = str(int(b) + 1) ans[0], ans[1] = a, b sf = False for i in range(1, len(n)): if sf: sf = False continue if f and b == a and i < len(n) - 1: ans[i] = a b = n[i + 1] bp1 = str(int(b) + 1) ib = i + 1 if b != a: f = False ans[i + 1] = b sf = True continue if n[i] < min(a, b): ans[i] = min(a, b) ans = ans[: i + 1] + [min(a, b)] * (len(n) - i - 1) break elif n[i] == min(a, b): ans[i] = min(a, b) i1 = i elif n[i] < max(a, b): ans[i] = max(a, b) ans = ans[: i + 1] + [min(a, b)] * (len(n) - i - 1) break elif n[i] == min(a, b): ans[i] = min(a, b) i1 = i elif n[i] == max(a, b): ans[i] = max(a, b) else: ta = tuple() if i1 != -1: ans[i1] = max(a, b) ans = ans[: i1 + 1] + [min(a, b)] * (len(n) - i1 - 1) ta = tuple(ans) ans[ib] = bp1 ans = ans[: ib + 1] + [min(a, bp1) if a != bp1 else "0"] * ( len(n) - ib - 1 ) if not ta or tuple(ans) < ta: ta = tuple(ans) ans = list(ta) break print(*ans, sep="")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP LIST FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP LIST FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP LIST FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP LIST VAR VAR FUNC_CALL VAR VAR VAR STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
It is a simplified version of problem F2. The difference between them is the constraints (F1: $k \le 2$, F2: $k \le 10$). You are given an integer $n$. Find the minimum integer $x$ such that $x \ge n$ and the number $x$ is $k$-beautiful. A number is called $k$-beautiful if its decimal representation having no leading zeroes contains no more than $k$ different digits. E.g. if $k = 2$, the numbers $3434443$, $55550$, $777$ and $21$ are $k$-beautiful whereas the numbers $120$, $445435$ and $998244353$ are not. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing two integers $n$ and $k$ ($1 \le n \le 10^9$, $1 \le k \le 2$). -----Output----- For each test case output on a separate line $x$ β€” the minimum $k$-beautiful integer such that $x \ge n$. -----Examples----- Input 4 1 1 221 2 177890 2 998244353 1 Output 1 221 181111 999999999 -----Note----- None
for s in [*open(0)][1:]: prefix, k = map(int, s.split()) n = str(prefix) while len(set(str(prefix))) > k: prefix = prefix // 10 if not prefix % 10 else prefix + 1 prefix = str(prefix) suffix = str(min(prefix) * (len(n) - len(prefix))) print(prefix + suffix)
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def rs(): return input().strip() def ri(): return int(input()) def ria(): return list(map(int, input().split())) def ia_to_s(a): return " ".join([str(s) for s in a]) def how_many(m, a, pre, j): return 2 ** (m - 1 - j) - sum([(1) for x in a if x.startswith(pre) and x[j] == "0"]) def solve(n, m, a): k = (2**m - n + 1) // 2 s = "" for j in range(m): x = how_many(m, a, s, j) if x >= k: s += "0" else: s += "1" k -= x return s def main(): for _ in range(ri()): n, m = ria() a = [] for __ in range(n): a.append(rs()) print(solve(n, m, a)) main()
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR STRING VAR STRING VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def solve(): n, m = map(int, input().split()) a = [int(input(), 2) for _ in range(n)] a = sorted(a) Del = {} def push(x): Del[x] = 1 def process(cur, dx): cur += dx while cur in Del: cur += dx return cur mean = (1 << m - 1) - 1 cur = 0 for x in a: if cur == 0: if x <= mean: mean = process(mean, 1) elif x >= mean: mean = process(mean, -1) push(x) cur = (cur + 1) % 2 l = len(bin(mean)[2:]) print("0" * (m - l) + bin(mean)[2:]) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_DEF VAR VAR WHILE VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
testcases = int(input()) for _ in range(testcases): n, m = map(int, input().split()) d = dict() mid = 2 ** (m - 1) - 1 for i in range(n): s = int(input(), 2) d[s] = 1 if i % 2 == 0: if s <= mid: mid += 1 while mid in d: mid += 1 elif s >= mid: mid -= 1 while mid in d: mid -= 1 s = "" + bin(mid) s = s[2:] if len(s) < m: s = "0" * (m - len(s)) + s print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def read_int(): return int(input()) def read_string_array(): return input().split() def read_int_array(): return [int(_) for _ in read_string_array()] def read_line(): return input() def solve(n, m, a): a = [int(_, 2) for _ in a] median = (2**m - 1) // 2 k = 2**m removed = set() c = 0 for b in a: removed.add(b) if b <= median: median += 1 while median in removed: median += 1 if k % 2 == 1: median -= 1 while median in removed: median -= 1 k -= 1 return bin(median)[2:].zfill(m) T = read_int() for _ in range(T): N, M = read_int_array() A = [input() for _ in range(N)] print(solve(N, M, A))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def count_smaller(value, banlist): total = value for v in banlist: if v < value: total -= 1 return total cases = int(input()) for case in range(cases): n, m = map(int, input().split()) banned = [] for i in range(n): banned.append(int(input(), 2)) desindex = ((1 << m) - n - 1) // 2 candid = desindex while count_smaller(candid, banned) < desindex or candid in banned: candid += 1 print(bin(candid)[2:].zfill(m))
FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
from sys import stdin, stdout def input(): return stdin.readline() def ans(n, m, l, f, low, high): for i in range(low, high + 1): mi = 0 ma = 0 for j in l: if j <= i: mi += 1 if j > i: ma += 1 rig = f - i - ma lef = i + 1 - mi if lef - rig == 1 or rig == lef: return i def main(): for _ in range(int(input())): n, m = map(int, input().split()) f = 2**m - 1 l = [] for i in range(n): l.append(int(input().strip(), 2)) med = 2 ** (m - 1) - 1 if med - n // 2 - 1 < 0: low = 0 else: low = med - n // 2 - 1 if med + n // 2 + 1 > f - 1: high = f else: high = med + n // 2 + 1 k = bin(ans(n, m, l, f, low, high))[2:] print("0" * (m - len(k)) + k) main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def add(s): b = [] carry = 1 for i in range(len(s) - 1, -1, -1): if carry == 0: cur = s[i] elif s[i] == "0": cur = "1" carry = 0 else: cur = "0" b.append(cur) res = "".join(b)[::-1] return res def sub(s): b = [] if s[-1] == "1": cur = "0" carry = 0 else: cur = "1" carry = 1 b.append(cur) for i in range(len(s) - 2, -1, -1): if carry == 0: cur = s[i] elif s[i] == "1": cur = "0" carry = 0 else: cur = "1" carry = 1 b.append(cur) res = "".join(b)[::-1] return res def update(x, func, b): x = func(x) while x in b: x = func(x) return x T = int(input()) for _ in range(T): n, m = list(map(int, input().split())) a = [input() for _ in range(n)] ans = "0" + "1" * (m - 1) b = set() k = 0 for i, x in enumerate(a): if x < ans: if k == 0: ans = update(ans, add, b) elif x > ans: if k == 1: ans = update(ans, sub, b) elif x == ans: if k == 0: ans = update(ans, add, b) else: ans = update(ans, sub, b) k = (k + 1) % 2 b.add(x) print(ans)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def binary_number(line): count = len(line) number = 0 degree = 0 for x in range(count): if line[x] == "1": number += pow(2, count - 1 - degree) degree += 1 return number def binary_line(number, long): line = "" for x in range(long): if number % 2 == 1: line = "1" + line else: line = "0" + line number = number // 2 return line tests = int(input()) for i in range(tests): numbers = input().split() n = int(numbers[0]) m = int(numbers[1]) deleted = [] position = (pow(2, m) - n - 1) // 2 for x in range(n): deleted.append(binary_number(input())) deleted.sort() for x in range(len(deleted)): if deleted[x] <= position: position += 1 else: break print(binary_line(position, m))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
T = int(input()) for _ in range(T): N, M = map(int, input().split()) binary = {int(input(), 2) for _ in range(N)} ALL = 1 << M L = -1 R = ALL K = (ALL - N) // 2 half_or_bigger = ( lambda x: ALL - x - sum(1 if bina > x else 0 for bina in binary) - 1 ) while R - L > 1: mid = (R + L) // 2 if half_or_bigger(mid) >= K: L = mid else: R = mid while L in binary: L -= 1 print(bin(L)[2:].zfill(M))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) cnt = int(pow(2, m)) a = [0] * n for i in range(n): a[i] = int(input(), 2) low = max(cnt // 2 - 105, 0) high = min(cnt // 2 + 105, cnt - 1) ans = 0 while low <= high: mid = (low + high) // 2 less = mid - sum(x < mid for x in a) more = cnt - mid - 1 - sum(x > mid for x in a) equal = sum(x == mid for x in a) if equal == 0 and (less == more or less == more - 1): ans = mid break if equal == 0: if less < more: low = mid + 1 else: high = mid - 1 elif equal == 1: if more > less: low = mid + 1 else: high = mid - 1 fv = bin(ans).replace("0b", "") for i in range(m - len(fv)): print("0", end="") print(fv)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
import sys input = sys.stdin.readline def print(val): sys.stdout.write(val + "\n") def prog(): for _ in range(int(input())): n, m = map(int, input().split()) missing = [int(input().strip(), 2) for i in range(n)] missing.sort() median = (2**m - n - 1) // 2 for i in missing: if median >= i: median += 1 output = bin(median)[2:] print("0" * (m - len(output)) + output) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def solve(): n, m = map(int, input().split()) a = [] for _ in range(n): a.append(input()) a.sort() ans = int((2**m - n - 1) // 2) for i in a: if f(i) > ans: s = [] for j in range(m): s.append(str(ans >> j & 1)) print("".join(s[::-1])) break else: ans += 1 else: s = [] for j in range(m): s.append(str(ans >> j & 1)) print("".join(s[::-1])) def f(x): tmp = 0 for i, j in enumerate(reversed(x)): tmp += 2**i * int(j) return tmp t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def dd(i, nn): s = bin(i).replace("0b", "") l = len(s) kk = "" for i in range(nn - l): kk = kk + "0" print(kk + s) t = int(input()) for i in range(t): n, m = map(int, input().split()) aa = [] for pp in range(n): pp = input() aa.append(int(pp, 2)) y = 2 ** (m - 1) - 1 a = max(0, y - n) b = min(y + n, 2**m - 1) for i in range(a, b + 1): l = i r = 2**m - 1 - i flag = 1 for k in aa: if k == i: flag = 0 break if k < i: l = l - 1 else: r = r - 1 if flag == 0: continue if n % 2 == 0 and l == r - 1: dd(i, m) if n % 2 != 0 and l == r: dd(i, m)
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
t = int(input()) for i in range(t): n, m = map(int, input().split()) median = (2**m - 1 - n) // 2 a = [int(input(), 2) for i in range(n)] for i in sorted(a): if i <= median: median += 1 y = bin(median)[2:] print("0" * (m - len(y)) + y)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
t = int(input()) for i in range(t): n, m = map(int, input().split()) arr = [] for j in range(n): x = input() arr.append(int(x, 2)) current_median = 2 ** (m - 1) - 1 to_left = (2**m - n - 1) // 2 + 1 while True: curr_to_left = current_median + 1 present = False for j in arr: if j == current_median: present = True if j <= current_median: curr_to_left -= 1 if curr_to_left == to_left and present == False: print(bin(current_median)[2:].zfill(m)) break elif curr_to_left < to_left: current_median += 1 else: current_median -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
t = int(input()) for i in range(t): n, m = map(int, input().split()) a = [int(input(), 2) for _ in range(n)] ans = (2**m - n - 1) // 2 for s in sorted(a): if s <= ans: ans += 1 ans = bin(ans) ans = ans[2:] ans = "0" * (m - len(ans)) + ans print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
t = int(input()) for y in range(t): n, m = map(int, input().split()) mid = 2 ** (m - 1) - 1 d = dict() for i in range(n): x = int(input(), 2) d[x] = 1 if i % 2 == 0: if x <= mid: mid += 1 while mid in d: mid += 1 elif x >= mid: mid -= 1 while mid in d: mid -= 1 t = "" for i in range(m - 1, -1, -1): x = 2**i if mid & x: t += "1" else: t += "0" print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def solve(): n, m = map(int, input().split()) a_s = [int(input(), 2) for _ in range(n)] total = 2**m - n med = (total + 1) // 2 l = -1 r = 2**m while r - l > 1: mid = (r + l) // 2 cnt = mid + 1 for a in a_s: if a <= mid: cnt -= 1 if cnt >= med: r = mid else: l = mid print(format(r, "b").zfill(m)) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def solve(): n, m = [int(x) for x in input().split()] a = [] for i in range(n): a.append(input()) a = [int(x, 2) for x in a] need = ((1 << m) - n - 1) // 2 + 1 cur = (1 << m - 1) - 1 while True: left = cur + 1 flag = bool(cur in a) for s in a: if s <= cur: left -= 1 if left == need and not flag: ans = bin(cur).split("b")[1] if len(ans) < m: ans = "0" * (m - len(ans)) + ans print(ans) return elif left < need: cur += 1 else: cur -= 1 t = int(input()) while t > 0: t -= 1 solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def binaryToDecimal(x): return int(x) if len(x) <= 1 else binaryToDecimal(x[:-1]) * 2 + int(x[-1]) def decimalToBinary(m, x): if x == 0: return "0" * m res = "" while x > 0: res = str(x % 2) + res x //= 2 while len(res) < m: res = "0" + res return res def upperBound(a, x): l = 0 r = len(a) - 1 res = -1 while l <= r: mid = (l + r) // 2 if a[mid] <= x: res = mid l = mid + 1 else: r = mid - 1 return 0 if res == -1 else res + 1 def lowerBound(a, x): l = 0 r = len(a) - 1 res = -1 while l <= r: mid = (l + r) // 2 if a[mid] >= x: res = mid r = mid - 1 else: l = mid + 1 return 0 if res == -1 else len(a) - res t = int(input()) for i in range(t): n, m = [int(i) for i in input().split()] a = [binaryToDecimal(input()) for j in range(n)] a.sort() l = 0 r = 2**m - 1 res = 0 key = 0 if n % 2 == 1 else -1 while l <= r: mid = (l + r) // 2 t1 = 0 if mid == 0 else mid - upperBound(a, mid - 1) t2 = 0 if mid == 2**m - 1 else 2**m - mid - 1 - lowerBound(a, mid + 1) if t1 - t2 == key: res = mid break elif t1 - t2 > key: r = mid - 1 else: l = mid + 1 print(decimalToBinary(m, res))
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN BIN_OP STRING VAR ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def solve(n, m, remove): l, r = 0, 2**m - 1 k = 2**m - n target = (k - 1) // 2 while l < r - 1: mid = (l + r) // 2 c = mid + 1 - sum(x <= mid for x in remove) if c - 1 >= target: r = mid else: l = mid c1 = l + 1 - sum(x <= l for x in remove) if c1 - 1 == target and l not in remove: print(("{0:0%db}" % m).format(l)) else: print(("{0:0%db}" % m).format(r)) return t = int(input()) for i in range(t): n, m = map(int, input().split()) remove = [] for j in range(0, n): remove.append(int(input(), 2)) solve(n, m, remove)
FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL BIN_OP STRING VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
import sys input = sys.stdin.readline def stringminus(s): i = -1 n = len(s) s2 = ["0"] * n for j in range(n): s2[j] = s[j] while s[i] == "0": s2[i] = "1" i -= 1 s2[i] = "0" sol = s2[0] for i in range(1, n): sol += s2[i] return sol def stringplus(s): i = -1 n = len(s) s2 = ["0"] * n for j in range(n): s2[j] = s[j] while s[i] == "1": s2[i] = "0" i -= 1 s2[i] = "1" sol = s2[0] for i in range(1, n): sol += s2[i] return sol for f in range(int(input())): rem = set() med = "0" isc = 0 n, m = map(int, input().split()) for i in range(m - 1): med += "1" for i in range(n): s = input() s = s[0:m] if s > med: isc += 1 if s < med: isc -= 1 if s == med: if isc == 0: isc = -1 else: isc = 2 rem.add(s) if isc == -1: med = stringplus(med) while med in rem: med = stringplus(med) isc = 1 if isc == 2: med = stringminus(med) while med in rem: med = stringminus(med) isc = 0 print(med)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR STRING ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def dec(b, m): return "{:0{}b}".format(int(b, 2) - 1, m) def inc(b, m): return "{:0{}b}".format(int(b, 2) + 1, m) def median(n, m, a): md = "0" + "1" * (m - 1) state = 1 seen = {} for e in a: if e > md: state -= 1 if state == -1: while True: md = dec(md, m) if md not in seen: break state = 1 elif e < md: state += 1 if state == 2: while True: md = inc(md, m) if md not in seen: break state = 0 elif e == md: if state == 1: while True: md = inc(md, m) if md not in seen: break state = 0 else: while True: md = dec(md, m) if md not in seen: break state = 1 seen[e] = 1 return md t = int(input().strip()) for _ in range(t): n, m = list(map(int, input().strip().split())) a = [] for _ in range(n): a.append(input().strip()) print(median(n, m, a))
FUNC_DEF RETURN FUNC_CALL STRING BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_DEF RETURN FUNC_CALL STRING BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def check(x, nums, k): small = x + 1 for i in nums: if i <= x: small -= 1 if small <= (k - 1) // 2: return False return True def solve(nums, m, ans): n = len(nums) low = 0 high = pow(2, m) - 1 x = 0 k = pow(2, m) - n while low <= high: mid = (low + high) // 2 if check(mid, nums, k): x = mid high = mid - 1 else: low = mid + 1 x = bin(x)[2:] if len(x) < m: x = "0" * (m - len(x)) + x ans.append(x) def main(): t = int(input()) ans = [] for i in range(t): n, m = map(int, input().split()) nums = set() for j in range(n): x = int(input(), 2) nums.add(x) solve(nums, m, ans) for i in ans: print(i) main()
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR WHILE VAR VAR ASSIGN 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 ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
import sys readline = sys.stdin.buffer.readline read = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") def solve(): n, m = nm() l = [int(ns(), 2) for t in [0] * n] s = set(l) k = ((1 << m) - n - 1) // 2 ng, ok = 1 << m, 0 while ng - ok > 1: mid = (ng + ok) // 2 c = mid - sum(x <= mid for x in l) if c <= k: ok = mid else: ng = mid for i in range(ok - 110, ok + 110): if i not in s and i - sum(x < i for x in l) == k: print(bin(i + (1 << 65))[-m:]) return return T = ni() for _ in range(T): solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN 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 VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR RETURN RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def op_one_median(median, removed, op): median += op while median in removed: median += op return median def add_one_median(median, removed): res = op_one_median(median, removed, 1) return res def remove_one_median(median, removed): res = op_one_median(median, removed, -1) return res T = int(input()) for t in range(T): N, M = list(map(int, input().split())) median = 2 ** (M - 1) - 1 removed = set() move_median_up = True for i in range(N): r = int(input(), 2) removed.add(r) if r < median: if move_median_up: median = add_one_median(median, removed) elif r > median: if not move_median_up: median = remove_one_median(median, removed) else: f = add_one_median if move_median_up else remove_one_median median = f(median, removed) move_median_up = not move_median_up print(format(median, "0{}b".format(M)))
FUNC_DEF VAR VAR WHILE VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
T = int(input()) for t in range(T): n, m = map(int, input().split()) nums = set() even = True curr_med = (2**m - 1) // 2 for i in range(n): num = int(input(), 2) nums.add(num) if num > curr_med: if not even: curr_med -= 1 while curr_med in nums: curr_med -= 1 elif num < curr_med: if even: curr_med += 1 while curr_med in nums: curr_med += 1 elif even: curr_med += 1 while curr_med in nums: curr_med += 1 else: curr_med -= 1 while curr_med in nums: curr_med -= 1 if even: even = False else: even = True binary_string = "{0:b}".format(curr_med) print((m - len(binary_string)) * "0" + binary_string)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def cast(now): if now == 0: return "" return cast(now // 2) + str(now % 2) def is_mediana(all_numbers, num, a): if num in a: return False left = num right = all_numbers - num - 1 for i in a: if i < num: left -= 1 else: right -= 1 return left == right or left + 1 == right for _ in range(int(input())): n, m = map(int, input().split()) a = [int(input(), 2) for i in range(n)] now = (2**m - n - 1) // 2 for i in range(100): if now - i >= 0 and is_mediana(2**m, now - i, a): ans = cast(now - i) ans = "0" * (m - len(ans)) + ans print(ans) break elif now + i < 2 ** m and is_mediana(2**m, now + i, a): ans = cast(now + i) ans = "0" * (m - len(ans)) + ans print(ans) break
FUNC_DEF IF VAR NUMBER RETURN STRING RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
from sys import stdin, stdout def solve(): n, m = map(int, stdin.readline().split()) median = (1 << m - 1) - 1 left, right = median, (1 << m) - 1 - median deleted = [] for _ in range(n): x = int(input(), 2) if left == right: left -= 1 if x >= median: median -= 1 while median in deleted: median -= 1 else: right -= 1 if x <= median: median += 1 while median in deleted: median += 1 deleted.append(x) median = bin(median)[2:] stdout.write("0" * (m - len(median)) + median + "\n") for _ in range(int(stdin.readline())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) arr = [] for i in range(n): arr.append(input().strip()) arr1 = [] for i in arr: arr1.append(int(i, 2)) a = (2**m - n - 1) // 2 arr1.sort() for i in arr1: if i <= a: a += 1 ans = bin(a).replace("0b", "") ans = "0" * (m - len(ans)) + ans print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def solve(): ans = 2 ** (m - 1) - 1 l = 0 removed = set() for i in range(n): removed.add(a[i]) if l == 0: if a[i] <= ans: ans = ansplus(ans, removed) elif a[i] >= ans: ans = ansminus(ans, removed) l ^= 1 ans = format(ans, "0" + str(m) + "b") print(ans) return def ansplus(ans, removed): ans += 1 while ans in removed: ans += 1 return ans def ansminus(ans, removed): ans -= 1 while ans in removed: ans -= 1 return ans t = int(input()) for tt in range(1, t + 1): n, m = [int(i) for i in input().split(" ")] a = [] for i in range(n): a.append(int(input(), 2)) solve()
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF VAR NUMBER WHILE VAR VAR VAR NUMBER RETURN VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def cvt(st): st = st[::-1] num = 0 for i, ix in enumerate(st): num += int(int(ix) * int(2**i)) return num for _ in range(int(input())): n, m = [int(i) for i in input().split()] maxx = 2**m - 1 ran = maxx + 1 med = maxx // 2 rem = set() num = maxx + 1 for i in range(n): if (ran - i) % 2 == 0: st = input() st = cvt(st) if st <= med: med += 1 while med in rem: med += 1 rem.add(st) else: st = input() st = cvt(st) if st >= med: med -= 1 while med in rem: med -= 1 rem.add(st) ans = [(0) for i in range(m)] i = 0 while med > 0: if med % 2 == 1: ans[i] = 1 else: ans[i] = 0 i += 1 med //= 2 a = "" for i in ans: a += str(i) print(a[::-1])
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
for _ in range(int(input())): n, m = map(int, input().split()) a = [None] * n for i in range(n): a[i] = input() a[i] = int(a[i], 2) now = 2 ** (m - 1) - 1 lr = (2**m - n - 1) // 2 + 1 while True: e, le = 0, now + 1 for i in range(n): if a[i] <= now: le = le - 1 if a[i] == now: e = e + 1 if le == lr and e == 0: ans = "" for i in range(m): ans = ans + chr(now % 2 + ord("0")) now = now // 2 for i in range(m): print(ans[m - i - 1], end="") print() break elif le < lr: now = now + 1 else: now = now - 1
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER STRING EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
import sys input = sys.stdin.readline (T,) = map(int, input().split()) for _ in range(T): N, M = map(int, input().split()) m = 2 ** (M - 1) - 1 vs = set() for i in range(N): x = int(input(), 2) vs.add(x) if i % 2: if m <= x: while True: m -= 1 if m not in vs: break elif x <= m: while True: m += 1 if m not in vs: break print(format(m, "0{}b".format(M)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF VAR VAR WHILE NUMBER VAR NUMBER IF VAR VAR IF VAR VAR WHILE NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
for _ in range(int(input())): n, m = map(int, input().split()) a = [int(input(), 2) for _ in range(n)] a.sort() k = (1 << m) - n median = (k - 1) // 2 for elem in a: if elem <= median: median += 1 median = bin(median)[2:] median = (m - len(median)) * "0" + median print(median)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
import sys def process(): n, m = [int(s) for s in sys.stdin.readline().split()] med = (2**m - 1 - n) // 2 ss = set() count = 0 for i in range(n): r = int(sys.stdin.readline().strip(), 2) if r < med: count += 1 else: ss.add(r) while med in ss: med += 1 for i in range(count): med += 1 while med in ss: med += 1 print(format(med, "b").zfill(m)) T = int(sys.stdin.readline()) for x in range(T): process()
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def convert(z): s = "" while z > 0: if z % 2: s = "1" + s else: s = "0" + s z = z // 2 return s def num(s): sumi = 0 for i in s: sumi = 2 * sumi + int(i) return sumi def f(x): global lfi cnt1 = 0 cnt2 = 0 for i in lfi: if i == x: cnt1 = 1 if i < x: cnt2 += 1 return cnt2, cnt1 def bs(L, R, req): M = (L + R) // 2 t = f(M) if M - t[0] == req and t[1] == 0: return M if M - t[0] <= req: return bs(M + 1, R, req) return bs(L, M - 1, req) for _ in range(int(input())): l = input().split() n = int(l[0]) m = int(l[1]) lfi = [] for i in range(n): s = input() no = num(s) lfi.append(no) req = (pow(2, m) - n - 1) // 2 zo = bs(0, pow(2, m) - 1, req) z = convert(zo) for i in range(m - len(z)): print("0", end="") print(z)
FUNC_DEF ASSIGN VAR STRING WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER RETURN VAR IF BIN_OP VAR VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def quick_gcd(a, b): for i in range(min(a, b), 0, -1): if a % i == 0 and b % i == 0: return i def go(): n, m = map(int, input().split()) s = [int(input(), 2) for _ in range(n)] sets = set() v = (2**m - 1 - n) // 2 for ss in s: if ss <= v: v += 1 while v in sets: v += 1 sets.add(ss) res = bin(v)[2:] return "0" * (m - len(res)) + res t = int(input()) ans = [] for _ in range(t): ans.append(str(go())) print("\n".join(ans))
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def read_int(): return int(input()) def read_ints(): return list(map(int, input().split(" "))) t = read_int() for case_num in range(t): n, m = read_ints() a = [] for i in range(n): a.append(int(input(), 2)) a.sort() k = 2**m - n ans = (k - 1) // 2 for ai in a: if ai <= ans: ans += 1 binary = "{:b}".format(ans) print(binary.rjust(m, "0"))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
for i in range(int(input())): n, m = map(int, input().split()) a = [int(input(), 2) for _ in range(n)] x = (2**m - n - 1) // 2 for v in sorted(a): if v <= x: x += 1 print("{0:0={1}b}".format(x, m))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
INF = 10**18 t = int(input()) n = int() m = int() def bin(x): global m res = str() while x > 0: res += str(x % 2) x //= 2 while len(res) < m: res += "0" return res[::-1] def get(x): res = 0 x = x[::-1] for i in range(len(x)): res += 2**i * int(x[i]) return res def solve(): ll, rr = map(int, input().split()) global n n = ll global m m = rr a = [] for i in range(n): a.append(get(input())) a.sort() it = 0 c = max((2**m - n) // 2 - 300, 0) lm = 2**m - 1 while True: while it < n and a[it] < c: it += 1 l = c - it r = lm - c - n + it if r < l: print(bin(c - 1)) return c += 1 for tt in range(t): solve()
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR STRING RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) binaryString = [] for _ in range(n): binaryString.append(input()) if m <= 8: isIncluded = [True] * 2**m for val in binaryString: isIncluded[int(val, 2)] = False curIndex = 0 medianIndex = 2 ** (m - 1) - n // 2 - 1 ans = -1 for i in range(len(isIncluded)): if isIncluded[i] == True and curIndex == medianIndex: ans = i break if isIncluded[i] == True: curIndex += 1 res = bin(ans)[2:] res = "0" * (m - len(res)) + res print(res) else: isIncluded = [True] * 512 minusOB = 0 plusOB = 0 for val in binaryString: if val[0] == "0": indicator = False for i in range(1, m - 8): if val[i] == "0": minusOB += 1 indicator = True break if not indicator: isIncluded[int(val[-8:], 2)] = False else: indicator = False for i in range(1, m - 8): if val[i] == "1": plusOB += 1 indicator = True break if not indicator: isIncluded[256 + int(val[-8:], 2)] = False curIndex = 0 medianIndex = 256 + minusOB - n // 2 - 1 isMedianInMinus = False for i in range(512): if isIncluded[i] == True and curIndex == medianIndex: ans = i break if isIncluded[i] == True: curIndex += 1 if ans < 256: res = bin(ans)[2:] res = "0" + "1" * (m - 1 - len(res)) + res print(res) else: res = bin(ans - 256)[2:] res = "1" + "0" * (m - 1 - len(res)) + res print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP STRING BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP STRING BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
from sys import stdin, stdout int_in = lambda: int(stdin.readline()) arr_in = lambda: [int(x) for x in stdin.readline().split()] mat_in = lambda rows: [arr_in() for y in range(rows)] str_in = lambda: stdin.readline().strip() out = lambda o: stdout.write("{}\n".format(o)) arr_out = lambda o: out(" ".join(map(str, o))) bool_out = lambda o: out("YES" if o else "NO") tests = lambda: range(1, int_in() + 1) case_out = lambda i, o: out("Case #{}: {}".format(i, o)) def solve(n, m, strings): strings = set(strings) median = pow(2, m - 1) while median in strings: median += 1 counts_lower = median for string in strings: if string < median: counts_lower -= 1 needed_lower = (pow(2, m) - 1 - n) // 2 while counts_lower != needed_lower: if counts_lower < needed_lower: median += 1 if median not in strings: counts_lower += 1 else: median -= 1 if median not in strings: counts_lower -= 1 return convert_to_binary_string_of_length(m, median) def convert_to_binary_string_of_length(m, median): median = bin(median)[2:] median = "0" * m + median return median[len(median) - m :] for i in tests(): n, m = arr_in() strings = [int(str_in(), 2) for x in range(n)] out(solve(n, m, strings))
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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
for _ in range(int(input())): n, m = map(int, input().split()) x = (2**m - n - 1) // 2 arr = [] for i in range(n): arr.append(int(input(), 2)) arr.sort() for i in range(0, n): if arr[i] <= x: x += 1 if x in arr: x += 1 y = bin(x)[2:] print("0" * (m - len(y)) + y)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def toBinary(i, digit): b = bin(i).replace("0b", "") n = digit - len(b) x = "" for itr in range(n): x = x + "0" return x + b def main(): t = int(input()) for _ in range(t): n, m = map(int, input().split()) l = [] for i in range(n): l.append(int(input(), 2)) l.sort() median = (2**m - n - 1) // 2 for i in l: if i <= median or i == median: median += 1 ans = toBinary(median, m) print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
for _ in range(int(input())): n, m = map(int, input().split()) a = [] mp = {} for i in range(n): s = input() s = int(s, 2) a.append(s) l = 1 << m median = (l - 1) // 2 for elem in a: l -= 1 mp[elem] = 1 if l % 2 == 1 and median >= elem: median += 1 while mp.get(median, 0) == 1: median += 1 elif l % 2 == 0 and median <= elem: median -= 1 while mp.get(median, 0) == 1: median -= 1 print(bin(median)[2:].zfill(m))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
from sys import stdin, stdout print = stdout.write input = stdin.readline def solve(): n, m = map(int, input().split()) x = pow(2, m - 1) - 1 cou = x * 2 se = set() for _ in range(n): y = int(input().strip(), 2) se.add(y) if y >= x and cou % 2 == 1: x -= 1 while x in se: x -= 1 elif y <= x and cou % 2 == 0: x += 1 while x in se: x += 1 cou -= 1 tt = bin(x)[2:] print("0" * (m - len(tt)) + tt + "\n") def main(): for t in range(int(input())): solve() main()
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Consider all binary strings of length $m$ ($1 \le m \le 60$). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly $2^m$ such strings in total. The string $s$ is lexicographically smaller than the string $t$ (both have the same length $m$) if in the first position $i$ from the left in which they differ, we have $s[i] < t[i]$. This is exactly the way strings are compared in dictionaries and in most modern programming languages when comparing them in a standard way. For example, the string 01011 is lexicographically smaller than the string 01100, because the first two characters are the same, and the third character in the first string is less than that in the second. We remove from this set $n$ ($1 \le n \le \min(2^m-1, 100)$) distinct binary strings $a_1, a_2, \ldots, a_n$, each of length $m$. Thus, the set will have $k=2^m-n$ strings. Sort all strings of the resulting set in lexicographical ascending order (as in the dictionary). We number all the strings after sorting from $0$ to $k-1$. Print the string whose index is $\lfloor \frac{k-1}{2} \rfloor$ (such an element is called median), where $\lfloor x \rfloor$ is the rounding of the number down to the nearest integer. For example, if $n=3$, $m=3$ and $a=[$010, 111, 001$]$, then after removing the strings $a_i$ and sorting, the result will take the form: $[$000, 011, 100, 101, 110$]$. Thus, the desired median is 100. -----Input----- The first line contains an integer $t$ ($1 \le t \le 1000$)Β β€” the number of test cases. Then, $t$ test cases follow. The first line of each test case contains integers $n$ ($1 \le n \le \min(2^m-1, 100)$) and $m$ ($1 \le m \le 60$), where $n$ is the number of strings to remove, and $m$ is the length of binary strings. The next $n$ lines contain $a_1, a_2, \ldots, a_n$Β β€” distinct binary strings of length $m$. The total length of all given binary strings in all test cases in one test does not exceed $10^5$. -----Output----- Print $t$ answers to the test cases. For each test case, print a string of length $m$Β β€” the median of the sorted sequence of remaining strings in the corresponding test case. -----Example----- Input 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 Output 100 010 0 1 11 -----Note----- The first test case is explained in the statement. In the second test case, the result after removing strings and sorting is $[$001, 010, 101, 110$]$. Therefore, the desired median is 010.
def prog(): for _ in range(int(input())): n, m = map(int, input().split()) missing = [int(input().strip(), 2) for i in range(n)] missing.sort() missing2 = set(missing) median = (2**m - n + 1) // 2 curr = median - 1 index = median while curr in missing2: curr -= 1 index -= 1 for i in missing: if curr > i: index -= 1 else: break while index != median: curr += 1 if curr not in missing: index += 1 value = curr value = bin(value)[2:] value = "0" * (m - len(value)) + value print(value) prog()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR