description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: self.res = set() def helper(idx): if idx < 0: return set() if idx == 0: self.res |= {A[idx]} return {A[idx]} val = {A[idx]} | {(x | A[idx]) for x in helper(idx - 1)} self.res |= val return val helper(len(A) - 1) return len(self.res)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, a: List[int]) -> int: res, prev = set(), set() res.add(a[0]) prev.add(a[0]) for i in range(1, len(a)): cur = set() cur.add(a[i]) res.add(a[i]) for j in prev: cur.add(j | a[i]) res.add(j | a[i]) prev = cur return len(res)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: res = set([]) tmp = set([]) for i in A: newtmp = set([]) for j in tmp: newtmp.add(i | j) res.add(i | j) newtmp.add(i) res.add(i) tmp = newtmp return len(res)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: my_set = [set([A[i]]) for i in range(len(A))] for i in range(1, len(A)): for prev_result in my_set[i - 1]: my_set[i].add(A[i] | prev_result) return len(set.union(*my_set))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: mp = {(0): 1} ans = {} for a in A: temp = {} for m in mp: temp[m | a] = 1 temp[a] = 1 for t in temp: ans[t] = 1 mp = {} for t in temp: mp[t] = 1 cnt = 0 for c in ans: cnt += 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER RETURN VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: res = [] left, right = 0, 0 for val in A: right = len(res) res.append(val) for i in range(left, right): if res[-1] != res[i] | val: res.append(res[i] | val) left = right return len(set(res))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: result_set = set() results = set() for num in A: results = {(j | num) for j in results} results.add(num) result_set.update(results) return len(result_set)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, arr: List[int]) -> int: s = set() prev = set() prev.add(arr[0]) s.add(arr[0]) for i in range(len(arr)): temp = set() for val in prev: temp.add(val | arr[i]) s.add(val | arr[i]) prev = temp prev.add(arr[i]) s.add(arr[i]) return len(s)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: vals = set() if len(A) < 2: return len(A) cur = {0} for a in A: cur = {(a | y) for y in cur} | {a} vals |= cur return len(vals)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans, dp = set(), set() for x in A: ndp = set() ndp.add(x) for y in dp: ndp.add(x | y) ans.add(y) dp = ndp for x in dp: ans.add(x) return len(ans)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: sets = [{i} for i in A] for i in range(1, len(A)): for prev in sets[i - 1]: sets[i].add(A[i] | prev) return len(set.union(*sets))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: n = len(A) dp = set() ans = set() for j in range(n): dp_new = set() dp_new.add(A[j]) for x in dp: dp_new.add(x | A[j]) dp = dp_new ans.update(dp) return len(ans)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: ans = set() prev = set() prev.add(0) for i in range(len(A)): cur = set() cur.add(A[i]) for p in prev: res = p | A[i] if res != p: ans.add(p) cur.add(res) prev = cur for p in prev: ans.add(p) if 0 not in A: ans.remove(0) return len(ans)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: dp = [] for i in range(len(A)): if i == 0: dp.append(set([A[i]])) else: inner = set([A[i]]) for d in dp[-1]: inner.add(d | A[i]) dp.append(inner) total = set() for d in dp: total.update(d) return len(total)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: S, prev = set(), set() for a in A: temp = set() temp.add(a) S.add(a) for p in prev: temp.add(a | p) S.add(a | p) prev = temp return len(S)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: if not A: return [] for i in range(len(A))[::-1]: if i > 0 and A[i] == A[i - 1]: A.pop(i) all_outcomes = set([A[0]]) outcomes = set([A[0]]) for elt in A[1:]: outcomes = {elt} | {(elt | val) for val in outcomes} all_outcomes |= outcomes return len(all_outcomes)
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A): nums, n, pre = set(), len(A), set() for a in A: pre = {a} | {(num | a) for num in pre} nums |= pre return len(nums)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: sets = [{i} for i in A] for i in range(1, len(A)): for prev in sets[i - 1]: sets[i].add(A[i] | prev) for num in sets[1:]: sets[0] |= num return len(sets[0])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR NUMBER VAR NUMBER VAR RETURN FUNC_CALL VAR VAR NUMBER VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: prev = set() curr = set() vals = set(A) for x in A: curr.add(x) for y in prev: curr.add(x | y) vals.add(x | y) prev = curr curr = set() return len(vals)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: alive = set() dead = set() for num1 in A: tmp = {num1} for num2 in alive: dead.add(num2) tmp.add(num1 | num2) alive = tmp return len(alive | dead)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: s = set() global_set = set() ans = 0 for v in A: s.add(v) s = {(x | v) for x in s} global_set |= s return len(global_set)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
We have an array A of non-negative integers. For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j]. Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)   Example 1: Input: [0] Output: 1 Explanation: There is only one possible result: 0. Example 2: Input: [1,1,2] Output: 3 Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2]. These yield the results 1, 1, 2, 1, 3, 3. There are 3 unique values, so the answer is 3. Example 3: Input: [1,2,4] Output: 6 Explanation: The possible results are 1, 2, 3, 4, 6, and 7.   Note: 1 <= A.length <= 50000 0 <= A[i] <= 10^9
class Solution: def subarrayBitwiseORs(self, A: List[int]) -> int: S, last = set(), set() for i in range(len(A)): last = {(a | A[i]) for a in last} last.add(A[i]) S |= last return len(S)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Nobody outside the cooking community knows that Chef is a big fan of Chefgram™ — a social network where chefs and cooks upload their secret kitchen photos. Recently Chef clicked a beautiful photo, which is represented using 10 pixels in a single row. Respecting Chefgram™'s boolean roots, every pixel is either white or black. Chefgram™ has N filters. Every filter is a string containing 10 symbols. Every symbol is either '+' or '-'. A '+' at the i^{th} position in a filter means that if Chef applies this filter to his photo, the i^{th} pixel will be inverted: it becomes black if it was originally white, and vice versa. A '-' at the i^{th} position in a filter string means that if Chef applies this filter to his photo, the i^{th} pixel will remain unchanged. Chef can apply as many filters as he wants from a list. He can pick any subset of filters and consequently apply them to a photo. For example: Imagine that Chef has a photo "bbwwbbwwbb" (where 'b' stands for black and 'w' stands for white). He applies filters "++--++--++", "-+-+-+-+-+". Applying the first filter will transform his picture to "wwwwwwwwww". Applying the second filter on the transformed picture will give Chef the picture "wbwbwbwbwb". Even if Chefgram™ has two or more identical filters, they are still considered different! Chef is extremely interested in knowing how many different subsets of all the Chefgram™ filters can he apply to transform his photo into 10 black pixels? ------ Input ------ The first line of input contains a single integer T — the number of test cases. First line of each test case contains a string S. Each symbol is either 'b' or 'w'. This is Chef's photo. Second line of each test case contains a single integer N — the number of Chefgram™ filters. Each of the next N lines contains a single string F_{i}, each symbol of which is either '+' or '-'. This string is the i_{th} Chefgram™ filter. ------ Output ------ For each test case, output a single line containing a single integer — answer to Chef's question modulo 10^{9}+7. ------ Constraints ------ $1 ≤ T ≤ 5$ $|S| = 10$ $1 ≤ N ≤ 10^{5}$ $|F_{i}| = 10$ ------ Subtasks ------ $Subtask 1: T ≤ 5; N ≤ 20; Points: 20$ $Subtask 2: T ≤ 5; N ≤ 10^{3}; Points: 30$ $Subtask 3: T ≤ 5; N ≤ 10^{5}; Points: 50$ ----- Sample Input 1 ------ 3 wwwwwwwwww 3 +-+-+-+-+- ---------- +--------- wbwbwbwbwb 3 +-+-+-+-+- +-+------- ----+-+-+- bbbbbbbbbb 2 ---------- ---------- ----- Sample Output 1 ------ 0 2 4 ----- explanation 1 ------ Example case 1. There is no filter or combination of filters transforming the picture to whole black. Example case 2. Chef can either apply the first filter (and invert all whites) or apply the second and third filters in any order. Example case 3. Picture is already fully black, and we have two different identity filters. Chef can either apply the empty subset of filters, the first filter only, the second filter only, or both.
mod = 10**9 + 7 N = 10**5 p2 = [1] * (N + 1) for i in range(N): p2[i + 1] = (p2[i] << 1) % mod def getmask(s): ans = 0 mask = 1 for i in range(9, -1, -1): if s[i] == "w" or s[i] == "+": ans |= mask mask = mask << 1 return ans val = 1 << 10 for _ in range(int(input())): s = input() n = int(input()) smask = getmask(s) cnt = [0] * val for i in range(n): fil = input() cnt[getmask(fil)] += 1 pre = [0] * val pre[0] = 1 for i in range(val): if cnt[i]: cur = [0] * val for j in range(val): cur[j] = (pre[j] + pre[i ^ j]) * p2[cnt[i] - 1] % mod pre = cur print(pre[smask])
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. Nobody outside the cooking community knows that Chef is a big fan of Chefgram™ — a social network where chefs and cooks upload their secret kitchen photos. Recently Chef clicked a beautiful photo, which is represented using 10 pixels in a single row. Respecting Chefgram™'s boolean roots, every pixel is either white or black. Chefgram™ has N filters. Every filter is a string containing 10 symbols. Every symbol is either '+' or '-'. A '+' at the i^{th} position in a filter means that if Chef applies this filter to his photo, the i^{th} pixel will be inverted: it becomes black if it was originally white, and vice versa. A '-' at the i^{th} position in a filter string means that if Chef applies this filter to his photo, the i^{th} pixel will remain unchanged. Chef can apply as many filters as he wants from a list. He can pick any subset of filters and consequently apply them to a photo. For example: Imagine that Chef has a photo "bbwwbbwwbb" (where 'b' stands for black and 'w' stands for white). He applies filters "++--++--++", "-+-+-+-+-+". Applying the first filter will transform his picture to "wwwwwwwwww". Applying the second filter on the transformed picture will give Chef the picture "wbwbwbwbwb". Even if Chefgram™ has two or more identical filters, they are still considered different! Chef is extremely interested in knowing how many different subsets of all the Chefgram™ filters can he apply to transform his photo into 10 black pixels? ------ Input ------ The first line of input contains a single integer T — the number of test cases. First line of each test case contains a string S. Each symbol is either 'b' or 'w'. This is Chef's photo. Second line of each test case contains a single integer N — the number of Chefgram™ filters. Each of the next N lines contains a single string F_{i}, each symbol of which is either '+' or '-'. This string is the i_{th} Chefgram™ filter. ------ Output ------ For each test case, output a single line containing a single integer — answer to Chef's question modulo 10^{9}+7. ------ Constraints ------ $1 ≤ T ≤ 5$ $|S| = 10$ $1 ≤ N ≤ 10^{5}$ $|F_{i}| = 10$ ------ Subtasks ------ $Subtask 1: T ≤ 5; N ≤ 20; Points: 20$ $Subtask 2: T ≤ 5; N ≤ 10^{3}; Points: 30$ $Subtask 3: T ≤ 5; N ≤ 10^{5}; Points: 50$ ----- Sample Input 1 ------ 3 wwwwwwwwww 3 +-+-+-+-+- ---------- +--------- wbwbwbwbwb 3 +-+-+-+-+- +-+------- ----+-+-+- bbbbbbbbbb 2 ---------- ---------- ----- Sample Output 1 ------ 0 2 4 ----- explanation 1 ------ Example case 1. There is no filter or combination of filters transforming the picture to whole black. Example case 2. Chef can either apply the first filter (and invert all whites) or apply the second and third filters in any order. Example case 3. Picture is already fully black, and we have two different identity filters. Chef can either apply the empty subset of filters, the first filter only, the second filter only, or both.
fs = "bbbbbbbbbb" MOD = 1000000007 par = [512, 256, 128, 64, 32, 16, 8, 4, 2, 1] for _ in range(0, int(input())): s = str(input()) ts = "" for i in range(0, len(s)): if s[i] != fs[i]: ts = ts + "1" else: ts = ts + "0" t = int(input()) f = [0] * t inb = [0] * 1024 for i in range(0, t): x = str(input()) for j in range(0, len(x)): if x[j] == "+": f[i] = f[i] + par[j] inb[f[i]] = 1 + inb[f[i]] q = int(ts, 2) way = [[(0) for i in range(1024)] for j in range(1025)] way[0][0] = 1 for i in range(1, 1025): if inb[i - 1] != 0: pw = 1 for pp in range(0, inb[i - 1] - 1): pw = pw * 2 % MOD for j in range(0, 1024): way[i][j] = (way[i - 1][j] + way[i - 1][j ^ i - 1]) * pw % MOD else: for j in range(0, 1024): way[i][j] = way[i - 1][j] print(way[1024][q])
ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
t = int(input()) for _ in range(t): N, K = list(map(int, input().split())) A = list(map(int, input().split())) max_ele = max(A) max_len = len(bin(max_ele)) - 2 buckets = {} for j in range(1, max_len + 1): buckets[j] = [] for a in A: l = len(bin(a)) - 2 buckets[l].append(a) m_A = [] for k in reversed(range(1, max_len + 1)): if buckets[k] != []: temp = buckets[k].pop() m_A.append(temp) while buckets[k] != []: n = buckets[k].pop() nx = n ^ temp if nx != 0: buckets[len(bin(nx)) - 2].append(nx) sol = K for i in range(len(m_A)): if sol ^ m_A[i] > sol: sol = sol ^ m_A[i] print(sol)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
t = int(input()) for _ in range(t): n, k = list(map(int, input().strip().split())) a = list(map(int, input().strip().split())) dp = [] for i in range(n): dp.append([]) for j in range(1024): dp[i].append(0) dp[0][0] = 1 dp[0][a[0]] = 1 for i in range(1, n): for j in range(1024): if dp[i - 1][j]: dp[i][j] = dp[i - 1][j] dp[i][j ^ a[i]] = dp[i - 1][j] ma = 0 val = n - 1 for i in range(1024): if dp[val][i]: ma = max(ma, k ^ i) print(ma)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for i in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) dp = [[(0) for i in range(1024)] for j in range(1024)] dp[0][0] = 1 for i in range(1, n + 1): for j in range(1024): dp[i][j] = dp[i - 1][j] | dp[i - 1][j ^ l[i - 1]] print(max([(dp[n][j] * (j ^ k)) for j in range(1024)]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
t = int(input()) for T in range(t): s = [] e = [] s = list(map(int, input().split())) e = list(map(int, input().split())) dp = [False] * 1024 dp[0] = True for i in e: for x in range(int(1024)): if dp[x]: dp[x ^ i] = True ans = 0 for i in range(1024): if dp[i]: ans = max(i ^ s[1], ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for _ in range(int(input())): n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] dp = [([0] * 1024) for i in range(n + 1)] dp[0][0] = 1 for i in range(1, n + 1): for j in range(1024): if dp[i - 1][j] == 1: dp[i][j] = 1 dp[i][a[i - 1] ^ j] = 1 m = k for i in range(1024): if dp[n][i] == 1: m = max(m, k ^ i) print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for _ in range(int(input())): n, k = map(int, input().split()) l = [int(i) for i in input().split()] dp = [0] * 1024 dp[0] = 1 for i in range(n): for j in range(1024): dp[j ^ l[i]] = dp[j] | dp[j ^ l[i]] maxi = 0 for i in range(1024): if dp[i]: maxi = max(maxi, i ^ k) print(maxi)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) ad = set(arr) w = [k] ans = k v = set() v.add(k) for i in ad: for j in range(len(w)): x = w[j] ^ i ans = max(ans, x) v.add(x) w = list(v) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
t = int(input()) for i in range(t): N, K = map(int, input().split()) A = list(map(int, input().split())) n = 1023 dp = [0] * (n + 1) dp[0] = 1 for i in range(N): for j in range(n + 1): x = j ^ A[i] if dp[j] == 1: dp[x] = 1 Max = 0 for i in range(n + 1): if dp[i] == 1: Max = max(Max, i ^ K) print(Max)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
__author__ = "Ronald Kaiser" __email__ = "raios dot catodicos at gmail dot com" def solve(A, K): m, n = len(A), 2 ** len(bin(max(max(A), K))[2:]) dp = [[(False) for _ in range(n)] for _ in range(m)] dp[0][0] = True maxi = 0 for i in range(1, m): for j in range(n): dp[i][j] = dp[i - 1][j] or dp[i - 1][A[i] ^ j] if dp[i][j]: maxi = max(j ^ K, maxi) return maxi for _ in range(int(input())): N, K = list(map(int, input().split())) A = list(map(int, input().split())) print(solve([0] + A, K))
ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for _ in range(int(input())): n, k = map(int, input().strip().split()) S = list(map(int, input().strip().split())) dp = [(0) for i in range(1024)] dp[0] = True for i in range(n): for j in range(1024): dp[j ^ S[i]] |= dp[j] ans = k for i in range(1024): if dp[i]: ans = max(ans, k ^ i) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for _ in range(int(input().strip())): n, k = map(int, input().strip().split()) lst = list(map(int, input().strip().split())) Matrix = [[(0) for x in range(1024)] for y in range(n + 1)] for i in lst: Matrix[1][i] = 1 for i in range(2, n + 1): for j in range(1024): if Matrix[i - 1][j]: Matrix[i][j] = 1 elif Matrix[i - 1][lst[i - 1] ^ j]: Matrix[i][j] = 1 master_max = k for i in range(1024): master_max = max(master_max, (k ^ i) * Matrix[n][i]) print(master_max)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR 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 VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
def solve(): n, k = map(int, input().split()) arr = list(map(int, input().split())) ans = {0} for i in range(n): temp = {0} for j in ans: temp.update({arr[i] ^ j}) temp.update({arr[i]}) ans.update(temp) ans_1 = 0 ans = list(ans) for m in ans: if ans_1 < m ^ k: ans_1 = m ^ k print(ans_1) t = int(input()) while t != 0: solve() t -= 1
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for _ in range(int(input())): n, k = map(int, input().split()) l = [int(i) for i in input().split()] ans = k s = {k} for i in l: temp = set() for j in s: curr = i ^ j ans = max(curr, ans) temp.add(curr) s = s | temp print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
def calc(arr, k): dp = [0] * 1024 dp[0] = 1 for ele in arr: for j in range(1023, -1, -1): neval = j ^ ele if dp[j]: dp[neval] = 1 ans = k for i in range(1024): if dp[i]: ans = max(ans, i ^ k) return ans t = int(input()) while t > 0: n, k = map(int, input().split()) arr = list(map(int, input().split())) ans = calc(arr, k) print(ans) t -= 1
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for tc in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) a = set() a.add(k) for i in l: temp = a.copy() for j in temp: a.add(i ^ j) print(max(a))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
t = int(input()) for i in range(t): n, k = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] temp = [[(0) for i in range(1024)] for i in range(n + 1)] temp[0][0] = 1 for i in range(1, n + 1): for j in range(1024): temp[i][j] = temp[i - 1][j] or temp[i - 1][j ^ arr[i - 1]] maxx = -float("inf") for i in range(1024): if temp[n][i] == 1: maxx = max(maxx, k ^ i) print(maxx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
T = int(input()) for i in range(T): n, k = map(int, input().split()) a = list(map(int, input().split())) dp = [] dp = [0] * 1024 dp[0] = 1 dp1 = [] dp1 = [dp] * (n + 1) for i in range(1, n + 1): for j in range(1024): if dp1[i - 1][j] == 1 or dp1[i - 1][j ^ a[i - 1]] == 1: dp1[i][j] = 1 ans = k for i in range(1024): if dp1[n][i] == 1: ans = max(ans, k ^ i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for _ in range(int(input())): n, k = map(int, input().split()) A = list(map(int, input().split())) dp = [([0] * 1024) for _ in range(n + 1)] dp[0][0] = 1 for i in range(len(A)): dp[i][0] = 1 for elem in A: dp[1][elem] = 1 for i in range(1, n + 1): for j in range(1024): dp[i][j] = dp[i - 1][j] or dp[i - 1][j ^ A[i - 1]] maxi = 0 for i in range(1024): if dp[n][i]: maxi = max(maxi, i ^ k) print(maxi)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for _ in range(int(input())): n, k = map(int, input().split()) dp = [(0) for i in range(1024)] dp[0] = 1 l = list(map(int, input().split())) for i in l: for j in range(1024): if dp[j]: val = i ^ j dp[val] = 1 ans = k for i in range(1024): if dp[i]: ans = max(ans, i ^ k) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for i in range(int(input())): n, k = [int(x) for x in input().split()] l = list(map(int, input().split())) s = {k} for i in l: a = [] for j in s: a.append(j ^ i) for d in a: s.add(d) print(max(s))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
T = int(input()) for t in range(T): n, k = map(int, input().split()) arr = [0] + [int(_) for _ in input().split()] ans = 0 check = [(0) for _ in range(1024)] check[0] = 1 for i in range(1, n + 1): for j in range(1024): index = j ^ arr[i] if check[j]: check[index] = 1 for i in range(1024): if check[i]: ans = max(ans, k ^ i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
t = int(input()) while t > 0: t -= 1 n, k = map(int, input().split()) arr = list(map(int, input().split())) dp = [0] * 1024 dp[0] = True for i in range(len(arr)): for j in range(0, 1024): dp[arr[i] ^ j] |= dp[j] ans = k for i in range(0, 1024): if dp[i]: ans = max(ans, k ^ i) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
def mp(): return map(int, input().split(" ")) def lst(): return list(mp()) for _ in range(int(input())): n, k = mp() a = lst() pre = [(0) for i in range(n)] pre[0] = a[0] h = [(0) for i in range(2026)] h[0] = 1 for i in range(n): h[a[i]] = 1 for j in range(1024): if h[j] == 1: h[a[i] ^ j] = 1 for i in range(1024, -1, -1): e = k ^ i if h[e] == 1: print(i) break
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
def answer(): dp = [[(False) for i in range(1025)] for i in range(n + 1)] dp[0][0] = True for i in range(1, n + 1): for j in range(1025): dp[i][j] = dp[i][j] | dp[i - 1][j] if dp[i][j]: dp[i][j ^ a[i - 1]] = True ans = 0 for i in range(1025): if dp[-1][i]: ans = max(ans, i ^ k) return ans for T in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) print(answer())
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
raw_input = input for _ in range(int(raw_input())): n, k = map(int, raw_input().split()) A = [int(x) for x in raw_input().split()] dp = [False] * 1024 dp[k] = True for i in A: dp[i ^ k] = True for j in range(1024): if dp[j]: dp[j ^ i] = True for i in range(1023, -1, -1): if dp[i]: print(i) break
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
import sys sys.setrecursionlimit(10**6) def max_xor(i, K, N, A, D): if i == N: return K elif (i, K) in D: return D[i, K] else: D[i, K] = max(max_xor(i + 1, K, N, A, D), max_xor(i + 1, K ^ A[i], N, A, D)) return D[i, K] T = int(input()) ans = [] for _ in range(T): N, K = [int(i) for i in input().split()] A = [int(i) for i in input().split()] ans.append(max_xor(0, K, N, A, {})) for i in ans: print(i)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
def read(): return input().strip() def readint(): return int(read()) t = readint() for i in range(t): n, k = map(int, read().split()) arr = [0] * 1025 a = [int(x) for x in read().split()] for x in a: arr[x] = 1 maxvalue = k for x in a: for j in range(1024): if arr[j] == 1: exor = x ^ j arr[exor] = 1 for j in range(1024): if arr[j] == 1: maxvalue = max(maxvalue, j ^ k) print(maxvalue)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
import sys sys.setrecursionlimit(10**7) class Codechef: dp = [] def __init__(self): self.dp = [-1] * 1024 self.dp = [self.dp] * 1024 def maxXor(self, arr, n, k): if n == 0: return k if self.dp[n][k] != -1: return self.dp[n][k] self.dp[n][k] = max( self.maxXor(arr, n - 1, k ^ arr[n - 1]), self.maxXor(arr, n - 1, k) ) return self.dp[n][k] T = int(input()) for t in range(T): n, k = map(int, input().split()) c = Codechef() arr = list(map(int, input().split())) print(c.maxXor(arr, n, k))
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
def solve(): n, k = map(int, input().split()) arr = list(map(int, input().split())) vis = set() vis.add(k) for i in arr: temp = set() for j in vis: temp.add(i ^ j) vis = vis.union(temp) print(max(vis)) for t in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
t = int(input()) while t: n_k = [int(i) for i in input().split()] n = n_k[0] k = n_k[1] arr = [int(i) for i in input().split()] msb = max(arr) index = 0 for i in range(31, -1, -1): maxInd = index maxEle = -2147483648 for j in range(index, n): if arr[j] & 1 << i != 0 and arr[j] > maxEle: maxEle = arr[j] maxInd = j if maxEle == -2147483648: continue temp = arr[index] arr[index] = arr[maxInd] arr[maxInd] = temp maxInd = index for j in range(n): if j != maxInd and arr[j] & 1 << i != 0: arr[j] = arr[j] ^ arr[maxInd] index = index + 1 res = k for i in range(n): if res < res ^ arr[i]: res = res ^ arr[i] print(res) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
import sys sys.setrecursionlimit(10**9) def rec(a, i, curr, k, dp): global mxor if i >= len(a) - 1: mxor = max(mxor, k ^ curr) return mxor if (curr, i) in dp: return dp[curr, i] for j in range(i + 1, len(a)): curr ^= a[j] r1 = rec(a, j, curr, k, dp) curr ^= a[j] r2 = rec(a, j, curr, k, dp) dp[curr, i] = max(r1, r2) return dp[curr, i] def calc_max(n): n -= 1 n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 return n t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) mxor = 0 mmxor = 1025 dp = [[(0) for i in range(mmxor)] for j in range(n + 1)] dp[0][0] = 1 for i in range(1, n + 1): for j in range(mmxor): x = a[i - 1] dp[i][j] = dp[i - 1][j] | dp[i][j] if dp[i][j]: dp[i][j ^ x] = 1 for i, v in enumerate(dp[-1]): if v: mxor = max(mxor, i ^ k) print(mxor)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR 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 ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
t = int(input()) for i in range(t): n, p = map(int, input().split()) a = list(map(int, input().split())) m = [([0] * 1024) for x in range(n + 1)] m[0][0] = 1 max1 = 0 for j in range(1, n + 1): for k in range(0, 1024): m[j][k] = m[j - 1][k] + m[j - 1][k ^ a[j - 1]] for j, each in enumerate(m[n]): if each > 0 and max1 < j ^ p: max1 = j ^ p print(max1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
T = int(input()) for _ in range(T): [n, k] = [int(s) for s in input().split()] a = [int(s) for s in input().split()] dp = [[0] * 1024] * (n + 1) for i in range(n + 1): for j in range(1024): dp[i][j] = 0 dp[0][0] = 1 for i in range(1, n + 1): for j in range(1024): dp[i][j] = dp[i - 1][j] or dp[i - 1][a[i - 1] ^ j] res = 0 for j in range(1024): res = max(res, dp[n][j] * (j ^ k)) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
import sys sys.setrecursionlimit(10**6) def rec(i, xor): if i >= n: return xor if (i, xor) in dp: return dp[i, xor] dp[i, xor] = max(rec(i + 1, xor ^ a[i]), rec(i + 1, xor)) return dp[i, xor] for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) dp = {} ans = rec(0, k) print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
for _ in range(int(input())): n, k = map(int, input().split()) a = [*map(int, input().split())] s = set([0]) for i in a: e = set(s) for x in s: e.add(x ^ i) s = e mx = k for i in s: mx = max(mx, i ^ k) print(mx)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. You have an array of integers A_{1}, A_{2}, ..., A_{N}. The function F(P), where P is a subset of A, is defined as the XOR (represented by the symbol ⊕) of all the integers present in the subset. If P is empty, then F(P)=0. Given an integer K, what is the maximum value of K ⊕ F(P), over all possible subsets P of A? ------ Input ------ The first line contains T, the number of test cases. Each test case consists of N and K in one line, followed by the array A in the next line. ------ Output ------ For each test case, print the required answer in one line. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ K, A_{i} ≤ 1000$ $Subtask 1 (30 points):1 ≤ N ≤ 20$ $Subtask 2 (70 points):1 ≤ N ≤ 1000$ ----- Sample Input 1 ------ 1 3 4 1 2 3 ----- Sample Output 1 ------ 7 ----- explanation 1 ------ Considering all subsets: F({}) = 0 ? 4 ? 0 = 4 F({1}) = 1 ? 4 ? 1 = 5 F({1,2}) = 3 ? 4 ? 3 = 7 F({1,3}) = 2 ? 4 ? 2 = 6 F({1,2,3}) = 0 ? 4 ? 0 = 4 F({2}) = 2 ? 4 ? 2 = 6 F({2,3}) = 1 ? 4 ? 1 = 5 F({3}) = 3 ? 4 ? 3 = 7 Therefore, the answer is 7.
import sys sys.setrecursionlimit(10**9) def solve(arr, index, N, K, ans, sub): if index < 0: return ans ^ K if ans in sub[index]: return sub[index][ans] sub[index][ans] = max( solve(arr, index - 1, N, K, ans, sub), solve(arr, index - 1, N, K, ans ^ arr[index], sub), ) return sub[index][ans] for _ in range(int(input())): N, K = map(int, input().split()) arr = list(map(int, input().split())) sub = [] for i in range(N): sub.append({}) print(max(K, solve(arr, N - 1, N, K, 0, sub)))
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR DICT EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: hat_per_map = defaultdict(list) for idx, i in enumerate(hats): for j in i: hat_per_map[j - 1].append(idx) @lru_cache(None) def dp(hat, mask): if mask == (1 << len(hats)) - 1: return 1 if hat >= 40: return 0 ans = dp(hat + 1, mask) % 1000000007 for i in hat_per_map[hat]: if not mask & 1 << i: ans += dp(hat + 1, mask | 1 << i) % 1000000007 return ans return dp(0, 0) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: n = len(hats) dic = collections.defaultdict(list) for i, hat in enumerate(hats): for h in hat: dic[h].append(i) bfs = {(0): 1} target = (1 << n) - 1 res = 0 for h in range(1, 41): new_bfs = bfs.copy() for p in dic[h]: for mask, cnt in list(bfs.items()): new_mask = 1 << p | mask if new_mask != mask: if new_mask not in new_bfs: new_bfs[new_mask] = 0 new_bfs[new_mask] += cnt bfs = new_bfs return bfs[target] % (10**9 + 7) if target in bfs else 0
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: N = len(hats) for i, h in enumerate(hats): hats[i] = set(h) mod = 10**9 + 7 @lru_cache(None) def rec(cur, mask): if cur > 41: return 0 if mask == 0: return 1 ans = 0 for i in range(N): if mask & 1 << i == 0: continue if cur not in hats[i]: continue ans += rec(cur + 1, mask ^ 1 << i) ans += rec(cur + 1, mask) return ans return rec(0, 2**N - 1) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: h2p = collections.defaultdict(list) n = len(hats) MOD = 10**9 + 7 for people, hat_list in enumerate(hats): for h in hat_list: h2p[h].append(people) def dp(hat, mask): if (hat, mask) in memo: return memo[hat, mask] if mask == (1 << n) - 1: return 1 if hat > 40: return 0 ans = dp(hat + 1, mask) if hat in h2p: peoples = h2p[hat] for p in peoples: if mask & 1 << p == 0: ans += dp(hat + 1, mask | 1 << p) memo[hat, mask] = ans return ans memo = {} return dp(0, 0) % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR DICT RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: MOD = int(1000000000.0) + 7 n = len(hats) d = {(0): 1} rev = [[] for _ in range(41)] for i, h in enumerate(hats): for e in h: rev[e].append(i) for i in range(40): r = rev[i + 1] nx = d.copy() for j in d: for k in r: if not j & 1 << k: nj = j | 1 << k nx[nj] = nx.get(nj, 0) + d[j] d = nx return d.get((1 << n) - 1, 0) % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: res, n, dp, hat2persons = 0, len(hats), {}, collections.defaultdict(list) for person, arr in enumerate(hats): for hat in arr: hat2persons[hat - 1].append(person) dp[0] = 1 for hat in range(40): dp_new = dict(dp) for person in hat2persons[hat]: for state, cnt in dp.items(): if state & 1 << person == 0: if state | 1 << person not in dp_new: dp_new[state | 1 << person] = cnt else: dp_new[state | 1 << person] += cnt dp = dp_new print(dp) return dp[(1 << n) - 1] % (10**9 + 7) if (1 << n) - 1 in dp else 0
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR DICT FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: n = len(hats) fullMask = (1 << n) - 1 mp = defaultdict(list) for idx, i in enumerate(hats): for hat in i: mp[hat - 1].append(idx) mod = 10**9 + 7 @lru_cache(None) def dp(hat, mask): if mask == fullMask: return 1 if hat >= 40: return 0 ans = dp(hat + 1, mask) % mod for person in mp[hat]: if not mask & 1 << person: ans += dp(hat + 1, mask ^ 1 << person) ans %= mod return ans % mod return dp(0, 0) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: ownhat = {} n = len(hats) for i in range(n): hat = hats[i] for h in hat: if h in ownhat: ownhat[h].append(i) else: ownhat[h] = [i] dp = [[(-1) for i in range(pow(2, n))] for i in range(41)] def dfs(ind, peoplemask, dp): if peoplemask == pow(2, n) - 1: return 1 if ind > 40: return 0 count = 0 if dp[ind][peoplemask] != -1: return dp[ind][peoplemask] count = dfs(ind + 1, peoplemask, dp) if ind in ownhat: for people in ownhat[ind]: if peoplemask & pow(2, people) == 0: count = ( count + dfs(ind + 1, peoplemask | pow(2, people), dp) ) % (pow(10, 9) + 7) dp[ind][peoplemask] = count return count return dfs(1, 0, dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR FOR VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: hat2ppl = defaultdict(set) for p, hats_i in enumerate(hats): for h in hats_i: hat2ppl[h].add(p) if len(hats) > len(hat2ppl): return 0 M = 10**9 + 7 @lru_cache(None) def dfs(h, mask): if h == 42: return 0 if bin(mask).count("1") == len(hats): return 1 ans = dfs(h + 1, mask) for p in hat2ppl[h]: x = 1 << p if mask & x == 0: mask |= x ans += dfs(h + 1, mask) mask ^= x return ans % M return dfs(1, 0) % M
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: h2p = defaultdict(list) n = len(hats) mod = 10**9 + 7 for p, hat_list in enumerate(hats): for h in hat_list: h2p[h].append(p) m = 1 << n dp = [([0] * m) for _ in range(41)] dp[0][0] = 1 for h in range(1, 41): for mask in range(m): dp[h][mask] = dp[h - 1][mask] for p in h2p[h]: if mask & 1 << p != 0: dp[h][mask] += dp[h - 1][mask ^ 1 << p] return dp[40][m - 1] % mod
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: hat2ppl = defaultdict(set) for p, hats_i in enumerate(hats): for h in hats_i: hat2ppl[h].add(p) M = 10**9 + 7 n = 1 << len(hats) dp = [0] * n dp[0] = 1 for i in range(41, 0, -1): for j in range(n - 1, -1, -1): for p in hat2ppl[i]: if j & 1 << p: dp[j] = (dp[j] + dp[j ^ 1 << p]) % M return dp[-1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR RETURN VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: total = 1 << len(hats) satisfy, bound = [0] * 41, 0 for i, likes in enumerate(hats): for hat in likes: satisfy[hat] |= 1 << i bound = max(bound, hat) MOD = 10**9 + 7 DP = [1] + [0] * (total - 1) for hat in range(1, bound + 1): new_DP = [0] * total for state in range(total): new_DP[state] = DP[state] can_satisfy, mask = satisfy[hat] & state, 1 while can_satisfy != 0: if can_satisfy & 1: new_DP[state] += DP[state ^ mask] can_satisfy >>= 1 mask <<= 1 new_DP[state] %= MOD DP = new_DP return DP[total - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: htop = [[] for i in range(41)] for p, prefer_hats in enumerate(hats): for h in prefer_hats: htop[h].append(p) htop = list([h for h in htop if h]) num_hats, num_people = len(htop), len(hats) if num_hats < num_people: return 0 MOD = 10**9 + 7 @functools.lru_cache(None) def dp(i, mask): if bin(mask).count("1") == num_people: return 1 if i == num_hats: return 0 res = dp(i + 1, mask) for p in htop[i]: if mask & 1 << p == 0: mask |= 1 << p res += dp(i + 1, mask) mask ^= 1 << p return res % MOD return dp(0, 0)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF FUNC_CALL FUNC_CALL VAR VAR STRING VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats) -> int: MOD = 10**9 + 7 n = len(hats) t = 1 << n people = [[] for _ in range(41)] for i, hat in enumerate(hats): for h in hat: people[h].append(i) dp = [[(0) for _ in range(t)] for _ in range(41)] dp[0][0] = 1 for i in range(1, 41): for mask in range(t): dp[i][mask] = dp[i - 1][mask] for p in people[i]: if mask & 1 << p > 0: dp[i][mask] = (dp[i][mask] + dp[i - 1][mask & ~(1 << p)]) % MOD ans = max(dp[i][t - 1] for i in range(40)) return dp[40][t - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: max_hat = max([item for hat in hats for item in hat]) n = len(hats) dp = [[(0) for _ in range(2**n)] for _ in range(max_hat + 1)] for i in range(max_hat + 1): dp[i][0] = 1 hat_bitmaps = [] for i in range(n): bitmap = 0 for j in hats[i]: bitmap |= 1 << j hat_bitmaps.append(bitmap) for i in range(1, max_hat + 1): for j in range(2**n): acc = 0 for k in range(n): cur_bit = 1 << k if j & cur_bit != 0 and 1 << i & hat_bitmaps[k] != 0: acc += dp[i - 1][j - cur_bit] dp[i][j] = (dp[i - 1][j] + acc) % 1000000007 return dp[max_hat][2**n - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def _to_hat_to_people(self, person_to_hats): min_hat, max_hat = None, None for hats in person_to_hats: for hat in hats: if min_hat is None or hat < min_hat: min_hat = hat if max_hat is None or hat > max_hat: max_hat = hat number_of_hats = max_hat - min_hat + 1 hats_to_people = [[] for _ in range(number_of_hats)] for person in range(len(person_to_hats)): for hat in person_to_hats[person]: hats_to_people[hat - min_hat].append(person) return hats_to_people def numberWays(self, person_to_hats): hat_to_people = self._to_hat_to_people(person_to_hats) def _number_of_ways(person_to_hat_presence, hat, cache): if all(person_to_hat_presence): return 1 if hat >= len(hat_to_people): return 0 key = hat, tuple(person_to_hat_presence) if key in cache: return cache[key] else: sum = 0 for person in hat_to_people[hat]: if not person_to_hat_presence[person]: updated_person_to_hat_presence = person_to_hat_presence.copy() updated_person_to_hat_presence[person] = True sum += _number_of_ways( updated_person_to_hat_presence, hat + 1, cache ) sum += _number_of_ways(person_to_hat_presence, hat + 1, cache) cache[key] = sum % (10**9 + 7) return cache[key] return _number_of_ways([False] * len(person_to_hats), 0, {})
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NONE NONE FOR VAR VAR FOR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER DICT
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: h2p = defaultdict(list) for i, hat in enumerate(hats): for h in hat: h2p[h].append(i) n = len(hats) dp, ndp = defaultdict(int), defaultdict(int) for hat in range(0, 41): for mask in range(1 << n): if mask == 0: ndp[mask] = 1 else: ndp[mask] = dp[mask] for p in h2p[hat]: if mask & 1 << p: ndp[mask] += dp[mask ^ 1 << p] ndp[mask] %= 10**9 + 7 dp, ndp = ndp, defaultdict(int) return dp[(1 << n) - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: dp = {} n_ppl = len(hats) hat_2_ppl = {} for i in range(n_ppl): for hat in hats[i]: hat_2_ppl[hat] = hat_2_ppl.get(hat, []) + [i] def dfs(hat, mask): if mask == (1 << n_ppl) - 1: return 1 if hat > 40: return 0 if hat in dp and mask in dp[hat]: return dp[hat][mask] res = dfs(hat + 1, mask) for ppl in hat_2_ppl.get(hat, []): if not mask & 1 << ppl: res += dfs(hat + 1, mask | 1 << ppl) res %= 1000000000 + 7 dp[hat] = dp.get(hat, {}) dp[hat][mask] = res return res return dfs(1, 0)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR LIST LIST VAR FUNC_DEF IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR LIST IF BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR DICT ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: mod = 10**9 + 7 k = len(hats) persons = defaultdict(list) for i, hs in enumerate(hats): for h in hs: persons[h].append(i) dic = defaultdict(int) dic[0] = 1 for hat, ps in persons.items(): nex = dic.copy() for mask in dic: for p in ps: if mask >> p & 1 == 0: nex[mask | 1 << p] = (nex[mask | 1 << p] + dic[mask]) % mod dic = nex return dic[(1 << k) - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: MOD = int(1000000000.0) + 7 n = len(hats) dp = [[(0) for _ in range(1 << n)] for _ in range(41)] rev = [[] for _ in range(41)] for i, h in enumerate(hats): for e in h: rev[e].append(i) dp[0][0] = 1 for i in range(40): r = rev[i + 1] for j in range(1 << n): dp[i + 1][j] += dp[i][j] dp[i + 1][j] %= MOD for k in r: if not j & 1 << k: dp[i + 1][j | 1 << k] += dp[i][j] dp[i + 1][j | 1 << k] %= MOD return dp[40][(1 << n) - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR RETURN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: self.visited = 0 hm = {} for n, i in enumerate(hats): for j in i: if j not in hm: hm[j] = [n] else: hm[j].append(n) keys = list(hm.keys()) self.saved = {} def helper(index): if (index, self.visited) in self.saved: return self.saved[index, self.visited] if self.visited == (1 << len(hats)) - 1: return 1 if index == len(keys): return 0 count = 0 for i in hm[keys[index]]: if not self.visited & 1 << i: self.visited |= 1 << i count += helper(index + 1) self.visited ^= 1 << i self.saved[index, self.visited] = count + helper(index + 1) return self.saved[index, self.visited] return helper(0) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: numPeople = len(hats) hatDesired = defaultdict(set) MAX_HATS = 0 BIG_NUMBER = 10**9 + 7 for index, hatPreferences in enumerate(hats): for hat in hatPreferences: hatDesired[hat].add(index) MAX_HATS = max(MAX_HATS, hat) numberWays = defaultdict(int) def computeWays(assigned, currentHat): if (assigned, currentHat) in numberWays: return numberWays[assigned, currentHat] else: if currentHat == MAX_HATS + 1: if sum(assigned) != numPeople: answer = 0 else: answer = 1 else: answer = 0 answer += computeWays(assigned, currentHat + 1) answer %= BIG_NUMBER for index, num in enumerate(assigned): if num == 0 and index in hatDesired[currentHat]: nextList = list(assigned) nextList[index] = 1 nextTuple = tuple(nextList) answer += computeWays(nextTuple, currentHat + 1) answer %= BIG_NUMBER numberWays[assigned, currentHat] = answer return answer return computeWays(tuple([0] * numPeople), 1)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: def helper(hat, assignedPeople): if assignedPeople == target_state: return 1 if hat > 40: return 0 if dp[hat][assignedPeople] is not None: return dp[hat][assignedPeople] res = 0 res += helper(hat + 1, assignedPeople) for person in hat2person[hat]: if assignedPeople & 1 << person != 0: continue res += helper(hat + 1, assignedPeople | 1 << person) dp[hat][assignedPeople] = res return dp[hat][assignedPeople] n = len(hats) M = 10**9 + 7 hat2person = [[] for _ in range(41)] for i in range(n): for hat in hats[i]: hat2person[hat].append(i) target_state = (1 << n) - 1 dp = [[None for j in range(1 << n)] for i in range(41)] return helper(1, 0) % M
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER VAR VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: tot = len(hats) h = [([False] * tot) for i in range(41)] for i in range(len(hats)): for j in range(len(hats[i])): h[hats[i][j]][i] = True f = [([0] * (1 << tot)) for i in range(41)] f[0][0] = 1 mod = 10**9 + 7 for i in range(41): for j in range(1 << tot): f[i][j] = (f[i][j] + f[i - 1][j]) % mod for k in range(tot): if j & 1 << k != 0 and h[i][k]: f[i][j] = (f[i][j] + f[i - 1][j ^ 1 << k]) % mod return f[40][(1 << tot) - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR RETURN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: N = len(hats) @lru_cache(None) def helper(mask, i): if i == 41: if mask == 0: return 1 else: return 0 elif mask == 0: return 1 else: total = 0 for pers in range(N): if mask & 1 << pers and i in hats[pers]: total += helper(mask ^ 1 << pers, i + 1) total += helper(mask, i + 1) return total return helper(2**N - 1, 1) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: const = 10**9 + 7 matr = [(0) for i in range(40)] maxx = -1 used_h = set() for j in range(len(hats)): for i in hats[j]: matr[i - 1] += 2**j maxx = max(i - 1, maxx) used_h.add(i - 1) matr = matr[: maxx + 1] mask_p = 2 ** len(hats) - 1 def s(mask_p, n, h, c_h, cache): if n > c_h: return 0 if cache[mask_p][h] != None: return cache[mask_p][h] if mask_p == 0: return 1 res = 0 if h in used_h: c_h -= 1 b = mask_p & matr[h] i = 0 while b > 0: if b & 1: res += s(mask_p ^ 1 << i, n - 1, h - 1, c_h, cache) b = b >> 1 i += 1 res += s(mask_p, n, h - 1, c_h, cache) cache[mask_p][h] = res return cache[mask_p][h] return ( s( mask_p, len(hats), maxx, len(used_h), [([None] * (maxx + 1)) for i in range(mask_p + 1)], ) % const )
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NONE BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats: List[List[int]]) -> int: self.hat_to_man = collections.defaultdict(set) for i, lt in enumerate(hats): for hat in lt: self.hat_to_man[hat].add(i) self.hat_nums = sorted(self.hat_to_man) self.hat_cnt = len(self.hat_nums) self.man_cnt = len(hats) self.mem = dict() return self.dfs(0, set()) def dfs(self, idx, used): if idx >= self.hat_cnt: return 0 key = idx, frozenset(used) if key in self.mem: return self.mem[key] hat = self.hat_nums[idx] res = 0 for x in self.hat_to_man[hat] - used: used.add(x) if len(used) == self.man_cnt: res += 1 used.remove(x) continue res += self.dfs(idx + 1, used) used.remove(x) res += self.dfs(idx + 1, used) self.mem[key] = res return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n people and 40 types of hats labeled from 1 to 40. Given a list of list of integers hats, where hats[i] is a list of all hats preferred by the i-th person. Return the number of ways that the n people wear different hats to each other. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: hats = [[3,4],[4,5],[5]] Output: 1 Explanation: There is only one way to choose hats given the conditions. First person choose hat 3, Second person choose hat 4 and last one hat 5. Example 2: Input: hats = [[3,5,1],[3,5]] Output: 4 Explanation: There are 4 ways to choose hats (3,5), (5,3), (1,3) and (1,5) Example 3: Input: hats = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] Output: 24 Explanation: Each person can choose hats labeled from 1 to 4. Number of Permutations of (1,2,3,4) = 24. Example 4: Input: hats = [[1,2,3],[2,3,5,6],[1,3,7,9],[1,8,9],[2,5,7]] Output: 111   Constraints: n == hats.length 1 <= n <= 10 1 <= hats[i].length <= 40 1 <= hats[i][j] <= 40 hats[i] contains a list of unique integers.
class Solution: def numberWays(self, hats): ppl = len(hats) hats_to_ppl = [[] for i in range(41)] dp = [[(-1) for j in range(2**ppl)] for i in range(41)] for i in range(ppl): for j in hats[i]: hats_to_ppl[j].append(i) return self.dfs(1, hats_to_ppl, dp, 0, (1 << ppl) - 1) def dfs(self, hat, hats_to_people, dp, cur_assignment, all_assignment): if cur_assignment == all_assignment: return 1 if hat > 40: return 0 if dp[hat][cur_assignment] != -1: return dp[hat][cur_assignment] res = self.dfs(hat + 1, hats_to_people, dp, cur_assignment, all_assignment) for i in hats_to_people[hat]: if cur_assignment >> i & 1 == 1: continue res += self.dfs( hat + 1, hats_to_people, dp, cur_assignment | 1 << i, all_assignment ) res %= 10**9 + 7 dp[hat][cur_assignment] = res return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: dic = {v: i for i, v in enumerate(req_skills)} dp = dict() for i, people_skill in enumerate(people): skill = 0 for single_skill in people_skill: skill |= 1 << dic[single_skill] dp[skill] = [i] for key, value in list(dp.items()): if key != skill: new_skill = key | skill if ( new_skill not in list(dp.keys()) or len(dp[new_skill]) > len(dp[key]) + 1 ): dp[new_skill] = dp[key] + [i] return dp[(1 << len(req_skills)) - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR LIST VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR LIST VAR RETURN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: num_skills, team_queue = len(req_skills), collections.deque() bit_map, people_map = { req_skills[i]: i for i in range(num_skills) }, collections.defaultdict(list) for i, lst in enumerate(people): person_bit = 0 for l in lst: person_bit = person_bit | 1 << bit_map[l] for l in lst: people_map[bit_map[l]].append((i, person_bit)) team_queue.append((0, [])) while team_queue: bits, team = team_queue.popleft() min_idx = 0 while min_idx < num_skills and bits & 1 << min_idx: min_idx += 1 if min_idx == num_skills: return team for p, person_bit in people_map[min_idx]: team_queue.append((bits | person_bit, team + [p])) return []
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR RETURN VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR LIST VAR RETURN LIST VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: skill2num = {} for i, skill in enumerate(req_skills): skill2num[skill] = i p2s = [0] * len(people) for i, p in enumerate(people): skills = 0 for skill in p: if skill in skill2num: skills += 1 << skill2num[skill] p2s[i] = skills N = len(req_skills) dp = [1000000000] * (1 << N) dp[0] = 0 ans = [[] for i in range(1 << N)] for i in range(len(people)): dp2 = dp.copy() for skill in range(1 << N): new_skills = skill | p2s[i] if dp2[new_skills] > dp[skill] + 1: dp2[new_skills] = dp[skill] + 1 ans[new_skills] = list(ans[skill]) ans[new_skills].append(i) dp = dp2 return ans[(1 << N) - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: INT_MAX = 2147483647 n = len(req_skills) pskills = list() target = (1 << n) - 1 dp = [INT_MAX for i in range(target + 1)] parent = [list() for i in range(target + 1)] umap = dict() for i in range(n): umap[req_skills[i]] = i for p in people: mask = 0 for sk in p: mask |= 1 << umap[sk] pskills.append(mask) dp[0] = 0 pLen = len(people) for i in range(pLen): k = pskills[i] if k == 0: continue for j in range(target + 1): if dp[j | k] > dp[j] + 1: dp[j | k] = dp[j] + 1 parent[j | k] = [j, i] t = target result = list() while t > 0: result.append(parent[t][1]) t = parent[t][0] return result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: lrs = len(req_skills) skills = collections.defaultdict(list) peo_skills = [] for i, skill in enumerate(people): for s in skill: skills[s].append(i) self.res = [0] * (lrs + 1) def dfs(idx, cover, path): if idx == lrs: self.res = path if idx >= lrs: return if req_skills[idx] in cover: dfs(idx + 1, cover, path) if len(path) + 1 < len(self.res): for p in skills[req_skills[idx]]: p_s = set(people[p]) new_cover = cover | p_s dfs(idx + 1, new_cover, path + [p]) dfs(0, set(), []) return self.res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR LIST RETURN VAR VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: skillid = {skill: i for i, skill in enumerate(req_skills)} skillsets = {(0): []} for i, p in enumerate(people): skillmask = 0 for skill in p: if skill in skillid: skillmask |= 1 << skillid[skill] current_skillsets = list(skillsets.keys()) for skillset in current_skillsets: within_i = skillset | skillmask if ( within_i not in skillsets or len(skillsets[within_i]) > len(skillsets[skillset]) + 1 ): skillsets[within_i] = skillsets[skillset] + [i] c, mult = 0, 1 for i in range(len(req_skills)): c += mult mult *= 2 return skillsets[c]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR LIST VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: def fulfill_skills(skills, person): remaining_skills = deque() for skill in skills: if skill not in people[person]: remaining_skills.appendleft(skill) return remaining_skills has_skill = dict() for person, skills in enumerate(people): for skill in skills: experts = has_skill.get(skill, []) experts.append(person) has_skill[skill] = experts rare_skills = [ (len(people), skill) for skill, people in list(has_skill.items()) ] rare_skills.sort() rare_skills = [skill for _, skill in rare_skills] q = deque() q.append((deque(rare_skills), [])) while q: skills, team = q.popleft() print((skills, team)) if not skills: return team skill = skills[0] for person in has_skill[skill]: remaining_skills = fulfill_skills(skills, person) q.append((remaining_skills, team + [person])) return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR RETURN VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR RETURN NUMBER VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: skillsMap = {s: i for i, s in enumerate(req_skills)} peopleSkills = collections.defaultdict(set) for i, p in enumerate(people): for s in p: peopleSkills[skillsMap[s]].add(i) n = len(req_skills) memo = {} res = [] self.dfs(1 << n, memo, peopleSkills, res, n, people, skillsMap) return memo[1 << n] def dfs(self, state, memo, peopleSkills, res, n, people, skillsMap): if state == 2 ** (n + 1) - 1: return [] if state in memo: return memo[state] currState = list(bin(state)[3:]) mini = float("inf") ans = [] i = currState.index("0") for p in peopleSkills[i]: for s in people[p]: currState[skillsMap[s]] = "1" nextState = int("1" + "".join(currState), 2) temp = self.dfs(nextState, memo, peopleSkills, res, n, people, skillsMap) currState = list(bin(state)[3:]) if len(temp) < mini: mini = len(temp) ans = [p] + temp memo[state] = ans return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN VAR BIN_OP NUMBER VAR VAR VAR FUNC_DEF IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER RETURN LIST IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR VAR RETURN VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: skill_id_map = {skill: (1 << i) for i, skill in enumerate(req_skills)} ids_indices_map = {(0): []} for index, person_skills in enumerate(people): person_ids = 0 for skill in person_skills: person_ids |= skill_id_map[skill] new_ids_indices_map = ids_indices_map.copy() for ids, indices in ids_indices_map.items(): new_ids = ids | person_ids new_indices = indices + [index] if len(new_indices) <= len( new_ids_indices_map.get(new_ids, new_indices) ): new_ids_indices_map[new_ids] = new_indices ids_indices_map = new_ids_indices_map return ids_indices_map[2 ** len(req_skills) - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR LIST VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: mapping = {v: i for i, v in enumerate(req_skills)} record = {(0): []} for i, p in enumerate(people): skills = 0 for s in p: if s in mapping: skills |= 1 << mapping[s] for v, l in list(record.items()): withThis = v | skills if withThis == v: continue if withThis not in record or len(record[withThis]) > len(l) + 1: record[withThis] = l + [i] return record[(1 << len(req_skills)) - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR LIST VAR RETURN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: l = len(req_skills) index_dict = {item: i for i, item in enumerate(req_skills)} mydict = {(0): []} for i, person in enumerate(people): skill_list = ["0"] * l for skill in person: index = index_dict[skill] skill_list[index] = "1" num = int("".join(skill_list), 2) new_dict = {key: list(item) for key, item in list(mydict.items())} for key in list(mydict.keys()): new_num = key | num if new_num in new_dict: if len(new_dict[new_num]) > 1 + len(mydict[key]): new_dict[new_num] = mydict[key] + [i] else: new_dict[new_num] = mydict[key] + [i] mydict = new_dict return mydict[2**l - 1]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR LIST VAR ASSIGN VAR VAR BIN_OP VAR VAR LIST VAR ASSIGN VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR
In a project, you have a list of required skills req_skills, and a list of people.  The i-th person people[i] contains a list of skills that person has. Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill.  We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3]. Return any sufficient team of the smallest possible size, represented by the index of each person. You may return the answer in any order.  It is guaranteed an answer exists.   Example 1: Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]] Output: [0,2] Example 2: Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]] Output: [1,2]   Constraints: 1 <= req_skills.length <= 16 1 <= people.length <= 60 1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16 Elements of req_skills and people[i] are (respectively) distinct. req_skills[i][j], people[i][j][k] are lowercase English letters. Every skill in people[i] is a skill in req_skills. It is guaranteed a sufficient team exists.
class Solution: def smallestSufficientTeam( self, req_skills: List[str], people: List[List[str]] ) -> List[int]: n, m = len(req_skills), len(people) key = {v: i for i, v in enumerate(req_skills)} @functools.lru_cache(None) def helper(cur=0): if cur == (1 << n) - 1: return [] ans = math.inf res = [] for i, p in enumerate(people): his_skill = 0 for skill in p: if skill in key: his_skill |= 1 << key[skill] if his_skill | cur == cur: continue temp = helper(cur | his_skill) if len(temp) + 1 < ans: res = temp + [i] ans = len(temp) + 1 return res return helper()
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN LIST ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR LIST VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR