description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def countBits(self, N): def findTwoPower(N): x = 0 while 1 << x <= N: x += 1 return x - 1 if N <= 1: return N x = findTwoPower(N) return x * int(2 ** (x - 1)) + (N - (1 << x) + 1) + self.countBits(N - (1 << x)) if __name__ == "__main__": t = int(input()) for _ in range(t): N = int(input()) ob = Solution() print(ob.countBits(N))
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
def power(n): x = 0 while 2**x <= n: x += 1 return x - 1 class Solution: def countBits(self, n): if n == 0: return 0 if n == 3: return 4 x = power(n) ans = x * 2 ** (x - 1) + (n - 2**x + 1) + self.countBits(n - 2**x) ans1 = int(ans) return ans1
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def powerof2(self, n): x = 1 c = 0 while x <= n: p = x x = x << 1 c += 1 return [p, c - 1] def countBits(self, n): if n == 0: return 0 l = self.powerof2(n) ans = (l[0] >> 1) * l[1] + (n - l[0] + 1) + self.countBits(n - l[0]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN LIST VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def countBits(self, N): ans = 0 tot_sets = 0 N1 = N while N1: tot_sets += 1 N1 &= N1 - 1 ans += tot_sets for i in range(32): if i == 0 and N & 1 << i: tot_sets -= 1 ans += tot_sets elif N & 1 << i: tot_sets -= 1 ans += 2 ** (i - 1) * i + 2**i * tot_sets return int(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN FUNC_CALL VAR VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def countBits(self, n): if n == 0: return 0 if n == 1: return 1 m, k = 1, 0 while n >= m: m = m << 1 k = k + 1 m = m >> 1 k = k - 1 return k * (m >> 1) + (n - m + 1) + self.countBits(n - m)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def countBits(self, N): if N <= 0: return 0 elif N == 1: return 1 x = 0 while 1 << x <= N: x += 1 x -= 1 return (1 << x - 1) * x + (N - (1 << x) + 1) + self.countBits(N - (1 << x))
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def countBits(self, N): if N == 0: return 0 p = 0 ans = 0 while pow(2, p) <= N: p += 1 p -= 1 ans += p * pow(2, p) // 2 + N - pow(2, p) + 1 + self.countBits(N - pow(2, p)) return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR RETURN VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def largestPowerOfTwoInRange(self, N): power = 0 while pow(2, power) <= N: power = power + 1 return power - 1 def countSetBits(self, num): setBitsCount = 0 while num != 0: bit = num & 1 if bit == 1: setBitsCount = setBitsCount + 1 num = num >> 1 return setBitsCount def countBits(self, N): if N == 0: return 0 x = self.largestPowerOfTwoInRange(N) ans = pow(2, x - 1) * x + (N - pow(2, x) + 1) + self.countBits(N - pow(2, x)) ans = int(ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def countBits(self, n): ans = 0 for i in range(32): part1 = n + 1 >> i + 1 << i part2 = n + 1 - (1 << i) - (n + 1 >> i + 1 << i + 1) if part2 > 0: ans += part1 + part2 else: ans += part1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def countBits(self, N): def findpow(n): x = 0 while 1 << x <= n: x = x + 1 return x - 1 def ans(n): if n <= 1: return n x = findpow(n) return x * pow(2, x - 1) + (n - pow(2, x) + 1) + ans(n - pow(2, x)) return ans(N)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR RETURN FUNC_CALL VAR VAR
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def countBits(self, N): return self.count1(N) def count1(self, A): if A == 0 or A == 1: return A x = self.po(A) ans = x * (1 << x - 1) + (A - (1 << x) + 1) + self.count1(A - (1 << x)) return ans def po(self, n): c = 1 while 1 << c <= n: c += 1 return c - 1
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER
You are given a number N. Find the total number of setbits in the numbers from 1 to N. Example 1: Input: N = 3 Output: 4 Explaination: 1 -> 01, 2 -> 10 and 3 -> 11. So total 4 setbits. Example 2: Input: N = 4 Output: 5 Explaination: 1 -> 01, 2 -> 10, 3 -> 11 and 4 -> 100. So total 5 setbits. Your Task: You do not need to read input or print anything. Your task is to complete the function countBits() which takes N as input parameter and returns the total number of setbits upto N. Expected Time Complexity: O(logN) Expected Auxiliary Space: O(1) Constraints: 1 ≤ N ≤ 10^{6}
class Solution: def countBits(self, n): if n == 0: return 0 def find_x(n): x = 0 while 2**x <= n: x += 1 return x - 1 x = find_x(n) remaining = self.countBits(n - 2**x) ans = 2 ** (x - 1) * x + (n - 2**x + 1) + remaining return int(ans)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR RETURN FUNC_CALL VAR VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): a = 0 for j in range(32): c = 0 for i in arr: if i >> j & 1 == 1: c += 1 a += 2 * c * (len(arr) - c) return a
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): ans = 0 for i in range(32): c1, c2 = 0, 0 for j in range(n): if arr[j] & 1 << i > 0: c1 += 1 else: c2 += 1 ans += c1 * c2 return ans * 2
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): res = [0] * 32 for i in arr: k = 0 while i: if i & 1: res[k] += 1 k += 1 i >>= 1 s = 0 n = len(arr) for i in res: if i: s += (n - i) * i * 2 return s
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): n = len(arr) sum_bit_diff = 0 for i in range(32): count_set_bits = 0 count_clear_bits = 0 for j in range(n): if arr[j] & 1 << i: count_set_bits += 1 else: count_clear_bits += 1 sum_bit_diff += count_set_bits * count_clear_bits * 2 return sum_bit_diff
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): res = 0 for i in range(32): count1s = 0 for x in arr: if x & 1 << i != 0: count1s += 1 count0s = len(arr) - count1s bitDiff = count1s * 2 * count0s res += bitDiff return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, A, N): cnt = 0 maxi = max(A) z = len(bin(maxi)[2:]) li = [bin(x)[2:].zfill(z) for x in A] l = [(0) for i in range(z)] for x in li: i = 0 for ch in x: if ch == "0": l[i] += 1 i += 1 for x in l: cnt += 2 * x * (N - x) return cnt
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): arr = list(arr) m = r = 0 for i in range(max(arr).bit_length()): m = 0 for j in arr: if j >> i & 1 == 1: m += 1 r += 2 * m * (len(arr) - m) return r
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
def diff(a, b): x = a ^ b c = 0 while x: c += 1 x = x & x - 1 return c class Solution: def sumBitDifferences(self, arr, n): m = len(bin(max(arr))) - 2 l0 = [0] * m l1 = [0] * m for x in arr: for i in range(m): if x & 1: l1[i] += 1 else: l0[i] += 1 x >>= 1 c = 0 for x in range(m): c += l0[x] * l1[x] return c << 1
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): d = [] ans = 0 for i in range(0, 31): c = 0 for j in arr: if j & 1 << i: c += 1 d.append(c) for i in d: ans += 2 * (i * (n - i)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): main_count = 0 c = 0 zero = 0 count = 0 for k in range(32): count = 0 for i in range(n): if arr[i] >> k & 1 == 1: count += 1 zero = n - count main_count += zero * count return main_count * 2
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): sum = 0 v = max(arr) for i in range(v.bit_length()): setbits = 0 for j in arr: if j >> i & 1: setbits += 1 sum += 2 * setbits * (n - setbits) return sum
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): sum = 0 for i in range(32): setbits = 0 for j in arr: k = j >> i if k & 1: setbits += 1 sum += 2 * setbits * (n - setbits) return sum
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): l = len(arr) mx = max(arr) bitlen = mx.bit_length() k = 0 for i in range(bitlen): c = 0 for j in arr: if j >> i & 1: c += 1 k += 2 * c * (l - c) return k
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
NOOFBITS = 17 class Solution: def sumBitDifferences(self, arr, siz): bitsum = [0] * NOOFBITS for n in arr: j = 0 while n > 0: bitsum[j] += n & 1 n >>= 1 j += 1 total = 0 for noofones in bitsum: noofzeroes = siz - noofones total += noofones * noofzeroes * 2 return total
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given an integer array of N integers, find sum of bit differences in all pairs that can be formed from array elements. Bit difference of a pair (x, y) is count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 (first and last bits differ in two numbers). Note: (x, y) and (y, x) are considered two separate pairs. Example 1: Input: N = 2 arr[] = {1, 2} Output: 4 Explanation: All pairs in array are (1, 1) (1, 2), 2, 1), (2, 2) Sum of bit differences = 0 + 2 + 2 + 0 = 4 Example 2: Input: N = 3 arr[] = {1, 3, 5} Output: 8 Explanation: All pairs in array are (1, 1), (1, 3), (1, 5), (3, 1), (3, 3) (3, 5),(5, 1), (5, 3), (5, 5) Sum of bit differences = 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0 = 8 Your Task: You don't need to read input or print anything. Your task is to complete the function sumBitDifferences() which takes the array arr[] and n as inputs and returns an integer denoting the answer. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 1 <= arr[i] <= 10^{5}
class Solution: def sumBitDifferences(self, arr, n): res = 0 for i in range(32): count = 0 for j in range(n): if arr[j] & 1 << i: count += 1 res += count * (n - count) * 2 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: def have_keys(status, key_id): return status & 2**key_id sx, sy = -1, -1 key_number = 0 key_idx = "abcdef" lock_idx = "ABCDEF" n, m = len(grid), len(grid[0]) for i in range(n): for j in range(m): if grid[i][j] == "@": sx, sy = i, j elif grid[i][j] in key_idx: key_number = max(key_number, key_idx.find(grid[i][j])) final_ownkey = 2 ** (key_number + 1) - 1 head, queue = 0, [(sx, sy, 0)] moves = [(0, 1), (0, -1), (1, 0), (-1, 0)] visited = {(sx, sy, 0): 0} while head < len(queue): x, y, status = queue[head] step = visited[queue[head]] for dx, dy in moves: tx, ty, new_status = x + dx, y + dy, status if n > tx >= 0 and m > ty >= 0: if grid[tx][ty] == "#": continue if grid[tx][ty] in lock_idx and not have_keys( new_status, lock_idx.find(grid[tx][ty]) ): continue if grid[tx][ty] in key_idx and not have_keys( new_status, key_idx.find(grid[tx][ty]) ): new_status += 2 ** key_idx.find(grid[tx][ty]) if new_status == final_ownkey: return step + 1 if (tx, ty, new_status) not in visited: queue.append((tx, ty, new_status)) visited[tx, ty, new_status] = step + 1 head += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR STRING IF VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: res, dire, all_keys = 0, [[0, 1], [1, 0], [0, -1], [-1, 0]], 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == "@": loc = i, j if grid[i][j] in "abcdef": all_keys += 1 cur, seen = set([(loc, ())]), set([(loc, ())]) def checker(loc, keys, di): x, y = loc[0] + di[0], loc[1] + di[1] if 0 <= x < len(grid) and 0 <= y < len(grid[0]): t = grid[x][y] if t in "@." or t in keys or t in "ABCDEF" and str.lower(t) in keys: return ((x, y), keys), False if t in "abcdef": newkeys = tuple(sorted(set(list(keys) + [t]))) if len(newkeys) == all_keys: return True, True return ((x, y), newkeys), False return False, False while cur: res, tmp = res + 1, [] for node in cur: loc, keys = node for di in dire: new_node, state = checker(loc, keys, di) if state: return res if new_node not in seen and new_node: tmp += [new_node] cur = set(tmp) seen |= cur return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST VAR FUNC_CALL VAR LIST VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR STRING VAR VAR VAR STRING FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR LIST VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER NUMBER RETURN VAR VAR VAR NUMBER RETURN NUMBER NUMBER WHILE VAR ASSIGN VAR VAR BIN_OP VAR NUMBER LIST FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR RETURN VAR IF VAR VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: m, n = len(grid), len(grid[0]) q = collections.deque() seen = set() tot_keys = 0 for i, row in enumerate(grid): for j, val in enumerate(row): if val == "@": q.append((i, j, 0)) seen.add((i, j, 0)) elif val in "ABCDEF": tot_keys += 1 steps = 0 while q: N = len(q) for _ in range(N): x, y, keys = q.popleft() if keys == (1 << tot_keys) - 1: return steps for nx, ny in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]: if 0 <= nx < m and 0 <= ny < n: if grid[nx][ny] == "#": continue if ( grid[nx][ny] in "ABCDEF" and not keys >> ord(grid[nx][ny]) - ord("A") & 1 ): continue nxt = keys if grid[nx][ny] in "abcdef": nxt |= 1 << ord(grid[nx][ny]) - ord("a") if (nx, ny, nxt) not in seen: seen.add((nx, ny, nxt)) q.append((nx, ny, nxt)) steps += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR VAR STRING IF VAR VAR VAR STRING BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR IF VAR VAR VAR STRING VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid) -> int: row, col = len(grid), len(grid[0]) key_num, key = 0, {} for i in range(row): for j in range(col): if grid[i][j] == "@": start_i, start_j = i, j if grid[i][j] in "abcdef" and grid[i][j] not in key: key[grid[i][j]] = key_num key_num += 1 q = [(start_i, start_j, 0, 0)] visited = set() while q: l = len(q) for _ in range(l): i, j, state, step = q.pop(0) if state == (1 << key_num) - 1: return step if (i, j, state) in visited: continue visited.add((i, j, state)) for m, n in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]: if 0 <= m < row and 0 <= n < col and grid[m][n] != "#": if grid[m][n] in "abcdef": q.append((m, n, state | 1 << key[grid[m][n]], step + 1)) elif grid[m][n] == "." or grid[m][n] == "@": q.append((m, n, state, step + 1)) elif grid[m][n] in "ABCDEF": if state >> key[grid[m][n].lower()] & 1 == 0: continue q.append((m, n, state, step + 1)) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR STRING IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR STRING IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: m, n = len(grid), len(grid[0]) k = 0 stx, sty = 0, 0 for i in range(m): for j in range(n): if grid[i][j] in "abcdef": k += 1 if grid[i][j] == "@": stx = i sty = j def keys(mask): ans = 0 for m in [1, 2, 4, 8, 16, 32]: if mask & m: ans += 1 return ans def neighbors(i, j): for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: if i + dx < 0 or i + dx >= m or j + dy < 0 or j + dy >= n: continue if grid[i + dx][j + dy] == "#": continue yield i + dx, j + dy q = collections.deque() seen = set() q.append((stx, sty, 0, 0)) while q: i, j, mask, moves = q.popleft() if grid[i][j] in "ABCDEF": id = "ABCDEF".index(grid[i][j]) if not mask & 1 << id: continue if (i, j, mask) in seen: continue seen.add((i, j, mask)) if keys(mask) == k: return moves - 1 if grid[i][j] in "abcdef": mask |= 1 << ord(grid[i][j]) - ord("a") for ii, jj in neighbors(i, j): q.append((ii, jj, mask, moves + 1)) return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR STRING EXPR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR STRING ASSIGN VAR FUNC_CALL STRING VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR VAR STRING VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: m = len(grid) n = len(grid[0]) q = collections.deque() keys = 0 visited = set() for i in range(m): for j in range(n): if grid[i][j] == "@": q.append((i, j, 0)) visited.add((i, j, 0)) elif grid[i][j] >= "a" and grid[i][j] <= "f": keys += 1 goal = (1 << keys) - 1 dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]] step = 0 while q: size = len(q) for _ in range(size): i, j, state = q.popleft() for di, dj in dirs: x = i + di y = j + dj if ( x < 0 or x >= m or y < 0 or y >= n or grid[x][y] == "#" or (x, y, state) in visited ): continue nstate = state if grid[x][y] >= "A" and grid[x][y] <= "F": temp = ord(grid[x][y]) - ord("A") if state & 1 << temp == 0: continue elif grid[x][y] >= "a" and grid[x][y] <= "f": temp = ord(grid[x][y]) - ord("a") nstate |= 1 << temp if nstate == goal: return step + 1 visited.add((x, y, nstate)) q.append((x, y, nstate)) step += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR STRING VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR STRING VAR VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR STRING VAR VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP NUMBER VAR IF VAR VAR RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: m, n = len(grid), len(grid[0]) q, seen, keys = deque(), set(), set() for i in range(m): for j in range(n): if grid[i][j] == "@": q.append((i, j, "")) seen.add((i, j, "")) elif "a" <= grid[i][j] <= "f": keys.add(grid[i][j]) steps = 0 while q: size = len(q) for _ in range(size): x, y, k = q.popleft() if len(k) == len(keys): return steps for i, j in [(0, 1), (0, -1), (1, 0), (-1, 0)]: nx, ny, nk = x + i, y + j, k if nx < 0 or nx >= m or ny < 0 or ny >= n or grid[nx][ny] == "#": continue if "A" <= grid[nx][ny] <= "F" and grid[nx][ny].lower() not in nk: continue if "a" <= grid[nx][ny] <= "f" and grid[nx][ny] not in nk: nk += grid[nx][ny] if (nx, ny, nk) not in seen: q.append((nx, ny, nk)) seen.add((nx, ny, nk)) steps += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING IF STRING VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR STRING IF STRING VAR VAR VAR STRING FUNC_CALL VAR VAR VAR VAR IF STRING VAR VAR VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: n, m = len(grid), len(grid[0]) queue = collections.deque() seen = set() nKeys = 0 moves = 0 for i in range(n): for j in range(m): if grid[i][j] == "@": queue.append([i, j, 0, 0, "abcdef.@"]) elif grid[i][j] in "abcdef": nKeys += 1 while queue: i, j, moves, curKeys, keys = queue.popleft() if grid[i][j] in "abcdef" and grid[i][j].upper() not in keys: keys += grid[i][j].upper() curKeys += 1 if curKeys == nKeys: return moves for x, y in ((-1, 0), (1, 0), (0, -1), (0, 1)): ni, nj = i + x, j + y if ( 0 <= ni < n and 0 <= nj < m and grid[ni][nj] in keys and (ni, nj, keys) not in seen ): seen.add((ni, nj, keys)) queue.append([ni, nj, moves + 1, curKeys, keys]) return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER STRING IF VAR VAR VAR STRING VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR STRING FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR FOR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: n, m = len(grid), len(grid[0]) startx, starty = -1, -1 nkeys = 0 for i in range(n): for j in range(m): if grid[i][j] == "@": startx, starty = i, j if grid[i][j] in "abcdef": nkeys += 1 queue = deque([(startx, starty, 0, "", 0)]) visited = set([(startx, starty, "")]) while queue: node = queue.popleft() x, y, nkey, keys, moves = node if nkey == nkeys: return moves for ni in [(x + 1, y), (x, y + 1), (x - 1, y), (x, y - 1)]: xx, yy = ni if xx < 0 or yy < 0 or xx >= n or yy >= m or grid[xx][yy] == "#": continue if grid[xx][yy] in "ABCDEF": if grid[xx][yy].lower() in keys and (xx, yy, keys) not in visited: queue.append((xx, yy, nkey, keys, moves + 1)) visited.add((xx, yy, keys)) elif grid[xx][yy] in "abcdef" and grid[xx][yy] not in keys: visited.add((xx, yy, keys + grid[xx][yy])) queue.append((xx, yy, nkey + 1, keys + grid[xx][yy], moves + 1)) elif (xx, yy, keys) not in visited: visited.add((xx, yy, keys)) queue.append((xx, yy, nkey, keys, moves + 1)) return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER STRING NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR FOR VAR LIST BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR STRING IF VAR VAR VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: row, col = len(grid), len(grid[0]) x, y = None, None num_keys = 0 for r in range(row): for c in range(col): if grid[r][c] == "@": x, y = r, c elif grid[r][c] in "abcdef": num_keys += 1 queue = collections.deque([(x, y, 0, 0, ".@abcdef")]) dires = [1, 0, -1, 0, 1] visited = set() while queue: r, c, steps, cnt_keys, accessible = queue.popleft() if grid[r][c] in "abcdef" and grid[r][c].upper() not in accessible: accessible += grid[r][c].upper() cnt_keys += 1 if cnt_keys == num_keys: return steps for d in range(len(dires) - 1): new_r, new_c = r + dires[d], c + dires[d + 1] if 0 <= new_r < row and 0 <= new_c < col and grid[r][c] in accessible: if (new_r, new_c, accessible) not in visited: visited.add((new_r, new_c, accessible)) queue.append((new_r, new_c, steps + 1, cnt_keys, accessible)) return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NONE NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR STRING FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: sr, sc = 0, 0 k, m, n = 0, len(grid), len(grid[0]) key_id = collections.defaultdict(int) for i, x in enumerate("abcdef"): key_id[x] = i + 1 for r in range(m): for c in range(n): if grid[r][c] == "@": sr, sc = r, c k = max(k, key_id[grid[r][c]]) q = [(sr, sc, "")] res = 0 vis = set((sr, sc, "")) while q: tmp = [] for r, c, keys in q: if len(keys) == k: return res for rr, cc in [(r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)]: if 0 <= rr < m and 0 <= cc < n and grid[rr][cc] != "#": x = grid[rr][cc] if x in "ABCDEF" and x.lower() not in keys: continue new_keys = keys[:] if x in "abcdef" and x not in new_keys: new_keys += x if (rr, cc, new_keys) not in vis: vis.add((rr, cc, new_keys)) tmp.append((rr, cc, new_keys)) res += 1 q = tmp return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR STRING WHILE VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR STRING FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR STRING VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: def bfs(r, c): start = grid[r][c] dist[start] = {} queue = collections.deque([(r, c, 0)]) visited = {(r, c): True} while queue: i, j, d = queue.pop() if grid[i][j] not in [".", "#", start]: dist[start][grid[i][j]] = d continue for ic, jc in [[i + 1, j], [i - 1, j], [i, j + 1], [i, j - 1]]: if ( 0 <= ic < m and 0 <= jc < n and (ic, jc) not in visited and grid[ic][jc] != "#" ): visited[ic, jc] = True queue.appendleft((ic, jc, d + 1)) m, n = len(grid), len(grid[0]) dist = {} for r in range(m): for c in range(n): if grid[r][c] not in [".", "#"]: bfs(r, c) prioq = [(0, "@", 0)] target_state = 0 visited = {} for key in dist: if "a" <= key <= "f": target_state += 1 << ord(key) - ord("a") while prioq: d, p, state = heapq.heappop(prioq) if (p, state) in visited: continue if state == target_state: return d visited[p, state] = True for dest, moves in list(dist[p].items()): if "A" <= dest <= "F" and 1 << ord(dest) - ord("A") & state == 0: continue new_state = state if "a" <= dest <= "f": new_state |= 1 << ord(dest) - ord("a") if (dest, new_state) not in visited: heapq.heappush(prioq, (d + moves, dest, new_state)) return -1
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR DICT VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR LIST STRING STRING VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR LIST STRING STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF STRING VAR STRING VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF STRING VAR STRING BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR VAR IF STRING VAR STRING VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: m = len(grid) n = len(grid[0]) def neighbor(i, j, keys): keys_list = list(keys) if grid[i][j] in [".", "@"]: return i, j, keys elif grid[i][j].islower(): if grid[i][j] in keys_list: return i, j, keys else: keys_list.append(grid[i][j]) new_keys = "".join(keys_list) return i, j, new_keys elif grid[i][j].isupper(): if grid[i][j].lower() in keys_list: return i, j, keys return None def move(i, j, keys): nexts = [] if i > 0: nei = neighbor(i - 1, j, keys) if nei: nexts.append(nei) if i < m - 1: nei = neighbor(i + 1, j, keys) if nei: nexts.append(nei) if j > 0: nei = neighbor(i, j - 1, keys) if nei: nexts.append(nei) if j < n - 1: nei = neighbor(i, j + 1, keys) if nei: nexts.append(nei) return nexts def get_start_and_keys(): x, y, keys = -1, -1, 0 for i in range(m): for j in range(n): if grid[i][j] == "@": x, y = i, j elif grid[i][j].islower(): keys += 1 return x, y, keys i0, j0, total_keys = get_start_and_keys() q = deque() start_point = i0, j0, "" q.append(start_point) visited = set() visited.add(start_point) steps = -1 while q: size = len(q) steps += 1 for x in range(size): pop = q.popleft() i = pop[0] j = pop[1] keys = pop[2] if len(keys) == total_keys: return steps for nex in move(i, j, keys): if nex not in visited: q.append(nex) visited.add(nex) return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR LIST STRING STRING RETURN VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR RETURN NONE FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: if not grid or not grid[0]: return -1 m, n = len(grid), len(grid[0]) sx = sy = target = -1 for i in range(m): for j in range(n): c = grid[i][j] if c == "@": sx, sy = i, j if c >= "a" and c <= "f": target = max(target, ord(c) - ord("a") + 1) target = (1 << target) - 1 q = deque([(sx, sy, 0)]) visited = set() visited.add(q[0]) step = 0 while q: for _ in range(len(q)): x, y, key = q.popleft() if key == target: return step for i, j in [[0, 1], [1, 0], [0, -1], [-1, 0]]: if x + i >= 0 and x + i < m and y + j >= 0 and y + j < n: c = grid[x + i][y + j] new_key = key if c == "#": continue if c >= "A" and c <= "F" and key >> ord(c) - ord("A") & 1 == 0: continue if c >= "a" and c <= "f": new_key |= 1 << ord(c) - ord("a") if (x + i, y + j, new_key) not in visited: visited.add((x + i, y + j, new_key)) q.append((x + i, y + j, new_key)) step += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR RETURN VAR FOR VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR STRING IF VAR STRING VAR STRING BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER IF VAR STRING VAR STRING VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: keylock = { "a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "A": "a", "B": "b", "C": "c", "D": "d", "E": "e", "F": "f", } m = len(grid) n = len(grid[0]) keys_target = 0 for i in range(m): for j in range(n): if grid[i][j] == "@": start = i, j elif grid[i][j] in "abcdef": keys_target = keys_target | 1 << keylock[grid[i][j]] def keyAllAcquired(value): if value & keys_target == keys_target: return True else: return False def isMovable(i, j): if 0 <= i <= m - 1 and 0 <= j <= n - 1 and grid[i][j] != "#": return True else: return False visited_states = {(start[0], start[1], 0)} q = collections.deque() q.append((start[0], start[1], 0)) step = 0 while q: q_len = len(q) for _ in range(q_len): i, j, keys = q.popleft() if keyAllAcquired(keys): return step directs = [-1, 0, 1, 0, -1] for d in range(4): newi = i + directs[d] newj = j + directs[d + 1] if isMovable(newi, newj): if grid[newi][newj] in "abcdef": new_state = ( newi, newj, keys | 1 << keylock[grid[newi][newj]], ) if new_state not in visited_states: visited_states.add(new_state) q.append(new_state) elif grid[newi][newj] in "ABCDEF": lower_int = keylock[keylock[grid[newi][newj]]] new_state = newi, newj, keys | 1 << lower_int + 6 if ( keys & 1 << lower_int and new_state not in visited_states ): visited_states.add(new_state) q.append(new_state) elif ( grid[newi][newj] in ".@" and (newi, newj, keys) not in visited_states ): visited_states.add((newi, newj, keys)) q.append((newi, newj, keys)) step += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR FUNC_DEF IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: init_x, init_y = -1, -1 m = len(grid) n = len(grid[0]) keycnt = 0 for i in range(m): for j in range(n): c = grid[i][j] if c == "@": init_x, init_y = i, j if c >= "a" and c <= "f": keycnt += 1 init_state = 0, init_x, init_y q = collections.deque() visited = set() visited.add(init_state) q.append(init_state) steps = -1 finalkey = 0 for i in range(keycnt): finalkey |= 1 << i while q: lens = len(q) steps += 1 for _ in range(len(q)): keys, x, y = q.popleft() for nx, ny in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)): if 0 > nx or nx >= m or 0 > ny or ny >= n: continue new_s = keys c = grid[nx][ny] if c == "#": continue if c >= "A" and c <= "F" and keys >> ord(c) - ord("A") & 1 == 0: continue if c >= "a" and c <= "f": new_s |= 1 << ord(c) - ord("a") if (new_s, nx, ny) in visited: continue if new_s == finalkey: return steps + 1 visited.add((new_s, nx, ny)) q.append((new_s, nx, ny)) return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR VAR IF VAR STRING VAR STRING VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FOR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING IF VAR STRING VAR STRING BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER IF VAR STRING VAR STRING VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: mx_key = max( grid[i][j] for i in range(len(grid)) for j in range(len(grid[0])) if grid[i][j].isalpha() and grid[i][j].islower() ) for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == "@": si, sj = i, j break needed_keys = (1 << ord(mx_key) - ord("a") + 1) - 1 def key_id(c): if c.isupper(): return ord(c) - ord("A"), True return ord(c) - ord("a"), False seen = {(si, sj, 0)} q = deque([(0, si, sj, 0)]) while q: cost, i, j, keys = q.popleft() if keys == needed_keys: return cost for ni, nj in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]: if ( not 0 <= ni < len(grid) or not 0 <= nj < len(grid[0]) or grid[ni][nj] == "#" ): continue new_keys = keys if grid[ni][nj].isalpha(): idx, capped = key_id(grid[ni][nj]) if capped: if not keys & 1 << idx: continue else: new_keys |= 1 << idx if (ni, nj, new_keys) in seen: continue seen.add((ni, nj, new_keys)) q.append((cost + 1, ni, nj, new_keys)) return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR RETURN VAR FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR STRING ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: if not grid: return -1 keys = [0] * 6 m, n = len(grid), len(grid[0]) si, sj = 0, 0 for i in range(m): for j in range(n): if grid[i][j] == "@": si, sj = i, j if grid[i][j].islower(): keys[ord(grid[i][j]) - ord("a")] = 1 mykeys = tuple(keys) q = [(si, sj, tuple([0] * 6))] visited = {(si, sj, tuple([0] * 6))} c = 0 while q: nextq = [] for i, j, keys in q: for x, y in ((i + 1, j), (i - 1, j), (i, j - 1), (i, j + 1)): if 0 <= x < m and 0 <= y < n and grid[x][y] != "#": if grid[x][y] in ".@": if (x, y, keys) not in visited: nextq.append((x, y, keys)) visited.add((x, y, keys)) elif grid[x][y].islower(): okeys = list(keys) okeys[ord(grid[x][y]) - ord("a")] = 1 nkeys = tuple(okeys) if nkeys == mykeys: return c + 1 elif (x, y, nkeys) not in visited: nextq.append((x, y, nkeys)) visited.add((x, y, nkeys)) elif grid[x][y].isupper(): if ( keys[ord(grid[x][y].lower()) - ord("a")] == 1 and (x, y, keys) not in visited ): nextq.append((x, y, keys)) visited.add((x, y, keys)) c += 1 q = nextq return -1
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR STRING IF VAR VAR VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def __init__(self): self.DIR = [(1, 0), (-1, 0), (0, 1), (0, -1)] def shortestPathAllKeys(self, grid: List[str]) -> int: init_x = init_y = -1 target_keys = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == "@": init_x, init_y = i, j elif grid[i][j].islower(): target_keys |= 1 << ord(grid[i][j]) - ord("a") pq = [(0, 0, init_x, init_y)] visited = set((0, init_x, init_y)) while pq: curr_steps, curr_keys, x, y = heapq.heappop(pq) if curr_keys == target_keys: return curr_steps for delta_x, delta_y in self.DIR: next_x, next_y = x + delta_x, y + delta_y if ( not self.inbound(grid, next_x, next_y) or grid[next_x][next_y] == "#" ): continue if grid[next_x][next_y].isupper(): if curr_keys >> ord(grid[next_x][next_y]) - ord("A") & 1: next_keys = curr_keys else: continue elif grid[next_x][next_y].islower(): next_keys = curr_keys | 1 << ord(grid[next_x][next_y]) - ord("a") else: next_keys = curr_keys if (next_keys, next_x, next_y) in visited: continue heapq.heappush(pq, (curr_steps + 1, next_keys, next_x, next_y)) visited.add((next_keys, next_x, next_y)) return -1 def inbound(self, grid, x, y): return 0 <= x < len(grid) and 0 <= y < len(grid[0])
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING IF FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER VAR FUNC_DEF RETURN NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid): m, n, k, keys, d = len(grid), len(grid[0]), 0, "", set() grid.append("#" * n) for i in range(m): grid[i] += "#" for i in range(m): for j in range(n): if grid[i][j] == "@": si, sj = i, j elif grid[i][j] in "abcdef": keys += grid[i][j] a, keyss = [(si, sj, keys)], "".join([c.upper() for c in keys]) while a: b = [] for i, j, keys in a: keys = keys.replace(grid[i][j], "") if not keys: return k for ii, jj in ((i, j + 1), (i + 1, j), (i, j - 1), (i - 1, j)): if ( grid[ii][jj] != "#" and not (grid[ii][jj] in keyss and grid[ii][jj].lower() in keys) and (ii, jj, keys) not in d ): d.add((ii, jj, keys)) b.append((ii, jj, keys)) a, k = b, k + 1 return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR STRING IF VAR RETURN VAR FOR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR VAR STRING VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: if not grid: return -1 m, n = len(grid), len(grid[0]) numKeys = 0 for i in range(m): for j in range(n): if grid[i][j] == "@": start = i, j, 0 elif ord("a") <= ord(grid[i][j]) <= ord("f"): numKeys += 1 bfs = [start] seen = set() d = [1, 0, -1, 0, 1] step = 0 while bfs: cur, bfs = bfs, [] for x, y, keys in cur: if keys == (1 << numKeys) - 1: return step for i in range(4): xx, yy = x + d[i], y + d[i + 1] if 0 <= xx < m and 0 <= yy < n and (xx, yy, keys) not in seen: seen.add((xx, yy, keys)) if ord("a") <= ord(grid[xx][yy]) <= ord("f"): bfs.append( (xx, yy, keys | 1 << ord(grid[xx][yy]) - ord("a")) ) elif ord("A") <= ord(grid[xx][yy]) <= ord("F"): if 1 << ord(grid[xx][yy].lower()) - ord("a") & keys == 0: continue else: bfs.append((xx, yy, keys)) elif grid[xx][yy] == "." or grid[xx][yy] == "@": bfs.append((xx, yy, keys)) step += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR LIST FOR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING IF BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR STRING VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: target = 0 queue, visited = [], set() for i in range(len(grid)): for j in range(len(grid[0])): if "a" <= grid[i][j] <= "f": target += 1 elif grid[i][j] == "@": queue.append((i, j, ".@abcdef", 0)) visited.add((i, j, ".@abcedf")) dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)] ans = 0 collected = set() while len(queue) != 0: size = len(queue) for _ in range(size): x, y, bag, collected = queue.pop(0) if collected == target: return ans for ix, iy in dirs: xx = x + ix yy = y + iy if ( 0 <= xx < len(grid) and 0 <= yy < len(grid[0]) and grid[xx][yy] in bag ): if ( "a" <= grid[xx][yy] <= "f" and grid[xx][yy].upper() not in bag ): if (xx, yy, bag + grid[xx][yy].upper()) not in visited: queue.append( (xx, yy, bag + grid[xx][yy].upper(), collected + 1) ) visited.add((xx, yy, bag + grid[xx][yy].upper())) elif (xx, yy, bag) not in visited: queue.append((xx, yy, bag, collected)) visited.add((xx, yy, bag)) ans += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF STRING VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING NUMBER EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR IF STRING VAR VAR VAR STRING FUNC_CALL VAR VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: def contains(k, v): return k // v % 2 == 1 adjs = [[-1, 0], [0, 1], [0, -1], [1, 0]] keys = ["a", "b", "c", "d", "e", "f"] locks = [c.upper() for c in keys] key_to_val = {k: (2**i) for i, k in enumerate(keys)} lock_to_val = {k: (2**i) for i, k in enumerate(locks)} N, M = len(grid), len(grid[0]) visited = [[([-1] * 64) for _ in range(M)] for _ in range(N)] queue = deque() target = 0 for i in range(N): for j in range(M): if grid[i][j] == "@": queue.append([i, j, 0]) visited[i][j][0] = 0 if grid[i][j] in keys: target += key_to_val[grid[i][j]] if target == 0: return 0 while len(queue) > 0: i, j, k = queue.popleft() for di, dj in adjs: ii, jj = i + di, j + dj if ii >= 0 and jj >= 0 and ii < N and jj < M: if grid[ii][jj] in (".", "@") and visited[ii][jj][k] < 0: visited[ii][jj][k] = visited[i][j][k] + 1 queue.append((ii, jj, k)) elif grid[ii][jj] in keys: if not contains(k, key_to_val[grid[ii][jj]]): kk = k + key_to_val[grid[ii][jj]] if kk == target: return visited[i][j][k] + 1 else: kk = k if visited[ii][jj][kk] < 0: visited[ii][jj][kk] = visited[i][j][k] + 1 queue.append((ii, jj, kk)) elif ( grid[ii][jj] in locks and contains(k, lock_to_val[grid[ii][jj]]) and visited[ii][jj][k] < 0 ): visited[ii][jj][k] = visited[i][j][k] + 1 queue.append((ii, jj, k)) return -1
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR STRING STRING VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER VAR
We are given a 2-dimensional grid. "." is an empty cell, "#" is a wall, "@" is the starting point, ("a", "b", ...) are keys, and ("A", "B", ...) are locks. We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.  We cannot walk outside the grid, or walk into a wall.  If we walk over a key, we pick it up.  We can't walk over a lock unless we have the corresponding key. For some 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first K letters of the English alphabet in the grid.  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. Return the lowest number of moves to acquire all keys.  If it's impossible, return -1.   Example 1: Input: ["@.a.#","###.#","b.A.B"] Output: 8 Example 2: Input: ["@..aA","..B#.","....b"] Output: 6   Note: 1 <= grid.length <= 30 1 <= grid[0].length <= 30 grid[i][j] contains only '.', '#', '@', 'a'-'f' and 'A'-'F' The number of keys is in [1, 6].  Each key has a different letter and opens exactly one lock.
class Solution: def shortestPathAllKeys(self, grid: List[str]) -> int: keys = "abcdef" locks = "ABCDEF" n, m = len(grid), len(grid[0]) x, y = -1, -1 target = 0 for i in range(n): for j in range(m): ch = grid[i][j] if ch == "@": x, y = i, j elif ch in keys: idx = keys.find(ch) target |= 1 << idx visited = set() visited.add((x, y, 0)) q = deque() q.append((x, y, 0, 0)) while q: x, y, state, move = q.popleft() if state == target: return move for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]: dx += x dy += y tmp = state if dx < 0 or dy < 0 or dx >= n or dy >= m: continue ch = grid[dx][dy] if ch == "#": continue if ch in locks: idx = locks.find(ch) if state & 1 << idx == 0: continue if ch in keys: idx = keys.find(ch) tmp |= 1 << idx if (dx, dy, tmp) not in visited: visited.add((dx, dy, tmp)) q.append((dx, dy, tmp, move + 1)) return -1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR RETURN VAR FOR VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = input().split(" ") n = int(n) q = int(q) a = [] pre = 0 b = [] arr = input() a = list(map(int, arr.split(" "))) for i in range(0, int(n)): pre = pre ^ a[i] b.append(pre) b.append(0) for i in range(0, int(q)): k = int(input()) k = k - 1 k = k % (n + 1) print(b[k])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = list(map(int, input().split())) a = list(map(int, input().split())) s = [0] * (n + 1) for i in range(n): if i == 0: s[i] = a[i] else: s[i] = s[i - 1] ^ a[i] s[n] = 0 for _ in range(q): k = int(input()) k -= 1 k = k % (n + 1) print(s[k])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = list(map(int, input().split())) arr = [f[0]] for i in range(1, n): arr.append(f[i] ^ arr[i - 1]) arr.append(0) n = n + 1 for i in range(q): k = int(input()) print(arr[(k - 1) % n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
l = list(map(int, input().split())) n = l[0] q = l[1] l = list(map(int, input().split())) num = 0 for i in range(len(l)): num = num ^ l[i] l.append(num) s = [] num = 0 for i in range(len(l)): num = num ^ l[i] s.append(num) for i in range(q): k = int(input()) k = k % (n + 1) k = k - 1 if k < 0: print(s[len(s) - 1]) else: print(s[k])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f1 = list(map(int, input().split())) f = [] size = n + 1 ans = 0 for i in range(n): ans ^= f1[i] f.append(ans) while q: k = int(input()) if k % size == 0: print(0) else: print(f[k % size - 1]) q -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
from sys import stdin def main(): n, q = map(int, stdin.readline().split()) arr = list(map(int, stdin.readline().split())) f = [0, arr[0]] xor = arr[0] ^ arr[1] f.append(xor) for index in range(2, n): xor ^= arr[index] f.append(xor) for query in range(q): k = int(stdin.readline()) if k <= n: print(f[k]) else: print(f[k % (n + 1)]) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
N, Q = map(int, input().split()) a = list(map(int, input().split())) s = [] c = 0 for i in a: c ^= i s.append(c) s.append(0) for i in range(Q): print(s[(int(input()) - 1) % len(s)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = list(map(int, input().split())) l = list(map(int, input().split())) v = [(0) for x in range(n)] v[0] = l[0] for j in range(1, n): v[j] = v[j - 1] ^ l[j] for i in range(q): k = int(input()) if k <= n: print(v[k - 1]) else: ans = (k - n) % (n + 1) - 1 if ans == 0: print(0) elif ans < 0: print(v[n - 1]) else: print(v[ans - 1])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) arr = [int(n) for n in input().split()] x = arr[0] arr1 = [] arr1.append(x) for i in range(1, n): x = x ^ arr[i] arr1.append(x) arr1.append(0) while q: q = q - 1 x = int(input()) r = x % (n + 1) print(arr1[r - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
l = input().split() n = int(l[0]) q = int(l[1]) l = input().split() li = [int(i) for i in l] xori = 0 si = [(0) for i in range(n)] for i in range(n): si[i] = xori xori = xori ^ li[i] si.append(xori) fi = list(li) fi.append(xori) for you in range(q): q1 = int(input()) print(si[q1 % (n + 1)])
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) F = [] S = [] line1 = input().split() last = 0 for i in range(n): F.append(int(line1[i])) last = last ^ int(line1[i]) S.append(last) S.append(0) for i in range(q): query = int(input()) pos = (query - 1) % len(S) final = S[pos] print(final)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) lst = list(map(int, input().split())) ans = [lst[0]] for i in range(1, n): ans.append(ans[i - 1] ^ lst[i]) ans.append(0) for _ in range(q): idx = int(input()) % (n + 1) - 1 print(ans[idx])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = [*map(int, input().split())] w = 0 for i in f: w ^= i f += [w] for i in range(1, len(f)): f[i] ^= f[i - 1] for i in range(q): k = int(input()) - 1 print(f[k % (n + 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = list(map(int, input().split())) c = f[0] s = [f[0]] for i in range(1, n): c = c ^ f[i] s.append(c) s.append(0) for _ in range(q): k = int(input()) print(s[k % (n + 1) - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) nli = [0] + list(map(int, input().split())) d = [0] for i in range(1, n + 1): d.append(d[i - 1] ^ nli[i]) for j in range(q): qi = int(input()) print(d[qi % (n + 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) l = list(map(int, input().split())) t = [] for i in range(q): t.append(int(input())) k = 0 l1 = [] for i in l: k ^= i l1.append(k) l1 += [0] lenl = len(l1) for i in t: print(l1[(i - 1) % lenl])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, k = map(int, input().split()) arr = list(map(int, input().split())) sol_arr = [0] var = 0 for i in range(n): var ^= arr[i] sol_arr.append(var) while k > 0: x = int(input()) print(sol_arr[x % (n + 1)]) k -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = [int(i) for i in input().split()] a = [int(i) for i in input().split()] xor = a[0] for i in range(1, len(a)): xor = xor ^ a[i] a.append(xor) s = [a[0]] for i in range(1, len(a)): s.append(s[-1] ^ a[i]) for I in range(q): k = int(input()) k -= 1 print(s[k % (n + 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = list(map(int, input().split())) for j in range(1, n): f[j] ^= f[j - 1] for g in range(q): k = int(input()) tyagibagi = k % (n + 1) if tyagibagi == 0: print(0) else: print(f[tyagibagi - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = list(map(int, input().split())) record = [] r = 0 for i in range(n): record.append(f[i]) r = r ^ f[i] record.append(r) s = [record[0]] for i in range(1, n + 1): s.append(s[i - 1] ^ record[i]) for i in range(q): k = int(input()) temp = k % (n + 1) print(s[temp - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = list(map(int, input().split())) d = {} ans = f[0] for i in range(1, n): ans = f[i] ^ ans f.append(ans) d[1] = f[0] for i in range(2, n + 2): d[i % (n + 1)] = d[i - 1] ^ f[i - 1] for _ in range(q): k = int(input()) print(d[k % (n + 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = list(map(int, input().split())) a = 0 na = [(0) for x in range(n + 1)] na[0] = f[0] na[n] = 0 for i in range(1, n): na[i] = na[i - 1] ^ f[i] for i in range(q): que = int(input()) print(na[que % (n + 1) - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
nq = [x for x in list(map(int, input().split(" ")))] tab = [x for x in list(map(int, input().split(" ")))] xs = tab[0] sol = [] sol.append(tab[0]) for i in range(1, nq[0]): xs ^= tab[i] sol.append(xs) sol.append(sol[-1] ^ xs) for i in range(nq[1]): q = int(input()) print(sol[(q - 1) % (nq[0] + 1)])
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) a = list(map(int, input().split())) f = [0] * n for i in range(n): if i == 0: f[i] = a[i] else: f[i] = f[i - 1] ^ a[i] for i in range(q): k = int(input()) x = k % (n + 1) if x == 0: print(0) else: print(f[x - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) a = list(map(int, input().split())) xor = 0 sl = [] for i in a: xor = xor ^ i sl.append(xor) for i in range(q): qval = int(input()) if qval % (n + 1) == 0: print(0) else: print(sl[qval % (n + 1) - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
N, Q = map(int, input().split()) l = list(map(int, input().split())) ans = [0] sk = 0 def tobinary(a): s = "" while a > 0: s = str(a % 2) + s a = a // 2 return s def XOR(a, b): s1 = tobinary(a) s2 = tobinary(b) if len(s1) > len(s2): s2 = "0" * (len(s1) - len(s2)) + s2 else: s1 = "0" * (len(s2) - len(s1)) + s1 xor = 0 for i in range(len(s1)): s = 1 if s1[i] == s2[i]: s = 0 xor += s * 2 ** (len(s1) - 1 - i) return xor for i in range(N): sk = XOR(sk, l[i]) ans.append(sk) for i in range(Q): k = int(input()) k = k % (N + 1) print(ans[k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR STRING WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
a, b = map(int, input().split()) A = list(map(int, input().split())) B = [] xor = 0 for i in range(a): xor ^= A[i] B.append(xor) for _ in range(b): t = int(input()) if t % (a + 1) == 0: print(0) else: hade = t % (a + 1) hade -= 1 print(B[hade])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split(" ")) ar, xor = list(map(int, input().split(" "))), 0 for x in ar: xor ^= x ar.append(xor) prefix = [0] * (n + 1) prefix[0] = ar[0] for i in range(1, len(prefix)): prefix[i] = ar[i] ^ prefix[i - 1] for i in range(q): print(prefix[(int(input()) - 1) % (n + 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split(" ")) F = list(map(int, input().split(" "))) S = [0] for i in range(n): x = S[i] ^ F[i] S.append(x) S.append(0) for i in range(q): k = int(input()) indx = k % (n + 1) print(S[indx])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = list(map(int, input().split())) s = [0] * (n + 1) f_seq = list(map(int, input().split())) for i in range(1, n + 1): s[i] = s[i - 1] ^ f_seq[i - 1] for _ in range(q): k = int(input()) print(s[k % (n + 1)])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
N, Q = map(int, input().split()) arr = list(map(int, input().split())) temp = arr[0] f = [arr[0]] for num in arr[1:]: temp ^= num f.append(temp) for i in range(Q): q = int(input()) s = q % (N + 1) ans = 0 if s >= 1: ans = f[s - 1] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
a = [int(s) for s in input().split()] b = [int(s) for s in input().split()] c = [0] d = 0 for e in range(a[0]): d = d ^ b[e] c.append(d) for j in range(a[1]): e = int(input()) if e <= a[0]: print(c[e]) else: print(c[(e - (a[0] + 1)) % (a[0] + 1)])
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = input().split() n = int(n) q = int(q) f = [int(fi) for fi in input().split()] cur = 0 s = [0] for fi in f: cur = cur ^ fi s.append(cur) for i in range(q): k = int(input()) print(s[k % (n + 1)])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
N, Q = map(int, input().split()) F = [0] + list(map(int, input().split())) x = F[1] for i in range(2, N + 1): x = x ^ F[i] F.append(x) S = [0] S.append(F[1]) for i in range(2, N + 2): S.append(F[i] ^ S[i - 1]) for _ in range(Q): i = int(input()) if i % (N + 1) == 0: print(0) else: print(S[i % (N + 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = list(map(lambda x: int(x), input().split())) a = list(map(lambda x: int(x), input().split())) xor_a = 0 for i in a: xor_a = xor_a ^ i a.append(xor_a) prefix = [a[0]] for i in range(1, len(a)): prefix.append(a[i] ^ prefix[i - 1]) for i in range(0, q): x = int(input()) print(prefix[(x - 1) % (n + 1)])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, m = map(int, input().split()) l = [int(i) for i in input().split()] pr = [0] * (n + 1) pr = 0 for i in l: pr ^= i l.append(pr) pre = [0] * (n + 1) pre[0] = l[0] for i in range(1, n + 1): pre[i] = pre[i - 1] ^ l[i] for i in range(m): x = int(input()) x -= 1 print(pre[x % (n + 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
N, Q = [int(x) for x in input().split()] F = [int(x) for x in input().split()] S = [F[0]] for i in range(1, len(F)): S.append(S[-1] ^ F[i]) for _ in range(Q): val = int(input()) % (N + 1) if not val: print(0) else: print(S[val - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = [int(a) for a in input().strip().split()] F = [int(a) for a in input().strip().split()] S = [0] for i in range(n): S.append(S[i] ^ F[i]) for _ in range(q): k = int(input()) print(S[k % (n + 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) arr = [int(x) for x in input().split()] x = arr[0] for i in range(1, len(arr)): x = x ^ arr[i] arr.append(x) xor = [] xor.append(arr[0]) for i in range(1, len(arr)): xor.append(xor[i - 1] ^ arr[i]) xor.insert(0, xor[n]) n += 1 while q != 0: q -= 1 t = int(input()) print(xor[t % n])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, Q = map(int, input().split()) a = [int(x) for x in input().split()] b = [a[0]] for i in range(1, n): b.append(b[len(b) - 1] ^ a[i]) b.append(0) n += 1 for q in range(Q): k = int(input()) if k <= n: k -= 1 print(b[k]) else: if k % n == 0: k = n else: k = k % n k -= 1 print(b[k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
N, Q = input().split() N = int(N) Farray = list(map(int, input().split())) requiredarray = [Farray[0]] for i in range(1, N): requiredarray.append(requiredarray[-1] ^ Farray[i]) requiredarray.append(0) for j in range(int(Q)): q = int(input()) print(requiredarray[q % (N + 1) - 1])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().strip().split()) flist = list(map(int, input().strip().split())) num = 0 for ele in flist: num = num ^ ele flist.append(num) slist = [] num = 0 for ele in flist: num = num ^ ele slist.append(num) for _ in range(q): ind = int(input()) ind -= 1 rem = ind % (n + 1) print(slist[rem]) del flist del slist
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) a = list(map(int, input().split())) fin = [a[0]] for i in range(1, n): fin.append(fin[i - 1] ^ a[i]) while q: q1 = int(input()) rem = q1 % (n + 1) if rem == 0: print(0) else: print(fin[rem - 1]) q -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = [int(w) for w in input().split()] ft = [] ft.append(f[0]) for i in range(1, n): temp = ft[i - 1] temp = temp ^ f[i] ft.append(temp) ft.append(0) while q: k = int(input()) index = k % (n + 1) - 1 print(ft[index]) q = q - 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = [int(fi) for fi in input().split()] s = [] sk = 0 for k in range(n): sk = sk ^ f[k] s.append(sk) s.append(0) for _ in range(q): k = int(input()) if k < n: print(s[k - 1]) else: print(s[k % (n + 1) - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split(" ")) f = list(map(int, input().split(" "))) c = [0] d = 0 for i in range(n): d = d ^ f[i] c.append(d) while q: e = int(input()) if e <= n: print(c[e]) else: print(c[(e - (n + 1)) % (n + 1)]) q = q - 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = input().strip().split(" ") n, q = int(n), int(q) l = list(map(int, input().strip().split(" "))) temp = l[0] for i in l[1:]: temp ^= i l.append(temp) ans = {} temp = 0 for i in range(n, -1, -1): ans[i] = temp ^ l[i] temp ^= l[i] for i in range(q): k = int(input()) print(ans[k % (n + 1)])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
import sys sys.setrecursionlimit(10**5 + 1) inf = int(10**20) max_val = inf min_val = -inf RW = lambda: sys.stdin.readline().strip() RI = lambda: int(RW()) RMI = lambda: [int(x) for x in sys.stdin.readline().strip().split()] RWI = lambda: [x for x in sys.stdin.readline().strip().split()] nb_elem, nb_query = RMI() elems = [0] + RMI() for i in range(1, nb_elem + 1): elems[i] ^= elems[i - 1] nb_elem += 1 for _ in range(nb_query): print(elems[RI() % nb_elem])
IMPORT EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR
Vietnamese and Bengali as well. An $N$-bonacci sequence is an infinite sequence $F_1, F_2, \ldots$ such that for each integer $i > N$, $F_i$ is calculated as $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N})$, where $f$ is some function. A XOR $N$-bonacci sequence is an $N$-bonacci sequence for which $f(F_{i-1}, F_{i-2}, \ldots, F_{i-N}) = F_{i-1} \oplus F_{i−2} \oplus \ldots \oplus F_{i−N}$, where $\oplus$ denotes the bitwise XOR operation. Recently, Chef has found an interesting sequence $S_1, S_2, \ldots$, which is obtained from prefix XORs of a XOR $N$-bonacci sequence $F_1, F_2, \ldots$. Formally, for each positive integer $i$, $S_i = F_1 \oplus F_2 \oplus \ldots \oplus F_i$. You are given the first $N$ elements of the sequence $F$, which uniquely determine the entire sequence $S$. You should answer $Q$ queries. In each query, you are given an index $k$ and you should calculate $S_k$. It is guaranteed that in each query, $S_k$ does not exceed $10^{50}$. -----Input----- - The first line of the input contains two space-separated integers $N$ and $Q$. - The second line contains $N$ space-separated integers $F_1, F_2, \ldots, F_N$. - The following $Q$ lines describe queries. Each of these lines contains a single integer $k$. -----Output----- For each query, print a single line containing one integer $S_k$. -----Constraints----- - $1 \le N, Q \le 10^5$ - $0 \le F_i \le 10^9$ for each $i$ such that $1 \le i \le N$ - $1 \le k \le 10^9$ -----Example Input----- 3 4 0 1 2 7 2 5 1000000000 -----Example Output----- 3 1 0 0
n, q = map(int, input().split()) f = [int(x) for x in input().split()] xortab = [] xor, xor1 = 0, 0 for i in f: xor ^= i xortab.append(xor) xortab.append(0) for i in range(q): k = int(input()) rem = k % (n + 1) print(xortab[rem - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER