description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
ans = "" for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) res = (1 << 31) - 1 for i in arr: res &= i count = 0 cur = (1 << 31) - 1 for i in range(n): if cur & arr[i] == res: count += 1 cur = (1 << 31) - 1 else: cur &= arr[i] ans += str(n - count) + "\n" print(ans)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
for p in range(int(input())): n = int(input()) l = list(map(int, input().split())) res = l[0] for ele in l: res = res & ele ans = 0 m = [] i = 0 for ele in l: if ele == res: m.append(ele) elif len(m) == 0: m.append(ele) elif m[-1] != res: t = m[-1] m.pop() m.append(ele & t) ans += 1 else: m.append(ele) i += 1 for ele in m: if ele != res: ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
t = int(input()) for test in range(t): n = int(input()) v = list(map(int, input().split())) ans = 0 r = v[0] for i in range(0, n): r &= v[i] for i in range(0, n - 1): if v[i] != r: ans += 1 v[i + 1] &= v[i] if v[n - 1] != r: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) mn = a[0] for i in range(1, n): mn &= a[i] tm = 0 for i in range(n): if a[i] == mn: tm += 1 tm = n - tm ans = 0 for i in range(n): if a[i] == mn: continue if i == n - 1: ans += 1 continue a[i + 1] &= a[i] ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) k = a[0] for i in a: k &= i a.append(k) tp, ct, r = a[0], 0, 0 for i in a: ct += 1 tp &= i if tp == k: r += ct - 1 ct = 0 tp = 2**30 - 1 print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
def fi(): return int(input()) def li(): return list(map(int, input().split())) t = fi() for i in range(t): n = fi() a = li() tand = (1 << 30) - 1 for i in range(len(a)): tand &= a[i] groups = 0 cand = (1 << 30) - 1 for i in range(len(a)): cand &= a[i] if cand == tand: groups += 1 cand = (1 << 30) - 1 ans = len(a) - groups print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) z = a[0] for i in range(1, n): z = z & a[i] r = 0 for i in range(n): if a[i] == z: continue if i == n - 1: r += 1 continue a[i + 1] = a[i] & a[i + 1] r += 1 print(r)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) fe = arr[0] for x in arr[1:]: fe &= x ng = 0 ta = arr[0] for i, x in enumerate(arr): ta &= x if ta == fe: ng += 1 if i + 1 < n: ta = arr[i + 1] print(n - ng)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
for _ in range(int(input())): n = int(input()) lis = list(map(int, input().split())) count = 0 z = [] check1 = 2**30 - 1 for i in lis: check1 &= i check2 = 2**32 - 1 for i in lis: check2 &= i if check2 == check1: count += 1 check2 = 2**32 - 1 print(len(lis) - count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
t = int(input()) for _ in range(t): n = int(input()) x = list(map(int, input().split(" "))) c = 0 a = sorted(x) if len(set(a)) == 1: print(0) else: d = x[0] for i in range(1, n): d &= x[i] for i in range(n): if x[i] == d: continue elif i == n - 1: c = c + 1 else: x[i + 1] &= x[i] c = c + 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) ele = a[0] for i in range(1, n): ele = ele & a[i] noz = a.count(ele) calc = 0 i = 0 while i < n - 1: if a[i] == ele: i = i + 1 continue a[i + 1] = a[i] & a[i + 1] calc = calc + 1 i = i + 1 if a[-1] == ele: print(calc) else: print(calc + 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
t = int(input()) while t: n = int(input()) arr = list(map(int, input().split())) tot = -1 for i in range(n): tot &= arr[i] cur = -1 ans = 0 for i in range(n): cur &= arr[i] if cur == tot: cur = -1 ans += 1 print(n - ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
for _ in range(int(input())): n = int(input()) lis = list(map(int, input().split())) least = (1 << 30) - 1 for i in lis: least &= i p = lis[0] i = 1 counti = 0 while i < n: if p == least: p = lis[i] counti += 1 else: p &= lis[i] i += 1 if p == least: counti += 1 print(n - counti)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) if len(set(l)) == 1: print(0) else: c = 0 a = 0 e = l[0] for i in range(1, n): e &= l[i] for i in range(n): if l[i] == e: continue elif i == n - 1: a += 1 continue else: l[i + 1] &= l[i] a += 1 print(a)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
T = int(input()) for ts in range(T): N = int(input()) A = list(map(int, input().split(" "))) k = A[0] for i in range(1, N): k &= A[i] ans = 0 local = A[0] for i in range(1, N): if local == k: local = A[i] else: local &= A[i] ans += 1 if local != k: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
result = [] ones = 2**30 - 1 for _ in range(int(input())): N = int(input()) A = [int(x) for x in input().split()] ad = ones for a in A: ad &= a tmp, c = ones, 0 ans = 0 for a in A: tmp &= a if tmp == ad: ans += c tmp, c = ones, 0 else: c += 1 if tmp != ad: ans += c result.append(ans) print(*result, sep="\n")
ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
t = int(input()) for i in range(t): n = int(input()) l = list([int(x) for x in input().split()]) a = (1 << 32) - 1 for i in l: a = a & i c = 0 ans = 0 b = (1 << 32) - 1 for i in l: b = b & i c += 1 if b == a: ans += c - 1 c = 0 b = (1 << 32) - 1 if c > 0: ans += c print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) k = a[0] for i in a: k &= i count = 0 for i in range(n - 1): if a[i] == k: continue a[i + 1] &= a[i] count += 1 print(count + (a[-1] != k))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) bc = [0] * 32 for i in a: for j in range(32): if i >> j & 1: bc[j] += 1 t = 0 for i, j in enumerate(bc): if j == n: t |= 1 << i c = 0 p = a[0] for i in range(1, n): if p == t: p = a[i] else: p &= a[i] c += 1 if p != t: c += 1 print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
t = int(input()) for x in range(t): n = int(input()) seq = list(map(int, input().split())) Big = 2**31 - 1 totAnd = Big for i in seq: totAnd = totAnd & i groups = 0 CurrAnd = Big for i in seq: CurrAnd = CurrAnd & i if CurrAnd == totAnd: groups += 1 CurrAnd = Big print(n - groups)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
tc = int(input()) def reset(): return (1 << 30) - 1 for t in range(tc): n = int(input()) a = list(map(int, input().split())) req = reset() for i in a: req &= i ans = 0 cur = reset() for i in a: cur &= i if cur == req: cur = reset() ans += 1 print(n - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) x = a[0] for i in range(1, n): x = x & a[i] c = a[0] cc = 0 for i in range(1, n): if c == x: cc += 1 c = a[i] c = c & a[i] if c == x: cc += 1 print(n - cc)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
def solve(n, arr): x = arr[0] for i in range(n): x &= arr[i] groups = 0 s = arr[0] for i in range(n): s &= arr[i] if s == x: groups += 1 if i < n - 1: s = arr[i + 1] return n - groups for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) print(solve(n, arr))
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}], consisting of N integers. In one move, you can take two adjacent numbers A_{i} and A_{i+1}, delete them, and then insert the number A_{i} \land A_{i+1} at the deleted position. Here, \land denotes [bitwise AND]. Note that after this operation, the length of the array decreases by one. Formally, as long as |A| > 1 (where |A| denotes the current length of A), you can pick an index 1 ≀ i < |A| and transform A into [A_{1}, A_{2}, \ldots, A_{i-1}, A_{i} \land A_{i+1}, A_{i+2}, \ldots, A_{|A|}]. Find the minimum number of moves required to make all numbers in the resulting array equal. ------ Input Format ------ - The first line of input contains an integer T β€” the number of test cases you need to solve. - The first line of each test case contains one integer N, the size of the array. - The second line of each test case contains N space-separated integers A_{1}, \ldots, A_{N} β€” the elements of the array A. ------ Output Format ------ For each test case, output on a new line the minimum number of moves required to make all numbers equal. ------ Constraints ------ $1 ≀T ≀10^{6}$ $2 ≀N ≀10^{6}$ - Sum of $N$ over all test cases is at most $10^{6}$. $0 ≀A_{i} < 2^{30}$ ------ subtasks ------ Subtask 1 (20 points): $0 ≀A_{i} ≀255$ Sum of $N$ over all test cases is at most $255$. Subtask 2 (30 points): Sum of $N$ over all test cases is at most $2000$. Subtask 3 (50 points): Original constraints. ----- Sample Input 1 ------ 4 4 0 0 0 1 2 1 1 6 1 2 3 4 5 6 4 2 28 3 22 ----- Sample Output 1 ------ 1 0 4 3 ----- explanation 1 ------ Test case $1$: Choose $i = 3$ to make the array $[0, 0, 0 \land 1] = [0, 0, 0]$. Test case $2$: All elements of the array are already equal. Test case $3$: One possible sequence of moves is as follows: - Choose $i = 1$, making the array $[1\land 2, 3, 4, 5, 6] = [0, 3, 4, 5, 6]$ - Choose $i = 2$, making the array $[0, 0, 5, 6]$ - Choose $i = 3$, making the array $[0, 0, 4]$ - Choose $i = 2$, making the array $[0, 0]$ It can be verified that in this case, making every element equal using $3$ or fewer moves is impossible.
T = int(input()) for i in range(T): N = int(input()) A = list(map(int, input().split())) s = A[0] res = 0 S = A[0] for i in A: S = S & i for i in range(1, N): if s == S: s = A[i] res += 1 s = s & A[i] if s == S: res += 1 print(N - res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Eikooc and Sushi play a game. The game is played on a tree having $n$ nodes numbered $1$ to $n$. Recall that a tree having $n$ nodes is an undirected, connected graph with $n-1$ edges. They take turns alternately moving a token on the tree. Eikooc makes the first move, placing the token on any node of her choice. Sushi makes the next move, followed by Eikooc, followed by Sushi, and so on. In each turn after the first, a player must move the token to a node $u$ such that $u$ is adjacent to the node $v$ the token is currently on $u$ has not been visited before $u \oplus v \leq min(u, v)$ Here $x \oplus y$ denotes the bitwise XOR operation on integers $x$ and $y$. Both the players play optimally. The player who is unable to make a move loses. The following are examples which demonstrate the rules of the game. Suppose Eikooc starts the game by placing the token at node $4$. Sushi then moves the token to node $6$, which is unvisited and adjacent to $4$. It also holds that $6 \oplus 4 = 2 \leq min(6, 4)$. In the next turn, Eikooc moves the token to node $5$, which is unvisited and adjacent to $6$. It holds that $5 \oplus 6 = 3 \leq min(5, 6)$. Sushi has no more moves to play, so she loses. Suppose Eikooc starts the game by placing the token at node $3$. Sushi moves the token to node $2$, which is unvisited and adjacent to $3$. It also holds that $3 \oplus 2 = 1 \leq min(3, 2)$. Eikooc cannot move the token to node $6$ since $6 \oplus 2 = 4 \nleq min(6, 2)$. Since Eikooc has no moves to play, she loses. Before the game begins, Eikooc decides to sneakily relabel the nodes of the tree in her favour. Formally, a relabeling is a permutation $p$ of length $n$ (sequence of $n$ integers wherein each integer from $1$ to $n$ occurs exactly once) where $p_i$ denotes the new numbering of node $i$. She wants to maximize the number of nodes she can choose in the first turn which will guarantee her a win. Help Eikooc find any relabeling which will help her do so. -----Input----- The first line contains a single integer $t~(1 \le t \le 10^5)$ β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains an integer $n~(1 \le n \le 2 \cdot 10^5)$ β€” the number of nodes in the tree. The next $n-1$ lines contain two integers $u$ and $v$ $(1 \le u, v \le n; u \neq v)$ β€” denoting an edge between nodes $u$ and $v$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print any suitable relabeling β€” a permutation of length $n$ which maximizes the number of nodes that can be chosen in the first turn that guarantee a win for Eikooc. If there are multiple such relabelings, you may print any of them. -----Examples----- Input 3 1 2 1 2 3 1 2 1 3 Output 1 2 1 1 2 3 -----Note----- In the first test case, Eikooc has only one choice. Sushi will have no moves to play after Eikooc chooses this node and Eikooc will win. In the second test case, $1 \oplus 2 = 3 \nleq min(1, 2)$. Hence, after Eikooc picks either of the nodes, Sushi will have no moves to play and Eikooc will win. Both $\{1, 2\}$ and $\{2, 1\}$ are optimal relabelings.
def solution(): n = int(input()) adj = [[] for _ in range(n)] for _ in range(n - 1): u, v = map(lambda x: int(x) - 1, input().strip().split()) adj[u].append(v) adj[v].append(u) colors = [-1] * n stk = [0] colors[0] = 0 while stk: u = stk.pop() for v in adj[u]: if colors[v] != -1: continue colors[v] = colors[u] ^ 1 stk.append(v) w, color = sum(colors), 1 if w > n - w: w, color = n - w, 0 result = [0] * n idx1 = [i for i, c in enumerate(colors) if c == color] idx2 = [j for j, c in enumerate(colors) if c != color] bit = 1 i = j = 0 while bit <= n: if w & bit: for x in range(bit, min(2 * bit - 1, n) + 1): result[idx1[i]] = x i += 1 else: for x in range(bit, min(2 * bit - 1, n) + 1): result[idx2[j]] = x j += 1 bit <<= 1 return " ".join(map(lambda x: str(x), result)) for case in range(int(input())): print("%s" % solution())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR
Eikooc and Sushi play a game. The game is played on a tree having $n$ nodes numbered $1$ to $n$. Recall that a tree having $n$ nodes is an undirected, connected graph with $n-1$ edges. They take turns alternately moving a token on the tree. Eikooc makes the first move, placing the token on any node of her choice. Sushi makes the next move, followed by Eikooc, followed by Sushi, and so on. In each turn after the first, a player must move the token to a node $u$ such that $u$ is adjacent to the node $v$ the token is currently on $u$ has not been visited before $u \oplus v \leq min(u, v)$ Here $x \oplus y$ denotes the bitwise XOR operation on integers $x$ and $y$. Both the players play optimally. The player who is unable to make a move loses. The following are examples which demonstrate the rules of the game. Suppose Eikooc starts the game by placing the token at node $4$. Sushi then moves the token to node $6$, which is unvisited and adjacent to $4$. It also holds that $6 \oplus 4 = 2 \leq min(6, 4)$. In the next turn, Eikooc moves the token to node $5$, which is unvisited and adjacent to $6$. It holds that $5 \oplus 6 = 3 \leq min(5, 6)$. Sushi has no more moves to play, so she loses. Suppose Eikooc starts the game by placing the token at node $3$. Sushi moves the token to node $2$, which is unvisited and adjacent to $3$. It also holds that $3 \oplus 2 = 1 \leq min(3, 2)$. Eikooc cannot move the token to node $6$ since $6 \oplus 2 = 4 \nleq min(6, 2)$. Since Eikooc has no moves to play, she loses. Before the game begins, Eikooc decides to sneakily relabel the nodes of the tree in her favour. Formally, a relabeling is a permutation $p$ of length $n$ (sequence of $n$ integers wherein each integer from $1$ to $n$ occurs exactly once) where $p_i$ denotes the new numbering of node $i$. She wants to maximize the number of nodes she can choose in the first turn which will guarantee her a win. Help Eikooc find any relabeling which will help her do so. -----Input----- The first line contains a single integer $t~(1 \le t \le 10^5)$ β€” the number of test cases. The description of each test case is as follows. The first line of each test case contains an integer $n~(1 \le n \le 2 \cdot 10^5)$ β€” the number of nodes in the tree. The next $n-1$ lines contain two integers $u$ and $v$ $(1 \le u, v \le n; u \neq v)$ β€” denoting an edge between nodes $u$ and $v$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print any suitable relabeling β€” a permutation of length $n$ which maximizes the number of nodes that can be chosen in the first turn that guarantee a win for Eikooc. If there are multiple such relabelings, you may print any of them. -----Examples----- Input 3 1 2 1 2 3 1 2 1 3 Output 1 2 1 1 2 3 -----Note----- In the first test case, Eikooc has only one choice. Sushi will have no moves to play after Eikooc chooses this node and Eikooc will win. In the second test case, $1 \oplus 2 = 3 \nleq min(1, 2)$. Hence, after Eikooc picks either of the nodes, Sushi will have no moves to play and Eikooc will win. Both $\{1, 2\}$ and $\{2, 1\}$ are optimal relabelings.
p, r = input, range for _ in r(int(p())): n = int(p()) h = [set() for i in r(n)] if n == 1: print(1) continue for i in r(n - 1): u, v = map(int, p().split()) h[u - 1].add(v - 1) h[v - 1].add(u - 1) c, s, g, i = [len(h[i]) for i in r(n)], [0] * n, [0], 1 while 2**i - 1 < n: g.append(2**i - 1) i += 1 g.append(n) l = [i for i in r(n) if c[i] == 1] while l != []: b = [] for v in l: c[v], e = 0, set() for u in h[v]: if c[u] > 0: c[u] -= 1 if c[u] == 1: b.append(u) elif s[u] != 0: e.add(len(bin(s[u])) - 2) for i in r(len(g) - 1, 0, -1): if i not in e and g[i] >= 2 ** (i - 1): s[v] = g[i] g[i] -= 1 break l = b print(" ".join([str(c) for c in s]))
ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR LIST NUMBER NUMBER WHILE BIN_OP BIN_OP NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR LIST ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
MAX = 64 def maxOR(L, R): if L == R: return L ans = 0 for i in range(MAX - 1, -1, -1): p = 1 << i lbit = L >> i & 1 rbit = R >> i & 1 if rbit == 1 and lbit == 0: ans += (p << 1) - 1 break if rbit == 1: ans += p return ans for _ in range(int(input())): l, r = map(int, input().split()) print(maxOR(l, r))
ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def max_bitwise_or(L, R): v1 = [] v2 = [] v3 = [] z = 0 i = 0 ans = 0 cnt = 1 while L > 0: v1.append(L % 2) L = L // 2 while R > 0: v2.append(R % 2) R = R // 2 while len(v1) != len(v2): v1.append(0) for i in range(len(v2) - 1, -1, -1): if v2[i] == 1 and v1[i] == 0 and z == 0: z = 1 continue if z == 1: v1[i] = 1 for i in range(len(v2)): v3.append(v2[i] | v1[i]) for i in range(len(v2)): if v3[i] == 1: ans += cnt cnt *= 2 return ans for _ in range(int(input())): a = list(map(int, input().split())) print(max_bitwise_or(a[0], a[1]))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
try: t = int(input()) for i in range(t): n, m = map(int, input().split()) r, s = bin(n), bin(m) if len(r) != len(s): k = max(len(r), len(s)) - 2 h = "1" * k print(int(h, 2)) else: i = 0 while i < len(r) and r[i] == s[i]: i += 1 ans = r[:i] + "1" * (len(r) - i) print(int(ans[2:], 2)) except: pass
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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): l, r = map(int, input().split()) first, second = bin(l)[2:], bin(r)[2:] if len(first) == len(second): cur = len(first) for i in range(len(first)): if first[i] == second[i]: cur -= 1 else: break print(r | (1 << cur) - 1) else: print((1 << len(second)) - 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def countTotalBits(num): binary = bin(num)[2:] return len(binary), str(binary) def comparision(string_l, string_r): l = len(string_l) c = 0 for k in range(l): if string_l[k] == string_r[k]: c += 1 else: break return c number_of_test_cases = int(input()) for t in range(number_of_test_cases): l, r = input().split() l, r = [int(l), int(r)] min_length, string_l = countTotalBits(l) max_length, string_r = countTotalBits(r) if max_length - min_length > 0: temp = int("1" * (max_length - 1), 2) print(temp | r) else: temp = comparision(string_l, string_r) answer = string_l[:temp] + "1" * (max_length - temp) print(int(answer, 2))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
tests = int(input()) while tests: tests -= 1 l, r = map(int, input().split()) l, r = bin(l)[2:], bin(r)[2:] m = max(len(l), len(r)) l = l.zfill(m) ans = "" for i, j in zip(l, r): if i == j: ans += i else: ans += "1" break for i in range(m - len(ans)): ans += "1" print(int(ans, 2))
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 VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): l, r = [int(x) for x in input().split()] c = 1 while l != r: l = l >> 1 r = r >> 1 c = c << 1 l = l * c c = c - 1 l = l | c print(l)
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 NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): x, y = map(int, input().split()) mi = str(bin(x)[2:]) ma = str(bin(y)[2:]) mi = "0" * (len(ma) - len(mi)) + mi for i in range(len(ma)): if mi[i] != ma[i]: break print(eval("0b" + ma[: i + 1] + "1" * (len(ma) - (i + 1))))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP VAR NUMBER BIN_OP STRING BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): l, r = map(int, input().split()) x = l ^ r m = 1 while x: x >>= 1 m <<= 1 print(r | m - 1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for _ in range(t): l, r = map(int, input().split()) a = str(bin(l).replace("0b", "")) b = str(bin(r).replace("0b", "")) k = "" q = 0 if len(a) == len(b): for i in range(len(a)): if a[i] == "1" and b[i] == "1": k += "1" elif a[i] == "0" and b[i] == "1": k += "1" q = 1 elif a[i] == "1" and b[i] == "0": k += "1" elif a[i] == "0" and b[i] == "0": if q == 1: k += "1" else: k += "0" else: k = "1" * len(b) print(int(k, 2))
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 FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR STRING IF VAR VAR STRING VAR VAR STRING VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR STRING IF VAR VAR STRING VAR VAR STRING IF VAR NUMBER VAR STRING VAR STRING ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for _ in range(t): l, r = map(int, input().split()) L = str(bin(l))[2:] R = str(bin(r))[2:] if len(L) == len(R): res = ["1"] * len(R) for i in range(len(R)): if R[i] == L[i]: res[i] = R[i] else: break res = "".join(res) else: res = "1" * len(R) num = int(res, 2) print(num)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
a = int(input()) for i in range(a): t, r = map(int, input().split()) s1 = bin(t) k1 = s1[2:] s2 = bin(r) k2 = s2[2:] final = "" fin = "" if len(k1) != len(k2): print(2 ** len(k2) - 1) else: for i in range(len(k1)): if k1[i] != k2[i]: for j in range(i + 1, len(k1)): final = final + "1" break fin = k1[0 : i + 1] + final print(r | int(fin, 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for x0 in range(t): l, r = input().split() l = int(l) r = int(r) strs1 = "{0:b}".format(max(l, r)) strs2 = "{0:b}".format(min(l, r)) if len(strs1) != len(strs2): ans = 2 ** max(len(strs1), len(strs2)) - 1 else: i = 0 fin = "" while i < len(strs1): if strs1[i] == strs2[i]: fin += strs1[i] else: while i < len(strs1): fin += "1" i += 1 i += 1 ans = int(fin, 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 FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): a, b = [int(j) for j in input().split()] s1, s2 = bin(a)[2:], bin(b)[2:] l1 = s1 l2 = s2 if len(l1) != len(l2): ans = "1" * len(l2) else: ans = "" for i in range(len(l1)): if l1[i] == l2[i]: ans = ans + l1[i] else: break ans = ans + (len(l1) - len(ans)) * "1" print(int(ans, 2))
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 VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for p in range(int(input())): l, r = map(int, input().split()) bl = bin(l)[2:] br = bin(r)[2:] br = str(br) bl = str(bl) bl = "0" * (len(br) - len(bl)) + str(bl) a = "" b = "" for i in range(len(bl)): if bl[i] == "0" and br[i] == "1" and i < len(bl) - 1: a += "01" b += "10" break else: a += bl[i] b += br[i] a += "1" * (len(br) - len(a)) b += "1" * (len(br) - len(b)) print(int(a, 2) | int(b, 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 FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR STRING VAR STRING VAR VAR VAR VAR VAR VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) while t > 0: t -= 1 l, r = list(map(int, input().split())) l = bin(l).replace("0b", "") r = bin(r).replace("0b", "") if len(l) < len(r): print(int("1" * len(r), 2)) elif len(l) == len(r): for i in range(len(l)): if r[i] == "1" and l[i] == "0": r = r[:i] + "1" * (len(l) - i) break print(int(r, 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): l, r = map(int, input().split()) i = 0 while l | 1 << i < r: l = l | 1 << i i += 1 print(l | 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 NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for w in range(int(input())): l, r = [int(n) for n in input().split()] l = str(bin(l)) r = str(bin(r)) while len(l) != len(r): l = l[0:2] + "0" + l[2:] index = 0 if l != r: while l[index] == r[index]: index += 1 l = l[: index + 1] + "".join(["1" * (len(l) - index - 1)]) r = r[: index + 1] + "".join(["0" * (len(r) - index - 1)]) print(int(l, 2) | int(r, 2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL STRING LIST BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL STRING LIST BIN_OP STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): l, r = map(int, input().split()) sl = bin(l)[2:] sr = bin(r)[2:] nl, nr = len(sl), len(sr) sl = "0" * (nr - nl) + sl idx = 0 for i in range(nr): if sl[i] != sr[i]: break idx = i + 1 sl = sl[:idx] + "1" * (nr - idx) print(int(sl, 2) | 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 VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def ans(a, b): if a == b: return a | b a1 = str(bin(a)[2:]) b1 = str(bin(b)[2:]) if len(a1) > len(b1): b1 = "0" * (len(a1) - len(b1)) + b1 elif len(b1) > len(a1): a1 = "0" * (len(b1) - len(a1)) + a1 i = 0 s = 0 while a1[i] == b1[i]: s += int(a1[i]) * 2 ** (len(a1) - i - 1) i += 1 for j in range(i, len(a1)): if a1[j] == "0" and b1[j] == "0": if a + 2 ** (len(a1) - j - 1) <= b: s = s + 2 ** (len(a1) - j - 1) a = a + 2 ** (len(a1) - j - 1) else: s = s + 2 ** (len(a1) - j - 1) return s for _ in range(int(input())): a, b = map(int, input().split()) print(ans(a, b))
FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING IF BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for f in range(t): L, R = map(int, input().split()) ls = bin(L) rs = bin(R) l = ls[2:] r = rs[2:] for i in range(len(rs) - len(ls)): l = "0" + l s = len(r) for i in range(s): if l[i] != r[i]: r = r[: i + 1] for j in range(i + 1, s): r += "1" break r = "0b" + r print(int(r, 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
from sys import stdin t = int(stdin.readline()) while t: l, r = [int(x) for x in stdin.readline().split()] if l == r: print(r) else: bin_l = f"{l:064b}" bin_r = f"{r:064b}" i = 0 max_val = "" while bin_l[i] == bin_r[i]: max_val += bin_l[i] i += 1 temp = 64 - i for i in range(temp): max_val += "1" print(int(max_val, 2)) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): L, R = map(int, input().split()) R1 = R L, R = bin(L)[2:], bin(R)[2:] x, y = len(L), len(R) L = ["0"] * (y - x) + list(L) R = list(R) i = 0 while i < y: if L[i] != R[i]: break i += 1 L = L[:i] + ["1"] * (y - i) L = "".join(L) print(int(L, 2) | R1)
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 ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP LIST STRING BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for T in range(int(input())): l, r = map(int, input().split()) lbit_pos = [] rbit_pos = [] for i in range(100, -1, -1): if l >> i & 1: lbit_pos.append(i) if r >> i & 1: rbit_pos.append(i) index = min(len(lbit_pos), len(rbit_pos)) flag = True value = 0 for i in range(index): if lbit_pos[i] != rbit_pos[i]: value += (1 << rbit_pos[i] + 1) - 1 flag = False break else: value += 1 << rbit_pos[i] if flag == True: if index != len(rbit_pos): value += (1 << rbit_pos[i + 1] + 1) - 1 print(value)
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 FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for you in range(t): l = input().split() L = int(l[0]) R = int(l[1]) if L == R: print(L) else: num = 0 for i in range(63, -1, -1): if L & 1 << i == 0 and R & 1 << i > 0: num = num + (2 * (1 << i) - 1) break else: num = num + (L & 1 << i) print(num)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
T = int(input()) ans = [] for _ in range(T): L, R = [int(i) for i in input().split()] l = bin(L)[2:] r = bin(R)[2:] if len(l) != len(r): ans.append(int(max(len(l), len(r)) * "1", 2)) else: i = 0 while i < len(r) and l[i] == r[i]: i += 1 s = l[:i] + "1" * (len(l) - i) ans.append(int(s, 2)) 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 VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for ii in range(int(input())): l, r = map(int, input().split()) lb = bin(l)[2:] rb = bin(r)[2:] lb = "0" * (len(rb) - len(lb)) + lb s = "" flag = 0 for i in range(len(rb)): if flag or lb[i] != rb[i]: flag = 1 s += "1" else: s += rb[i] x = 0 ss = s[::-1] p = 1 for i in ss: x += int(i) * p p = p * 2 print(x)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR STRING VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def bi(X, l): return (l - len(bin(X)[2:])) * "0" + bin(X)[2:] for _ in range(int(input())): L, R = list(map(int, input().split())) q = bin(R)[2:] length = len(q) p = bi(L, length) changed = False ans = "" for i in range(length): if p[i] != q[i]: ans += (length - i) * "1" break else: ans += p[i] print(int(ans, 2))
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER 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 VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR STRING VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): x, y = map(int, input().split()) x = list(bin(x)[2:]) y = list(bin(y)[2:]) x = list("0" * (len(y) - len(x))) + x for i in range(len(y)): if y[i] == "1" and x[i] == "0": for j in range(i + 1, len(y)): y[j] = "1" break y = int("".join(y), 2) print(y)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for _ in range(t): L, R = map(int, input().split()) bl = bin(L)[2:] br = bin(R)[2:] if len(bl) < len(br): nl = "1" * len(br) print(int(nl, 2)) else: com_end = 0 while com_end < len(bl): if bl[com_end] != br[com_end]: break com_end = com_end + 1 t = "1" * (len(bl) - com_end) ans = br[:com_end] + t print(int(ans, 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) while t: t -= 1 l, r = [int(i) for i in input().split()] bl = bin(l)[2:] br = bin(r)[2:] ans = "" if l == r: print(r) elif len(bl) < len(br): for i in range(len(br)): ans += "1" print(int("0b" + ans, 2)) else: for i in range(len(br)): if br[i] == "1" and bl[i] == "0": temp = i break for i in range(temp): ans += br[i] for i in range(temp, len(br)): ans += "1" print(int("0b" + ans, 2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP STRING VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def IntegerCreator(x2, start, end): for i in range(start, end): if x2[i] == "0": x2[i] = "1" return x2 def preComputation(): arr = [1] for i in range(1, 64): arr.append(arr[-1] * 2) return arr t = int(input()) arr = preComputation() while t: t -= 1 l, r = map(int, input().split()) x1 = bin(l)[2:] x2 = bin(r)[2:] l1 = len(x1) l2 = len(x2) for i in range(l2 - l1): x1 = "0" + x1 x1 = list(x1) x2 = list(x2) for i in range(l2): if x2[i] == "1": if x1[i] == "1": pass else: x2 = IntegerCreator(x2, i, l2) break x2 = "".join(x2) ans = 0 for i in range(l2): if x2[l2 - i - 1] == "1": ans += arr[i] print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
try: num = int(input()) while num > 0: l, r = map(int, input().split()) r = bin(r) l = bin(l) size = len(r) count = 0 i = 2 if len(r) != len(l): r = list(r) while i < size: r[i] = "1" i += 1 r = "".join(r) else: while i < size: if r[i] == l[i]: count += 1 else: break i += 1 index = 2 + count r = list(r) while index < size: r[index] = "1" index += 1 r = "".join(r) print(int(r, 2)) num -= 1 except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) while t > 0: n, m = map(int, input().split()) a = bin(n) a = a[2:] c = bin(m) c = c[2:] minlen = min(len(a), len(c)) if len(a) > len(c): maxdiff = len(a) - minlen c = "0" * maxdiff + c else: maxdiff = len(c) - minlen a = "0" * maxdiff + a res = "" for i in range(len(a)): if a[i] == "0" and c[i] == "1": res = res + (len(a) - i) * "1" break else: res = res + a[i] p = int(res, 2) print(p) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def convert(n, arr): for i in range(60): arr[-(i + 1)] = n % 2 n = n // 2 t = int(input()) for i in range(t): l, r = [int(i) for i in input().split()] l_bit = [(0) for i in range(60)] r_bit = [(0) for i in range(60)] convert(l, l_bit) convert(r, r_bit) ans = 0 for i in range(60): if l_bit[i] == 1 and r_bit[i] == 1: ans += 2 ** (59 - i) elif l_bit[i] == 0 and r_bit[i] == 1: ans += 2 ** (60 - i) - 1 break print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for _ in range(t): a, b = input().split() a = int(a) b = int(b) p = bin(a).replace("0b", "") q = bin(b).replace("0b", "") if len(p) < len(q): q = q.replace("0", "1") print(int(q, 2)) else: temp = 0 for i in range(1, len(q) + 1): if q[0:i] != p[0:i]: temp = i - 1 break if temp != 0: q = q[0:temp] + q[temp:].replace("0", "1") print(int(q, 2))
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 FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): l, r = map(int, input().split()) bl, br = bin(l)[2:], bin(r)[2:] l1, l2 = len(bl), len(br) bl = "0" * (l2 - l1) + bl for i in range(l2): if bl[i] != br[i]: break bl = bl[: i + 1] + (l2 - (i + 1)) * "1" print(int(bl, 2) | int(br, 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 VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for i in range(t): l, r = map(int, input().split()) binary = bin(r) binary = binary[2:] binary = list(binary) ans = r for i in range(len(binary)): if binary[i] == "0": num = ["1"] * (len(binary) - i) num = int("".join(num), 2) if num < r and num >= l: ans = r | num break else: alter = bin(l) alter = alter[2:] alter = list(alter) for j in range(len(alter) - 1, -1, -1): if alter[j] == "0": x = alter[:] x[j] == "1" x = int("".join(x), 2) if x >= r: break else: alter[j] = "1" alter = "".join(alter) alter = int(alter, 2) ans = r | alter break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP LIST STRING BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER 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 EXPR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): l, r = map(int, input().split()) if l == r: print(l) continue bin_l = bin(l)[2:] bin_r = bin(r)[2:] if len(bin_l) < len(bin_r): bin_l = "0" * (len(bin_r) - len(bin_l)) + bin_l i = 0 while bin_l[i] == bin_r[i]: i += 1 ans = bin_r[:i] + "1" * (len(bin_r) - i) print(int(ans, 2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def decimalToBinary(n): return bin(n).replace("0b", "") for _ in range(int(input())): l, r = map(int, input().split()) a, b = decimalToBinary(l), decimalToBinary(r) if len(a) != len(b): print(pow(2, max(len(a), len(b))) - 1) else: ans = "" index = 0 for i in range(len(a)): if a[i] == b[i]: ans += a[i] continue index = i break if index != 0: for i in range(index, len(a)): ans += "1" print(int(ans, 2))
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
T = int(input()) for _ in range(T): N, M = map(int, input().split()) if N == M: print(N) continue a = 0 b = 0 x = N y = M s = 0 t = 0 while N > 0: a = a * 10 + N % 2 N = N // 2 s += 1 while M > 0: b = b * 10 + M % 2 M = M // 2 t += 1 N = x M = y if s == t: p = s p = 2 ** (p - 1) v = 0 while b > 0: if a % 10 != b % 10: break v += p * (b % 10) a = a // 10 b = b // 10 p = p // 2 v += p + p - 1 print(v) else: p = t p = 2 ** (p - 1) v = p + p - 1 print(v)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): l, r = map(int, input().split()) lb, rb = bin(l)[2:], bin(r)[2:] ll, rl = len(lb), len(rb) if l == 0 and r == 0: print(0) continue if l == r: print(l) continue ans = "" if ll == rl: i = 0 for i in range(ll): if lb[i] != rb[i]: break else: ans += lb[i] ans += "1" * (ll - i) else: ans += "1" * rl print(int(ans, 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 VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def decimalToBinary(n): return bin(n).replace("0b", "") def binaryToDecimal(n): return int(n, 2) t = int(input()) for i in range(t): a, b = map(int, input().split()) if a == 0 and b == 0: print(0) elif a == b: print(a) else: a_bin = decimalToBinary(a) b_bin = decimalToBinary(b) if len(a_bin) == len(b_bin): for i in range(len(a_bin)): if a_bin[i] == b_bin[i]: continue else: ind = i break ans_st = "" for i in range(len(a_bin)): if i < ind: ans_st += a_bin[i] else: ans_st += "1" ans = binaryToDecimal(ans_st) print(ans) elif len(a_bin) < len(b_bin): ans_st = "" for i in range(len(b_bin)): ans_st += "1" ans = binaryToDecimal(ans_st) print(ans)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for _ in range(t): l, r = map(int, input().split()) ans = "" x = f"{l:64b}" y = f"{r:64b}" pos = 64 for i in range(64): if x[i] != y[i]: pos = i break else: ans += x[i] for j in range(pos, 64): ans += "1" print(int(ans, 2))
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 STRING ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for _ in range(t): L, R = map(int, input().split()) first = "{0:b}".format(R) second = "{0:b}".format(L) first = "0" * (64 - len(first)) + first second = "0" * (64 - len(second)) + second i = 0 while i < 64: if first[i] == "1" and second[i] == "0": break i += 1 ans = 0 for j in range(i): if first[j] == "1": ans += 2 ** (63 - j) for k in range(i, 64): ans += 2 ** (63 - k) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
while True: try: for _ in range(int(input())): l, r = map(int, input().split()) l, r = min(l, r), max(l, r) binL = bin(l)[2:] binR = bin(r)[2:] llen = len(binL) rlen = len(binR) binL = "0" * (rlen - llen) + binL llen = rlen ans = [] ind = -1 i = 0 while i < rlen and binR[i] == binL[i]: ans.append(binR[i]) ind = i i += 1 ans += ["1"] * (rlen - 1 - ind) print(int("".join(ans), 2)) except (ValueError, EOFError) as e: break
WHILE NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP LIST STRING BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for _ in range(int(input())): L, R = map(int, input().split()) maxl = 1 while maxl << 1 <= R: maxl = maxl << 1 if maxl - 1 >= L: print(maxl - 1 | R) else: while L | L + 1 <= R: L |= L + 1 print(L | 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 NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def binaryToDecimal(n): return int(n, 2) def decimalToBinary(n): return bin(n).replace("0b", "") t = int(input()) for i in range(t): l, r = input().split() l = int(l) r = int(r) str1 = decimalToBinary(l) str2 = decimalToBinary(r) lendiff = len(str2) - len(str1) temp = "0" * lendiff newstr = temp + str1[:] cnt = 0 for j in range(len(str2)): if str2[j] == newstr[j]: cnt += 1 else: break ans = str2[:cnt] + "1" * (len(str2) - cnt) print(binaryToDecimal(ans))
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP STRING BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def till_what_length(str1, str2): count = 0 for i in range(len(str1)): if str1[i] != str2[i]: break count += 1 return count def get_bin(x, l): return format(x, "b").zfill(l) test = int(input()) for _ in range(test): l, r = input().split() l = int(l) r = int(r) n = len(bin(r)[2:]) l1 = get_bin(l, n) r1 = get_bin(r, n) counter = till_what_length(l1, r1) final_bin = l1[0:counter] + "1" * (n - counter) print(int(final_bin, 2))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
import sys def different_bit_pos(x, y): max_len = max(len(x), len(y)) if len(x) == max_len: new_y = "" for i in range(max_len - len(y)): new_y += "0" new_y += y y = new_y elif len(y) == max_len: new_x = "" for i in range(max_len - len(x)): new_x += "0" new_x += x x = new_x for i in range(max_len): if x[i] != y[i]: return i, x, y return -1, x, y t = int(sys.stdin.readline()) for test_case in range(t): l, r = map(int, sys.stdin.readline().split()) left_digit = bin(l)[2:] right_digit = bin(r)[2:] pos, left_digit, right_digit = different_bit_pos(left_digit, right_digit) if pos != -1: constructed_left = left_digit[:pos] constructed_right = right_digit[:pos] constructed_left += "0" constructed_right += "1" for i in range(len(left_digit) - pos - 1): constructed_left += "1" constructed_right += "0" l = int(constructed_left, 2) r = int(constructed_right, 2) print(l | r) else: print(l)
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR STRING VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR STRING VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR STRING VAR STRING FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
for i in range(int(input())): l, r = map(int, input().split()) sr = bin(r).replace("0b", "") sl = bin(l).replace("0b", "") ans = 0 if len(sr) > len(sl): for i in range(len(sr)): ans += 2**i print(ans) else: k = r t = l for i in range(len(sr)): if sr[i] == "0": if sl[i] == "0": if t + 2 ** (len(sr) - 1 - i) <= r: k += 2 ** (len(sr) - 1 - i) t += 2 ** (len(sr) - 1 - i) if sl[i] == "1": k += 2 ** (len(sr) - 1 - i) 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 FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR VAR STRING IF BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
t = int(input()) for i in range(t): l, r = map(int, input().split()) a, b = bin(l), bin(r) a, b = a[2:], b[2:] if len(b) > len(a): x = len(b) - len(a) a = "0" * x + a ans = "" for i in range(len(a)): if a[i] == b[i]: ans += a[i] elif a[i] == "0" and b[i] == "1": ans += "1" break y = len(ans) if y < len(b): z = len(a) - y ans += "1" * z print(int(ans, 2))
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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
test = int(input()) for _ in range(test): l, r = map(int, input().split()) xor1 = l ^ r max1 = 0 while xor1: max1 = max1 << 1 max1 = max1 | 1 xor1 = xor1 >> 1 max1 = r | max1 print(max(max1, r))
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 VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Dr.D got so famous thanks to his first problem that he decided to commemorate it by making yet another problem with an almost identical statement β€” he only changed one word. We want to choose exactly two integers (not necessarily distinct) from the interval $[L, R]$. Find the maximum possible value of the bitwise OR of the chosen integers. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first and only line of each test case contains two space-separated integers $L$, $R$. ------ Output ------ For each test case, print a single line containing one integer β€” the maximum bitwise OR. ------ Constraints ------ $1 ≀ T ≀ 10^{5}$ $0 ≀ L ≀ R ≀ 10^{18}$ ----- Sample Input 1 ------ 4 1 3 4 5 1031 93714 1000000000123 1000000123456 ----- Sample Output 1 ------ 3 5 131071 1000000192511
def solve(l, r): r = bin(r)[2:] l = bin(l)[2:] l = "0" * (len(r) - len(l)) + l res, beg = "", True for u, d in zip(r, l): if u == d and beg: res += u elif beg: beg = False res += u else: res += "1" return int(res, 2) for _ in range(int(input())): l, r = map(int, input().split()) print(solve(l, r))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR STRING NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
t = int(input()) for _ in range(t): n, m = map(int, input().split()) A = list(map(int, input().split())) if n > sum(A): ans = -1 else: power = [0] * 62 for a in A: c = 0 while a != 1: a //= 2 c += 1 power[c] += 1 n_power = [0] * 62 c = 0 while n > 0: n_power[c] = n % 2 n //= 2 c += 1 ans = 0 for i in range(61): if n_power[i] == 0: if power[i] >= 2: power[i + 1] += power[i] // 2 elif power[i] >= 1: power[i + 1] += (power[i] - 1) // 2 else: for j in range(1, 62): if power[i + j] == 0: power[i + j] = 1 ans += 1 else: power[i + j] -= 1 ans += 1 break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import sys input = sys.stdin.readline Q = int(input()) Query = [] for _ in range(Q): N, M = map(int, input().split()) A = list(map(int, input().split())) Query.append((N, M, A)) for N, M, A in Query: delta = sum(A) - N if delta < 0: print(-1) else: L = max(N, max(A)).bit_length() D = [0] * L for a in A: D[a.bit_length() - 1] += 1 S = 0 to = -1 ans = 0 for l in range(L): if to != -1 and D[l] > 0: ans += l - to to = -1 D[l] -= 1 S += D[l] * (1 << l) if 1 << l & N: if S >= 1 << l: S -= 1 << l elif to == -1: to = l if to != -1: print(-1) else: print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP BIN_OP NUMBER VAR VAR IF VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import sys sz = 1 temp = 1 while temp <= 1e18: sz += 1 temp <<= 1 def main(): t = int(input()) allans = [] for _ in range(t): n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] if sum(a) < n: allans.append(-1) else: setBitCnts = [(0) for _ in range(sz)] for x in a: for i in range(sz): if x & 1 << i > 0: setBitCnts[i] += 1 ans = 0 smallerBitSum = 0 for i in range(sz): smallerBitSum += setBitCnts[i] * 2**i if n & 1 << i > 0: if smallerBitSum >= 1 << i: smallerBitSum -= 1 << i else: j = i while setBitCnts[j] == 0: j += 1 while j > i: setBitCnts[j] -= 1 setBitCnts[j - 1] += 2 j -= 1 ans += 1 setBitCnts[j] -= 1 allans.append(ans) for x in allans: print(x) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(*args): assert ( len(args) >= 2 ), "makeArr args should be (default value, dimension 1, dimension 2,..." if len(args) == 2: return [args[0] for _ in range(args[1])] else: return [makeArr(args[0], *args[2:]) for _ in range(args[1])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
def solve(n, arr): if sum(arr) < n: return -1 m = len(arr) bits = [0] * 61 for num in arr: bits[num.bit_length() - 1] += 1 i = 0 l = n.bit_length() res = 0 while i < l: if n >> i & 1: if bits[i]: bits[i] -= 1 else: while bits[i] == 0: i += 1 res += 1 bits[i] -= 1 continue bits[i + 1] += bits[i] >> 1 i += 1 return res tests = int(input()) for _ in range(tests): n, m = list(map(int, input().split())) arr = list(map(int, input().split())) print(solve(n, arr))
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) sum_a = sum(a) if sum_a < n: print(-1) continue cnts = [0] * 40 for i in range(m): bit = a[i].bit_length() - 1 cnts[bit] += 1 ans = 0 for i in range(39): if not 1 << i & n: pass elif cnts[i] != 0: cnts[i] -= 1 else: for j in range(i, 40): if cnts[j] > 0: cnts[j] -= 1 break for k in range(i, j): ans += 1 cnts[k] += 1 cnts[i] -= 1 if cnts[i] != 0: cnts[i + 1] += cnts[i] // 2 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
def solve(num, arr): freq = [(0) for i in range(64)] for i in arr: x = i pos = 0 while x > 0: if x & 1: freq[pos] += 1 x = x >> 1 pos += 1 if num > sum(arr): return -1 ans = 0 pos = 0 while num > 0: if num & 1: if freq[pos] > 0: freq[pos] -= 1 else: while pos < 64 and freq[pos] <= 0: ans += 1 freq[pos] += 1 pos += 1 num = num >> 1 freq[pos] -= 1 continue freq[pos + 1] += freq[pos] // 2 pos += 1 num = num >> 1 return ans t = int(input()) for j in range(t): num, n = [int(ele) for ele in input().strip().split()] arr = [int(ele) for ele in input().strip().split()] print(solve(num, arr))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
def log2(n): i = 0 while n >> i != 1: i += 1 return i def solve(n, po2s): if sum(po2s) < n: return -1 po_cnt = [0] * 60 for x in po2s: po_cnt[log2(x)] += 1 ans = 0 for i in range(60): if n & 1 << i: if po_cnt[i]: po_cnt[i] -= 1 else: j = i while po_cnt[j] == 0: j += 1 while j > i: ans += 1 po_cnt[j] -= 1 po_cnt[j - 1] += 2 j -= 1 po_cnt[i] -= 1 if i < 59: po_cnt[i + 1] += po_cnt[i] // 2 return ans t = int(input()) for _ in range(t): n, m = map(int, input().split()) po2s = list(map(int, input().split())) print(solve(n, po2s))
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
t = int(input()) for _ in range(0, t): n, m = map(int, input().split()) aa = [int(i) for i in input().split()] bits = [0] * 70 for i in aa: bb = bin(i)[2:][::-1] for j in range(0, len(bb)): bits[j] += int(bb[j]) ss = 0 ans = [0] * 70 bb = bin(n)[2:][::-1] for j in range(0, len(bb)): ans[j] += int(bb[j]) i, j = 0, 0 while i < 64: if ans[i] == 0: bits[i + 1] += bits[i] // 2 i += 1 if ans[i] == 1 and bits[i]: bits[i] -= 1 bits[i + 1] += bits[i] // 2 i += 1 elif ans[i] == 1 and not bits[i]: j = i while j < 64 and bits[j] == 0: j += 1 if j == 64: ss = -1 break bits[j] -= 1 ss += j - i i = j print(ss)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
def problem(n, a): if sum(a) < n: return -1 bit = [0] * 61 for i in a: bit[i.bit_length() - 1] += 1 i, r, l = 0, 0, n.bit_length() while i < l: if n >> i & 1: if bit[i]: bit[i] -= 1 else: while bit[i] == 0: i, r = i + 1, r + 1 bit[i] -= 1 continue bit[i + 1] += bit[i] >> 1 i += 1 return r for i in range(int(input())): n = int(input().split()[0]) a = list(map(int, input().split())) print(problem(n, a))
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
t = int(input()) def az(i): j = i while j > -1: if ab[j] != 0: return j j -= 1 return j for _ in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) nb = [(0) for i in range(60)] ab = [(0) for i in range(60)] i = 59 while n > 0: nb[i] = n % 2 i -= 1 n >>= 1 for j in a: k = 0 while 1 << k != j: k += 1 ab[59 - k] += 1 ans = 0 for i in range(59, -1, -1): if nb[i]: j = az(i) if ab[i] != 0: ab[i] -= 1 ab[i - 1] += ab[i] // 2 elif j != -1: ab[j] -= 1 ans += i - j for k in range(j + 1, i + 1): ab[k] += 1 else: print(-1) break else: ab[i - 1] += ab[i] // 2 else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
for t in range(int(input())): n, m = [int(i) for i in input().split()] a = [int(i) for i in input().split()] pows = [0] * 50 for i in a: pows[i.bit_length() - 1] += 1 p = 1 i = 0 tot = 0 make = 100 while (n > 0 or make < 100) and i < len(pows) - 1: if n % (2 * p) != 0: if pows[i] > 0: pows[i] -= 1 else: make = min(make, i) n -= p if pows[i] > 0 and make < i: tot += i - make make = 100 pows[i] -= 1 pows[i + 1] += pows[i] // 2 pows[i] %= 2 i += 1 p *= 2 if n == 0 and make == 100: print(tot) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) if sum(a) < n: print(-1) continue c = [0] * 61 for i in range(m): x = 0 while a[i] % 2 == 0: a[i] //= 2 x += 1 c[x] += 1 ans = 0 i = 0 while i < 60: if n >> i & 1: if c[i] > 0: c[i] -= 1 else: while i < 60 and c[i] == 0: i += 1 ans += 1 c[i] -= 1 continue c[i + 1] += c[i] // 2 i += 1 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [int(item) for item in input().split()] if sum(a) < n: print(-1) continue comp = dict() for i in range(65): comp[2**i] = i nums = [0] * 65 for item in a: nums[comp[item]] += 1 filled = [False] * 65 target = 0 cumsum = 0 ans = 0 for i in range(65): cumsum += nums[i] * 2**i if n & 1 << i: target += 2**i if target <= cumsum: continue if filled[i]: continue r = i while nums[r] == 0: r += 1 nums[r] -= 1 cumsum += 2**r ans += r - i for j in range(i, r): filled[j] = True 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ def solve(): n, m = map(int, input().split()) a = list(map(int, input().split())) if sum(a) < n: print(-1) return k = 62 count = [0] * k for ai in a: count[ai.bit_length() - 1] += 1 ans = 0 for i in range(k - 1): if n >> i & 1: if not count[i]: for j in range(i + 1, k): if count[j]: ans += j - i count[j] -= 1 for l in range(j - 1, i - 1, -1): count[l] += 1 count[i] += 1 break count[i] -= 1 count[i + 1] += count[i] // 2 print(ans) t = int(input()) for i in range(t): solve()
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import sys input = sys.stdin.readline def solve(): n, m = map(int, input().split()) a = list(map(int, input().split())) cnt = [0] * 64 for i in range(m): cnt[a[i].bit_length() - 1] += 1 sum = 0 ans = 0 for j in range(64): bit = 1 << j sum += cnt[j] * bit if bit & n: if sum >= bit: sum -= bit else: found = False for k in range(j + 1, 64): if cnt[k] > 0: found = True cnt[k] -= 1 break if not found: ans = -1 break sum += 1 << k ans += k - j sum -= bit print(ans) T = int(input()) for t in range(T): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP 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
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
t = int(input()) for case in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) if n > sum(a): print(-1) continue last = 0 bn = bin(n) ba = [(len(bin(i)) - 3) for i in a] dig_a = [0] * 100 for i in ba: dig_a[i] += 1 ans = 0 for i in range(len(bn) - 2): last = last // 2 + dig_a[i] if int(bn[-1 - i]) == 1: if last > 0: last -= 1 else: j = 1 while True: if i + j >= 100: break if dig_a[i + j] > 0: ans += j last += 2**j - 1 dig_a[i + j] -= 1 break else: j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
t = int(input()) for _ in range(t): n, m = map(int, input().split(" ")) a = list(map(int, input().strip().split(" "))) if sum(a) < n: print("-1") continue b = [(0) for _ in range(64)] for z in a: for q in range(64): b[q] += z >> q & 1 tar = [(0) for _ in range(64)] for q in range(64): tar[q] += n >> q & 1 for i in range(64): if tar[i] == 1 and b[i] >= 1: tar[i] -= 1 b[i] -= 1 splits = 0 for i in range(63): if tar[i] == 0: continue for j in range(i): if b[j] > 1: b[j + 1] += b[j] // 2 b[j] = b[j] % 2 if tar[i] == 1 and b[i] >= 1: tar[i] -= 1 b[i] -= 1 continue for j in range(i + 1, 64): if b[j] >= 1: splits += 1 b[j] -= 1 b[i] -= 1 break else: b[j] += 1 splits += 1 print(splits)
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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
t = int(input()) for ijkl in range(t): n, m = map(int, input().split()) a = [int(x) for x in input().split()] f = max(a) f = max(f, n) number = 1 power = 2 while power <= f: power = power * 2 number += 1 have = [(0) for i in range(number)] for x in a: copy = x deg = 0 while copy > 1: copy = copy // 2 deg += 1 have[deg] += 1 numbers = [(0) for i in range(number)] for i in range(number): last = n % 2 numbers[i] = last n = n // 2 ans = 0 ok = True for i in range(number): if numbers[i] == 1: if have[i] >= 1: have[i] -= 1 else: j = i + 1 if j == number: ok = False break while have[j] == 0: j += 1 if j == number: ok = False break ans += j - i while j > i: have[j] -= 1 have[j - 1] += 2 j = j - 1 have[i] -= 1 if i + 1 != number: have[i + 1] += have[i] // 2 have[i] = 0 if ok: print(ans) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
def power(x): ans = 0 while x != 1: ans += 1 x >>= 1 return ans t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [int(x) for x in input().split()] cnt = [0] * 80 for i in range(m): cnt[power(a[i])] += 1 if sum(a) < n: print(-1) else: ans, idx = 0, 0 while idx < 60: if 1 << idx & n: for i in range(idx): cnt[i + 1] += cnt[i] // 2 cnt[i] = cnt[i] & 1 if cnt[idx]: n -= 1 << idx cnt[idx] -= 1 else: j = idx + 1 while not cnt[j] and j < 60: j += 1 cnt[j] -= 1 for ii in range(idx, j): ans += 1 cnt[ii] += 1 n -= 1 << idx cnt[idx] -= 1 idx += 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) β€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) β€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer β€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import sys input = sys.stdin.readline TWO = {(1 << i): i for i in range(64)} t = int(input()) for tests in range(t): n, m = map(int, input().split()) A = list(map(int, input().split())) if sum(A) < n: print(-1) continue SUM = [0] * 64 for b in range(64): if 1 << b & n != 0: SUM[b] = 1 C = [0] * 64 for a in A: C[TWO[a]] += 1 ANS = 0 for i in range(64): if SUM[i] > C[i]: for j in range(i + 1, 64): if C[j] > 0: break ANS += j - i C[i] += 1 C[j] -= 1 for k in range(i, j): C[k] += 1 if C[i] > SUM[i]: x = (C[i] - SUM[i]) // 2 C[i] -= 2 * x C[i + 1] += x print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR