description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
for _ in range(int(input())): n1, m = map(int, input().split()) sum = 0 p = 1 for i in range(31): n = n1 z = max(m % (1 << i + 1) - (1 << i) + 1, 0) + (m // (1 << i + 1) << i) p = 1 while n: if n % 2: p = p * z % 998244353 z = z * z % 998244353 n //= 2 sum = (sum + (p << i)) % 998244353 % 998244353 print(sum)
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 NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
MOD = 998244353 for _ in range(int(input())): n, m = [int(s) for s in input().split()] m += 1 ans = 0 cur = 1 while cur <= m: cur = cur << 1 w = m // cur * (cur // 2) + max(0, m % cur - cur // 2) ans = (ans + cur // 2 * pow(w, n, MOD) % MOD) % MOD print(ans)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
MODULE = 998244353 for _ in range(int(input())): N, M = list(map(int, input().split())) ans = 0 for i in range(M.bit_length()): suma = M // 2 ** (i + 1) * 2**i + max(M % 2 ** (i + 1) - 2**i + 1, 0) ans += 2**i * pow(suma, N, MODULE) print(ans % MODULE)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
mod = 998244353 for i in range(int(input())): n, m = map(int, input().split()) ans = 0 mult = 1 for k in range(31): block = m // (2 * mult) cnt = block * mult cnt += max(0, m % (2 * mult) - mult + 1) ans += pow(cnt, n, mod) * mult ans %= mod mult *= 2 print(ans)
ASSIGN 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
def Sets(n: int, m: int): if m == 1: return [[n]] ans = [] for i in range(n + 1): ans += [([i] + e) for e in Sets(n - i, m - 1)] return ans def lpow(e: int, i: int): if i == 0: return 2**31 - 1 return e def relu(x): return max(0, x) def nCr2(n: int, r: int): ans = 1 for i in range(r): ans *= n - i ans //= i + 1 return ans def nCr(n: int, r: list): ans = 1 for i in range(len(r)): ans *= nCr2(n, r[i]) n -= r[i] return ans def fc(a, b): t1 = a % (1 << b + 1) - (1 << b) + 1 t2 = a // (1 << b + 1) ret = relu(t1) + (t2 << b) return ret def binpow(a, b): res = 1 while b > 0: if b & 1: res = res * a % 998244353 a = a * a % 998244353 b >>= 1 return res def f(n: int, m): ans = 0 for e in Sets(n, m): term = 2**31 - 1 p = 1 for i in e: term &= lpow(p, i) p += 1 ans += term * nCr(n, e) % 998244353 return ans % 998244353 def f2(n, m): ans = 0 for i in range(20 + 11): ans += binpow(fc(m, i), n) << i return ans % 998244353 for _ in range(int(input())): n, m = map(int, input().split()) print(f2(n, m))
FUNC_DEF VAR VAR IF VAR NUMBER RETURN LIST LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
for k in range(int(input())): n, i = list(map(int, input().split())) s = 0 for j in range(0, 32): q = (i // (1 << j + 1) << j) + max(0, i % (1 << j + 1) - (1 << j) + 1) s += pow(q, n, 998244353) << j print(s % 998244353)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
mod = 998244353 def power(x, y): a = 1 while y: if y & 1: a = a * x % mod x = x * x % mod y >>= 1 return a def func(m, j): upper = m >> j + 1 ans = upper * 2**j if m & 1 << j: ans += m - (upper << j + 1) - 2**j + 1 return ans for _ in range(int(input())): n, m = list(map(int, input().split())) ans = 0 for j in range(30): ans += power(func(m, j), n) * 2**j % mod print(ans % mod)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
mod = 998244353 for _ in range(int(input())): n, m = list(map(int, input().split())) l = [(0) for i in range(31)] bi = str(bin(m))[2:][::-1] l1 = [] for i in range(len(bi)): x = m >> i + 1 x = x << i if bi[i] == "1": x = x + (m - (m >> i << i) + 1) l1.append(x) ans = 0 c = 0 for i in l1: ans += pow(i, n, mod) * pow(2, c, mod) c += 1 ans = ans % mod print(ans)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
for i in range(int(input())): n, m = map(int, input().split()) def array(a, n): c = 1 while n: if n % 2 == 1: c = c * a % 998244353 a = a * a % 998244353 n = n // 2 return c def sol(q, w): return (q // (1 << w + 1) << w) + max(0, q % (1 << w + 1) - (1 << w) + 1) def res(): v = 0 for j in range(31): v = (v + (array(sol(m, j), n) << j) % 998244353) % 998244353 return v print(res())
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
MOD = 998244353 def modpow(base, exp, mod): result = 1 while exp: if exp & 1: result = result * base % mod base = base * base % mod exp >>= 1 return result def cardinalBitX1toM(M, bit): if M < bit: return 0 elif M < bit << 1: return M - bit + 1 return bit * (M // (bit << 1)) + cardinalBitX1toM(M % (bit << 1), bit) def main(N, M): ANS = 0 for i in range(30): ANS += (1 << i) * modpow(cardinalBitX1toM(M, 1 << i), N, MOD) ANS %= MOD return ANS T = int(input().strip()) for _ in range(T): N, M = [int(x) for x in input().strip().split()[:2]] print(main(N, M))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
def getcount(n, k): res = n >> k + 1 << k if n >> k & 1: res += n & (1 << k) - 1 return res for _ in range(int(input())): n, m = map(int, input().split()) ans = 0 mod = 998244353 new = [0] * 32 for i in range(31): new[i] = getcount(m + 1, i) for i in range(32): ans = (ans + (1 << i) * pow(new[i], n, mod) % mod) % mod print(ans)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
inf = 998244353 def an(n, a): z = 1 while n: if n % 2: z = z * a % inf a = a * a % inf n //= 2 return z for _ in range(int(input())): n, m = map(int, input().split()) ans = 0 for i in range(31): c = (m // (1 << i + 1) << i) + max(0, m % (1 << i + 1) - (1 << i) + 1) ans = (ans + (an(n, c) << i) % 998244353) % 998244353 print(ans)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
mod = 998244353 def counts(m, i): count = m >> i + 1 << i if m >> i & 1: count += m & (1 << i) - 1 return count def solve(n, m): ans = 0 for i in range(32): count = counts(m + 1, i) if count: ans = (ans + pow(count, n, mod) * pow(2, i, mod) % mod) % mod else: break return ans for _ in range(int(input())): print(solve(*map(int, input().split())))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
def solve(n, m): m += 1 ans = 0 mod = 998244353 for bit in range(30): cnt = m // (2 << bit) cnt = cnt * (1 << bit) cnt += max(0, m % (2 << bit) - (1 << bit)) ans += pow(cnt, n, mod) * (1 << bit) print(ans % mod) testCase = int(input()) for _ in range(testCase): n, limit = map(int, input().split()) solve(n, limit)
FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
from itertools import product output = [] for _ in range(int(input())): N, M = map(int, input().split(" ")) ways = 0 two_power = 1 while two_power <= M: c = M // (two_power << 1) * two_power r = (M & (two_power << 1) - 1) - (two_power - 1) if r > 0: c += r ways += pow(c, N, 998244353) * two_power % 998244353 two_power <<= 1 output.append(ways % 998244353) print("\n".join(map(str, output)))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
mod = 998244353 t = int(input()) for _ in range(t): n, m = map(int, input().split()) res = 0 for bit in range(31): b2 = 2 << bit count = m // b2 * (b2 >> 1) + max(0, m % b2 - (b2 >> 1) + 1) res += pow(count, n, mod) << bit print(res % mod)
ASSIGN 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
md = 998244353 t = int(input()) for _ in range(t): n, m = input().split() n = int(n) m = int(m) ans = 0 for i in range(0, 32): ans += ( pow( (m // pow(2, i + 1) << i) + max(0, m % pow(2, i + 1) - pow(2, i) + 1), n, md, ) << i ) print(ans % md)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
def mi(): return map(int, input().split()) def li(): return list(mi()) def si(): return str(input()) def ni(): return int(input()) def find(m, k): sk = 1 << k c1 = m >> k + 1 << k c0 = c1 << 1 return c1 + max(0, m + 1 - c0 - sk) for t in range(int(input())): n, m = mi() ans = 0 mod = 998244353 for i in range(32): curr = find(m, i) if curr > 0: ans += (1 << i) * pow(curr, n, mod) ans %= mod print(ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
mod = 998244353 def arrctr(n, m): ans = 0 for bit in range(30): ct = m // (2 << bit) * (1 << bit) ct += max(0, m % (2 << bit) - (1 << bit) + 1) ans += pow(ct, n, mod) << bit return ans % mod t = int(input()) for _ in range(t): n, m = [int(x) for x in input().split()] print(arrctr(n, m))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN 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 VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given two positive integers N and M, let S denote the set of all the arrays of size N such that each element of the array lies in the range [1, M]. Since there are M^{N} such arrays, the size of S is M^{N}. Let X_{i} denote the [bitwise AND] of all elements of the i^{th} array in the set, where 1 ≤ i ≤ M^{N}. Find the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - The first and only line of each test case contains two integers N and M, the size of the array, and the maximum limit of elements. ------ Output Format ------ For each test case, print the value \sum_{i = 1}^{M^{N}} X_{i}. Since the answer can be huge, output the answer modulo 998244353. ------ Constraints ------ $1 ≤ T ≤ 1000$ $1 ≤ N ≤ 2\cdot10^{5}$ $1 ≤ M ≤ 10^{9}$ ----- Sample Input 1 ------ 2 2 2 2 3 ----- Sample Output 1 ------ 3 12 ----- explanation 1 ------ Test case $1$: The set $S$ contains $\{[1,1], [1,2], [2,1], [2,2]\}$. The array $X = [1\& 1, 1\& 2, 2\& 1, 2\& 2] = [1, 0, 0, 2]$. Thus, sum of all elements of $X$ is $1+0+0+2 = 3$. Test case $2$: The set $S$ contains $\{[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]\}$. The array $X = [1\& 1, 1\& 2, 1\& 3, 2\& 1, 2\& 2, 2\& 3, 3\& 1, 3\& 2, 3\& 3] = [1, 0, 1, 0, 2, 2, 1, 2, 3]$. Thus, sum of all elements of $X$ is $12$.
MOD = 998244353 def getex(till, exp): ex = till >> exp + 1 << exp powxp = 1 << exp if till & powxp: ex += till % powxp return ex def main(N, M): summation = 0 for exp in range(M.bit_length()): expenses = getex(M + 1, exp) expbitsum = (pow(expenses, N, MOD) << exp) % MOD summation = (summation + expbitsum) % MOD return summation T = int(input()) for i in range(T): N, M = map(int, input().split()) print(main(N, M))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
In Conway's Game of Life, cells in a grid are used to simulate biological cells. Each cell is considered to be either alive or dead. At each step of the simulation each cell's current status and number of living neighbors is used to determine the status of the cell during the following step of the simulation. In this one-dimensional version, there are N cells numbered 0 through N-1. The number of cells does not change at any point in the simulation. Each cell i is adjacent to cells i-1 and i+1. Here, the indices are taken modulo N meaning cells 0 and N-1 are also adjacent to eachother. At each step of the simulation, cells with exactly one living neighbor change their status (alive cells become dead, dead cells become alive). For example, if we represent dead cells with a '0' and living cells with a '1', consider the state with 8 cells: 01100101 Cells 0 and 6 have two living neighbors. Cells 1, 2, 3, and 4 have one living neighbor. Cells 5 and 7 have no living neighbors. Thus, at the next step of the simulation, the state would be: 00011101 Given some state of the game, your task is to determine the state immediately preceding it. In some cases there may be more than one answer or no possible answer. ------ Input ------ Input will begin with an integer T<100, the number of test cases. Each test case consists of a single line, with between 3 and 50 characters, inclusive. Each character will be either '0' or '1'. Each '0' represents a dead cell, and each '1' represents an alive cell. ------ Output ------ For each test case, output the state of the game that precedes the given state. If there is no possible solution, print "No solution" (quotes for clarity only). If there are multiple possible solutions, print "Multiple solutions" (quotes for clarity only). ----- Sample Input 1 ------ 4 00011101 000 000001 11110 ----- Sample Output 1 ------ 01100101 Multiple solutions No solution 10010
def check(s): p = ["00", "01", "10", "11"] for val in s[1:-1]: for i in range(4): p[i] = p[i] + str(int(p[i][-2]) ^ int(p[i][-1]) ^ int(val)) count = 0 for x in p: if convert(x) == s: count += 1 r = x if count == 1: return r elif count == 0: return "No solution" else: return "Multiple solutions" def convert(s): s = s[-1] + s + s[0] n = len(s) result = "" for i in range(1, n - 1): if (s[i - 1] + s[i + 1]).count("1") == 1: result += "1" if s[i] == "0" else "0" else: result += s[i] return result t = int(input()) for _ in range(t): s = input().rstrip() print(check(s))
FUNC_DEF ASSIGN VAR LIST STRING STRING STRING STRING FOR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN STRING RETURN STRING FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING NUMBER VAR VAR VAR STRING STRING STRING VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
In Conway's Game of Life, cells in a grid are used to simulate biological cells. Each cell is considered to be either alive or dead. At each step of the simulation each cell's current status and number of living neighbors is used to determine the status of the cell during the following step of the simulation. In this one-dimensional version, there are N cells numbered 0 through N-1. The number of cells does not change at any point in the simulation. Each cell i is adjacent to cells i-1 and i+1. Here, the indices are taken modulo N meaning cells 0 and N-1 are also adjacent to eachother. At each step of the simulation, cells with exactly one living neighbor change their status (alive cells become dead, dead cells become alive). For example, if we represent dead cells with a '0' and living cells with a '1', consider the state with 8 cells: 01100101 Cells 0 and 6 have two living neighbors. Cells 1, 2, 3, and 4 have one living neighbor. Cells 5 and 7 have no living neighbors. Thus, at the next step of the simulation, the state would be: 00011101 Given some state of the game, your task is to determine the state immediately preceding it. In some cases there may be more than one answer or no possible answer. ------ Input ------ Input will begin with an integer T<100, the number of test cases. Each test case consists of a single line, with between 3 and 50 characters, inclusive. Each character will be either '0' or '1'. Each '0' represents a dead cell, and each '1' represents an alive cell. ------ Output ------ For each test case, output the state of the game that precedes the given state. If there is no possible solution, print "No solution" (quotes for clarity only). If there are multiple possible solutions, print "Multiple solutions" (quotes for clarity only). ----- Sample Input 1 ------ 4 00011101 000 000001 11110 ----- Sample Output 1 ------ 01100101 Multiple solutions No solution 10010
t = int(input()) zero_list = ["000", "101", "110", "011"] one_list = ["100", "001", "010", "111"] for iter in range(t): cells = list(input()) if cells[-1] == "0": possibles = zero_list[:] else: possibles = one_list[:] possibles.sort() for i in range(len(cells) - 2, -1, -1): for j in range(4): if cells[i] == "0": possibles[j] = zero_list[j][0] + possibles[j] else: possibles[j] = one_list[j][0] + possibles[j] possibles.sort() answers = [] for poss in possibles: if poss[0:2] == poss[-2:]: answers.append(poss[1:-1]) if len(answers) == 0: print("No solution") elif len(answers) > 1: print("Multiple solutions") else: print(answers[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
m = [] onesarr = [] for i in range(128): a = [] ones = 0 for j in range(7): l = i % 2 ** (7 - j) // 2 ** (6 - j) a.append(l) ones += l onesarr.append(7 - ones) m.append(a) t = int(input()) for _ in range(t): poslag = True n = int(input()) maxd = 0 mind = 8 arr = [8] * 10 numbers = [ [0, 1, 2, 3, 5, 6], [2, 5], [1, 2, 3, 4, 6], [1, 2, 4, 5, 6], [0, 2, 4, 5], [0, 1, 4, 5, 6], [0, 1, 3, 4, 5, 6], [1, 2, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 4, 5, 6], ] for _ in range(n): x, y = [int(i) for i in input().split()] arr[x] = y for i in range(128): posflag = True for j in range(10): if arr[j] != 8: ch = 0 for k in numbers[j]: ch += m[i][k] if ch != arr[j]: posflag = False break if posflag: mind = min(mind, onesarr[i]) maxd = max(maxd, onesarr[i]) if mind == 8: print("invalid") else: print(mind, maxd)
ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
seg = [ [True, True, True, True, True, True, False], [False, False, False, True, True, False, False], [False, True, True, False, True, True, True], [False, False, True, True, True, True, True], [True, False, False, True, True, False, True], [True, False, True, True, False, True, True], [True, True, True, True, False, True, True], [False, False, False, True, True, True, False], [True] * 7, [True, False, True, True, True, True, True], ] T = int(input()) for _ in range(T): n = int(input()) l = [[int(e) for e in input().split()] for _ in range(n)] maxOn = -1 minOn = 8 for segOn in range(2**7): band = True bits = [(1 << 6 - i & segOn >= 1) for i in range(7)] for j, e in enumerate(l): eq = (bits[i] and seg[e[0]][i] for i in range(7)) if sum(eq) != e[1]: band = False break if band: a = sum(bits) minOn = min(minOn, 7 - a) maxOn = max(maxOn, 7 - a) if minOn == 8 and maxOn == -1: print("invalid") else: print("{} {}".format(minOn, maxOn))
ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER BIN_OP LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER 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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
digits = [119, 18, 93, 91, 58, 107, 111, 82, 127, 123] t = int(input()) for ti in range(t): n = int(input()) x, y = [None for _ in range(n)], [None for _ in range(n)] for i in range(n): x[i], y[i] = map(int, input().split()) any_ok = False min_dead, max_dead = 7, 0 for mask in range(1 << 7): ok = True for i in range(n): if bin(digits[x[i]] & mask).count("1") != y[i]: ok = False if ok: any_ok = True num_dead = 7 - bin(mask).count("1") min_dead = min(min_dead, num_dead) max_dead = max(max_dead, num_dead) if any_ok: print(min_dead, max_dead) else: print("invalid")
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER 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 VAR NONE VAR FUNC_CALL VAR VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
def count_alive(num): return bin(num).count("1") for _ in range(int(input())): li = [] for i in range(int(input())): xx, yy = map(int, input().split()) li.append((xx, yy)) ideal = [119, 18, 93, 91, 58, 107, 111, 82, 127, 123] comb = list(range(128)) valid = 1 for x, y in li: if y > count_alive(ideal[x]): valid = 0 break ncomb = [] if valid: for x, y in li: for j in comb: if count_alive(ideal[x] & j) == y: ncomb.append(j) comb = ncomb ncomb = [] if len(comb) == 0: valid = 0 if valid: alive = [] for i in range(len(comb)): alive.append(count_alive(comb[i])) print(7 - max(alive), 7 - min(alive)) else: print("invalid")
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR FOR VAR VAR VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR 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 BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
mask = [119, 36, 93, 109, 46, 107, 123, 37, 127, 111] def NumOn(x): i = 1 ans = 0 while i < 128: if x & i > 0: ans += 1 i = i << 1 return ans def NumOff(x): return 7 - NumOn(x) t = int(input()) while t: t -= 1 n = int(input()) inp = [list(map(int, input().split())) for _ in range(n)] configs = [] for config in range(128): valid = True for i in range(n): if NumOn(mask[inp[i][0]] & config) != inp[i][1]: valid = False break if valid: configs.append(config) if len(configs) == 0: print("invalid") else: max = -1 min = 8 for c in configs: dead = NumOff(c) if dead > max: max = dead if dead < min: min = dead print(min, max)
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
def solve(i): for k in range(n): a, b = arr[k][0], arr[k][1] count = 0 for j in range(7): if mask[a] & 1 << j and i & 1 << j: count += 1 if count != b: return False return True mask = [119, 36, 93, 109, 46, 107, 123, 37, 127, 111] tests = int(input()) for te in range(tests): n = int(input()) arr = [] for i in range(n): arr.append(list(map(int, input().split()))) flag = 0 mina = 10 maxa = -1 for i in range(1 << 7): if solve(i): flag = 1 count = 0 for j in range(7): if i & 1 << j == 0: count += 1 mina = min(mina, count) maxa = max(maxa, count) if flag == 0: print("invalid") else: print(mina, maxa)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER 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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
from itertools import combinations def solve(tests): for i in tests: if len(i[0]) < i[1]: print("invalid") return i = 0 mini, maxi = -1, 8 flag = True while mini <= i <= maxi: for s in combinations(range(7), i): for j in tests: if len(j[0] & set(s)) != j[1]: break else: if flag: flag = False mini = i i = 8 break else: maxi = i print(7 - maxi, 7 - mini) return if flag: i += 1 else: i -= 1 if mini < 0: print("invalid") for _ in range(int(input())): tests = [] for _ in range(int(input())): x, y = map(int, input().split()) if x == 0: tests.append(({0, 1, 2, 4, 5, 6}, y)) elif x == 1: tests.append(({2, 5}, y)) elif x == 2: tests.append(({0, 2, 3, 4, 6}, y)) elif x == 3: tests.append(({0, 2, 3, 5, 6}, y)) elif x == 4: tests.append(({1, 2, 3, 5}, y)) elif x == 5: tests.append(({0, 1, 3, 5, 6}, y)) elif x == 6: tests.append(({0, 1, 3, 4, 5, 6}, y)) elif x == 7: tests.append(({0, 2, 5}, y)) elif x == 8: tests.append(({0, 1, 2, 3, 4, 5, 6}, y)) elif x == 9: tests.append(({0, 1, 2, 3, 5, 6}, y)) solve(tests)
FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR RETURN IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
num_dic = { (0): "1110111", (1): "0010010", (2): "1011101", (3): "1011011", (4): "0111010", (5): "1101011", (6): "1101111", (7): "1010010", (8): "1111111", (9): "1111011", } for z in range(int(input())): n = int(input()) tuples = [tuple(map(int, input().split())) for i in range(n)] valid_config = [] for i in range(128): valid = True for j in range(n): x, y = tuples[j] x_bin = int(num_dic[x], 2) tot = bin(x_bin & i)[2:].count("1") if tot != y: valid = False if valid: valid_config.append(i) mn = 7 mx = 0 for k in valid_config: cnt_bin = bin(k) cnt_bin = "0" * (7 - len(cnt_bin[2:])) + cnt_bin[2:] cnt = cnt_bin.count("0") if cnt > mx: mx = cnt if cnt < mn: mn = cnt if len(valid_config) == 0: print("invalid") else: print(mn, mx)
ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
def n1(n): s = bin(n) if s[2] == "0": return 0 else: return s.count("1") conf = [119, 18, 93, 91, 58, 107, 111, 82, 127, 123] for z in range(int(input())): n = int(input()) bf = [] for i in range(128): bf.append(i) li = [] for i in range(n): li.append(tuple(map(int, input().split()))) for x, y in li: bf1 = [] for j in bf: if n1(conf[x] & j) == y: bf1.append(j) if not bf1: print("invalid") break else: bf = list(set(bf) & set(bf1)) if not bf: print("invalid") break else: mx = n1(bf[0]) mn = mx for j in bf: mx = max(mx, n1(j)) mn = min(mn, n1(j)) print(7 - mx, 7 - mn)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING RETURN NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
d = [ [0, 3, 4, 2, 6, 5], [5, 6], [0, 5, 1, 4, 2], [0, 5, 1, 6, 2], [3, 1, 5, 6], [0, 3, 1, 6, 2], [0, 3, 4, 2, 6, 1], [0, 5, 6], [0, 1, 2, 3, 4, 5, 6], [0, 3, 1, 5, 6, 2], ] def check(ar, b): for i in ar: c = 0 for j in d[i[0]]: if b[j]: c = c + 1 if c != i[1]: return False return True t = int(input()) while t: n = int(input()) ar = [list(map(int, input().split())) for i in range(n)] v = 0 mi = 8 ma = 0 for i in range(2**7): b = [int(j) for j in bin(i)[2:]] while len(b) < 7: b = [0] + b if check(ar, b): v = 1 mi = min(mi, b.count(0)) ma = max(ma, b.count(0)) if v: print(str(mi) + " " + str(ma)) else: print("invalid") t = t - 1
ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
def invalid(n, l, list1, list5, max_dead): list2 = [] for i in range(n): if len(list1[l[i][0]]) < l[i][1]: print("invalid") return for i in range(127): list3 = list5[i].copy() list4 = list({0, 1, 2, 3, 4, 5, 6} - set(list3)) list4.sort() flag = 0 dead = -1 for k in range(n): dead = len(list1[l[k][0]]) - l[k][1] if dead < 0: print("invalid") return for m in list1[l[k][0]]: if dead == 0: if m not in list4: flag = 1 break elif m in list3: dead -= 1 if flag == 1: break if dead != 0: break if dead == 0 and flag == 0: list2.append(list3) if max_dead == 0: if len(list2) == 0: print(0, 0, sep=" ") else: print(0, len(list2[len(list2) - 1]), sep=" ") elif len(list2) == 0: print("invalid") else: print(len(list2[0]), len(list2[len(list2) - 1]), sep=" ") t = int(input()) list1 = [ [0, 1, 3, 4, 5, 6], [1, 3], [0, 1, 2, 4, 5], [0, 1, 2, 3, 4], [1, 2, 3, 6], [0, 2, 3, 4, 6], [0, 2, 3, 4, 5, 6], [0, 1, 3], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 6], ] list5 = [[0], [1], [2], [3], [4], [5], [6]] for i in range(127): list6 = list5[i].copy() for j in range(list6[len(list6) - 1] + 1, 7): list5.append(list6 + [j]) for i in range(t): n = int(input()) l = [] max_dead = -999 for j in range(n): l1 = [] s = input().split(" ") l1.append(int(s[0])) l1.append(int(s[1])) if len(list1[l1[0]]) - l1[1] > max_dead: max_dead = len(list1[l1[0]]) - l1[1] l.append(l1) if max_dead < 0: print("invalid") continue invalid(n, l, list1, list5, max_dead)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
def countSetBits(n): count = 0 while n: count += n & 1 n >>= 1 return count for _ in range(int(input())): la = [] lb = [] masks = [63, 6, 91, 79, 102, 109, 125, 7, 127, 111] conf = [] for o in range(int(input())): n, k = list(map(int, input().split())) la.append(n) lb.append(k) q = len(la) for i in range(128): f = 1 for j in range(q): if countSetBits(masks[la[j]] & i) != lb[j]: f = 0 break if f: conf.append(i) k = max(lb) mi = 999 ma = -999 if len(conf) > 0: for u in conf: p = 7 - countSetBits(u) mi = min(mi, p) ma = max(ma, p) print(mi, ma) else: print("invalid")
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
t = int(input()) d = { (0): (1, 2, 3, 5, 6, 7), (1): (3, 7), (2): (2, 3, 4, 5, 6), (3): (2, 3, 4, 6, 7), (4): (1, 3, 4, 7), (5): (1, 2, 4, 6, 7), (6): (1, 2, 4, 5, 6, 7), (7): (2, 3, 7), (8): (1, 2, 3, 4, 5, 6, 7), (9): (1, 2, 3, 4, 6, 7), } ll = [] for i in range(128): ll.append(bin(i)[2:]) for i in range(len(ll)): if len(ll[i]) != 7: di = 7 - len(ll[i]) ll[i] = "0" * di + ll[i] while t: re = [] l = [] n = int(input()) for i in range(n): x, y = map(int, input().split()) l.append((x, y)) for j in ll: lol = 0 for i in l: x, y = i a = d[x] cou = 0 for k in a: if j[7 - k] == "1": cou += 1 if cou != y: lol = 1 break if lol == 0: re.append(j) mi = 10000000000000000000000 mx = -1 if len(re) == 0: print("invalid") else: for i in re: xc = i.count("0") if xc > mx: mx = xc if xc < mi: mi = xc print(mi, mx) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP STRING VAR VAR VAR WHILE VAR ASSIGN VAR LIST ASSIGN VAR 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 EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP NUMBER VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
sevseg = [126, 48, 109, 121, 51, 91, 95, 112, 127, 123] for _ in range(int(input())): n = int(input()) x, y = [None] * n, [None] * n mini = 8 maxi = -1 for i in range(n): x[i], y[i] = map(int, input().rstrip().split()) badaflag = 0 for check in range(128): flag = 1 for i in range(n): if bin(sevseg[x[i]] & check).count("1") != y[i]: flag = 0 break if flag: deadCell = 7 - bin(check).count("1") mini = min(mini, deadCell) maxi = max(maxi, deadCell) badaflag = 1 if badaflag: print(mini, maxi) else: print("invalid")
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NONE VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR VAR VAR STRING VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
t = int(input()) for _ in range(t): n = int(input()) f = { (0): [0, {1, 2, 3, 5, 6, 7}], (1): [2, {3, 7}], (2): [5, {2, 3, 4, 5, 6}], (3): [5, {2, 3, 4, 7, 6}], (4): [4, {1, 4, 3, 7}], (5): [5, {2, 1, 4, 7, 6}], (6): [6, {1, 2, 4, 5, 6, 7}], (7): [3, {2, 3, 7}], (8): [7, {1, 2, 3, 4, 5, 6, 7}], (9): [6, {1, 2, 3, 4, 6, 7}], } f2 = { (0): "1110111", (1): "0010001", (2): "0111110", (3): "0111011", (4): "1011001", (5): "1101011", (6): "1101111", (7): "0110001", (8): "1111111", (9): "1111011", } data = [] for i in range(n): no, disp = map(int, input().split()) data.append([no, disp]) min_b, max_b = 100, -1 setbulb = [] ans = [] for i in range(128): poss = 1 for j in data: no, disp = j x = i & int(f2[no], 2) y = bin(x).count("1") if y != disp: poss = 0 break if poss: setbulb.append(i) if len(setbulb): for i in setbulb: y = bin(i).count("1") max_b = max(max_b, 7 - y) min_b = min(min_b, 7 - y) print(min_b, max_b) else: print("invalid")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
dicti = { (1): "0110000", (2): "1101101", (3): "1111001", (4): "0110011", (5): "1011011", (6): "1011111", (7): "1110000", (8): "1111111", (9): "1111011", (0): "1111110", } Arr = [] for i in range(2**7): to_Append = str(bin(i))[2:] Arr.append("0" * (7 - len(to_Append)) + to_Append) def check(Ans_Arr, p): temp_Arr = [] no_of_ones = p[1] ones_places = [i for i in range(7) if dicti[p[0]][i] == "1"] for i in Ans_Arr: if i.count("1") >= p[1]: cco = 0 for j in ones_places: if i[j] == "1": cco += 1 if cco == p[1]: temp_Arr.append(i) return temp_Arr for i in range(int(input())): flag = False Ans_Arr = Arr[:] queries = [] n = int(input()) for j in range(n): queries.append(list(map(int, input().split()))) for p in queries: if p[1] > dicti[p[0]].count("1"): print("invalid") flag = True break Ans_Arr = check(Ans_Arr, p) if len(Ans_Arr) == 0: print("invalid") flag = True break if not flag: for m in range(len(Ans_Arr)): Ans_Arr[m] = Ans_Arr[m].count("0") print(min(Ans_Arr), max(Ans_Arr))
ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR STRING FOR VAR VAR IF FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Read problems statements [Mandarin] , [Bengali] , [Hindi] , [Russian] and [Vietnamese] as well. Consider a seven-segment display, which can be used to show a decimal digit ($0$ through $9$). When a digit is shown on a working display, specific segments of the display turn on, as depicted in the following image (for digits $1$ through $9$): $0$ is depicted as usual, with all the segments turned on, except the middle one. Some of the segments of this display (possibly all or none) are dead ― they never turn on. In order to find out which segments could be dead, we ran $N$ tests with the display. For each $i$ ($1 ≤ i ≤ N$), in the $i$-th test, we tried to show the digit $x_{i}$ and out of the $7$ segments of the display, $y_{i}$ segments turned on. It is possible that the results of tests have been recorded incorrectly. Determine if the recorded results are valid, i.e. if there is a set of dead segments such that in each test, the recorded number of segments that turned on is correct. If it is valid, calculate the minimum possible number of dead segments and the maximum possible number of dead segments consistent with the results of the tests. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≤ i ≤ N$), the $i$-th of these lines contains two space-separated integers $x_{i}$ and $y_{i}$. ------ Output ------ For each test case: If the results of tests are invalid, print a single line containing the string "invalid" (without quotes). Otherwise, print a single line containing two space-separated integers ― the minimum and maximum number of dead segments. ------ Constraints ------ $1 ≤ T ≤ 1,000$ $1 ≤ N ≤ 10$ $0 ≤ x_{i} ≤ 9$ for each valid $i$ $0 ≤ y_{i} ≤ 7$ for each valid $i$ $x_{1}, x_{2}, \ldots, x_{N}$ are pairwise distinct ------ Subtasks ------ Subtask #1 (20 points): $N = 1$ Subtask #2 (80 points): original constraints ----- Sample Input 1 ------ 2 2 2 4 5 4 2 8 6 0 7 ----- Sample Output 1 ------ 1 2 invalid
t = int(input()) segment_map = [ (True, True, True, False, True, True, True), (False, False, True, False, False, True, False), (True, False, True, True, True, False, True), (True, False, True, True, False, True, True), (False, True, True, True, False, True, False), (True, True, False, True, False, True, True), (True, True, False, True, True, True, True), (True, False, True, False, False, True, False), (True, True, True, True, True, True, True), (True, True, True, True, False, True, True), ] def satisfies_all(configuration, constrains): for number, count in constrains: min_count = max_count = 0 for a, b in zip(configuration, segment_map[number]): if not b: continue if a is None: max_count += 1 elif a: min_count += 1 max_count += 1 if min_count > count or max_count < count: return False return True def configuration_step_satisfied(config): try: i = config.index(None) config[i] = False except ValueError: configuration_step_not_satisfied(config) def configuration_step_not_satisfied(config): i = 6 - config[::-1].index(False) config[i] = True config[i + 1 :] = [None] * (6 - i) def print_config(c): print(c) print( """.{0}{0}{0}. {1} {2} {1} {2} {1} {2} .{3}{3}{3}. {4} {5} {4} {5} {4} {5} .{6}{6}{6}. """.format( "-" if c[0] else " ", "|" if c[1] else " ", "|" if c[2] else " ", "-" if c[3] else " ", "|" if c[4] else " ", "|" if c[5] else " ", "-" if c[6] else " ", ) ) for _ in range(t): max_w = -9 min_w = 9 n = int(input()) constrains = [None] * n for i in range(n): x, y = map(int, input().split()) constrains[i] = x, y configuration = [None] * 7 while not all(configuration): if satisfies_all(configuration, constrains): if all(x is not None for x in configuration): working = 7 - sum(configuration) max_w = max(max_w, working) min_w = min(min_w, working) configuration_step_satisfied(configuration) else: try: configuration_step_not_satisfied(configuration) except ValueError: break if satisfies_all(configuration, constrains): working = 7 - sum(configuration) max_w = max(max_w, working) min_w = min(min_w, working) if max_w == -9: print("invalid") else: print(min_w, max_w)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR IF VAR NONE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR NONE ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP LIST NONE BIN_OP NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER STRING STRING VAR NUMBER STRING STRING VAR NUMBER STRING STRING VAR NUMBER STRING STRING VAR NUMBER STRING STRING VAR NUMBER STRING STRING VAR NUMBER STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE NUMBER WHILE FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NONE VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
def main(): n, m = read() m -= 1 perm = [0] * n lf = 0 rt = n - 1 for i in range(n): if m >= 2 ** (n - i - 2): perm[rt] = i + 1 rt -= 1 else: perm[lf] = i + 1 lf += 1 m %= 2 ** (n - i - 2) write(perm) def read(mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s="\n"): if s is None: s = "" if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") write(main())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF STRING IF VAR NONE ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) arr = [(0) for i in range(0, n + 1)] fac = [1] for i in range(1, n + 1): fac.append(2 * fac[i - 1]) def dfs(x, lft, rgt, m): if lft == rgt: arr[lft] = x return elif m <= fac[n - x - 1]: arr[lft] = x dfs(x + 1, lft + 1, rgt, m) else: arr[rgt] = x dfs(x + 1, lft, rgt - 1, m - fac[n - x - 1]) dfs(1, 1, n, m) for i in range(1, n + 1): print(arr[i], end="") if i == n: print("\n", end="") else: print(" ", end="")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR RETURN IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = input().split() n, m = int(n), int(m) - 1 pow2 = [] u = 1 for i in range(n): pow2.append(u) u *= 2 r = [] k = 1 while k < n: if m < pow2[n - k - 1]: r.append(k) else: m -= pow2[n - k - 1] k += 1 z = [] for i in range(n): if not n - i in r: z.append(n - i) r += z r = [str(i) for i in r] print(" ".join(r))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
import itertools __author__ = "yushchenko" def countf(f): sum = 0 for i in range(len(f)): for j in range(len(f))[i:]: sum += min(f[i : j + 1]) return sum n, m = input().split() n = int(n) m = int(m) if n == 1: print(1) exit() maxcount = pow(2, n - 2) t = 1 saved = [] result = [] for i in range(n): while m > maxcount: m -= maxcount if maxcount > 1: maxcount /= 2 saved.append(t) t += 1 else: result.append(str(t)) if maxcount > 1: maxcount /= 2 t += 1 if t > n: break for x in reversed(saved): result.append(str(x)) print(" ".join(result))
IMPORT ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) t = [n] for i in range(1, n): t = t + [n - i] if m - 1 & 1 << i - 1 else [n - i] + t for q in t: print(q)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR LIST BIN_OP VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) def ans(elems, m): n = len(elems) pow = 2 ** (n - 1) if m >= pow: return elems[::-1] pow //= 2 j = 0 while m > pow: j += 1 m -= pow pow //= 2 return [elems[j]] + ans(elems[j + 1 :], m) + elems[:j][::-1] elems = [(i + 1) for i in range(n)] print(" ".join(map(str, ans(elems, m))))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP LIST VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
ii = lambda: int(input()) kk = lambda: map(int, input().split()) ll = lambda: list(kk()) n, k = kk() pre, post = [], [] k -= 1 v = 1 for i in range(n - 2, -1, -1): if k & 2**i: post.append(v) else: pre.append(v) v += 1 print(*pre, n, *reversed(post))
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 LIST LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) s = 1 c = n - 1 arr = [0] * n i = 0 while i <= c: r = 0 j = s while j <= n and r < m: if j < n: r += 2 ** (n - j - 1) j += 1 if j > s and j != n + 1: r -= 2 ** (n - j) m -= r j -= 1 arr[i] = j while s < j: arr[c] = s c -= 1 s += 1 s += 1 i += 1 for i in arr: print(i, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = [int(i) for i in input().split()] big = 1 << n - 1 def r(i, m, half): half >>= 1 if half == 0: print(i, end=" ") return 0 if m <= half: print(i, end=" ") r(i + 1, m, half) else: r(i + 1, m - half, half) print(i, end=" ") r(1, m, big)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) a = [(0) for i in range(n)] l, r = 0, n - 1 m -= 1 for i in range(1, n + 1): cur = 2 ** (n - i - 1) if m >= cur: m -= cur a[r] = i r -= 1 else: a[l] = i l += 1 print(*a)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) s = 0 flag = False k = 0 myset = set() flag1 = True for j in range(n): if n in myset: for i in range(n, 0, -1): if i not in myset: print(i, end=" ") flag1 = False if flag1 == False: break flag = False s = 0 for i in range(n): if s < m <= s + 2 ** (n - i - 2 - k): print(i + 1 + k, end=" ") myset.add(i + 1 + k) k = i + 1 + k flag = True break s += 2 ** (n - i - 2 - k) if flag == False: print(n, end=" ") myset.add(n) k = n m -= s
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
def gen(n, start, t): if n == 1: return [start] if t <= 2 ** (n - 2): return [start] + gen(n - 1, start + 1, t) else: return gen(n - 1, start + 1, t - 2 ** (n - 2)) + [start] n, t = map(int, input().split()) print(" ".join(map(str, gen(n, 1, t))))
FUNC_DEF IF VAR NUMBER RETURN LIST VAR IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN BIN_OP LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER LIST VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
def func(n, a, b): if a > n: return val = 2 ** (n - a - 1) if b >= val: func(n, a + 1, b - val) print(a, end=" ") else: print(a, end=" ") func(n, a + 1, b) def main(): n, m = map(int, input().split()) func(n, 1, m - 1) main()
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) seq = [i for i in range(1, n + 1)] m -= 1 dig = [] digs = 0 while m > 0: dig.append(m % 2) m //= 2 digs += 1 swap = 0 sw = [0] * digs for i in range(digs - 1, -1, -1): if swap == 1: sw[i] = 1 - dig[i] else: sw[i] = dig[i] swap = (swap + sw[i]) % 2 for i in range(digs): if sw[i] == 1: seq[-i - 2 :] = seq[: -i - 3 : -1] print(" ".join(map(str, seq)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
from itertools import permutations as p n, m = map(int, input().split()) def f(p): ret = 0 for i in range(len(p)): for j in range(i, len(p)): ret += min(p[i : j + 1]) return ret def get(n, last, avail, rank): if rank == n: return sorted(avail, reverse=True) n //= 2 for x in sorted(avail)[:-1]: if x > last: if rank <= n: nx = avail[:] nx.remove(x) return [x] + get(n, x, nx, rank) rank -= n n //= 2 print(" ".join(map(str, get(1 << n - 1, 0, list(range(1, n + 1)), m))))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP LIST VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) lst = [0] * n l, r = 0, n - 1 i = 1 while r > l: if m > 2 ** (r - l - 1): lst[r] = i m -= 2 ** (r - l - 1) r -= 1 else: lst[l] = i l += 1 i += 1 lst[l] = i print(" ".join(map(str, lst)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
ip = [int(i) for i in input().split()] n, m = ip[0], ip[1] - 1 per = "0" * (n + 1 - len(bin(m))) + bin(m)[2 : len(bin(m))] p = str(n) for i in range(n - 1, 0, -1): if per[i - 1] == "0": p = str(i) + " " + p else: p = p + " " + str(i) print(p)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) def gen(n, m, now): if n == 1: return [now] if m <= 2 ** (n - 2): return [now] + gen(n - 1, m, now + 1) else: return gen(n - 1, m - 2 ** (n - 2), now + 1) + [now] p = 2 ** (n - 2) print(" ".join(map(str, gen(n, m, 1))))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN LIST VAR IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER RETURN BIN_OP LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) res = [] num = 0 x = n for i in range(1, n): if i not in res: if pow(2, x - len(res) - 2) + num >= m: res.append(i) else: num += pow(2, x - len(res) - 2) x -= 1 i = n while i > 0: if i not in res: res.append(i) i -= 1 for i in res: print(i, end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR IF BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
def find_kth(i, j, p, n, k, a): d = j - i if d == 1: a[i] = p return a if k < 1 << d - 2: a[i] = p return find_kth(i + 1, j, p + 1, n, k, a) else: a[j - 1] = p return find_kth(i, j - 1, p + 1, n, k - (1 << d - 2), a) n, m = map(int, input().split()) ans = find_kth(0, n, 1, n, m - 1, [0] * n) print(" ".join(map(str, ans)))
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR IF VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
arc = [] def sv(a, b, c, n, v): if n < c // 2: arc[a] = v if b - a > 1: sv(a + 1, b, c // 2, n, v + 1) else: arc[b - 1] = v if b - a > 1: sv(a, b - 1, c // 2, n - c // 2, v + 1) n, m = map(int, input().split()) arc = [0] * n ssc = 1 << n - 1 sv(0, n, ssc, m - 1, 1) print(" ".join(map(str, arc)))
ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
import sys def read_input(input_path=None): if input_path is None: f = sys.stdin else: f = open(input_path, "r") n, m = map(int, f.readline().split()) return n, m def sol(n, m): v = [(0) for _ in range(n + 1)] left, right = 1, n for i in range(1, n + 1): if n - i - 1 <= 0: pw = 1 else: pw = 1 << n - i - 1 if m <= pw: v[left] = i left += 1 else: v[right] = i right -= 1 m -= pw return [" ".join(map(str, v[1:]))] def solve(input_path=None): return sol(*read_input(input_path)) def main(): for line in sol(*read_input()): print(f"{line}") main()
IMPORT FUNC_DEF NONE IF VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR RETURN LIST FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) out = [n] i = n - 1 m -= 1 for _ in range(n - 1): if m % 2: out.append(i) else: out = [i] + out m //= 2 i -= 1 for i in out: print(i, end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split(" ")) cnt = 2 ** (n - 1) L = [-1] * n left = 0 right = n - 1 for x in range(1, n + 1): if m <= cnt // 2: L[left] = x left += 1 else: L[right] = x right -= 1 m -= cnt // 2 cnt //= 2 for i in L: print(i, end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
inp = input().split(" ") n = int(inp[0]) m = bin(int(inp[1]) - 1)[2:] lenbin = n - 1 while len(m) < lenbin: m = "0" + m start = [] end = [] for x in range(n - 1): if m[x] == "0": start = start + [x + 1] else: end = [x + 1] + end final = start + [n] + end for x in final: print(x, end=" ")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR LIST BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR LIST VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
def main(): n, m = map(int, input().split()) m -= 1 aa = [0] * n lo, hi = 0, n - 1 t = 2 ** (n - 2) for i in range(1, n + 1): if m < t: aa[lo] = i lo += 1 else: aa[hi] = i hi -= 1 m -= t t //= 2 print(*aa) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
def fun(n, m): m -= 1 solu = [0] * n l = n - 1 po = l - 1 o = 0 for i in range(n): if m >= pow(2, po): solu[l] = i + 1 m -= pow(2, po) l -= 1 else: solu[o] = i + 1 o += 1 po -= 1 print(*solu) def fun2(n, m): m -= 1 solu = [0] * n l = n - 1 po = l - 1 o = 0 for i in range(n - 1): if m >= 1 << po: solu[l] = i + 1 m -= 1 << po l -= 1 else: solu[o] = i + 1 o += 1 po -= 1 solu[o] = n print(*solu) temp = [int(x) for x in input().split()] n = temp[0] m = temp[1] fun2(n, m)
FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum: <image> Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p). Input The single line of input contains two integers n and m (1 ≤ m ≤ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. * In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold. * In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. Output Output n number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
n, m = map(int, input().split()) if n == 1: print(1) exit() m -= 1 perm = [0] * n k = 2 ** (n - 2) cleft = 0 cright = n - 1 for i in range(1, n + 1): if m & k: perm[cright] = i cright -= 1 else: perm[cleft] = i cleft += 1 k //= 2 print(" ".join(map(str, perm)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number x to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals x? If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket. Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on. Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him. -----Input----- The first line contains number m (1 ≤ m ≤ 2000), showing how many numbers are scattered around the room. The next m lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10^600 that doesn't contain leading zeroes. -----Output----- For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer k showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once. -----Examples----- Input 7 7 6 5 4 3 2 1 Output 0 0 0 3 0 1 2 2 1 2 2 0 2 2 0 1 Input 2 5 5 Output 0 1 0 -----Note----- The XOR sum of numbers is the result of bitwise sum of numbers modulo 2.
m = int(input()) b = [] k = [] for i in range(m): x = int(input()) c = 0 for j in range(len(b)): v = b[j] d = k[j] if x ^ v < x: x ^= v c ^= d if x != 0: print(0) c ^= 2**i b.append(x) k.append(c) else: a = [] for j in range(m): if c & 1 == 1: a.append(j) c >>= 1 print(len(a), end="") for v in a: print(" ", v, sep="", end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR STRING VAR STRING STRING EXPR FUNC_CALL VAR
After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number x to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals x? If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket. Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on. Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him. -----Input----- The first line contains number m (1 ≤ m ≤ 2000), showing how many numbers are scattered around the room. The next m lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10^600 that doesn't contain leading zeroes. -----Output----- For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer k showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once. -----Examples----- Input 7 7 6 5 4 3 2 1 Output 0 0 0 3 0 1 2 2 1 2 2 0 2 2 0 1 Input 2 5 5 Output 0 1 0 -----Note----- The XOR sum of numbers is the result of bitwise sum of numbers modulo 2.
n = int(input()) t = [(0) for i in range(2000)] c = [(0) for i in range(2000)] for i in range(n): x = int(input()) r = 0 ok = False for j in range(2000): if x >> j & 1: if t[j] != 0: x ^= t[j] r ^= c[j] else: t[j] = x c[j] = r ^ 1 << i ok = True break if ok: print(0) continue a = [] for j in range(2000): if r >> j & 1: a.append(j) print(len(a)) for y in a: print(y)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number x to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals x? If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket. Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on. Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him. -----Input----- The first line contains number m (1 ≤ m ≤ 2000), showing how many numbers are scattered around the room. The next m lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10^600 that doesn't contain leading zeroes. -----Output----- For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer k showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once. -----Examples----- Input 7 7 6 5 4 3 2 1 Output 0 0 0 3 0 1 2 2 1 2 2 0 2 2 0 1 Input 2 5 5 Output 0 1 0 -----Note----- The XOR sum of numbers is the result of bitwise sum of numbers modulo 2.
m = int(input()) values = [] idx = [] for i in range(m): x = int(input()) ans = 0 for j, xx in enumerate(values): if xx ^ x < x: x ^= xx ans ^= idx[j] if x == 0: anss = [] for j in range(i): if ans & 1 != 0: anss.append(j) ans >>= 1 print(len(anss), *anss) else: print(0) values.append(x) idx.append(ans ^ 2**i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number x to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals x? If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket. Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on. Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him. -----Input----- The first line contains number m (1 ≤ m ≤ 2000), showing how many numbers are scattered around the room. The next m lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10^600 that doesn't contain leading zeroes. -----Output----- For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer k showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once. -----Examples----- Input 7 7 6 5 4 3 2 1 Output 0 0 0 3 0 1 2 2 1 2 2 0 2 2 0 1 Input 2 5 5 Output 0 1 0 -----Note----- The XOR sum of numbers is the result of bitwise sum of numbers modulo 2.
n = int(input()) b = [] bb = [] for i in range(n): x = int(input()) idx = 0 for j in range(len(b)): nxt = b[j] ^ x if nxt < x: x = nxt idx ^= bb[j] if x == 0: cnt = 0 v = [] for k in range(2000): if idx & 1 << k: v.append(k) print(len(v), end=" ") for e in v: print(e, end=" ") print() else: print(0) idx ^= 1 << i b.append(x) bb.append(idx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number x to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals x? If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket. Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on. Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him. -----Input----- The first line contains number m (1 ≤ m ≤ 2000), showing how many numbers are scattered around the room. The next m lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10^600 that doesn't contain leading zeroes. -----Output----- For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer k showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once. -----Examples----- Input 7 7 6 5 4 3 2 1 Output 0 0 0 3 0 1 2 2 1 2 2 0 2 2 0 1 Input 2 5 5 Output 0 1 0 -----Note----- The XOR sum of numbers is the result of bitwise sum of numbers modulo 2.
buck = [[0, 0] for i in range(2201)] m = int(input()) for i in range(m): a = int(input()) ok = True br = 0 for j in range(2200, -1, -1): if a & 1 << j: if buck[j][0]: a ^= buck[j][0] br ^= buck[j][1] else: ok = False buck[j][0] = a buck[j][1] = br | 1 << i break if not ok: print("0") else: lst = [] for j in range(2201): if br & 1 << j: lst.append(j) print(len(lst), end=" ") for j in lst: print(j, end=" ") print("\n", end="")
ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING
You are given an array A, consisting of N distinct integers. Calculate number of pairs (i,j) (1 ≤ i < j ≤ N), such that 2 \cdot (A_{i} \oplus A_{j}) = A_{i} + A_{j}, where \oplus denotes [bitwise XOR]. ------ Input Format ------ - The first line contains two integers N - the size of the array. - The second line contains N space-separated integers A_{1}, \ldots, A_{N} - the elements of the array A. ------ Output Format ------ For each test case, output the number of pairs satisfying constraints. ------ Constraints ------ $2 ≤ N ≤ 10^{6}$ $1 ≤ A_{i} ≤ 2^{60}-1$ ------ subtasks ------ Subtask $1$ (20 points): $2 ≤ N ≤ 2000$ Subtask $2$ (40 points): $1 ≤ A_{i} ≤ 2^{20}-1$ Subtask $3$ (40 points): Original constraints. ----- Sample Input 1 ------ 4 1 2 3 6 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ There are $2$ pairs $(i,j)$ satisfying all the conditions. - $(i,j)= (1, 3)$. Thus, $2\cdot (A_{1}\oplus A_{3}) = 2\cdot (1\oplus 3) = 2\cdot 2 = 4$. Also, $A_{1}+A_{3} = 1+3 = 4$. - $(i,j)= (2, 4)$. Thus, $2\cdot (A_{2}\oplus A_{4}) = 2\cdot (2\oplus 6) = 2\cdot 4 = 8$. Also, $A_{2}+A_{4} = 2+6 = 8$. ----- Sample Input 2 ------ 5 4 3 9 7 1 ----- Sample Output 2 ------ 1 ----- explanation 2 ------ There is only one pair $(i,j)$ satisfying all the conditions: - $(i,j) = (2, 5)$. Thus, $2\cdot (A_{2}\oplus A_{5}) = 2\cdot (3\oplus 1) = 2\cdot 2 = 4$. Also, $A_{2}+A_{5} = 3+1 = 4$.
def do_bitwise_xor(x, y): return (x | y) & (~x | ~y) def split_list_to_even_odd(A: list): A_EVEN = [] A_ODD = [] for item in A: if item % 2 == 0: A_EVEN.append(item) else: A_ODD.append(item) return A_EVEN, A_ODD def get_xor_times_two_is_sum_count(A: list): length = len(A) count = 0 if length == 0: return count for i in range(length): for j in range(i + 1, length): if 2 * do_bitwise_xor(A[i], A[j]) == A[i] + A[j]: count += 1 return count def xor2ptnr(val): sb = ["0"] + list(bin(val)[2:]) sx = len(sb) while sx: sx -= 1 if sb[sx] == "1": sx -= 1 sb[sx] = "1" if sb[sx] == "0" else "0" return int("".join(sb), 2) N = int(input()) Ays = set(map(int, input().split())) print( len( Ays.intersection(xor2ptnr(a) for a in Ays if not "1" in bin(a).rsplit("11")[-1]) ) )
FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR STRING STRING STRING RETURN FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER
You are given an array A, consisting of N distinct integers. Calculate number of pairs (i,j) (1 ≤ i < j ≤ N), such that 2 \cdot (A_{i} \oplus A_{j}) = A_{i} + A_{j}, where \oplus denotes [bitwise XOR]. ------ Input Format ------ - The first line contains two integers N - the size of the array. - The second line contains N space-separated integers A_{1}, \ldots, A_{N} - the elements of the array A. ------ Output Format ------ For each test case, output the number of pairs satisfying constraints. ------ Constraints ------ $2 ≤ N ≤ 10^{6}$ $1 ≤ A_{i} ≤ 2^{60}-1$ ------ subtasks ------ Subtask $1$ (20 points): $2 ≤ N ≤ 2000$ Subtask $2$ (40 points): $1 ≤ A_{i} ≤ 2^{20}-1$ Subtask $3$ (40 points): Original constraints. ----- Sample Input 1 ------ 4 1 2 3 6 ----- Sample Output 1 ------ 2 ----- explanation 1 ------ There are $2$ pairs $(i,j)$ satisfying all the conditions. - $(i,j)= (1, 3)$. Thus, $2\cdot (A_{1}\oplus A_{3}) = 2\cdot (1\oplus 3) = 2\cdot 2 = 4$. Also, $A_{1}+A_{3} = 1+3 = 4$. - $(i,j)= (2, 4)$. Thus, $2\cdot (A_{2}\oplus A_{4}) = 2\cdot (2\oplus 6) = 2\cdot 4 = 8$. Also, $A_{2}+A_{4} = 2+6 = 8$. ----- Sample Input 2 ------ 5 4 3 9 7 1 ----- Sample Output 2 ------ 1 ----- explanation 2 ------ There is only one pair $(i,j)$ satisfying all the conditions: - $(i,j) = (2, 5)$. Thus, $2\cdot (A_{2}\oplus A_{5}) = 2\cdot (3\oplus 1) = 2\cdot 2 = 4$. Also, $A_{2}+A_{5} = 3+1 = 4$.
def patt(n): s = format(n, "b")[1:] i = len(s) - 1 while i >= 0: if s[i] == "1": if i - 1 < 0: return False i -= 2 else: i -= 1 return True def rev(n): s = format(n, "b")[1:] ans = "" i = len(s) - 1 while i >= 0: if s[i] == "1": if s[i - 1] == "1": ans = "01" + ans else: ans = "11" + ans i -= 2 else: ans = "0" + ans i -= 1 ans = "11" + ans return int(ans, 2) n = int(input()) li = list(map(int, input().split())) li.sort() mapp = {} ans = 0 for i in range(len(li) - 1, -1, -1): p = patt(li[i]) if p and rev(li[i]) in mapp: ans += 1 mapp[li[i]] = True print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP STRING VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR RETURN FUNC_CALL VAR VAR NUMBER 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 ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
M = int(1000000000.0 + 7) T = int(input()) for i in range(T): L, R = map(int, input().split()) ans = 0 if L == R: ans = L % M else: b1 = bin(L)[2:] b2 = bin(R)[2:] val = -1 if len(b2) == len(b1): for j in range(len(b1)): if b1[j] != b2[j]: val = j break cur = L zl = len(b1) - 1 last = 0 ans = L % M cv = 1 while zl > val: if b1[zl] == "0": ans = (ans + cur % M * cv % M) % M else: cur -= cv cv <<= 1 zl -= 1 if val != -1: pp = b2[val + 1 :] cv = int(pp, 2) + 1 ans = (ans + cur % M * cv % M) % M print(ans % M)
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER 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 NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
mod = 10**9 + 7 for i in range(int(input())): l, r = [int(i) for i in input().split()] if l == r: print(l % mod) continue res = 0 diff = r - l + 1 i = 1 j = 1 temp = l while temp: if temp & 1: temp2 = l & i k = min(r + 1 - l, i + 1 - temp2) res = (res + j * k) % mod temp = temp // 2 i = i * 2 + 1 j = j * 2 print(res % mod)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
for _ in range(int(input())): l, r = map(int, input().split()) binary = bin(l)[2:] a, b, s = 1, 0, 0 for i in range(len(binary) - 1, -1, -1): if binary[i] == "1": s += a * min(a - b, r - l + 1) s %= 1000000007 b += a a *= 2 print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
m = [1] mod = 10**9 + 7 for i in range(60): a = m[-1] a *= 2 a %= mod m.append(a) for i in range(int(input())): L, R = map(int, input().split()) tmp = 0 l = bin(L)[2:] r = bin(R)[2:] a = 1 ans = L % mod for i in range(1, len(l) + 1): if l[-i] == "0": if tmp + 2 ** (i - 1) <= R - L: ans += m[i - 1] * (L - a + 1) % mod tmp += 2 ** (i - 1) ans %= mod else: ans += (R - L - tmp) * (L - a + 1) % mod ans %= mod break else: a += m[i - 1] print(ans)
ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
for _ in range(int(input())): l, r = map(int, input().split()) a = 1 b = 0 s = 0 x = l binary = "" while l > 0: binary += str(l % 2) l //= 2 for i in binary: if i == "1": s += a * min(a - b, r - x + 1) % 1000000007 s %= 1000000007 b += a a *= 2 print(s)
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR STRING VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
MOD = 10**9 + 7 for _ in range(int(input())): l, r = map(int, input().split()) max_no_elm = r - l + 1 ans, multi, l_p, r_p = 0, 1, l, 0 while l_p != 0: curr_bit = l_p & 1 if curr_bit == 1: no_elm = multi - r_p ans = (ans + multi * min(no_elm, max_no_elm)) % MOD l_p, r_p = l_p >> 1, r_p + multi * curr_bit multi = multi << 1 print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER 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 BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
l = [] for i in range(62): l.append(2**i) T = int(input()) flag = 0 for t in range(T): L, R = [int(i) for i in input().split()] bL = bin(L) lL = len(bL) - 2 index = 1 ans = 0 temp = 0 while index <= lL: temp = L % l[index] if temp >= l[index - 1]: if l[index] - temp <= R - L + 1: ans = (ans + l[index - 1] * (l[index] - temp)) % 1000000007 else: ans = (ans + l[index - 1] * (R - L + 1)) % 1000000007 index += 1 print(ans)
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER 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 ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
t = int(input()) while t > 0: a, b = list(map(int, input().split())) k = 1 m = a n = b sum = 0 x = 0 y = 1 while a > 0: a = a // 2 l = m // k if l % 2 != 0: p = k * l q = k * (l + 1) else: p = k * (l + 1) q = k * (l + 2) k *= 2 if m >= p and m < q: if b < q: o = b - m + 1 lum = o % 1000000007 * (y % 1000000007) % 1000000007 sum = sum + lum sum = sum % 1000000007 else: o = q - m lum = o % 1000000007 * (y % 1000000007) % 1000000007 sum = sum + lum sum = sum % 1000000007 y *= 2 print(sum) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
MVAL = 1000000007 def rangeand(low, hi): odf = hi - low + 1 ra = 0 p2 = 1 lmd = 0 while low & p2 == 0: p2 *= 2 pmdf = p2 if pmdf >= odf: return odf * low % MVAL while p2 <= low: if low & p2: lmd += p2 ra += pmdf * p2 % MVAL else: pmdf += p2 if pmdf >= odf: ra += odf * (low - lmd) % MVAL break p2 *= 2 return ra % MVAL t = int(input()) ans = [] for ti in range(t): l, r = map(int, input().split()) ans.append(rangeand(l, r)) print(*ans, sep="\n")
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
for i in range(int(input())): l, r = map(int, input().split()) binl = bin(l)[2:] diff = r - l + 1 ans, ans1, ans2 = 0, 0, 0 for j in range(len(binl)): if diff <= 0: break if binl[len(binl) - j - 1] == "1": x = 2**j ans += ( (l - ans2) % (10**9 + 7) * min(diff, x - ans1) % (10**9 + 7) % (10**9 + 7) ) ans %= 10**9 + 7 diff -= x - ans1 ans1 = 2 * x ans2 += x print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
t = int(input()) m = 10**9 + 7 while t: p = 0 res = 0 l, r = map(int, input().split()) d = r - l + 1 br = bin(r)[2:] lbr = len(br) l = bin(l)[2:] k = -1 lol = 0 ll = len(l) ind = ll - 1 if 2**ll > r: for i in range(ll): if l[i] == "0" and br[i] == "1": ind = i break for i in range(len(l) - 1, ind, -1): k += 1 if l[i] == "1": a = 2**k p += a res += (min(r + 1, 2 ** (k + 1)) - p) * a for i in range(ind + 1): if l[i] == "1": res += 2 ** (ll - i - 1) * d else: for i in range(len(l) - 1, -1, -1): k += 1 if l[i] == "1": a = 2**k p += a res += (min(r + 1, 2 ** (k + 1)) - p) * a print(res % m) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
mod = 10**9 + 7 for _ in range(int(input())): l, r = map(int, input().split()) a1 = bin(l)[2:] if l == 1: print(1) else: k = [] k1 = [] su = 0 if a1[-1] == "1": k.append(1) k1.append(1) su = 1 p = len(a1) ca = 2 for i in range(p - 2, -1, -1): if a1[i] == "1": k.append(ca) k1.append(ca - su) su += ca ca *= 2 re = r - l + 1 ans = 0 for i in range(len(k)): ans += k[i] % mod * min(re, k1[i]) % mod % mod ans %= mod print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER 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 VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
mod = 1000000007 t = int(input()) while t: l, r = map(int, input().split()) ans = 0 if l == r: print(l % mod) else: temp = l i = 1 j = 1 while temp: if temp & 1: tp2 = l & i k = min(r - l + 1, i - tp2 + 1) ans = (ans + j * k) % mod i <<= 1 i += 1 j <<= 1 temp >>= 1 print(ans % mod) t -= 1
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
modulo = int(1000000000.0) + 7 for t in range(int(input())): l, r = map(int, input().split()) curr = 1 ans = 0 for i in range(61): q = l // curr if q & 1: end = min(r, (q + 1) * curr - 1) ans = (ans + curr * (end - l + 1) % modulo % modulo) % modulo curr *= 2 print(ans)
ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
t = int(input()) mod = 10**9 + 7 for i in range(t): a, b = (int(x) for x in input().split()) sum = a check = 2 while check <= a: rem = a // check to = (rem + 1) * check - a - 1 if to > b - a: to = b - a if rem % 2 != 0: sum = sum + to * (check % mod) sum = sum % mod check = check * 2 print(sum % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
mod = 10**9 + 7 t = int(input()) while t > 0: l, r = map(int, input().split()) ans = 0 for i in range(63): if l & 1 << i: x = (1 << i) - (l & (1 << i) - 1) x = min(x, r - l + 1) ans += x * (1 << i) print(ans % mod) t = t - 1
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
e = 10**9 + 7 t = int(input()) for _ in range(t): a, b = map(int, input().split()) ans = 0 s = "{0:0b}".format(a) s = s[-1::-1] for i in range(len(s)): if s[i] == "1": d = pow(2, i) x = a % d m = d - x n = b - a + 1 ans = (ans + d * min(m, n)) % e print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER 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 NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR 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 BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
MOD = 1000000007 a = int(input()) for i in range(a): x, y = map(int, input().split()) start = x end = y ans = x prev = x z = x j = -1 while start != 0: j += 1 if z & 1 != 0: add = 1 << j next = start + add if next > end: ans = ans + (end - prev) * start break ans = ans + ((next - prev - 1) * start + (start - add)) ans %= MOD start -= add if next == end: break prev = next z = z >> 1 print(ans % MOD)
ASSIGN 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 VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
MOD = 10**9 + 7 II = lambda: [int(x) for x in input().split()] I2 = lambda: int(input()) T = int(input()) for _ in range(T): L, R = (int(x) for x in input().split()) sum = 0 max_R1 = 1 while max_R1 <= L: max_R1 *= 2 val = int(max_R1 / 2) max_R1 -= 1 while val >= 1: if L & val: R = min(R, max_R1) a = R - L + 1 count = min(val, a) % MOD sum += val * count % MOD L = L - val R -= val max_R1 -= val val = int(val / 2) print(sum % MOD)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
MOD = int(10**9) + 7 for _ in range(int(input())): L, R = map(int, input().split()) ans = 0 for i in range(0, 61): pw = 1 << i if L & pw == 0: continue diff = pw - 1 - (L & pw - 1) + 1 ans += min(diff, R - L + 1) % MOD * (pw % MOD) % MOD ans %= MOD ans %= MOD print(ans)
ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER NUMBER 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
for t in range(int(input())): c, c1, summ = 0, 0, 0 M = 1000000007 l, r = list(map(int, input().split())) for i in range(60): if l & 1 << i: summ += min(r - l + 1, c1 - c + 1) % M * (1 << i) % M % M c += 1 << i c1 += 1 << i print(summ % M)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
t = int(input()) p = pow(10, 9) + 7 p = int(p) while t: t -= 1 l, r = map(int, input().split()) f = bin(l)[2:] po = 1 s = 0 res = 0 for i in range(len(f) - 1, -1, -1): if f[i] == "1": res += po * min(po - s, r - l + 1) res = res % p s += po po = po * 2 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
MOD = 10**9 + 7 T = int(input()) for _ in range(T): l, r = map(int, input().split()) bl = bin(l)[2:] sum = 0 val = 1 s = 0 for i in range(len(bl) - 1, -1, -1): if bl[i] == "1": sum += val * min(val - s, r - l + 1) sum = sum % MOD s += val val *= 2 print(sum)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER 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 VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
t = int(input()) while t: t -= 1 l, r = [int(i) for i in input().split()] bl = bin(l)[2:] bl = bl[::-1] sub = 0 val = 1 ans = 0 mod = 10**9 + 7 for i in range(len(bl)): if bl[i] == "1": ans += val * min(val - sub, r - l + 1) % mod sub += val val *= 2 print(ans % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
MVAL = 1000000007 def rangeand(low, hi): ra = 0 p2 = 1 while p2 <= low: ld, lm = divmod(low, p2 * 2) hd, hm = divmod(hi, p2 * 2) if ld == hd: ra += (hm - lm + 1) * (p2 & lm) % MVAL else: ra += (p2 * 2 - lm) * (p2 & lm) % MVAL p2 *= 2 return ra % MVAL t = int(input()) for ti in range(t): l, r = map(int, input().split()) print(rangeand(l, r))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given positive integers $L$ and $R$. You have to find the sum S=∑i=LR(L∧(L+1)∧…∧i),S=∑i=LR(L∧(L+1)∧…∧i),S = \sum_{i=L}^R \left(L \wedge (L+1) \wedge \ldots \wedge i\right) \,, where $\wedge$ denotes the bitwise AND operation. Since the sum could be large, compute it modulo $10^9+7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first and only line of each test case contains two space-separated integers $L$ and $R$. -----Output----- For each test case, print a single line containing one integer — the sum $S$ modulo $10^9+7$. -----Constraints----- - $1 \le T \le 10^5$ - $1 \le L \le R \le 10^{18}$ -----Example Input----- 2 1 4 4 10 -----Example Output----- 1 16 -----Explanation----- Example case 1: The sum is 1 + 1 AND 2 + 1 AND 2 AND 3 + 1 AND 2 AND 3 AND 4 = 1 + 0 + 0 + 0 = 1. Example case 2: The sum is 4 + 4 AND 5 + 4 AND 5 AND 6 + 4 AND 5 AND 6 AND 7 + … + 4 AND 5 AND … AND 10 = 4 + 4 + … + 0 = 16.
mod = int(1000000000.0) + 7 t = int(input()) while t: t -= 1 l, r = map(int, input().split()) binl = bin(l)[:1:-1] f, cnt, ans = 1, 0, 0 for i in binl: if i == "1": ans = (ans + f * min(f - cnt, r - l + 1)) % mod cnt += f f *= 2 print(ans)
ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR