description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given two integers $n$ and $m$. Find the $\operatorname{MEX}$ of the sequence $n \oplus 0, n \oplus 1, \ldots, n \oplus m$. Here, $\oplus$ is the bitwise XOR operator . $\operatorname{MEX}$ of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, $\operatorname{MEX}(0, 1, 2, 4) = 3$, and $\operatorname{MEX}(1, 2021) = 0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 30000$) β€” the number of test cases. The first and only line of each test case contains two integers $n$ and $m$ ($0 \le n, m \le 10^9$). -----Output----- For each test case, print a single integer β€” the answer to the problem. -----Examples----- Input 5 3 5 4 6 3 2 69 696 123456 654321 Output 4 3 0 640 530866 -----Note----- In the first test case, the sequence is $3 \oplus 0, 3 \oplus 1, 3 \oplus 2, 3 \oplus 3, 3 \oplus 4, 3 \oplus 5$, or $3, 2, 1, 0, 7, 6$. The smallest non-negative integer which isn't present in the sequence i. e. the $\operatorname{MEX}$ of the sequence is $4$. In the second test case, the sequence is $4 \oplus 0, 4 \oplus 1, 4 \oplus 2, 4 \oplus 3, 4 \oplus 4, 4 \oplus 5, 4 \oplus 6$, or $4, 5, 6, 7, 0, 1, 2$. The smallest non-negative integer which isn't present in the sequence i. e. the $\operatorname{MEX}$ of the sequence is $3$. In the third test case, the sequence is $3 \oplus 0, 3 \oplus 1, 3 \oplus 2$, or $3, 2, 1$. The smallest non-negative integer which isn't present in the sequence i. e. the $\operatorname{MEX}$ of the sequence is $0$.
t = int(input()) for i in range(t): n, m = map(int, input().split()) s1 = [] s2 = [] for j in range(33): u = n >> j v = m >> j s1.append(u & 1) s2.append(v & 1) ans = 0 for j in range(33): if s2[j] == 1: v = j for j in range(v + 1): ans = ans + 2**j for j in range(v, -1, -1): if s1[j] == 0 and ans - 2**j > m: ans = ans - 2**j if ans == m: ans = n + 2 ** (v + 1) if n <= m: print(ans ^ n) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given two integers $n$ and $m$. Find the $\operatorname{MEX}$ of the sequence $n \oplus 0, n \oplus 1, \ldots, n \oplus m$. Here, $\oplus$ is the bitwise XOR operator . $\operatorname{MEX}$ of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, $\operatorname{MEX}(0, 1, 2, 4) = 3$, and $\operatorname{MEX}(1, 2021) = 0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 30000$) β€” the number of test cases. The first and only line of each test case contains two integers $n$ and $m$ ($0 \le n, m \le 10^9$). -----Output----- For each test case, print a single integer β€” the answer to the problem. -----Examples----- Input 5 3 5 4 6 3 2 69 696 123456 654321 Output 4 3 0 640 530866 -----Note----- In the first test case, the sequence is $3 \oplus 0, 3 \oplus 1, 3 \oplus 2, 3 \oplus 3, 3 \oplus 4, 3 \oplus 5$, or $3, 2, 1, 0, 7, 6$. The smallest non-negative integer which isn't present in the sequence i. e. the $\operatorname{MEX}$ of the sequence is $4$. In the second test case, the sequence is $4 \oplus 0, 4 \oplus 1, 4 \oplus 2, 4 \oplus 3, 4 \oplus 4, 4 \oplus 5, 4 \oplus 6$, or $4, 5, 6, 7, 0, 1, 2$. The smallest non-negative integer which isn't present in the sequence i. e. the $\operatorname{MEX}$ of the sequence is $3$. In the third test case, the sequence is $3 \oplus 0, 3 \oplus 1, 3 \oplus 2$, or $3, 2, 1$. The smallest non-negative integer which isn't present in the sequence i. e. the $\operatorname{MEX}$ of the sequence is $0$.
from sys import * ws = lambda: map(int, stdin.readline().strip().split()) li = lambda: list(map(int, stdin.readline().strip().split())) mod = 1000000007 def ncr(n, r, p): num = den = 1 for i in range(r): num = num * (n - i) % p den = den * (i + 1) % p return num * pow(den, p - 2, p) % p def gcd(a, b): if b == 0: return a return gcd(b, a % b) def prod(l): ans = 1 for i in range(len(l)): ans = ans * l[i] return ans def sortindex(l, a): c = [] if a == -1: rev = True else: rev = False for i in range(len(l)): c.append([l[i], i]) x = sorted(c, reverse=rev) print(x) c = [] for i in range(len(l)): c.append(x[i][1]) return c for _ in range(int(input())): n, m = ws() if n > m: print(0) else: t1 = bin(m + 1) t1 = t1[2:] t2 = bin(n) t2 = t2[2:] t8 = len(t1) - len(t2) t3 = "0" * t8 t3 += t2 t4 = t1[:t8] flag = 0 for i in range(t8, len(t1)): if flag == 0: if t1[i] == "1": if t3[i] == "1": t4 += "0" else: t4 += "1" elif t3[i] == "1": t4 += "0" flag = 1 else: t4 += "0" else: t4 += "0" print(int(t4, 2))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR 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 IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING IF VAR VAR STRING VAR STRING VAR STRING IF VAR VAR STRING VAR STRING ASSIGN VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
T = int(input()) for i in range(T): Nlen, Query = map(int, input().split()) Alist = list(map(int, input().split())) prefixlist = [([0] * 60) for i in range(Nlen + 1)] for i in range(1, Nlen + 1): for k in range(60): prefixlist[i][-k - 1] = prefixlist[i - 1][-k - 1] + Alist[i - 1] % 2 Alist[i - 1] //= 2 for i in range(Query): k, l1, r1, l2, r2 = map(int, input().split()) s1, s2 = ( prefixlist[r1][-k - 1] - prefixlist[l1 - 1][-k - 1], prefixlist[r2][-k - 1] - prefixlist[l2 - 1][-k - 1], ) u1, u2 = r1 - l1 + 1 - s1, r2 - l2 + 1 - s2 print(s1 * u2 + s2 * u1)
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 BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for _ in range(int(input())): a = list(map(int, input().split())) l = list(map(int, input().split())) re = [] for i in range(62): r = [] d = 0 for j in l: if j & 1 << i: d += 1 r.append(d) re.append(r) for i in range(a[1]): k = list(map(int, input().split())) res = re[k[0]] if k[1] == 1: a = res[k[2] - 1] else: a = res[k[2] - 1] - res[k[1] - 2] b = k[2] - k[1] + 1 - a if k[3] == 1: c = res[k[4] - 1] else: c = res[k[4] - 1] - res[k[3] - 2] d = k[4] - k[3] + 1 - c print(a * d + b * c)
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for i in range(int(input())): n, q = map(int, input().split()) a = list(map(int, input().split())) bits = [0] * 60 f = [] for s in a: for j in range(60): if s & 2**j == 2**j: bits[j] += 1 k = bits.copy() f.append(k) for j in range(q): k, l1, r1, l2, r2 = map(int, input().split()) ones1 = f[r1 - 1][k] - f[l1 - 1][k] if l1 != 1 and f[l1 - 2][k] != f[l1 - 1][k]: ones1 += 1 if l1 == 1 and f[l1 - 1][k] == 1: ones1 += 1 ones2 = f[r2 - 1][k] - f[l2 - 1][k] if f[l2 - 2][k] != f[l2 - 1][k]: ones2 += 1 zero1 = r1 - l1 + 1 - ones1 zero2 = r2 - l2 + 1 - ones2 print(ones1 * zero2 + ones2 * zero1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for t in range(int(input())): n, q = map(int, input().split()) a = list(map(int, input().split())) check = [[(0) for i in range(60)] for j in range(n + 1)] for i in range(n): for j in range(60): check[i + 1][j] = check[i][j] + (a[i] >> j & 1) for x in range(q): k, l1, r1, l2, r2 = map(int, input().split()) o1, o2 = check[r1][k] - check[l1 - 1][k], check[r2][k] - check[l2 - 1][k] z1, z2 = r1 - l1 + 1 - o1, r2 - l2 + 1 - o2 print(o1 * z2 + o2 * z1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def KthBit(n, k): if n < 2**k: return 0 else: k += 1 return (n & 1 << k - 1) >> k - 1 for _ in range(int(input())): n, q = map(int, input().split()) z = lambda x: bin(int(x))[2:] arr = list(map(int, input().split())) pre = [] for i in range(60): temp = [0] p = 0 for j in arr: p += KthBit(j, i) temp.append(p) pre.append(temp) for i in range(q): k, L1, R1, L2, R2 = map(int, input().split()) o1 = pre[k][R1] - pre[k][L1 - 1] o2 = pre[k][R2] - pre[k][L2 - 1] ans = o1 * (R2 - L2 - o2 + 1) + o2 * (R1 - L1 - o1 + 1) print(ans)
FUNC_DEF IF VAR BIN_OP NUMBER VAR RETURN NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
BITS = 60 class Numbers: def __init__(self, numbers): self.numbers = numbers self.ac = [([0] * BITS) for _ in range(len(numbers))] previous = [0] * BITS for i, number in enumerate(numbers): for k in range(BITS): self.ac[i][k] = previous[k] + (1 if number & 1 << k else 0) previous[k] = self.ac[i][k] def query(self, k, l1, r1, l2, r2): ones1 = self.ones(l1, r1, k) zeros1 = r1 - l1 + 1 - ones1 ones2 = self.ones(l2, r2, k) zeros2 = r2 - l2 + 1 - ones2 return ones1 * zeros2 + ones2 * zeros1 def ones(self, l, r, k): s2 = self.ac[r - 1][k] if l == 1: return s2 else: s1 = self.ac[l - 2][k] return s2 - s1 for _ in range(int(input())): _, q = map(int, input().split()) numbers = Numbers([int(x) for x in input().split()]) for _ in range(q): k, l1, r1, l2, r2 = map(int, input().split()) print(numbers.query(k, l1, r1, l2, r2))
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for i in range(int(input())): n, q = map(int, input().split()) l = list(map(int, input().split())) b = [0] * 60 d = {} d[0] = [0] * 60 j = 1 for i in l: b1 = bin(i)[2:][::-1] for i in range(len(b1)): if b1[i] == "1": b[i] += 1 b2 = b.copy() d[j] = b2 del b2 j += 1 for i in range(q): k, l1, r1, l2, r2 = map(int, input().split()) ones1 = d[r1][k] - d[l1 - 1][k] zero1 = r1 - l1 - ones1 + 1 ones2 = d[r2][k] - d[l2 - 1][k] zero2 = r2 - l2 - ones2 + 1 print(ones1 * zero2 + ones2 * zero1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
R = lambda: map(int, input().split()) (t,) = R() for _ in range(t): n, q = R() a = [([0] * (n + 1)) for _ in range(60)] for i, x in enumerate(R()): for j, b in enumerate(a): b[i + 1] = b[i] + (x >> j & 1) for _ in range(q): k, l1, r1, l2, r2 = R() l1 -= 1 l2 -= 1 b = a[k] (d1, c1), (d2, c2) = ((r - l, b[r] - b[l]) for r, l in ((r1, l1), (r2, l2))) print(c1 * (d2 - c2) + (d1 - c1) * c2)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for _ in range(int(input())): n, q = map(int, input().split()) m = [int(i) for i in input().split()] l = [] lx = [] for i in range(n): z = [0] * 60 l.append(z) z1 = [0] * 60 lx.append(z1) for i in range(n): k = 0 j = m[i] while j: if j & 1: l[i][k] = 1 lx[i][k] = 1 j = j >> 1 k += 1 for i in range(1, n): for j in range(60): l[i][j] += l[i - 1][j] for i in range(q): k, l1, r1, l2, r2 = map(int, input().split()) x = l[r1 - 1][k] - l[l1 - 1][k] + lx[l1 - 1][k] y = l[r2 - 1][k] - l[l2 - 1][k] + lx[l2 - 1][k] nx = r1 - l1 + 1 - x ny = r2 - l2 + 1 - y print(x * ny + y * nx)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
t = int(input()) for _ in range(t): n, q = map(int, input().split()) arr = list(map(int, input().split())) prefik = dict() for k in range(60): setk = 0 prefik[0, k] = 0 for i in range(n): setk += (arr[i] >> k) % 2 prefik[i + 1, k] = setk for _ in range(q): k, l1, r1, l2, r2 = map(int, input().split()) ans = (prefik[r1, k] - prefik[l1 - 1, k]) * ( r2 - l2 + 1 - (prefik[r2, k] - prefik[l2 - 1, k]) ) + (prefik[r2, k] - prefik[l2 - 1, k]) * ( r1 - l1 + 1 - (prefik[r1, k] - prefik[l1 - 1, 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def toBin(n): return bin(n).replace("0b", "") def isValid(num, k): st = toBin(num) st = list(st) st = st[::-1] if k < len(st) and st[k] == "1": return True return False for _ in range(int(input())): n, q = map(int, input().split()) nums = list(map(int, input().split())) arr = [[(0) for i in range(n)] for j in range(64)] for i in range(n): num = nums[i] for j in range(64): arr[j][i] = int(num % 2) num = num // 2 for i in range(64): flg = 0 for j in range(n): flg += arr[i][j] arr[i][j] = flg for __ in range(q): k, l1, r1, l2, r2 = map(int, input().split()) pairs, seen = 0, set() pair1 = arr[k][r1 - 1] if l1 == 1 else arr[k][r1 - 1] - arr[k][l1 - 2] pair2 = arr[k][r2 - 1] if l2 == 1 else arr[k][r2 - 1] - arr[k][l2 - 2] totalPairs = (r1 - l1 + 1 - pair1) * pair2 + (r2 - l2 + 1 - pair2) * pair1 print(totalPairs)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR STRING RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for t in range(int(input())): n, q = [int(i) for i in input().split(" ")] a = [int(i) for i in input().split(" ")] pre = [] for i in range(n + 1): pre.append([]) pre[i] = [(0) for i in range(64)] for i in range(1, n + 1): for j in range(64): pre[i][j] += pre[i - 1][j] if a[i - 1] & 1 << j != 0: pre[i][j] += 1 for _ in range(q): k, l1, r1, l2, r2 = [int(i) for i in input().split(" ")] x = pre[r1][k] - pre[l1 - 1][k] y = pre[r2][k] - pre[l2 - 1][k] c = r1 - l1 + 1 d = r2 - l2 + 1 print(x * (d - y) + y * (c - x))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def main(): t = int(input()) for _ in range(t): n, q = map(int, input().split()) l = [int(x) for x in input().split()] a = [[(0) for i in range(n + 1)] for j in range(61)] for i in range(61): for j in range(1, n + 1): if (l[j - 1] >> i) % 2 == 1: a[i][j] = a[i][j - 1] + 1 else: a[i][j] = a[i][j - 1] for i in range(q): k, l1, r1, l2, r2 = map(int, input().split()) ro = a[k][r1] - a[k][l1 - 1] rz = r1 - l1 - ro + 1 lo = a[k][r2] - a[k][l2 - 1] lz = r2 - l2 - lo + 1 print(ro * lz + rz * lo) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for i in range(int(input())): n, q = map(int, input().split()) a = list(map(int, input().split())) c = a.copy() mp = [] for f in range(61): c[0] = c[0] >> f & 1 for j in range(1, n): c[j] = (c[j] >> f & 1) + c[j - 1] mp.append(c) c = a.copy() for j in range(q): k, l1, r1, l2, r2 = map(int, input().split()) c = mp[k][l1 - 2] if l1 == 1: c = 0 one1 = mp[k][r1 - 1] - c zero1 = r1 - l1 - one1 + 1 one2 = mp[k][r2 - 1] - mp[k][l2 - 2] zero2 = r2 - l2 - one2 + 1 print(zero1 * one2 + one1 * zero2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
t = int(input()) for _ in range(t): n, q = map(int, input().split()) arr = list(map(int, input().split())) pf = [[0] for _ in range(60)] for i in range(n): for b in range(60): if arr[i] & 1 << b: pf[b].append(pf[b][-1] + 1) else: pf[b].append(pf[b][-1]) for _ in range(q): res = 0 k, l1, r1, l2, r2 = map(int, input().split()) xctr = r1 - l1 + 1 yctr = r2 - l2 + 1 x = pf[k][r1] - pf[k][l1 - 1] y = pf[k][r2] - pf[k][l2 - 1] print(x * (yctr - y) + y * (xctr - x))
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 LIST NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
power = [pow(2, i) for i in range(60)] t = int(input()) for T in range(t): n, q = map(int, input().split()) a = list(map(int, input().split())) pre = [[(0) for j in range(n)] for i in range(60)] for i in range(n): for j in range(60): if a[i] & power[j] > 0: pre[j][i] = 1 for i in range(60): for j in range(1, n): pre[i][j] += pre[i][j - 1] for i in range(q): k, l1, r1, l2, r2 = map(int, input().split()) count1 = pre[k][r1 - 1] - pre[k][l1 - 1] count2 = pre[k][r2 - 1] - pre[k][l2 - 1] if a[l1 - 1] & power[k] > 0: count1 += 1 if a[l2 - 1] & power[k] > 0: count2 += 1 ans = count1 * (r2 - l2 + 1 - count2) + count2 * (r1 - l1 + 1 - count1) print(ans)
ASSIGN VAR FUNC_CALL VAR NUMBER 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def soln(n, q, arr, queries): n = len(arr) dp = [([0] * (n + 1)) for _ in range(60)] for i in range(60): prev = 0 mask = 1 << i for j in range(n): if mask & arr[j]: prev += 1 dp[i][j + 1] = prev else: dp[i][j + 1] = prev for k, l1, r1, l2, r2 in queries: ones = dp[k][r1] - dp[k][l1 - 1] zeros = r1 - l1 + 1 - ones rones = dp[k][r2] - dp[k][l2 - 1] rzeros = r2 - l2 + 1 - rones res = ones * rzeros + rones * zeros print(res) for _ in range(int(input())): n, q = map(int, input().split()) arr = list(map(int, input().split())) queries = [list(map(int, input().split())) for i in range(q)] soln(n, q, arr, queries)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def solve(): n, q = map(int, input().split()) a = list(map(int, input().split())) p = [] for i in range(60): p.append([0] * (n + 1)) d = 1 for j in range(60): for i in range(n): p[j][i + 1] = p[j][i] + (1 if a[i] & d > 0 else 0) d *= 2 for _q in range(q): k, l1, r1, l2, r2 = map(int, input().split()) p1 = p[k][r1] - p[k][l1 - 1] p2 = p[k][r2] - p[k][l2 - 1] r = p1 * (r2 - l2 + 1 - p2) + p2 * (r1 - l1 + 1 - p1) print(r) t = 1 t = int(input()) for _test_ in range(t): solve()
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 LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for _ in range(int(input())): n, q = map(int, input().split()) a = list(map(int, input().split())) d = {} for i in range(60): t0 = [0] t1 = [0] for j in a: if j & 2**i == 0: t0.append(t0[-1] + 1) t1.append(t1[-1] + 0) else: t0.append(t0[-1] + 0) t1.append(t1[-1] + 1) d[i] = [t0, t1] for t in range(q): k, l1, r1, l2, r2 = map(int, input().split()) t0 = d[k][0] t1 = d[k][1] i0 = t0[r1] - t0[l1 - 1] i1 = t1[r1] - t1[l1 - 1] j0 = t0[r2] - t0[l2 - 1] j1 = t1[r2] - t1[l2 - 1] print(i0 * j1 + i1 * j0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
t = int(input()) while t > 0: n, q = input().split() q = int(q) Arr = [int(x) for x in input().split()] m = max(Arr) msb = 0 for i in range(31, 0, -1): if m >> i & 1 == 1: msb = i break temp = [[(0) for _ in range(60)]] for i in Arr: te = [(0) for _ in range(60)] for sh in range(60): if i >> sh & 1 == 1: te[sh] = temp[-1][sh] + 1 else: te[sh] = temp[-1][sh] temp.append(te) temp = temp[1:] for _ in range(q): i_cnt_0 = 0 i_cnt_1 = 0 j_cnt_0 = 0 j_cnt_1 = 0 l = [int(x) for x in input().split()] if l[2] - 1 >= len(temp): x = temp[-1][l[0]] else: x = temp[l[2] - 1][l[0]] y = 0 if l[1] - 2 >= 0: y = temp[l[1] - 2][l[0]] i_cnt_1 = x - y i_cnt_0 = l[2] - l[1] + 1 - i_cnt_1 if l[4] - 1 >= len(temp): x = temp[-1][l[0]] else: x = temp[l[4] - 1][l[0]] y = 0 if l[3] - 2 >= 0: y = temp[l[3] - 2][l[0]] j_cnt_1 = x - y j_cnt_0 = l[4] - l[3] + 1 - j_cnt_1 ans = 0 ans += i_cnt_0 * j_cnt_1 ans += i_cnt_1 * j_cnt_0 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for _ in range(int(input())): msn, msq = map(int, input().split()) msl = list(map(int, input().split())) bit_count = [([0] * 60) for __ in range(msn + 1)] for i in range(1, msn + 1): num = msl[i - 1] for j in range(60): bit_count[i][j] = bit_count[i - 1][j] + (num >> j & 1) for q in range(msq): k, l1, r1, l2, r2 = map(int, input().split()) ons1 = bit_count[r1][k] - bit_count[l1 - 1][k] zero1 = abs(r1 - l1 + 1 - ons1) ons2 = bit_count[r2][k] - bit_count[l2 - 1][k] zero2 = abs(r2 - l2 + 1 - ons2) print(zero1 * ons2 + ons1 * zero2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def solution(n, q, a, queries): dp = [[(0) for j in range(n + 1)] for i in range(60)] for i in range(n): num = a[i] k = 0 while num >> k: dp[k][i + 1] = num >> k & 1 k += 1 for k in range(60): for i in range(n): dp[k][i + 1] += dp[k][i] for query in queries: L1 = query[1] R1 = query[2] L2 = query[3] R2 = query[4] bit = query[0] leftCountOne = dp[bit][R1] - dp[bit][L1 - 1] leftCountZero = R1 - L1 + 1 - leftCountOne RightCountOne = dp[bit][R2] - dp[bit][L2 - 1] RightCountZero = R2 - L2 + 1 - RightCountOne pairs = leftCountOne * RightCountZero + leftCountZero * RightCountOne print(pairs) return None for _ in range(int(input())): queries = [] n, q = map(int, input().split()) a = list(map(int, input().split()))[:n] for i in range(q): temp = list(map(int, input().split())) queries.append(temp) solution(n, q, a, queries)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def prefix(a, n): pref = [] for i in range(61): pref.append([]) for j in range(n): tst = a[j] & 1 << i if tst: pref[i].append(1) else: pref[i].append(0) for i in range(61): for j in range(1, n): pref[i][j] += pref[i][j - 1] return pref def solve2(qu, pref): l1, r1 = qu[1], qu[2] l2, r2 = qu[3], qu[4] k = qu[0] o1 = pref[k][r1 - 1] if l1 - 2 >= 0: o1 = o1 - pref[k][l1 - 2] z1 = r1 - l1 + 1 - o1 o2 = pref[k][r2 - 1] if l2 - 2 >= 0: o2 = o2 - pref[k][l2 - 2] z2 = r2 - l2 + 1 - o2 return o1 * z2 + o2 * z1 t = int(input()) for _ in range(t): n, q = map(int, input().split()) a = list(map(int, input().split())) pref = prefix(a, n) for i in range(q): qu = list(map(int, input().split())) print(solve2(qu, pref))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
t = int(input()) for i in range(t): n, q = map(int, input().split()) a = list(map(int, input().split())) zeros = [[(0) for di in range(n + 1)] for dj in range(60)] ones = [[(0) for ci in range(n + 1)] for cj in range(60)] for bi in range(1, n + 1): bf = bin(a[bi - 1])[::-1] for bj in range(60): if bj >= len(bf) - 2: zeros[bj][bi] = zeros[bj][bi - 1] + 1 ones[bj][bi] = ones[bj][bi - 1] else: if bf[bj] == "1": ones[bj][bi] = ones[bj][bi - 1] + 1 zeros[bj][bi] = zeros[bj][bi - 1] if bf[bj] == "0": zeros[bj][bi] = zeros[bj][bi - 1] + 1 ones[bj][bi] = ones[bj][bi - 1] for j in range(q): pairs = 0 k, l1, r1, l2, r2 = map(int, input().split()) z1 = zeros[k][r1] - zeros[k][l1 - 1] o1 = ones[k][r1] - ones[k][l1 - 1] z2 = zeros[k][r2] - zeros[k][l2 - 1] o2 = ones[k][r2] - ones[k][l2 - 1] pairs = z1 * o2 + z2 * o1 print(pairs)
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 NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def isKthSet(n: int, k: int): return int(bool(n & 1 << k)) t = int(input()) for _ in range(t): n, q = map(int, input().split()) arr = list(map(int, input().split())) c1 = [] for i in range(60): l = [0] for j in range(n): l.append(l[-1] + isKthSet(arr[j], i)) c1.append(l) for __ in range(q): k, l1, r1, l2, r2 = map(int, input().split()) c1a = c1[k][r1] - c1[k][l1 - 1] c1b = c1[k][r2] - c1[k][l2 - 1] c0a = r1 - l1 + 1 - c1a c0b = r2 - l2 + 1 - c1b print(c1a * c0b + c0a * c1b)
FUNC_DEF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER 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 LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): return input().strip() def invr(): return map(int, input().split()) def outp(n): sys.stdout.write(str(n) + "\n") def outlt(lst): sys.stdout.write(" ".join(map(str, lst)) + "\n") def outplt(lst): sys.stdout.write("\n".join(map(str, lst))) def outpltlt(lst): sys.stdout.write("\n".join(map(str, (" ".join(map(str, a)) for a in lst)))) def prepare(N, A, ma): mat = [([0] * (ma + 1)) for x in range(N)] M = [(2**x) for x in range(ma + 1)] for i, a in enumerate(A): for j, m in enumerate(M): mat[i][j] = mat[i - 1][j] + bool(a & m) return mat def solve(N, mat, k, l1, r1, l2, r2): o1, o2 = mat[r1 - 1][k] - (mat[l1 - 2][k] if l1 > 1 else 0), mat[r2 - 1][k] - ( mat[l2 - 2][k] if l2 > 1 else 0 ) z1, z2 = r1 - l1 + 1 - o1, r2 - l2 + 1 - o2 return z1 * o2 + z2 * o1 ans = [] for _ in range(inp()): N, Q = inlt() A = inlt() ma = len(bin(max(A))[2:]) mat = prepare(N, A, ma) for q in range(Q): k, l1, r1, l2, r2 = inlt() if k > ma: an = 0 else: an = solve(N, mat, k, l1, r1, l2, r2) ans.append(an) outplt(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def binary(n): ans = "" count = 0 while n > 0: ans += str(n % 2) count += 1 n = n // 2 return (64 - count) * "0" + ans[::-1] for _ in range(int(input())): n, q = map(int, input().split()) lst = list(map(int, input().split())) binary_lst = [0] * n grand_count_zero_lst = [] grand_count_one_lst = [] for i in range(n): binary_lst[i] = binary(lst[i]) for j in range(64): count_zero = 0 count_one = 0 count_zero_lst = [] count_one_lst = [] for i in range(n): if binary_lst[i][j] == "0": count_zero += 1 else: count_one += 1 count_zero_lst.append(count_zero) count_one_lst.append(count_one) grand_count_zero_lst.append([0] + count_zero_lst) grand_count_one_lst.append([0] + count_one_lst) for i in range(q): k, l1, r1, l2, r2 = map(int, input().split()) var_1 = ( grand_count_zero_lst[64 - k - 1][r1] - grand_count_zero_lst[64 - k - 1][l1 - 1] ) * ( grand_count_one_lst[64 - k - 1][r2] - grand_count_one_lst[64 - k - 1][l2 - 1] ) var_2 = ( grand_count_one_lst[64 - k - 1][r1] - grand_count_one_lst[64 - k - 1][l1 - 1] ) * ( grand_count_zero_lst[64 - k - 1][r2] - grand_count_zero_lst[64 - k - 1][l2 - 1] ) print(var_1 + var_2)
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
import sys input = sys.stdin.readline def main(): for testcases in range(int(input())): n, q = map(int, input().split()) array = list(map(int, input().split())) prefk = [[] for _ in range(60)] for k in range(60): for i in range(n): bitset = array[i] >> k & 1 if i == 0: prefk[k].append(bitset) else: prefk[k].append(prefk[k][-1] + bitset) for _ in range(q): k, l1, r1, l2, r2 = map(int, input().split()) S1 = ( prefk[k][r1 - 1] - prefk[k][l1 - 2] if l1 - 1 != 0 else prefk[k][r1 - 1] ) U1 = r1 - l1 + 1 - S1 S2 = prefk[k][r2 - 1] - prefk[k][l2 - 2] U2 = r2 - l2 + 1 - S2 print(S1 * U2 + S2 * U1) main()
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for i in range(int(input())): n, q = input().split() n = int(n) q = int(q) arr = [int(x) for x in input().split()] matrix = [[(0) for i in range(60)] for j in range(len(arr))] for i in range(len(arr)): j = 0 for j in range(60): if arr[i] & 1 == 1: matrix[i][j] += 1 if i != 0: matrix[i][j] += matrix[i - 1][j] else: matrix[i][j] += matrix[i - 1][j] arr[i] = arr[i] >> 1 for i in range(q): k, l, r, L, R = input().split() k, l, r, L, R = int(k), int(l) - 1, int(r) - 1, int(L) - 1, int(R) - 1 ans1 = 0 if l != 0: ans1 = matrix[r][k] - matrix[l - 1][k] else: ans1 = matrix[r][k] ans2 = r - l + 1 - ans1 ans3 = matrix[R][k] - matrix[L - 1][k] ans4 = R - L + 1 - ans3 print(ans1 * ans4 + ans2 * ans3)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def ans(l): n = len(l) z = [[(0) for i in range(61)] for j in range(len(l) + 1)] for i in range(len(l)): for j in range(61): z[i + 1][j] = z[i][j] + (l[i] >> j) % 2 return z t = int(input()) for _ in range(t): a, b = map(int, input().split()) l = list(map(int, input().split())) f = ans(l) for i in range(b): q = list(map(int, input().split())) k = q[0] m = f[q[2]][k] - f[q[1] - 1][k] n = f[q[4]][k] - f[q[3] - 1][k] we = (q[2] - q[1] + 1 - m) * n + m * (q[4] - q[3] + 1 - n) print(we)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def preprocess(n): prep = [([0] * 60) for _ in range(len(n) + 1)] for i in range(len(n)): for j in range(60): prep[i + 1][j] = prep[i][j] if n[i] >> j & 1 == 1: prep[i + 1][j] += 1 return prep def solve(prep, q): toti = q[2] - q[1] + 1 totj = q[4] - q[3] + 1 counti = prep[q[2]][q[0]] - prep[q[1] - 1][q[0]] countj = prep[q[4]][q[0]] - prep[q[3] - 1][q[0]] return counti * totj - 2 * countj * counti + countj * toti def chset(n, k): if n & 1 << k: return 1 return 0 for _ in range(int(input())): n, q = map(int, input().split()) n = list(map(int, input().split())) prep = preprocess(n) for __ in range(q): print(solve(prep, list(map(int, input().split()))))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF IF BIN_OP VAR BIN_OP NUMBER VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
def checkbit(n, i): if n >> i & 1 == 1: return True return False t = int(input()) for _ in range(t): n, q = map(int, input().split()) l = [] arr = list(map(int, input().split())) for i in range(60): l1 = [] l1.append(0) s = 0 for j in range(n): if checkbit(arr[j], i) == 1: s = s + 1 l1.append(s) l.append(l1) for __ in range(q): k, l1, r1, l2, r2 = map(int, input().split()) x1 = l[k][r1] - l[k][l1 - 1] x0 = r1 - l1 + 1 - x1 y1 = l[k][r2] - l[k][l2 - 1] y0 = r2 - l2 + 1 - y1 print(x1 * y0 + x0 * y1)
FUNC_DEF IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN 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 LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
for _ in range(int(input())): n, q = map(int, input().split()) l = [int(x) for x in input().split()] ans = [] for i in range(60): x = [] s = 0 for j in range(n): if 1 << i & l[j]: s += 1 x.append(s) ans.append(x) for i in range(q): k, l1, r1, l2, r2 = map(int, input().split()) if l1 != 1: c1 = ans[k][r1 - 1] - ans[k][l1 - 2] else: c1 = ans[k][r1 - 1] c2 = r1 - l1 + 1 - c1 if l2 != 1: d1 = ans[k][r2 - 1] - ans[k][l2 - 2] else: d1 = ans[k][r2 - 1] d2 = r2 - l2 + 1 - d1 print(c1 * d2 + c2 * d1)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
*A Binod is a person who is very good with bitwise operations. Help Alice solve the following problem and become a Binod.* You are given an array A of N elements. Process Q queries on this array, of the following form: Each query contains 5 integers k, L_{1}, R_{1}, L_{2}, R_{2}. It is guaranteed that L_{1} ≀ R_{1} < L_{2} ≀ R_{2}. The answer to a query is the number of pairs (i, j) such that: - L_{1} ≀ i ≀ R_{1} and L_{2} ≀ j ≀ R_{2} - A_{i} \oplus A_{j} has its k-th bit set. Here \oplus denotes the [bitwise XOR] operation. Note: An integer x is said to have its k-th bit set if the (unique) binary representation of x contains 2^{k}. For example, 5 = 101_{2} = 2^{0} + 2^{2} has its zeroth and second bits set but not the first, while 16 = 10000_{2} = 2^{4} has only its fourth bit set. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains two space-separated integers N and Q β€” the number of elements in array and number of queries, respectively. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. - The next Q lines describe queries. The i-th of these Q lines contains 5 space-separated integers k, L_{1}, R_{1}, L_{2}, R_{2} β€” the parameters described in the statement. ------ Output Format ------ For each test case, output Q lines.The i-th of these lines should be the answer to the i-th query. ------ Constraints ------ $1 ≀ T ≀ 5 \cdot 10^{4}$ $2 ≀ N ≀ 10^{5}$ $1 ≀ Q ≀ 5\cdot 10^{5}$ $0 ≀ A_{i} < 2^{60}$ $1 ≀ L_{1} ≀ R_{1} < L_{2} ≀ R_{2} ≀ N$. $0 ≀ k < 60$ - The sum of $N$ and $Q$ over all test cases won't exceed $10^{5}$ and $5\cdot 10^{5}$ respectively. ----- Sample Input 1 ------ 2 5 2 1 2 4 3 2 1 1 3 5 5 2 1 2 3 5 6 2 3 5 6 13 12 20 1 1 4 5 6 3 2 3 4 6 ----- Sample Output 1 ------ 2 2 4 4 ----- explanation 1 ------ Test case $1$: The array is $A = [1, 2, 4, 3, 2]$. - Query $1$: the ranges are $[1, 3]$ and $[5, 5]$, and $k = 1$. There are three pairs of $(i, j)$: $(1, 5), (2, 5), (3, 5)$. - $A_{1} \oplus A_{5} = 1 \oplus 2 = 3 = 11_{2}$ has its first bit set - $A_{2} \oplus A_{5} = 2 \oplus 2 = 0 = 0_{2}$ doesn't have its first bit set - $A_{3} \oplus A_{5} = 4 \oplus 2 = 6 = 110_{2}$ has its first bit set - So, the answer is $2$. - Query $2$: the ranges are $[1, 2]$ and $[3, 5]$, and now $k = 2$. This time, there are $6$ pairs of indices. Of them, it can be verified that $(1, 3)$ and $(2, 3)$ are the ones that satisfy the given condition.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n, q = map(int, input().split()) A = list(map(int, input().split())) DP = [[(0) for i in range(n)] for j in range(61)] for i in range(n): val = A[i] for j in range(61): if val & 1 << j: DP[j][i] = 1 if i != 0: DP[j][i] += DP[j][i - 1] for i in range(q): k, l1, r1, l2, r2 = map(int, input().split()) l1 -= 1 l2 -= 1 r1 -= 1 r2 -= 1 cnt1 = DP[k][r1] - DP[k][l1 - 1] cnt2 = DP[k][r2] - DP[k][l2 - 1] if l1 == 0: cnt1 = DP[k][r1] if l2 == 0: cnt2 = DP[k][r2] ans = cnt1 * (r2 - l2 + 1 - cnt2) + cnt2 * (r1 - l1 + 1 - cnt1) 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 NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
t = int(input()) for _ in range(t): n = int(input()) d = [[(0) for i in range(33)] for j in range(n + 1)] l = list(map(int, input().strip().split(" "))) for i in range(n): t = l[i] c = 0 while t != 0: t = t // 2 c = c + 1 for j in range(33): d[i + 1][j] = d[i][j] d[i + 1][c] = d[i + 1][c] + 1 k = int(input()) for i in range(k): a, b, h = map(int, input().split(" ")) a = a - 1 c = 0 while h != 0: h = h // 2 c = c + 1 ans = n - a print(b - a - d[b][c] + d[a][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 NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) q = int(input()) o = {x: [(1) for _ in range(n)] for x in range(1, 33)} for i, item in enumerate(a): e = bin(item)[2:] index = None for j, digit in enumerate(e[::-1]): if digit == "1": index = j + 1 if index: o[index][i] = 0 u = {} zero_count = [(0) for _ in range(n)] for i, item in enumerate(a): if item == 0: zero_count[i] = 1 zero_count_u = [] for i, item in enumerate(zero_count): j = item if i: j += zero_count_u[-1] zero_count_u.append(j) for key, value in o.items(): g = [] for i, item in enumerate(value): h = item if i: h += g[-1] g.append(h) u[key] = g for _ in range(q): l, r, x = map(int, input().split()) e = bin(x)[2:] b = len(e) l -= 1 r -= 1 y = u[b][r] if l: y -= u[b][l - 1] if x == 0: ans = r - l + 1 d = zero_count_u[r] if l: d -= zero_count_u[l - 1] print(ans - d) continue print(y)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
import sys sys.setrecursionlimit(10**5) def hsb(x): for i in range(31, -1, -1): if x & 1 << i: return i return 32 def pro(arr, q): n = len(arr) lst = [[0] * 33] for i in range(n): if i: lst.append(lst[i - 1].copy()) lst[i][hsb(arr[i])] += 1 for l, r, x in q: l -= 1 r -= 1 R = lst[r][hsb(x)] if l > 0: L = lst[l - 1][hsb(x)] else: L = 0 print(r - l + 1 - (R - L)) t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) q = [] qu = int(input()) for i in range(qu): q.append(map(int, input().split())) pro(arr, q)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) sett = [[(0) for i in range(32)] for j in range(n + 1)] for i in range(n): ele = arr[i] msb = 0 while ele > 0: msb += 1 ele = ele // 2 for k in range(32): sett[i + 1][k] = sett[i][k] sett[i + 1][msb] += 1 q = int(input()) for i in range(q): count = 0 vv = list(map(int, input().split())) l = vv[0] r = vv[1] x = vv[2] ms = 0 while x > 0: ms += 1 x = x // 2 for m in range(32): if m != ms: count += sett[r][m] - sett[l - 1][m] print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
def calculateMSB(number): if number == 0: return 0 block = 0 while number >> block: block += 1 return block def solution(): N = int(input()) array = list(map(int, input().split())) p = [[(0) for i in range(32)] for i in range(N + 1)] for i in range(N): b = calculateMSB(array[i]) for j in range(32): p[i + 1][j] = p[i][j] + (b == j) Q = int(input()) while Q: L, R, X = map(int, input().split()) b = calculateMSB(X) ans = R - L + 1 - (p[R][b] - p[L - 1][b]) print(ans) Q -= 1 T = int(input()) while T: solution() T -= 1
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) res = [] for k in range(32): L = [] c = 0 for l in range(n): if k == 0: if arr[l] == 0: c += 1 L.append(c) else: a = bin(arr[l])[2:] if len(a) == k and a != "0": c += 1 L.append(c) res.append(L) q = int(input()) for j in range(q): l, r, x = list(map(int, input().split())) if x == 0: b = 0 else: b = len(bin(x)[2:]) if l - 1 == 0: tot = res[b][r - 1] else: tot = res[b][r - 1] - res[b][l - 2] print(r - l + 1 - tot)
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 LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
def top_set_bit(x): if x == 0: return int(0) return 1 + int(top_set_bit(x >> 1)) T = int(input()) for i in range(T): N = int(input()) values = [int(i) for i in input().split()] for i in range(N): values[i] = top_set_bit(values[i]) dp = [] for j in range(32): dp.append([0]) for i in range(N): dp[j].append(dp[j][i]) if j != values[i]: dp[j][i + 1] += 1 Q = int(input()) for j in range(Q): L, R, X = map(int, input().split()) X = top_set_bit(X) print(dp[X][R] - dp[X][L - 1])
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
LIM = 2 * 10**5 + 10 def HBIT(n): bit = -1 while n: bit += 1 n >>= 1 return bit hbitCount = [([0] * LIM) for i in range(32)] totalPos = [0] * LIM def prep(A, N): for ind in range(N + 1): totalPos[ind] = 0 for bit in range(32): hbitCount[bit][ind] = 0 for ind, el in enumerate(A): totalPos[ind + 1] = totalPos[ind] if el: totalPos[ind + 1] += 1 hbitCount[HBIT(el)][ind + 1] = 1 for bit in range(32): for ind in range(1, N + 1): hbitCount[bit][ind] += hbitCount[bit][ind - 1] def solve(L, R, X): if X == 0: return totalPos[R] - totalPos[L - 1] size = R - L + 1 hbit = HBIT(X) sameHighBits = hbitCount[hbit][R] - hbitCount[hbit][L - 1] return size - sameHighBits for case in range(int(input())): N = int(input()) A = list(map(int, input().split())) prep(A, N) T = int(input()) for i in range(T): L, R, X = map(int, input().split()) print(solve(L, R, X))
ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR 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 VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] q = int(input()) pre = [] for i in a: if i == 0: pre.append(0) else: pre.append(len(bin(i)) - 2) mat = [] k = [0] * 32 mat.append([0] * 32) for i in pre: s = [0] * 32 s = k.copy() s[i] += 1 k[i] += 1 mat.append(s) for i in range(q): l, r, x = [int(x) for x in input().split()] k = 0 if x != 0: k = len(bin(x)) - 2 c = mat[r][k] - mat[l - 1][k] print(r - l + 1 - 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
T = int(input()) for t in range(T): N = int(input()) A = [int(a) for a in input().split()] B = [] for a in A: if a == 0: B.append(0) elif a == 1: B.append(1) else: i = 0 b = a while b > 0: b = b >> 1 i += 1 B.append(i) C = [[(0) for j in range(N)] for i in range(32)] b = B[0] C[b][0] = 1 for i in range(1, N): b = B[i] C[b][i] = C[b][i - 1] + 1 for j in range(32): if j != b: C[j][i] = C[j][i - 1] Q = int(input()) for i in range(Q): L, R, X = map(int, input().split()) x = 0 if X == 0: x = 0 elif X == 1: x = 1 else: i = 0 c = X while c > 0: c = c >> 1 i += 1 x = i if L > 1: print(R - L + 1 - (C[x][R - 1] - C[x][L - 2])) else: print(R - L + 1 - C[x][R - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
t = int(input()) for i in range(0, t): n = int(input()) myList = list(map(int, input().strip().split()))[:n] myProcessedData = [] for i in range(0, 33): temp = [] for j in range(0, n + 1): temp.append(0) myProcessedData.append(temp) for j in range(1, n + 1): if j == 1: if myList[j - 1] == 0: myProcessedData[32][j] += 1 else: power = len(bin(myList[j - 1])[2:]) - 1 myProcessedData[power][j] += 1 else: for k in range(0, 33): myProcessedData[k][j] = myProcessedData[k][j - 1] if myList[j - 1] == 0: myProcessedData[32][j] += 1 else: power = len(bin(myList[j - 1])[2:]) - 1 myProcessedData[power][j] += 1 q = int(input()) for i in range(0, q): l, r, x = map(int, input().split()) if x == 0: check = 32 print( r - l + 1 - (myProcessedData[check][r] - myProcessedData[check][l - 1]) ) else: check = len(bin(x)[2:]) - 1 res = ( r - l + 1 - (myProcessedData[check][r] - myProcessedData[check][l - 1]) ) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
def msb(n): ans = 1 count = 0 while n: n = n >> 1 count += 1 return count t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) dp = [[(0) for j in range(32)] for i in range(n + 1)] for i in range(n): res = msb(arr[i]) for j in range(32): if res == j: dp[i + 1][j] = dp[i][j] + 1 else: dp[i + 1][j] = dp[i][j] q = int(input()) for j in range(q): l, r, x = [int(i) for i in input().split()] ans = r - l + 1 - (dp[r][msb(x)] - dp[l - 1][msb(x)]) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
def readInt(): return int(input()) def readInts(): return [int(x) for x in input().split()] def readString(): return input().rstrip() def readStrings(): return input().split() def readCase(): n = readInt() numbers = readInts() q = readInt() queries = [readInts() for _ in range(q)] return n, numbers, q, queries def solve(n, numbers, q, queries): prefix = [[(0) for _ in range(32)] for _ in range(n + 1)] for pos, num in enumerate(numbers): num_len = num.bit_length() for bit in range(32): if bit == num_len: prefix[pos + 1][bit] = prefix[pos][bit] + 1 else: prefix[pos + 1][bit] = prefix[pos][bit] for l, r, x in queries: x_bit = x.bit_length() failures = prefix[r][x_bit] - prefix[l - 1][x_bit] print(r - l + 1 - failures) cases = readInt() for case in range(cases): solve(*readCase())
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
def log(x, y): coun = 0 while x >= y: coun += 1 x //= y return coun for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) q = int(input()) lst = [([0] * (n + 1)) for i in range(32)] for i in range(1, n + 1): for j in range(32): lst[j][i] = lst[j][i - 1] exp = 31 if arr[i - 1] != 0: exp = log(arr[i - 1], 2) lst[exp][i] += 1 for i in range(q): l, r, x = list(map(int, input().split())) exp = 31 if x != 0: exp = log(x, 2) coun = lst[exp][r] - lst[exp][l - 1] print(r - l + 1 - coun)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR
A number M is said to be a *Mystical Number* with respect to a number X if (M \oplus X) > (M \& X). You are given an array A of size N. You are also given Q queries. Each query consists of three integers L, R, and X. For each query, find the count of *Mystical Numbers* in the subarray A[L:R] with respect to the number X. Notes: \oplus represents the [Bitwise XOR] operation and \& represents the [Bitwise AND] operation. A[L:R] denotes the subarray A[L], A[L+1], \ldots, A[R]. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. - The third line of each test case containsΒ an integerΒ Q -Β denoting the number of queries. - The i^{th} of the next Q lines contains three space-separated integers L , R and X. ------ Output Format ------ For each testcase, - For each query, print in a new line, the count of *Mystical Numbers* among A[L], A[L+1], \ldots, A[R] with respect to the number X. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ N ≀ 2 \cdot 10^{5}$ $0 ≀ A_{i} < 2^{31}$ $1 ≀ Q ≀ 2 \cdot 10^{5}$ $1 ≀ L ≀ R ≀ N$ $0 ≀ X < 2^{31}$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. - Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 1 5 1 2 3 4 5 2 1 5 4 2 5 2 ----- Sample Output 1 ------ 3 2 ----- explanation 1 ------ Test case $1$: - Query $1$: $L = 1 , R = 5 , X = 4$. - $A_{1} \oplus X = 5, A_{1} \& X = 0$. - $A_{2} \oplus X = 6, A_{2} \& X = 0$. - $A_{3} \oplus X = 7, A_{3} \& X = 0$. - $A_{4} \oplus X = 0, A_{4} \& X = 4$. - $A_{5} \oplus X = 1, A_{5} \& X = 4$. Mystical numbers are $A_{1} , A_{2},$ and $A_{3}$ with respect to $4$. Therefore, the answer to this query is $3$. - Query $2$: $L = 2 , R = 5 , X = 2$. - $A_{2} \oplus X = 0, A_{2} \& X = 2$. - $A_{3} \oplus X = 1, A_{3} \& X = 2$. - $A_{4} \oplus X = 6, A_{4} \& X = 0$. - $A_{5} \oplus X = 7, A_{5} \& X = 0$. Mystical numbers are $A_{4}$ and $A_{5}$ with respect to $2$. Therefore , the answer to this query is $2$.
def msb(n): ans = 0 while n > 0: n >>= 1 ans += 1 return ans for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr1 = [([0] * 32) for _ in range(n)] arr1[0][msb(arr[0])] += 1 for i in range(1, n): temp = msb(arr[i]) for j in range(32): arr1[i][j] = arr1[i - 1][j] arr1[i][temp] += 1 q = int(input()) for _ in range(q): l, r, x = map(int, input().split()) temp = msb(x) ans = arr1[r - 1][temp] - arr1[l - 1][temp] if msb(arr[l - 1]) == temp: ans += 1 ans = r - l + 1 - ans print(ans)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
c, d = map(int, input().split()) a = min(c, d) b = max(c, d) w = b - a c = b * a e = 0 for i in range(1, int(w**0.5) + 1): if w % i == 0: if b % i == 0: q1 = b // i q2 = a // i else: q1 = b // i + 1 q2 = a // i + 1 if i * q1 * q2 < c: c = i * q1 * q2 e = i * q1 - b elif i * q1 * q2 == c and i * q1 - b < e: e = i * q1 j = w // i if b % j == 0: q1 = b // j q2 = a // j else: q1 = b // j + 1 q2 = a // j + 1 if j * q1 * q2 < c: c = j * q1 * q2 e = j * q1 - b elif j * q1 * q2 == c and j * q1 - b < e: e = j * q1 print(e)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = map(int, input().split()) if a < b: a, b = b, a if a == b: print(0) exit() def update(g): global ans if a % g != b % g: return n = g - a % g if a % g else 0 lcm = (a + n) * (b + n) // g ans = min(ans, (lcm, n)) d = a - b ans = 10**31, 10**31 for gcd in range(1, int(d**0.5) + 2): if d % gcd: continue update(gcd) update(d // gcd) print(ans[1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR VAR BIN_OP VAR VAR RETURN ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = map(int, input().split()) if a < b: a, b = b, a d = [] ab = a - b for i in range(1, int(ab**0.5) + 1): if ab % i == 0: d.append(i) if i != ab // i: d.append(ab // i) mink = 0 minlcm = float("inf") for i in d: if b % i == 0: k = 0 else: k = (b // i + 1) * i - b lcm = (a + k) * (b + k) // i if lcm < minlcm: minlcm = lcm mink = k print(mink)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def gcd(a, b): return a if b == 0 else gcd(b, a % b) def lcm(a, b): return a / gcd(a, b) * b def main(): a, b = [int(x) for x in input().split()] d = 1 if a == b: print("%d" % 0) return if a > b: a, b = b, a ans = lcm(a, b) kans = 0 while d * d <= b - a: if (b - a) % d == 0: q1 = (a + d - 1) // d k = q1 * d - a if lcm(a + k, b + k) < ans: ans = lcm(a + k, b + k) kans = k dp = d d = (b - a) // d q1 = (a + d - 1) // d k = q1 * d - a if lcm(a + k, b + k) < ans: ans = lcm(a + k, b + k) kans = k d = dp d += 1 print("%d" % kans) main()
FUNC_DEF RETURN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING NUMBER RETURN IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def mp(): return map(int, input().split()) def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return a * b // gcd(a, b) a, b = mp() a, b = min(a, b), max(a, b) x = [] w = b - a dl = 1 while dl**2 <= w: if w % dl == 0: x.append(dl) x.append(w // dl) dl += 1 kk = 0 m = 10**20 for d in x: r = (a + d - 1) // d k = r * d - a if lcm(a + k, b + k) < m: m = lcm(a + k, b + k) kk = k print(kk)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def gcd(a, b): if b == 0: return a return gcd(b, a % b) a1, b1 = map(int, input().split()) a = min(a1, b1) b = max(a1, b1) diff = b - a div = [] if a == b: print(0) exit() for i in range(1, int(diff**0.5) + 1): if diff % i == 0: zz = diff // i div.append(i) if i != zz: div.append(zz) ans = 100000000000000000 cc = -1 for i in div: k = (i - a % i) % i xx = (a + k) * (b + k) yy = gcd(a + k, b + k) temp = xx // yy if ans > temp: ans = temp cc = k print(cc)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def gcd(p, q): while p: p, q = q % p, p return q I = lambda: map(int, input().split()) a, b = I() a, b = min(a, b), max(a, b) if b % a == 0: exit(print("0")) k1 = 0 t = b - a ans = 100 minn = 10**31 k = 100 for i in range(1, int(t**0.5) + 1): if t % i == 0: g = i k = (g - a % g) % g p = (a + k) * (b + k) // gcd(a + k, b + k) if p < minn: minn = p ans = k elif p == minn: ans = min(ans, k) g = t // i k = (g - a % g) % g p = (a + k) * (b + k) // gcd(a + k, b + k) if p < minn: minn = p ans = k elif p == minn: ans = min(ans, k) print(ans)
FUNC_DEF WHILE VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return a * b / gcd(a, b) arr = input() A, B = [int(x) for x in arr.split(" ")] d = abs(A - B) divisiors = [] for i in range(1, 100000): if i * i > d: break elif d % i == 0: divisiors.append(i) divisiors.append(d // i) mini = -1 res = 0 for div in divisiors: new_A = A // div if A % div != 0: new_A += 1 new_A = new_A * div new_B = B // div if B % div != 0: new_B += 1 new_B = new_B * div L = lcm(new_A, new_B) if mini == -1: mini = L res = new_A - A elif L <= mini: if L == mini: res = min(res, new_A - A) else: res = new_A - A mini = L print(res)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = sorted(list(map(int, input().split()))) if a == b: print(0) elif a > b - a: if a % (b - a) == 0: print(0) else: print(b - a - a % (b - a)) elif (b - a) % a == 0: print(0) else: ans = float("inf") for q in range(1, int((b - a) ** (1 / 2)) + 2): if (b - a) % q == 0: if (b - a) // q >= a: ans = min(ans, (b - a) // q) if q >= a: ans = min(ans, q) print(ans - a)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def solve(a, b): if a > b: a, b = b, a d = b - a if d <= 1: return 0 if d > a: if d % a == 0: return 0 i = a q = [] while i * i <= d: if d % i == 0: return i - a i += 1 if a * a > d: i = d // a while i >= 0: if d % i == 0: return d // i - a i -= 1 elif d < a: if a % d == 0: return 0 else: return d - a % d return 0 a, b = map(int, input().split()) print(solve(a, b))
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = map(int, input().split()) a, b = min(a, b), max(a, b) if a == b: print(0) else: M, u = 2 * a * b, 0 D = 1 while D * D <= b - a: if (b - a) % D == 0: for d in (D, (b - a) // D): k = 0 if a % d != 0: k = d - a % d Q = (a + k) * (b + k) / d if Q < M: M = Q u = k D += 1 print(u)
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 IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = list(map(int, input().split())) a, b = min(a, b), max(a, b) if b < 2 * a: if a == b: print(0) exit() print(-a % (b - a)) exit() s = [1] q = b - a for i in range(2, int((b - a) ** (1 / 2) + 2)): while q % i == 0: t = [(j * i) for j in s] for aa in t: s.append(aa) s = list(set(s)) q = q // i if q != 1: t = [(j * q) for j in s] for aa in t: s.append(aa) s = list(set(s)) s.sort() for i in s: if i >= a: print(i - a) exit()
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
A, B = map(int, input().split()) def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) def lcm(a, b): return a * b // gcd(a, b) def primeFactor(N): i = 2 ret = {} n = N if n < 0: ret[-1] = 1 n = -n if n == 0: ret[0] = 1 d = 2 sq = int(n ** (1 / 2)) while i <= sq: k = 0 while n % i == 0: n //= i k += 1 ret[i] = k if k > 0: sq = int(n ** (1 / 2)) if i == 2: i = 3 elif i == 3: i = 5 elif d == 2: i += 2 d = 4 else: i += 4 d = 2 if n > 1: ret[n] = 1 return ret def divisors(N): pf = primeFactor(N) ret = [1] for p in pf: ret_prev = ret ret = [] for i in range(pf[p] + 1): for r in ret_prev: ret.append(r * p**i) return sorted(ret) if A == B: print(0) else: mi = 10**100 ans = -1 D = divisors(abs(B - A)) for d in D: k = -A % d L = lcm(A + k, B + k) if mi > L or mi == L and ans > k: mi = L ans = k print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def euc(x, y): x, y = max(x, y), min(x, y) if x % y == 0: return y while x % y != 0: x, y = y, x % y return y def lcm(x, y): return x * y // euc(x, y) a, b = map(int, input().split()) a, b = max(a, b), min(a, b) if a == b: ans = 0 else: s = a - b c, d = a + (1 + b // s) * s - b, (1 + b // s) * s ans = (1 + b // s) * s - b mm = lcm(a, b) ans = 0 for i in range(1, int(s**0.5) + 3): if s % i == 0: k = (1 + b // i) * i - b xx = lcm(a + k, b + k) if xx < mm: ans = k mm = xx k = (1 + b // (s // i)) * (s // i) - b xx = lcm(a + k, b + k) if xx < mm: ans = k mm = xx print(ans)
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR 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 IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def hcf(a, b): while a: temp = a a = b % a b = temp return b def lcm(a, b): return a * b // hcf(a, b) def check(a, b, ko, kn, min_lcm): val = lcm(a + kn, b + kn) if min_lcm == -2: min_lcm = val elif val < min_lcm: min_lcm = val ko = min(ko, kn) elif val == min_lcm: ko = min(ko, kn) return [ko, min_lcm] def doit(a, b, x): i = 1 min_lcm = -2 k = pow(10, 9) + 1 while i * i <= x: if x % i == 0: f1 = i f2 = x // i [k, min_lcm] = check(a, b, k, f1 - a % f1, min_lcm) [k, min_lcm] = check(a, b, k, f2 - a % f2, min_lcm) if f1 % a == 0: [k, min_lcm] = check(a, b, k, 0, min_lcm) if f2 % a == 0: [k, min_lcm] = check(a, b, k, 0, min_lcm) if k == 0: break i += 1 [k, min_lcm] = check(a, b, k, 0, min_lcm) return k [a, b] = [int(i) for i in input().split()] if a == b: print(0) else: print(doit(a, b, max(a, b) - min(a, b)))
FUNC_DEF WHILE VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN LIST VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR RETURN VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = input().split() a, b = int(a), int(b) a, b = min(a, b), max(a, b) def eu(a, b): if a == 0: return b if b == 0: return a if a > b: return eu(a % b, b) return eu(a, b % a) opt = b - a factor = [] i = 1 while i**2 < opt + 1: if opt % i == 0: factor.append(i) factor.append(int(opt / i)) i += 1 target = a * b / eu(a, b) drop = 0 for i in factor: firstupd = a - a % i + i secondupd = b - b % i + i dres = firstupd * int(secondupd / eu(firstupd, secondupd)) if dres <= target: if dres == target: drop = min(i - a % i, drop) else: target = dres drop = i - a % i print(drop)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
n = list(map(int, list(input().split()))) v = [n[0], n[1]] x = max(v) y = min(v) def gcd(x, y): while y != 0: x, y = y, x % y return x def lcm2(x, y): return x * y / gcd(x, y) def getFactors(n): factors1 = [] factors = [] for i in range(1, n + 1): if n % i == 0: factors1.append(i) factors.append(i) if i > n ** (1 / 2): for foo in factors1[::-1]: factors.append(int(n / foo)) return factors return factors if x - y == 0: print(0) else: l = getFactors(x - y) li = sorted(set([(-x % foo) for foo in l]), reverse=True) n = [] for foo in li: n.append((lcm2(x + foo, y + foo), foo)) print(min(n)[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP NUMBER NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR RETURN VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = map(int, input().split()) a, b = min(a, b), max(a, b) def divisors(n): div = [] for i in range(1, int(n ** (1 / 2)) + 1): if n % i == 0: div.append(i) if i * i != n: div.append(n // i) return sorted(div) div = divisors(b - a) ans = res = 1e50 for i in div: k = i - a % i if a % i else 0 t = (a + k) * (b + k) // i if ans > t: ans = t res = k print(res if b - a else 0)
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 FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = list(map(int, input().split())) def gcd(a, b): if b == 0: return a return gcd(b, a % b) def make_divisors(n): divisors = [] for i in range(1, int(n**0.5) + 1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n // i) return divisors a, b = (b, a) if a > b else (a, b) c = b - a if c == 0: print(0) return pc = make_divisors(c) ctr = a * b // gcd(a, b) ans = 0 for p in pc: ak = -(-a // p) * p k = ak - a bk = -(-b // p) * p lc = ak * bk // gcd(ak, bk) if ctr > lc: ctr = lc ans = k print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
are, bre = list(map(int, input().split())) def plottt(xre, yre): if xre == 0: return yre if yre == 0: return xre if yre > xre: yre, xre = xre, yre return plottt(xre % yre, yre) if are > bre: are, bre = bre, are kre = bre - are plkiyer = [] ire = 1 while ire**2 <= kre: if kre % ire == 0: plkiyer.append(ire) plkiyer.append(kre // ire) ire += 1 plotttd = are * bre / plottt(are, bre) result = 0 for dre in plkiyer: aare = are - are % dre + dre bbre = bre - bre % dre + dre if aare * bbre // plottt(aare, bbre) <= plotttd: if aare * bbre // plottt(aare, bbre) == plotttd: result = min(dre - are % dre, result) else: plotttd = aare * bbre // plottt(aare, bbre) result = dre - are % dre print(result)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = map(int, input().split()) q = abs(a - b) if q == 0: print(0) else: dq = [] dq2 = [] for i in range(1, int(q**0.5) + 1): if not q % i: dq.append(i) if i != q // i: dq2 = [q // i] + dq2 dq = dq + dq2 dq = dq[::-1] if a < b: a, b = b, a def gcd(a, b): for ch in dq: if a % ch == 0 and b % ch == 0: return ch def agcd(a, b): return a * b // gcd(a, b) ks = [0] rezults = [[agcd(a, b), 0]] for ch in dq: k = ch - a % ch if k not in ks: ks.append(k) rezults.append([agcd(a + k, b + k), k]) rezults.sort() print(rezults[0][1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF FOR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST LIST FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = list(map(int, input().split())) a, b = min(a, b), max(a, b) gap = b - a arr = [] i = 1 while i * i < gap: if gap % i == 0: arr.append(i) arr.append(gap // i) i += 1 if i * i == gap: arr.append(i) arr.sort() answer = 0 value = 10**20 for i in arr: plus = 0 if a % i: plus = i - a % i ta, tb = a + plus, b + plus mod = ta % tb while mod: ta = tb tb = mod mod = ta % tb temp = (a + plus) * (b + plus) // tb if temp < value: value = temp answer = plus if temp == value: answer = min(answer, plus) print(answer)
ASSIGN VAR VAR FUNC_CALL 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 BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = list(map(int, input().split())) a, b = min(a, b), max(a, b) if b % a == 0: print(0) return x = b - a dels = set() for i in range(1, int(x**0.5) + 1): if x % i == 0: dels.add(i) dels.add(x // i) dels = list(dels) j = 10**20 for i in dels: if i >= a: j = min(j, i - a) print(min((x % a - a) % x, j))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = [int(x) for x in input().split()] lcm = K = 10**100 def gcd(a, b): if b > a: a, b = b, a if b == 0: return a return gcd(a % b, b) delit = [] if a == b: print(0) else: if a > b: a, b = b, a for i in range(1, int((b - a) ** 0.5) + 1): if (b - a) % i == 0: delit.append(i) delit.append((b - a) // i) for item in delit: if a % item == 0: k = 0 else: k = item - a % item if (a + k) * (b + k) // gcd(b - a, a + k) < lcm: lcm = (a + k) * (b + k) // gcd(b - a, a + k) K = k elif (a + k) * (b + k) // gcd(b - a, a + k) == lcm and k < K: K = k print(K)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def divisor(n): n2 = int(n**0.5 + 1) ret = set() for i in range(1, n2 + 1): if n % i == 0: ret.add(i) ret.add(n // i) ret = list(ret) ret.sort() return ret def gcd(a, b): while b != 0: a, b = b, a % b return a def lcm(a, b): return a // gcd(a, b) * b def solve(): inf = 1 << 60 a, b = list(map(int, input().split())) if a > b: a, b = b, a if a == b: return 0 ma = min(a, b) D = divisor(b - a) best_lcm = inf best_k = 0 for d in D: r1 = a % d r2 = b % d if r1 == r2: k = 0 if r1 != 0: k = d - r1 lcm1 = lcm(a + k, b + k) if best_lcm > lcm1: best_lcm = lcm1 best_k = k elif best_lcm == lcm1: best_k = min(best_k, k) return best_k print(solve())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
import sys __version__ = "1.1" __date__ = "2019-04-24" def gcd(a, b): if b == 0: return a return gcd(b, a % b) def solve(a, b): a, b = min(a, b), max(a, b) if b % a == 0: return 0 diff = b - a k, lcm = 0, a * b factors = [1, diff] fact = 2 while fact * fact <= diff: if diff % fact == 0: factors.append(fact) if fact != diff // fact: factors.append(diff // fact) fact += 1 for f in sorted(factors): step = a % f if step > 0: step = f - step this_lcm = (a + step) * (b + step) // gcd(a + step, b + step) if this_lcm < lcm: lcm = this_lcm k = step return k def main(argv=None): a, b = map(int, input().split()) print(solve(a, b)) return 0 STATUS = main() sys.exit(STATUS)
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF NONE ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
def gcd(a, b): while b > 0: a, b = b, a % b return a def lcm(a, b): return a * b / gcd(a, b) ans = 0 ans_lcm = 100000000000000000000 a = 0 b = 0 def update(d): global ans, a, b, ans_lcm k = d - a % d lcm_ = lcm(a + k, b + k) if lcm_ < ans_lcm or lcm_ == ans_lcm and d < ans: ans_lcm = lcm_ ans = k def start(): global a, b, ans, ans_lcm a, b = map(int, input().split()) if a < b: a, b = b, a ans_lcm = lcm(a, b) i = 1 while i * i <= a - b: if (a - b) % i == 0: update(i) update((a - b) // i) i += 1 print(ans) start()
FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) a, b = mi() a, b = min(a, b), max(a, b) X = b - a def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) def lcm(a, b): return a * b // gcd(a, b) def prime_factor(x): pf = [] for i in range(1, int(x**0.5) + 1): if x % i == 0: pf.append(i) pf.append(x // i) pf = sorted(list(set(pf))) return pf if a == b: print(0) else: D = prime_factor(X) minimum = 10**100 for d in D: k = -a % d L = lcm(a + k, b + k) if minimum > L: minimum = L ans = k print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. Neko has two integers $a$ and $b$. His goal is to find a non-negative integer $k$ such that the least common multiple of $a+k$ and $b+k$ is the smallest possible. If there are multiple optimal integers $k$, he needs to choose the smallest one. Given his mathematical talent, Neko had no trouble getting Wrong Answer on this problem. Can you help him solve it? -----Input----- The only line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$). -----Output----- Print the smallest non-negative integer $k$ ($k \ge 0$) such that the lowest common multiple of $a+k$ and $b+k$ is the smallest possible. If there are many possible integers $k$ giving the same value of the least common multiple, print the smallest one. -----Examples----- Input 6 10 Output 2 Input 21 31 Output 9 Input 5 10 Output 0 -----Note----- In the first test, one should choose $k = 2$, as the least common multiple of $6 + 2$ and $10 + 2$ is $24$, which is the smallest least common multiple possible.
a, b = map(int, input().split()) d = abs(a - b) def factor(x): S = set() i = 1 while i * i <= x: if x % i == 0: S.add(i) S.add(x // i) i += 1 return S def gcd(a, b): if b == 0: return a return gcd(b, a % b) def lcm(a, b): if a == 0 or b == 0: return 0 return a * b // gcd(a, b) S = factor(d) ans = 0 for f in S: x = (f - a % f) % f y = (f - b % f) % f k = lcm(x, y) if lcm(a + k, b + k) < lcm(a + ans, b + ans): ans = k print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
t = int(input()) while t: n, k = [int(x) for x in input().split()] arr = [int(x) for x in input().split()] num = [[(0) for i in range(n)] for j in range(n)] i = 0 j = 0 count = 0 while i < n: while j < n: num[i][j] = arr[i] + arr[j] j = j + 1 i = i + 1 j = 0 min = 1000000000000000 i = 0 j = 0 while i < n: while j < n: if abs(num[i][j] - k) == min and i != j: count = count + 1 elif abs(num[i][j] - k) <= min and i != j: min = abs(num[i][j] - k) count = 1 j = j + 1 i = i + 1 j = i print("{} {}".format(min, count)) t = 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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
for _ in range(int(input())): N, K = map(int, input().split()) array = list(map(int, input().split())) count = 0 mn = float("inf") for i in range(N): for j in range(i + 1, N): if abs(array[i] + array[j] - K) < mn: mn = abs(array[i] + array[j] - K) count = 1 elif abs(array[i] + array[j] - K) == mn: count += 1 print(mn, count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
from sys import stdin T = int(stdin.readline().strip()) while T > 0: [N, K] = [int(n) for n in stdin.readline().strip().split()] arr = [int(n) for n in stdin.readline().strip().split()] minAbs = abs(arr[0] + arr[1] - K) count = 0 for i in range(0, N - 1): for j in range(i + 1, N): absVal = abs(arr[i] + arr[j] - K) if absVal < minAbs: minAbs = absVal count = 1 elif absVal == minAbs: count += 1 print(minAbs, count) T -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER ASSIGN LIST 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 ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
T = int(input()) for i in range(T): n, k = [int(s) for s in input().split()] l = [int(s) for s in input().split()] m = [] for j in range(n): for a in range(j + 1, n): m.append(abs(l[a] + l[j] - k)) print(min(m), m.count(min(m)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
t = int(input()) for I in range(t): n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [] for i in range(len(a)): for j in range(i + 1, len(a)): b.append(abs(a[i] + a[j] - k)) count = 0 p = min(b) for i in b: if i == p: count += 1 print(p, count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
from itertools import combinations for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) l = list(combinations(l, 2)) x = [] for i in l: x.append(abs(sum(i) - k)) print(min(x), x.count(min(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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
def solution(): N, K = map(int, input().split()) A = list(map(int, input().split())) min_val = 10000000000.0 count = 0 for i in range(N): for j in range(i + 1, N): dif = abs(A[i] + A[j] - K) if min_val > dif: min_val = dif count = 1 elif min_val == dif: count += 1 print(min_val, end=" ") print(count) T = int(input()) while T > 0: T = T - 1 solution()
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
for _ in range(int(input())): n, k = map(int, input().split()) mini = 10000000000000000000 l = [int(i) for i in input().split()] cmaxi = 0 for i in range(n): for j in range(i + 1, n): curr = abs(l[i] + l[j] - k) if curr < mini: mini = curr cmaxi = 1 elif curr == mini: cmaxi += 1 print(mini, cmaxi)
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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
n1 = int(input()) for i in range(n1): n, b = map(int, input().split(" ")) l = list(map(int, input().split(" "))) s = [] for j in range(0, n - 1): for k in range(j + 1, n): s.append(abs(l[j] + l[k] - b)) p = min(s) c = 0 for j in s: if j == p: c = c + 1 print(p, c)
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 VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
t = int(input()) for i in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() minimum_diff = k num_of_pairs = 0 sum_of_two_list = [] for j in range(n): for l in range(j + 1, n): sum_value = a[j] + a[l] sum_of_two_list.append(abs(sum_value - k)) print(min(sum_of_two_list), sum_of_two_list.count(min(sum_of_two_list)))
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 ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
t = int(input()) while t > 0: n, k = map(int, input().split()) t -= 1 l = list(map(int, input().split())) sum = 0 a = [] for i in range(0, n): for j in range(i + 1, n): x = l[i] + l[j] - k x = abs(x) a.append(x) q = min(a) z = a.count(q) print(q, z)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
T = int(input()) for i in range(T): n, k = map(int, input().split()) d = list(map(int, input().split())) f = 0 l = [] for i in range(n + 1): for j in range(i + 1, n): f = abs(d[i] + d[j] - k) l.append(f) print(min(l), l.count(min(l)))
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 NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
t = int(input()) for i in range(t): l = [] mi = 99999999999 n, m = map(int, input().split()) a = list(map(int, input().split())) for j in range(n - 1): for k in range(j + 1, n): a1 = a[j] a2 = a[k] b1 = a1 + a2 - m b2 = abs(b1) l.append(b2) if mi > b2: mi = b2 print(mi, l.count(mi))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) l.sort() c = 0 m = 100 d = [] for i in range(n - 1): for j in range(i + 1, n): m = abs(l[i] + l[j] - k) d.append(m) m = min(d) c = d.count(m) print(m, "", c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
from itertools import combinations for t in range(int(input())): n, k = map(int, input().split()) li = list(map(int, input().split())) li2 = list(combinations(li, 2)) mn, ct = 10000000000, 0 for x in li2: if abs(x[0] + x[1] - k) < mn: mn = abs(x[0] + x[1] - k) for x in li2: if abs(x[0] + x[1] - k) == mn: ct += 1 print(mn, ct)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
t = int(input()) for i in range(t): B = list(map(eval, input().split())) A = list(map(eval, input().split())) count = 0 minimum = 10000000000 for j in range(len(A) - 1): for m in range(j + 1, len(A)): if abs(A[j] + A[m] - B[1]) < minimum: count = 0 minimum = abs(A[j] + A[m] - B[1]) if abs(A[j] + A[m] - B[1]) == minimum: count += 1 L = [] L.append(minimum) L.append(count) print(*L)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
t = int(input()) while t >= 1: n = input() arr = n.split() a = int(arr[0]) k = int(arr[1]) c = input() c = c.split() arr1 = [] sum = [] for i in range(len(c)): arr1.append(int(c[i])) arr1.sort() numberofpairs = 0 min_value = 10000000000 count = 0 assist = 0 d = 0 f = 0 while d < len(arr1): e = d + 1 while e < len(arr1): if abs(arr1[d] + arr1[e] - k) < min_value: min_value = abs(arr1[d] + arr1[e] - k) count = 1 elif abs(arr1[d] + arr1[e] - k) == min_value: count = count + 1 e = e + 1 d = d + 1 print("{} {}".format(min_value, count)) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
for t in range(int(input())): temp = list(map(int, input().split())) n = temp[0] k = temp[1] a = list(map(int, input().split())) count = 0 minn = -1 for i in range(n): for j in range(i + 1, n): if minn == -1: count += 1 minn = abs(a[i] + a[j] - k) elif abs(a[i] + a[j] - k) == minn: count += 1 elif abs(a[i] + a[j] - k) < minn: minn = abs(a[i] + a[j] - k) count = 1 print(minn, count)
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 ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
t = int(input()) for z in range(t): n, k = map(int, input().split()) l = input().split() for i in range(n): l[i] = int(l[i]) i = 0 m = [] while i < n - 1: s = i + 1 while s < n: x, y = l[i], l[s] tot = x + y - k if tot < 0: tot = -tot m.append(tot) s = s + 1 i = i + 1 print(str(min(m)) + " " + str(m.count(min(m))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that - i β‰  j - |ai + aj - K| is minimal possible Output the minimal possible value of |ai + aj - K| (where i β‰  j) and the number of such pairs for the given array and the integer K. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case consists of two space separated integers - N and K respectively. The second line contains N single space separated integers - a1, a2, ..., aN respectively. -----Output----- For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached. -----Constraints----- - 1 ≀ T ≀ 50 - 1 ≀ ai, K ≀ 109 - N = 2 - 31 point. - 2 ≀ N ≀ 1000 - 69 points. -----Example----- Input: 1 4 9 4 4 2 6 Output: 1 4 -----Explanation:----- The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4.
for _ in range(int(input())): N, K = map(int, input().split()) A = list(map(int, input().split())) m = abs(A[0] + A[1] - K) c = 0 for i in range(N - 1): for j in range(i + 1, N): m = min(abs(A[i] + A[j] - K), m) for i in range(N - 1): for j in range(i + 1, N): if abs(A[i] + A[j] - K) == m: c += 1 print(m, c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR