description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
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 o in range(int(input())): n, m = map(int, input().split()) M = 1 << m s = [] for i in range(n): a = input() s.append(a) l = max(0, M // 2 - 110) r = min(M - 1, M // 2 + 110) A = [int(a, 2) for a in s] for md in range(l, r + 1): if md in A: continue smaller = md for a in A: if a < md: smaller -= 1 if smaller == (M - len(s) - 1) // 2: print(bin(md + M)[3:])
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 NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP 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.
t = int(input()) for _ in range(t): m, n = map(int, input().split()) topn = (1 << n) - 1 data = [int(input(), 2) for _ in range(m)] data.sort() data.reverse() median = topn // 2 expected = (topn - m) // 2 base = max(median - 200, 0) top = min(median + 200, topn) l = base for x in range(base, top + 1): l += 1 while data and data[-1] <= x: data.pop() l -= 1 if l - 1 == expected: num = bin(x)[2:] print("0" * (n - len(num)) + num) break
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 VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR 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 main(): for t in range(int(input())): 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: median += 1 while median in l: median += 1 ans = bin(median)[2:] temp = "" for i in range(len(ans), m): temp += "0" print(temp + ans) main()
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 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 NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP 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(mid): small = mid large = 2**m - mid - 1 for val in s: if val < mid: small -= 1 if val > mid: large -= 1 return small < large def solve2(mid): small = mid large = 2**m - mid - 1 for val in s: if val < mid: small -= 1 if val > mid: large -= 1 return small <= large t = int(input()) for _ in range(t): n, m = map(int, input().split()) s = set([int(input(), 2) for i in range(n)]) ng = -1 ok = 2**m + 2 if len(s) % 2 == 0: while abs(ok - ng) > 1: mid = (ok + ng) // 2 if mid in s: for i in range(ng + 1, ok): if i not in s: mid = i break if solve(mid): ng = mid else: ok = mid print("{:060b}".format(ng)[::-1][0:m][::-1]) else: while abs(ok - ng) > 1: mid = (ok + ng) // 2 if mid in s: for i in range(ng + 1, ok): if i not in s: mid = i break if solve2(mid): ng = mid else: ok = mid print("{:060b}".format(ng)[::-1][0:m][::-1])
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN 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 FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER 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.
TC = int(input()) for t_itr in range(TC): N, M = map(int, input().rstrip().split()) A = [] for _ in range(N): r = 0 for x in map(int, input()): r = r * 2 + x A.append(r) A.sort() mid = (2**M - 1 - N) // 2 for a in A: if a <= mid: mid += 1 print("{{:0>{}b}}".format(M).format(mid))
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 FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR 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 EXPR FUNC_CALL VAR FUNC_CALL 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.
for _ in range(int(input())): n, m = map(int, input().split()) a = (0 + 2**m - n - 1) // 2 arr = [] for i in range(n): arr.append(int(input(), 2)) arr.sort() flag = True for i in arr: if i <= a: a += 1 c = bin(a).replace("0b", "") for _ in range(m - len(c)): print("0", end="") print(c)
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 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 ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR 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.
t = int(input()) for i in range(t): n, m = map(int, input().split()) l = [] for j in range(n): s = input() l.append(int(s, 2)) l = sorted(l) length = 2**m newlen = length - n med = newlen // 2 if newlen % 2 == 0: med = med - 1 ele = med for j in range(len(l)): if l[j] <= med: med = med + 1 med = bin(med) med = med[2:] count = m - len(med) while count > 0: print(0, end="") count = count - 1 for j in range(len(med)): print(med[j], end="") print()
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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING 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 t = int(sys.stdin.readline().strip()) for _ in range(t): n, m = map(int, sys.stdin.readline().strip().split()) s = set() mid = 2 ** (m - 1) - 1 flag = 0 for i in range(n): x = int(sys.stdin.readline().strip(), 2) s.add(x) if x > mid and flag == 0: flag = 1 elif x > mid and flag == 1: flag = 0 mid -= 1 while mid in s: mid -= 1 elif x < mid and flag == 0: flag = 1 mid += 1 while mid in s: mid += 1 elif x < mid and flag == 1: flag = 0 elif flag == 1: flag = 0 mid -= 1 while mid in s: mid -= 1 else: flag = 1 mid += 1 while mid in s: mid += 1 ans = bin(mid)[2:] ans = "0" * (m - len(ans)) + ans print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER 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 VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR 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
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 kek in range(t): n, m = map(int, input().split()) de = [] for i in range(n): a = input() de.append(int(a, 2)) mid = 2 ** (m - 1) - 1 test = set(range(mid - 150, mid + 151)) delta = 0 for i in de: if i in test: test.remove(i) else: if i < mid - 150: delta -= 1 if i > mid + 150: delta += 1 test = list(test) test.sort() ans = test[(len(test) - delta) // 2] ans = bin(ans)[2:] print("0" * (m - len(ans)) + 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 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 FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL 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 main(): for _ in range(int(input())): n, m = map(int, input().split()) a = {int(input(), 2) for _ in range(n)} median = (2**m - n - 1) // 2 new_a = a.copy() for _ in range(n): for ai in a: if ai <= median: median += 1 new_a.discard(ai) a = new_a.copy() try: res = bin(median)[2:] print((m - len(res)) * "0" + res) except: print("0" * m) main()
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 VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR EXPR FUNC_CALL VAR BIN_OP STRING 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()) while t != 0: t -= 1 n, m = map(int, input().split()) p = 2**m median = (p - n - 1) // 2 a = [] for _ in range(n): s = int(input(), 2) a.append(s) a.sort() for i in a: if i <= median: median += 1 while median in a: median += 1 ans = bin(median)[2:] for i in range(m - len(ans)): ans = "0" + ans print(ans)
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 BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP 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.
for _ in range(int(input())): n, k = map(int, input().split()) a = [int(input(), 2) for i in range(n)] L, R = 0, 2**k - 1 r = R while L <= R: m = (L + R) // 2 c = m + 1 - sum(x <= m for x in a) if c >= (2**k - n + 1) // 2: r, R = m, m - 1 else: L = m + 1 while r in a: r -= 1 print(("{0:0%db}" % k).format(r))
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 VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR 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 VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL BIN_OP 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.
def b_n(s): return int(s, 2) def n_b(n): p = str(bin(n).replace("0b", "")) while len(p) < m: p = "0" + p return p t = int(input()) for w in range(t): nm = list(map(int, input().strip().split())) n = nm[0] m = nm[1] arr = [] for g in range(n): arr.append(str(input())) for i in range(n): arr[i] = b_n(arr[i]) k1 = 2**m - n k = (k1 - 1) // 2 for i in sorted(arr): if i <= k: k += 1 print(n_b(k))
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR 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.buffer.readline t = int(input()) for i in range(t): a, b = list(map(int, input().split())) x = 2 ** (b - 1) zz = min(100, 2 ** (b - 1)) middle = [j for j in range(x - zz, x + zz)] left = 0 right = 0 for j in range(a): s = input() s = int(s, 2) if s in middle: middle.remove(s) elif s < min(middle): left += 1 else: right += 1 pos = (len(middle) - 1 + left - right) // 2 ans = middle[pos] ans = "{0:b}".format(ans) ans = "0" * (b - len(ans)) + ans print(ans)
IMPORT ASSIGN VAR 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 NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL STRING 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 search(array, x): l = len(array) for i in range(l): if array[i] == x: return True return False def binarySearch(array, l, r, x): while l <= r: m = (l + r) // 2 if array[m] == x: return m elif array[m] > x: r = m - 1 else: l = m + 1 t = int(input()) for i in range(t): n, m = list(map(int, input().split())) arr = [] number = 2**m // 2 - 1 for j in range(n): s = str(input()) arr.append(s) num1 = [] for j in range(n): s = 0 for k in range(1, m + 1): s += int(arr[j][-k]) * 2 ** (k - 1) num1.append(s) c = 1 for j in range(n): if num1[j] < number: if c == 1: number += 1 while search(num1[:j], number) == True: number += 1 c = 0 else: c = 1 if num1[j] > number: if c == 1: c = 0 else: number -= 1 while search(num1[:j], number) == True: number -= 1 c = 1 if num1[j] == number: if c == 0: number -= 1 while search(num1[:j], number) == True: number -= 1 c = 1 else: number += 1 while search(num1[:j], number) == True: number += 1 c = 0 ans = bin(number) ans = ans[2:] len2 = len(ans) s = "" for j in range(m - len2): s += "0" print(s + ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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 LIST 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 EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING 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.
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**19 MOD = 10**9 + 7 for _ in range(INT()): N, M = MAP() A = [int(input(), 2) for i in range(N)] mid = 2**M // 2 - 1 used = set() ln = 2**M for a in A: if a <= mid and ln % 2 == 0: tmp = mid + 1 while tmp in used: tmp += 1 mid = tmp if a >= mid and ln % 2 == 1: tmp = mid - 1 while tmp in used: tmp -= 1 mid = tmp used.add(a) ln -= 1 ans = format(mid, "b").zfill(M) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR 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 NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL 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 inp = [x for x in sys.stdin.read().split()] ii = 0 ttt = int(inp[ii]) ii += 1 res = [] for _ in range(ttt): n, m = int(inp[ii]), int(inp[ii + 1]) ii += 2 a = [] for _ in range(n): temp = inp[ii] ii += 1 x = 0 for i in range(m): if temp[i] == "1": x += 1 << m - 1 - i a.append(x) t = ((1 << m) - n - 1) // 2 low, high = 0, 1 << m while high - low > 1: mid = low + high >> 1 if mid - sum([(x < mid) for x in a]) <= t: low = mid else: high = mid res.append("".join(str(int(not not low & 1 << i)) for i in range(m - 1, -1, -1))) print("\n".join(str(x) for x in res))
IMPORT ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING 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 t in range(int(input())): n, m = map(int, input().split()) rem = [] real_rem = [] med = 2 ** (m - 1) - 1 for i in range(n): iii = input() rem.append(int(iii, 2)) even = True while rem: cur = rem[0] real_rem += [cur] rem = rem[1:] if even: if med < cur: pass else: med += 1 while med in real_rem: med += 1 elif med <= cur: med -= 1 while med in real_rem: med -= 1 else: pass even = not even print(bin(med)[2:].rjust(m, "0"))
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 LIST ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER VAR LIST VAR ASSIGN VAR VAR NUMBER IF VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER 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.
T = int(input()) def move_up(taken, s): s += 1 while s in taken: s += 1 return s def move_down(taken, s): s -= 1 while s in taken: s -= 1 return s for t in range(T): N, M = [int(_) for _ in input().split()] start = (1 << M - 1) - 1 taken = set() rd = True for i in range(N): el = int(input(), 2) taken.add(el) if el > start: if not rd: start = move_down(taken, start) elif el < start: if rd: start = move_up(taken, start) elif rd: start = move_up(taken, start) else: start = move_down(taken, start) rd = not rd print(format(start, "0{}b".format(M)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR NUMBER RETURN VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR 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 IF VAR ASSIGN VAR FUNC_CALL 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.
from sys import stdin input = stdin.readline for Ti in range(int(input().strip())): n, m = [int(x) for x in input().strip().split()] tc = 2**m mc = (tc - n + 1) // 2 a = [] for ni in range(n): a.append(int(input().strip(), 2)) a.sort() a.append(tc) ne = a[0] ns = -1 ci = 0 while ci < len(a): cc = ne - ns - 1 if mc - cc <= 0: ans = ns + mc break mc -= cc ci += 1 ns = ne ne = a[ci] print(bin(ans)[2:].zfill(m))
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR 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.
import sys input = lambda: sys.stdin.readline().strip() def solve(): n, m = map(int, input().split()) a = [] for i in range(n): a.append(int(input(), 2)) need = ((1 << m) - n - 1) // 2 + 1 cur = (1 << m - 1) - 1 while True: left = cur + 1 flag = False for i in range(n): flag |= a[i] == cur if a[i] <= cur: left -= 1 if left == need and not flag: ans = bin(cur)[2:] print("0" * (m - len(ans)) + ans) return elif left < need: cur += 1 else: cur -= 1 t = int(input()) while t: t -= 1 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 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 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 NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR 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.
for i in range(int(input())): n, m = list(map(int, input().split())) a = sorted([int(input(), base=2) for i in range(n)]) k = (2**m - n - 1) // 2 i = 0 while i < n and a[i] <= k: i += 1 k += 1 print("0" * (m - len(bin(k)[2:])) + bin(k)[2:])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER 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.
def add(middle): middle = list(middle) for i in range(len(middle) - 1, -1, -1): if middle[i] == "0": middle[i] = "1" break middle[i] = "0" s = "" for i in middle: s += i return s def subtract(middle): middle = list(middle) for i in range(len(middle) - 1, -1, -1): if middle[i] == "1": middle[i] = "0" break middle[i] = "1" s = "" for i in middle: s += i return s t = int(input()) while t: t += -1 n, m = map(int, input().split()) l = [] middle = "0" + "1" * (m - 1) mp = {} for i in range(max(0, 2 ** (m - 1) - 100), min(2**m, 2 ** (m - 1) + 101)): tmp = bin(i)[2:] tmp = "0" * (m - len(tmp)) + tmp mp[tmp] = 1 c1, c2 = 0, 0 for i in range(n): s = input() if s <= middle: c1 -= -1 if s == middle: if c1 % 2 == 0: middle = subtract(middle) while mp[middle] == 0: middle = subtract(middle) else: middle = add(middle) while mp[middle] == 0: middle = add(middle) elif c1 % 2: middle = add(middle) while mp[middle] == 0: middle = add(middle) else: c1 -= -1 if c1 % 2 == 0: middle = subtract(middle) while mp[middle] == 0: middle = subtract(middle) mp[s] = 0 print(middle)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR 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.
for _ in range(int(input())): n, m = map(int, input().split()) kl = [] for i in range(n): kl.append(int(input(), 2)) kl.sort() k = (2**m - 1 - n) // 2 for i in kl: if k >= i: k += 1 k = bin(k).replace("0b", "") k = (m - len(k)) * "0" + k print(k)
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 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 NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING 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.
for _ in range(int(input())): n, m = map(int, input().split()) x = pow(2, m - 1) - 1 cou = x * 2 se = set() for _ in range(n): y = int(input(), 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)
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 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 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 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.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, m = list(map(int, input().split())) A = sorted([int(input().strip(), 2) for i in range(n)]) A += [1 << 63] ind = (2**m - n - 1) // 2 indskip = 0 ANS = 0 while True: if A[indskip] == ANS: ANS += 1 indskip += 1 continue if A[indskip] - ANS <= ind: ind -= A[indskip] - ANS ANS = A[indskip] + 1 indskip += 1 else: ANS += ind break print(bin(ANS)[2:].zfill(m))
IMPORT ASSIGN VAR 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 FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR 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 for _ in range(int(stdin.readline())): n, m = map(int, stdin.readline().split()) median = (2**m - n - 1) // 2 countless = 0 countlarge = 0 arr = [int(stdin.readline(), 2) for i in range(n)] arr.sort() for i in arr: if i <= median: median += 1 median = bin(median)[2:] if m > len(median): stdout.write("0" * (m - len(median)) + median + "\n") else: stdout.write(median + "\n")
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP 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.
def calc(numbers, newans, i): count = 0 ansarr = "".join([str(x) for x in newans]) for num in numbers: if num[: i + 1] == ansarr: count += 1 return count t = int(input()) for _ in range(t): n, m = [int(c) for c in input().split()] numbers = [] for i in range(n): numbers.append(input()) ans = 0 ansarr = [] k = ((1 << m) - n - 1) // 2 + 1 for i in range(m): nums = 1 << m - 1 - i newans = ans | 0 remainums = nums - calc(numbers, ansarr + [0], i) if remainums < k: k -= remainums ansarr.append(1) newans = ans | 1 << m - i - 1 else: ansarr.append(0) newans = ans | 0 ans = newans print("".join([str(c) for c in ansarr]))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR LIST NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING 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 def main(): import sys input = sys.stdin.readline for __ in [0] * int(input()): n, m = map(int, input().split()) A = [int(input(), 2) for _ in [0] * n] mid = (1 << m - 1) - 1 L = set(range(max(0, mid - 120), min(mid + 121, 1 << m))) pari = 0 for a in A: if a in L: L.remove(a) if a < mid: if pari == 0: mid += 1 while mid not in L: mid += 1 else: pass elif a > mid: if pari == 0: pass else: mid -= 1 while mid not in L: mid -= 1 elif a == mid: if pari == 0: mid += 1 while mid not in L: mid += 1 else: mid -= 1 while mid not in L: mid -= 1 pari ^= 1 print(("0" * 60 + bin(mid)[2:])[-m:]) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR FOR VAR BIN_OP LIST NUMBER 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 BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING NUMBER FUNC_CALL VAR VAR NUMBER 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 def bin_median(size: int, removed: "Set[int]"): median = 2 ** (size - 1) - 1 straddling = True skips_up = {} skips_down = {} for removed_x in removed: cutoff = median + 1 if straddling else median up = skips_up.get(removed_x, removed_x + 1) down = skips_down.get(removed_x, removed_x - 1) skips_down[up] = down skips_up[down] = up if removed_x >= cutoff: if not straddling: median = skips_down.get(median, median - 1) elif straddling: median = skips_up.get(median, median + 1) straddling = not straddling return median count = int(sys.stdin.readline().rstrip()) for i in range(count): t_count, size = map(int, sys.stdin.readline().rstrip().split()) removed = set(int(sys.stdin.readline().rstrip(), 2) for _ in range(t_count)) print(f"{{:0{size}b}}".format(bin_median(size, removed)))
IMPORT FUNC_DEF VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR STRING 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.
import sys input = sys.stdin.readline def moveleft(s): n = int("0b" + s, 2) n -= 1 ss = format(n, "#b")[2:] return "0" * (len(s) - len(ss)) + ss def moveright(s): n = int("0b" + s, 2) n += 1 ss = format(n, "#b")[2:] return "0" * (len(s) - len(ss)) + ss def solve(n, L, check): mid = "0" + "1" * (n - 1) even = True for i in L: check[i] = True if i > mid: if even: even = False else: mid = moveleft(mid) while mid in check: mid = moveleft(mid) even = True elif i == mid: if even: mid = moveright(mid) while mid in check: mid = moveright(mid) even = False else: mid = moveleft(mid) while mid in check: mid = moveleft(mid) even = True elif even: mid = moveright(mid) while mid in check: mid = moveright(mid) even = False else: even = True return mid for i in " " * int(input()): n, m = map(int, input().split()) L = [] check = dict() for j in " " * n: L.append(input().strip()) print(solve(m, L, check))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER RETURN BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER RETURN BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP STRING BIN_OP STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR BIN_OP STRING 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.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) k = 2**m - n l = [-1] for i in range(n): s = input()[::-1] c = 0 for j in range(m): if s[j] == "1": c += pow(2, j) l.append(c) l.sort() l.append(2**m) count = (k + 1) // 2 for i in range(1, n + 2): dif = l[i] - l[i - 1] - 1 if dif < count: count -= dif else: ans = l[i - 1] + count break ans = bin(ans)[2:] print(ans.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 BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR 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 max_int = 1000000001 min_int = -max_int t = int(input()) for _t in range(t): n, m = map(int, sys.stdin.readline().split()) to_skip = [] for _n in range(n): s = int(sys.stdin.readline()[:-1], base=2) to_skip.append(s) to_skip.sort() mid = (2**m - n - 1) // 2 for elem in to_skip: if elem <= mid: mid += 1 else: break print(("{0:0>" + str(m) + "b}").format(mid))
IMPORT ASSIGN VAR NUMBER 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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR 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 EXPR FUNC_CALL VAR FUNC_CALL BIN_OP BIN_OP STRING 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.
import sys for _ in range(int(input())): n, m = map(int, sys.stdin.readline().split()) mid = 2**m - 1 >> 1 nume, deno = 2**m >> 1, 2**m deleted = set() for num in (int(sys.stdin.readline().rstrip(), 2) for _ in range(n)): deleted.add(num) deno -= 1 if num < mid: nume -= 1 elif num == mid: while mid in deleted: mid += 1 k = (deno + 1) // 2 while k > nume: nume += 1 mid += 1 while mid in deleted: mid += 1 while k < nume: nume -= 1 mid -= 1 while mid in deleted: mid -= 1 print(bin(mid)[2:].zfill(m))
IMPORT 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 NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER 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.
def check(ct, val, incre): while bin(val) in ct: val += incre return val for _ in range(int(input())): n, m = map(int, input().split()) length = 2**m mid_index = (2**m - 1) // 2 ct = set() for _ in range(n): res = bin(int(input(), 2)) ct.add(res) if int(res, 2) < mid_index: length -= 1 if length % 2: mid_index = check(ct, mid_index + 1, 1) elif int(res, 2) > mid_index: length -= 1 if not length % 2: mid_index = check(ct, mid_index - 1, -1) else: length -= 1 if not length % 2: mid_index = check(ct, mid_index - 1, -1) else: mid_index = check(ct, mid_index + 1, +1) diff = abs(m - len(str(bin(mid_index))[2:])) print("0" * diff + bin(mid_index)[2:])
FUNC_DEF WHILE FUNC_CALL VAR VAR VAR VAR VAR RETURN 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 NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR 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.
T = int(input()) for _ in range(T): n, m = input().split() n = int(n) m = int(m) li = [] for i in range(n): temp = input() li.append(temp) li.sort() temp5 = (2**m - n - 1) // 2 for i in range(n): if int(li[i], 2) <= temp5: temp5 += 1 else: break temp6 = len(bin(temp5)[2:]) ans = "0" * (m - temp6) + str(bin(temp5)[2:]) print(ans)
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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR 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 FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR 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.
for _ in range(int(input())): n, m = map(int, input().split()) med = (2**m - 1) // 2 l = 2**m been = {} for i in range(n): s = int(input(), 2) been[s] = 1 if s == med: med += -1 if l % 2 else 1 while been.get(med): med += -1 if l % 2 else 1 elif s < med and l % 2 == 0: med += 1 while been.get(med): med += 1 elif s > med and l % 2: med -= 1 while been.get(med): med -= 1 l -= 1 print("0" * (m - len(bin(med)) + 2) + bin(med)[2:])
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 NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER 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.
cin = lambda: int(input()) mi = lambda: map(int, input().split()) for _ in range(cin()): n, m = mi() k = pow(2, m) - n ans = (k - 1) // 2 d = set() for i in range(n): a = int(input(), 2) if a <= ans: ans += 1 while ans in d: ans += 1 d.add(a) ans = bin(ans)[2:] print("0" * (m - len(ans)) + ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP 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 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 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.
from sys import stdin for case in range(int(stdin.readline())): n, m = [int(x) for x in stdin.readline().split()] mid = 2 ** (m - 1) - 1 half = True used = [] for a in range(n): used.append(int(stdin.readline(), 2)) used.sort() passed = set() for a in used: passed.add(a) if a == mid: if half: half = False mid += 1 while mid in passed: mid += 1 else: half = True mid -= 1 while mid in passed: mid -= 1 elif a < mid: if half: mid += 1 while mid in passed: mid += 1 half = False else: half = True elif half: half = False else: half = True mid -= 1 while mid in passed: mid -= 1 bruh = "{0:b}".format(mid) print("0" * (m - len(bruh)) + bruh)
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 BIN_OP VAR NUMBER NUMBER ASSIGN VAR 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 ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR 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.
tc = int(input()) for _ in range(tc): n, m = map(int, input().split()) cache = {} curr = (2**m - 1) // 2 for i in range(n): s = input() x = int(s, 2) cache[x] = 1 if i % 2 == 0: if x <= curr: while len(cache) != 0 and curr + 1 in cache: curr += 1 curr += 1 elif x >= curr: while len(cache) != 0 and curr - 1 in cache: curr -= 1 curr -= 1 temp = bin(curr)[2:] if m != len(temp): temp = "0" * abs(m - len(temp)) + temp print(temp)
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 DICT ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two integers N \ ( N β‰₯ 2) and S. You have to construct an array A containing N integers such that: 0 ≀ A_{i} ≀ S for each 1 ≀ i ≀ N A_{1} + A_{2} + \ldots + A_{N} = S A_{1} \mathbin{\&} A_{2} \mathbin{\&} \ldots \mathbin{\&} A_{N} = 0, where \mathbin{\&} denotes [bitwise AND] operator. The maximum element of the array is minimized. Find the maximum element of the array A. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two space-separated integers N and S β€” the length of array and sum of array elements respectively. ------ Output Format ------ For each test case, output on a new line the maximum element of the array A. ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $2 ≀ N ≀ 10^{9}$ $1 ≀ S ≀ 10^{9}$ ----- Sample Input 1 ------ 4 2 7 3 6 5 21 100 256455 ----- Sample Output 1 ------ 4 3 5 2570 ----- explanation 1 ------ Test case $1$: One possible array is $A = [4,3]$. Here $4 +3=7$ and $4 \mathbin{\&} 3=0$. Test case $2$: One possible array is $[1, 2, 3]$. Here $1+2+3=6$ and $1 \mathbin{\&} 2 \mathbin{\&} 3=0$. Test case $3$: One possible array is $[2, 4, 5,5,5]$. Here $2+4+5+5+5=21$ and $2 \mathbin{\&} 4 \mathbin{\&} 5 \mathbin{\&} 5 \mathbin{\&} 5=0$.
for _ in range(int(input())): N, S = map(int, input().split()) L, R = 0, S + 1 while L + 1 < R: Mid = L + R >> 1 s = 0 cnt = 0 for i in range(30, -1, -1): if Mid >> i & 1: s += (N - 1) * (1 << i) cnt = min(cnt + 1, N - 1) else: s += (1 << i) * cnt if s >= S: R = Mid else: L = Mid print(R)
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 NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): st = input() p, i, x = 0, 0, 0 ans = 0 for i in range(1, min(20, len(st)) + 1): p = 0 for j in range(len(st) - i + 1): if st[j] == "0": p += 1 else: x = int(st[j : j + i], 2) if p + i >= x: ans += 1 p = 0 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
from sys import stdin def input(): return next(stdin)[:-1] def main(): def solve(): ss = input() count = 0 l = 0 sums = {} count = 0 last1 = -1 for r in range(len(ss)): d = 1 if ss[r] == "1" else 0 if d == 1: sums[last1 + 1, r] = 0 last1 = r for (ll, lr), v in list(sums.items()): v += v + d if v > r - ll + 1: del sums[ll, lr] else: if v <= r - ll + 1 and v >= r - lr + 1: count += 1 sums[ll, lr] = v print(count) q = int(input()) for _ in range(q): solve() main()
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR 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 a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s = input() q1 = ans = 0 for q in range(len(s)): if s[q] == "0": q1 += 1 else: ans += 1 + (q != len(s) - 1 and s[q + 1] == "0") q2 = size = 1 for q3 in range(1, q1 + 1): size += 1 if q2 == size: ans += 1 while q2 < size and size - q3 + q < len(s): q2 *= 2 q2 += ord(s[q + size - q3]) - ord("0") size += 1 if q2 == size: ans += 1 q1 = 0 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): st = input() pre = 0 dic = {} le = len(st) for i in range(le): if st[i] == "0": pre += 1 else: dic[i - 1] = pre pre = 0 dic[i] = pre ans = 0 for i in dic: tem = "" temle = dic[i] for j in range(i + 1, le): tem += st[j] temle += 1 nu = int(tem, 2) if nu <= temle: ans += 1 else: break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for _ in range(t): s = input() zeros = [0] * len(s) for i in range(len(s)): if s[i] == "0": zeros[i] = (zeros[i - 1] if i > 0 else 0) + 1 res = 0 for i in range(len(s)): cur = 0 for j in range(19): if i - j < 0: break if s[i - j] == "1": cur += 2**j if s[i - j] == "1" and ( cur == j + 1 or i - j > 0 and zeros[i - j - 1] + j + 1 >= cur ): res += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR VAR STRING VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for _ in range(t): s = input() ans = 0 l = 0 a = [] o = [0] for c in s: l += 1 if c == "0": a = [(2 * val, len + 1) for val, len in a] o.append(o[-1]) else: a = [(2 * val + 1, len + 1) for val, len in a] a.append((1, 1)) o.append(o[-1] + 1) a = list(filter(lambda x: x[0] <= l, a)) for x in a: if o[l - x[0]] == o[l - x[1]]: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) while t > 0: s = input() good = 0 length = 0 for i in range(0, len(s)): if s[i] == "0": length += 1 else: f = 0 for j in range(i, len(s)): length += 1 f = (f << 1) + int(s[j]) if f > length: break good += 1 length = 0 print(good) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
inf = 2 * 10**5 def count_occurrences(string, amount_of_zero_left, bit_len): ans = 0 for i in range(0, len(string) - bit_len + 1): if string[i] == "1": num = int(string[i : i + bit_len], 2) rem = num - bit_len if num <= inf and rem <= amount_of_zero_left[i]: ans += 1 return ans inf_bin = len(bin(inf)[2:]) for q in range(0, int(input())): s = str(input()) amount_of_zero_left = [0] * len(s) cur_amount = 0 for bit_len in range(0, len(s)): if s[bit_len] == "0": cur_amount += 1 elif s[bit_len] == "1": amount_of_zero_left[bit_len] = cur_amount cur_amount = 0 ans = 0 for bit_len in range(1, inf_bin + 1): ans += int(count_occurrences(s, amount_of_zero_left, bit_len)) print(ans)
ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def main(): T = int(input().strip()) for case in range(T): ans, cur = 0, 0 s = input().strip() lens = len(s) for i in range(lens): if s[i] == "0": cur += 1 continue num = 0 for j in range(i, lens): if s[j] == "1": num = num * 2 + 1 else: num = num * 2 if cur >= num - j + i - 1: ans += 1 else: break cur = 0 print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s = input() n = len(s) a = [0] * n l = 0 for i in range(n): if s[i] == "0": l += 1 a[i] = l if s[i] == "1": l = 0 ans = 0 for i in range(n): if s[i] == "1": sn = "" for j in range(i, min(n, i + 20)): sn += s[j] if int(sn, 2) <= j - i + 1 + a[i]: ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys input = sys.stdin.readline for _ in range(int(input())): s = input().rstrip() ans = zeroes = 0 n = len(s) for i in range(n): if s[i] == "0": zeroes += 1 else: for j in range(1, min(19, n - i + 1)): k = int(s[i : i + j], 2) if zeroes + j >= k: ans += 1 else: continue zeroes = 0 print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for nt in range(t): s = input() n = len(s) ans = 0 for i in range(n): if s[i] == "1": j = i - 1 while j > -1 and s[j] == "0": j -= 1 cntzero = i - j - 1 j = i number = 0 while j < n: number = number * 2 + int(s[j]) if number > cntzero + j - i + 1: break j += 1 ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
q = int(input()) for _ in range(q): s = input() n = len(s) ans = 100000000 r = [(0) for i in range(n)] for i in range(n - 1, -1, -1): if s[i] == "1": ans = i r[i] = ans ansss = 0 for i in range(n): ns = 0 for j in range(r[i], n): ns = 2 * ns + (ord(s[j]) - ord("0")) if ns == j - i + 1: ansss += 1 if ns > n: break print(ansss)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
T = int(input()) for t in range(T): s = input() n = len(s) res = 0 zeros = 0 for i, c in enumerate(s): if c == "0": zeros += 1 else: tail = 1 j = 1 while tail <= zeros + j: res += 1 j += 1 if i - 1 + j == n: break tail *= 2 tail += int(s[i - 1 + j]) zeros = 0 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def f(s): res = 0 cc = 0 for i in range(len(s)): if s[i] == "0": cc += 1 else: for j in range(i + 1, len(s) + 1): num = int(s[i:j], 2) if num >= j - i and num <= j - i + cc: res += 1 if num > j - i + cc: break cc = 0 return res Q = int(input().strip()) for q in range(Q): s = input().strip() r = f(s) print(r)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) c = [0] * 200100 for _ in range(t): s = input() for i in range(len(s)): c[i] = s[i] == "0" if i and s[i] == "0": c[i] += c[i - 1] sol = 0 for i in range(len(s)): tmp = 0 for k in range(20): if i - k < 0: break tmp += (s[i - k] == "1") << k if tmp == k + 1: sol += 1 if i >= 20: top = 20 + c[i - 20] if tmp > 20 and tmp <= top: sol += 1 print(sol)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR STRING IF VAR VAR VAR STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR STRING VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for i in range(int(input())): s = input() n = len(s) t = [0] * n for i in range(n): if s[i] == "1": t[i] = i else: t[i] = -1 if i == 0 else t[i - 1] res = 0 for i in range(n): sm = 0 j = i while j >= 0 and i - j < 20: if s[j] == "0": j -= 1 continue sm += 1 << i - j if sm <= i - (-1 if j == 0 else t[j - 1]): res += 1 j -= 1 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for __ in range(int(input())): s = input() a = [0] * len(s) for i in range(1, len(s)): if s[i - 1] == "0": a[i] = a[i - 1] + 1 else: a[i] = 0 ans = 0 for i in range(len(s)): if s[i] == "1": t = 1 ans += 1 for j in range(i + 1, min(len(s), i + 18)): if s[j] == "1": t = t * 2 + 1 else: t = t * 2 if a[i] >= t - (j - i + 1): ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys t = int(input()) for _ in range(t): s: str = "*" + sys.stdin.readline().rstrip() + "*" n = len(s) zero = [0] * n p = [] for i in range(1, n): if s[i] == "0": zero[i] = zero[i - 1] + 1 else: zero[i] = 0 if s[i] == "1": p.append(i) ans = s.count("1") + s.count("10") + s.count("011") for j in p: for k in range(4, n): target = bin(k)[2:] leading_zero = k - len(target) suf = len(target) if leading_zero > zero[j - 1]: break if target == s[j : j + suf]: ans += 1 print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP STRING FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s = input() L = len(s) NOZBT = [] cnt = 0 ans = 0 for i in s: NOZBT.append(cnt) if i == "0": cnt += 1 else: cnt = 0 for i in range(L): v = 0 if s[i] == "1": for j in range(i, min(i + 18, L)): v = (v << 1) + int(s[j]) if NOZBT[i] >= v - j + i - 1: ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s = input() N = len(s) ons = [0] * N ons[0] = 1 if s[0] == "1" else 0 for i in range(1, N): ons[i] = ons[i - 1] + (1 if s[i] == "1" else 0) tot = 0 for r in range(N): l = r pln = 0 while l >= 0 and r - l < 32: ss = s[l : r + 1] ln = int(ss, base=2) ones = ss.count("1") if ln <= pln: pln = ln l -= 1 continue pln = ln st = r - ln + 1 if st < 0: break cnt = ons[r] - (ons[st - 1] if st - 1 >= 0 else 0) if cnt == ones: tot += 1 l -= 1 print(tot)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys input = sys.stdin.readline al = [] def solve(): global al cur = input() n = len(cur) cnt = ans = 0 for i in range(n): if cur[i] == "0": cnt += 1 continue val = 0 while cnt + 17 >= al[val]: val += 1 for j in range(val): if i + j + 1 >= n: break if int(cur[i : i + j + 1], 2) <= cnt + j + 1: ans += 1 cnt = 0 print(ans) def main(): t = int(input()) for _ in range(t): solve() al.append(0) cur = 1 while cur < 1000000: al.append(cur) cur *= 2 main()
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER 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 NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def bin(s): return int(s, 2) for _ in range(int(input())): s = str(input()) q = 0 ans = 0 for i in range(len(s)): if s[i] == "1": for ii in range(i, len(s)): string = s[q : ii + 1] if bin(string) <= ii + 1 - q: ans += 1 else: break q = i + 1 print(ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
q = int(input()) ans = [] for z in range(q): a = [int(i) for i in input()] n = len(a) kol = 0 b = [100000] * n t = n - 1 y = 100000 while t >= 0: b[t] = y if a[t]: y = t t -= 1 for i in range(n): kol += a[i] d = a[i] * 2 if a[i]: for j in range(i + 1, min(i + 20, n)): d += a[j] if d == j - i + 1: kol += 1 d *= 2 else: for j in range(b[i], min(b[i] + 20, n)): d += a[j] if d == j - i + 1: kol += 1 d *= 2 ans.append(kol) for i in ans: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for i in range(t): st = input() n = len(st) st = "1" + st res = 0 first1 = n for i in range(n, 0, -1): sum = 0 p2 = 1 for j in range(i, max(i - 18, 0), -1): sum += p2 * (st[j] == "1") p2 *= 2 if sum == i - j + 1: res += 1 while first1 > max(i - 18, 0) or st[first1] != "1": first1 -= 1 if i - first1 >= sum and sum > 18: res += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR STRING VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for i in range(t): s = [int(i) for i in list(input())] id_now = -1 right_nearest = [-1] * len(s) for i in range(len(s) - 1, -1, -1): if s[i] == 1: id_now = i right_nearest[i] = id_now ans = 0 for i in range(len(s)): if s[i] == 1: ans += 1 if i != len(s) - 1: if s[i + 1] == 0: ans += 1 elif right_nearest[i] < 0: continue else: s_now = 0 for j in range(right_nearest[i], min(right_nearest[i] + 20, len(s))): s_now = 2 * s_now + s[j] if s_now == j - i + 1: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for t in range(int(input())): s = input() count = 0 x = s.count("0") if x == len(s): print(0) continue last = 0 for i in range(len(s)): val = 0 if s[i] == "1": for j in range(i, min(i + 18, len(s))): if s[j] == "0": val *= 2 else: val = val * 2 + 1 if val <= j - last + 1: count += 1 last = i + 1 print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
from sys import setrecursionlimit as SRL from sys import stdin SRL(10**7) rd = stdin.readline rrd = lambda: map(int, rd().strip().split()) T = int(input()) while T: s = "".join(map(str, rd().strip())) ans = 0 max0 = 0 for i in range(len(s)): if s[i] == "0": max0 += 1 else: ans += 1 tot = 1 for j in range(1, 20): tot = tot << 1 if i + j < len(s): if s[i + j] == "1": tot += 1 if max0 >= tot - j - 1: ans += 1 max0 = 0 print(ans) T -= 1
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def run(a, ind, l): newSt = "" ans = 0 for i in range(ind, len(a)): newSt += a[i] if int(newSt, 2) == i - l + 1: ans += 1 if int(newSt, 2) > i - l + 1: return ans return ans n = int(input()) for kkk in range(n): st = input() uk = [0] * len(st) for i in range(len(uk)): uk[i] = i for j in range(len(uk)): if st[j] == "1": uk[0] = j break for i in range(1, len(uk)): if i < uk[i - 1]: uk[i] = uk[i - 1] else: for j in range(i, len(uk)): if st[j] == "1": uk[i] = j break s = 0 for i in range(len(uk)): if uk[i] != i or st[i] == "1": s += run(st, uk[i], i) print(s)
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR STRING VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
LOG = 20 def solve(s): n = len(s) res = 0 z = 0 for t in range(0, n): if s[t] == "0": z += 1 continue for l in range(1, min(LOG, n - t + 1)): x = int(s[t : t + l], 2) if l + z >= x: res += 1 z = 0 return res t = int(input()) while t > 0: t -= 1 s = input() print(solve(s))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for _ in range(t): s = input() ct = int(0) ans = int(0) for i in range(len(s)): if s[i] == "0": ct += 1 else: num = int(0) for j in range(i, len(s)): num *= 2 if s[j] == "1": num += 1 if num <= ct + j - i + 1: ans += 1 if num > j - i + 1 + ct: break ct = 0 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys def input(): return sys.stdin.readline().rstrip() def slv(): s = list(input()[::-1]) s = list(map(int, s)) ans = 0 n = len(s) RPOS = [n] * n for i in range(n): if s[i] == 1: RPOS[i] = i for i in range(n - 1, -1, -1): if i + 1 < n: RPOS[i] = min(RPOS[i], RPOS[i + 1]) for l in range(n): left_comp, right_comp = 0, 0 for r in range(l, min(l + 20, n)): left_comp += pow(2, r - l) * s[r] right_comp += 1 if left_comp == right_comp: ans += 1 m = min(l + 20, n) - 1 if right_comp < left_comp <= RPOS[m] - l: ans += 1 print(ans) return def main(): t = int(input()) for i in range(t): slv() return def main(): t = int(input()) for i in range(t): slv() return main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def read_int(): return int(input()) def read_list(): return list(map(int, input().split(" "))) def solve(s: str) -> int: ones = [] for i, num in enumerate(s): if num == "1": ones.append(i) ans = 0 for s_iter, letter in enumerate(s): current_val = 0 if letter == "1": ones.pop(0) current_len = 1 t_iter = s_iter else: if len(ones) <= 0: return ans next_one = ones[0] current_len = next_one - s_iter + 1 t_iter = next_one while t_iter < len(s) and current_val <= current_len: current_val = 2 * current_val + int(s[t_iter]) if current_val == current_len: ans += 1 t_iter += 1 current_len += 1 return ans t = read_int() for _ in range(t): print(solve(input()))
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 FUNC_DEF VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for case_num in range(t): s = input() sum = [0] for i in s: sum.append(sum[-1] + int(i)) ans = 0 for i in range(len(s)): if s[i] == "1": ans += 1 curr = 1 j = i + 1 while j < len(s): curr = curr * 2 + int(s[j]) if curr > j + 1: break right = j - i + 1 left = curr - right if i >= left and sum[i] == sum[i - left]: ans += 1 else: break j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s = input() a = 0 for w in range(1, 20): z = 0 x = int(s[: w - 1], 2) if w > 1 else 0 for c in range(len(s) - w + 1): x <<= 1 x += s[c + w - 1] == "1" a += s[c] == "1" and x >= w and x - w <= z z = z + 1 if s[c] == "0" else 0 if s[c] == "1": x -= 1 << w - 1 print(a)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR VAR STRING VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR STRING BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
occur1 = [] large = 150000 def count(a, b): ret = occur1[b] if a - 1 >= 0: ret -= occur1[a - 1] return ret def findcount(st, s): ssum = 0 po = 1 ans = 0 for i in range(s, min(s + 20, len(st))): if st[i] == "1": ssum += po po *= 2 if ssum == i - s + 1: ans += 1 end = min(s + 20, len(st)) if s + 20 < len(st) and ssum > 20: need1 = s + 20 need2 = s + ssum - 1 if need2 < len(st) and count(need1, need2) == 0: ans += 1 return ans def solve(): s = input() s = s[::-1] occur1.clear() for i in range(len(s)): if s[i] == "1": occur1.append(1) else: occur1.append(0) for i in range(1, len(occur1)): occur1[i] += occur1[i - 1] ans = 0 for i in range(0, len(s)): ans += findcount(s, i) print(ans) def main(): test = eval(input()) for t in range(test): solve() main()
ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR 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 a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s = input() ans = 0 p = 0 for i in range(len(s)): if s[i] == "0": p += 1 continue ans += 1 a1 = 1 a2 = 1 for j in range(i + 1, min(i + 19, len(s))): a2 += 1 a1 *= 2 a1 += s[j] == "1" ans += a1 >= a2 and a1 <= p + a2 p = 0 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR STRING VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def binaryToDecimal(n): return int(n, 2) t = int(input()) for _ in range(t): s = input() ct = 0 zr = 0 for i in range(len(s)): if s[i] == "0": zr += 1 else: for j in range(i, len(s)): if binaryToDecimal(s[i : j + 1]) <= zr + j - i + 1: ct += 1 else: break zr = 0 print(ct)
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): l = input() n = len(l) zeros, cnt = 0, 0 for i in range(n): if l[i] == "0": zeros += 1 else: for j in range(i, n): if zeros + (j - i + 1) >= int(l[i : j + 1], 2): cnt += 1 else: break zeros = 0 print(cnt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
n = int(input()) for _ in range(n): s = input() ans = 0 zeros = 0 for i in range(len(s)): if s[i] == "1": for j in range(1, 19): if i + j > len(s): continue a = s[i : i + j] f = int(a, 2) if zeros + j >= f: ans += 1 zeros = 0 else: zeros += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s = input() n = len(s) i = 0 j = 0 c = 0 ans = 0 while i < n: if s[i] == "0": c += 1 if s[i] == "1": x = 0 y = 0 for j in range(i, n): x = x * 2 if s[j] == "1": x += 1 if x - y - 1 <= c: ans += 1 else: break y += 1 c = 0 i += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
ii = lambda: int(input()) kk = lambda: map(int, input().split()) ll = lambda: list(kk()) for _ in range(ii()): l = 0 cnt = 0 ls = [] for d in input(): if d == "1": ls.append(l) l += 1 if not ls: print(0) continue ls = list(reversed(ls)) minl = 0 for end in range(l - 1, -1, -1): num = 0 while minl < len(ls) and end < ls[minl]: minl += 1 if minl == len(ls): break i = minl while i < len(ls) and num <= l: num += 1 << end - ls[i] if end - num >= -1 and (len(ls) == i + 1 or ls[i + 1] <= end - num): cnt += 1 i += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys input = sys.stdin.readline t = int(input()) for you in range(t): s = input() n = len(s) - 1 ans = 0 z = [(n - 1) for i in range(n)] curr = n - 1 for i in range(n - 1, -1, -1): if s[i] == "1": curr = i z[i] = curr for i in range(n): ok = 0 for j in range(max(z[i] - 20, i), n): ok = 2 * ok + int(s[j]) if ok > n: break if ok == j - i + 1: ans += 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for _ in range(t): s = input() cnt = 0 for l in range(1, 19): lead = 0 val = 0 for i in range(len(s)): d = int(s[i]) if i <= l - 1: val += d * 2 ** (l - i - 1) else: d2 = int(s[i - l]) val -= d2 * 2 ** (l - 1) val *= 2 val += d if d2 == 0: lead += 1 else: lead = 0 if i >= l - 1 and s[i - l + 1] == "1" and lead + l >= val: cnt += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER STRING BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys def main(): import sys input = sys.stdin.readline for _ in range(int(input())): S = input().rstrip("\n") N = len(S) cnt = 0 ans = 0 for i, s in enumerate(S): if s == "0": cnt += 1 else: j = 0 f = 0 while True: f *= 2 f += int(S[i + j]) if f - (j + 1) <= cnt: ans += 1 else: break j += 1 if i + j == N: break cnt = 0 print(ans) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for i in range(t): s = input() l = len(bin(len(s))) - 2 ans = 0 num_1 = [] num_0 = 0 for char in s: if char == "0": num_0 += 1 num_1.append(-1) else: num_1.append(num_0) num_0 = 0 for j in range(len(s)): if num_1[j] == -1: continue for k in range(l): if j + k + 1 > len(s): break f = int(s[j : j + k + 1], 2) if f - k - 1 > num_1[j]: break ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def calc(s): return int(s, 2) for nt in range(int(input())): s = input() n = len(s) pref = [-1] * n for i in range(n): if s[i] == "1": pref[i] = i else: pref[i] = pref[i - 1] ans = 0 for i in range(18): for j in range(n - 1, i - 1, -1): temp = s[j - i : j + 1] if calc(temp) == i + 1 and temp[0] == "1": ans += 1 elif ( s[j - i] == "1" and j - i - 1 >= 0 and calc(temp) > i + 1 and calc(temp) <= j - pref[j - i - 1] ): ans += 1 print(ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER STRING VAR NUMBER IF VAR BIN_OP VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for _ in range(t): s = [int(x) for x in input()] next1 = [(0) for _ in range(len(s))] idx1 = -1 for i in range(len(s) - 1, -1, -1): if s[i] == 1: idx1 = i next1[i] = idx1 c = 0 for i in range(len(s)): val = 0 if next1[i] < 0: continue for j in range(next1[i], min(next1[i] + 20, len(s))): val = val * 2 + s[j] if val == j - i + 1: c += 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
T = int(input()) d = dict() num = int(2 * 100000.0 + 11) for t in range(T): s = input() n = len(s) stack = set() tr = 0 for i in range(n): if s[i] == "1": stack.add(i) break d_trail = dict() for i in range(n): if s[i] == "0": tr += 1 else: d_trail[i] = tr tr = 0 stack.add(i) ans = 0 ct = 0 while stack: ct += 1 k = stack.pop() for i in range(k + 1, min(k + 35, n)): if int(s[k : i + 1], 2) == i - k + 1: ans += 1 if int(s[k : i + 1], 2) > i - k + 1: if int(s[k : i + 1], 2) <= i + 1 - k + d_trail[k]: ans += 1 print(ans + s.count("1"))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR STRING
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def find(s): powers = {} for i in range(30): powers[i] = 2**i s_rev = s[::-1] zeros = [0] * len(s) cur = 0 for i in range(len(s)): if s[i] == "0": cur += 1 else: cur = 0 zeros[i] = cur zeros = zeros[::-1] count = 0 for i in range(len(s)): if s[i] == "1": count += 1 for i in range(len(s_rev)): if s_rev[i] == "1": temp = int(s_rev[i]) if i == len(s_rev) - 1: reminding_zeros = 0 else: reminding_zeros = zeros[i + 1] for j in range(1, 19): if i - j < 0: break temp *= 2 temp += int(s_rev[i - j]) if reminding_zeros >= temp - (j + 1): count += 1 return count for _ in range(int(input())): print(find(input()))
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for i in range(t): s = input() n = len(s) st = [] for i in range(n): st.append(s[n - 1 - i]) st += "1" ans = 0 last1 = 0 for i in range(n): sum = 0 p2 = 1 for j in range(i, min(i + 18, n), 1): sum += p2 * (st[j] == "1") p2 *= 2 if sum == j - i + 1: ans += 1 while last1 < min(i + 18, n) or st[last1] != "1": last1 += 1 if last1 - i >= sum and sum > 18: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR STRING VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR STRING VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys def rint(): return map(int, sys.stdin.readline().split()) t = int(input()) for tt in range(t): s = input() nxti = [0] * len(s) if s[0] == "1": nxti[0] = 0 else: nxti[0] = -1 for i in range(1, len(s)): if s[i] == "1": nxti[i] = i else: nxti[i] = nxti[i - 1] cnt = 0 for r in range(len(s)): sumv = 0 for l in range(r, max(-1, r - 22), -1): assert l >= 0 if s[l] == "1": sumv += 1 << r - l if l == 0: ll = -1 else: ll = nxti[l - 1] max_len = r - ll assert max_len >= 1 if sumv <= max_len: cnt += 1 print(cnt)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
from sys import setcheckinterval, setrecursionlimit, stdin setcheckinterval(1000) setrecursionlimit(10**7) def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) for _ in range(iin()): a = input() n = len(a) a1 = [] for i in range(n): if a[i] == "1": a1.append(i) else: a1.append(a1[-1] if i > 0 else -1) ans = 0 for r in range(n): sol = 0 l = r while l >= 0 and r - l < 20: if a[l] == "1": sol += 1 << r - l if sol <= r - (a1[l - 1] if l > 0 else -1): ans += 1 l -= 1 print(ans)
EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys def rint(): return map(int, sys.stdin.readline().split()) num_table = set() for i in range(2 * 10**5): pre_zero = i - (len(bin(i)) - 2) bin_str = bin(i)[2:] num_table.add((pre_zero, bin_str)) t = int(input()) for tt in range(t): s = input() pre_z = [] z_cnt = 0 for i in range(len(s) - 1, -1, -1): if s[i] == "0": z_cnt += 1 else: z_cnt = 0 pre_z.append(z_cnt) pre_z.reverse() ans = 0 for j in range(len(s)): pz = pre_z[j] for k in range(1 + 19): if j + pz + k > len(s): break if (pz, s[j + pz : j + pz + k]) in num_table: ans += 1 print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) l = [2, 4] val = 8 cnt = 5 for i in range(2, 200001): if cnt == val - 1: val *= 2 cnt += 1 l.append(cnt) cnt += 1 for t1 in range(t): s = input() n = len(s) ans = 0 cnt = 0 ind = 0 while ind < n: if s[ind] == "0": cnt += 1 else: a = 1 end = l[cnt] while a <= end: no = bin(a)[2:] if no == s[ind : ind + len(no)]: ans += 1 a += 1 cnt = 0 ind += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import sys stdin = sys.stdin test_case = int(stdin.readline()) while test_case: test_case -= 1 s = stdin.readline().rstrip() nxt_one = [0] * len(s) nxt_one[0] = -1 tmp = -1 if s[0] == "0" else 0 for i in range(1, len(s)): nxt_one[i] = tmp if s[i] == "1": tmp = i ans = 0 for i in range(len(s)): cur = 0 new_one = nxt_one[i] if s[i] == "0" else i while i - new_one < 18 and new_one != -1: cur |= 1 << i - new_one if cur <= i - nxt_one[new_one]: ans += 1 new_one = nxt_one[new_one] print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR STRING VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
from sys import stdin, stdout def main(): from sys import stdin, stdout input = stdin.readline print = stdout.write for _ in range(int(input())): ans = 0 i = -1 s = input() for j in range(len(s) - 1): if s[j] == "1": c = 0 for k in range(j, len(s)): c = c * 2 + (s[k] == "1") + (s[k] == "\n") * k if k - i < c: ans += k - j break i = j print(f"{ans}\n") main()
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR STRING BIN_OP VAR VAR STRING VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) a = [] for i in range(t): a.append(input()) for i in a: p = -1 l = len(i) d = 0 for j in range(l): if i[j] == "1": q = j s = j - p - 1 while q < l: if q == j + 18: break f = int(i[j : q + 1], 2) r = q - j + 1 if r + s >= f: d += 1 q += 1 p = j print(d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer β€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t = int(input()) for _ in range(t): A = input() zeroes = 0 result = 0 for i in range(len(A)): if A[i] == "0": zeroes += 1 else: j = 0 while 1 << j <= len(A) and i + j + 1 <= len(A): a = int(A[i : i + j + 1], 2) if j + 1 <= a and a <= j + 1 + zeroes: result += 1 j += 1 zeroes = 0 print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR