description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: total_chars_1 = defaultdict(int) total_chars_1[s[0]] = 1 total_chars_2 = defaultdict(int) splits = 0 for i in range(1, len(s)): total_chars_2[s[i]] += 1 for i in range(len(s) - 1): if len(total_chars_1) == len(total_chars_2): splits += 1 total_chars_1[s[i + 1]] += 1 total_chars_2[s[i + 1]] -= 1 if total_chars_2[s[i + 1]] == 0: del total_chars_2[s[i + 1]] return splits
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: dictionary = {} sol = 0 for i in range(len(s)): dictionary[s[i]] = i dictionary2 = {} for i in range(len(s) - 1): dictionary2[s[i]] = i if i == dictionary[s[i]]: dictionary.pop(s[i]) if len(dictionary) == len(dictionary2): sol += 1 return sol
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: good_splits = 0 first_map = {} s_map = {} for letter in s: if s_map.get(letter): s_map[letter] += 1 else: s_map[letter] = 1 for split_point in range(len(s)): if first_map.get(s[split_point]): first_map[s[split_point]] = first_map[s[split_point]] + 1 else: first_map[s[split_point]] = 1 s_map[s[split_point]] = s_map[s[split_point]] - 1 f_keys = [k for k, v in list(first_map.items()) if v > 0] s_keys = [k for k, v in list(s_map.items()) if v > 0] if len(f_keys) == len(s_keys): good_splits += 1 return good_splits
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def sub(self, h, h2): nh = h.copy() for k in h2: if k in nh: nh[k] = nh[k] - h2[k] if nh[k] == 0: del nh[k] return nh def numSplits(self, s: str) -> int: h = {} for c in s: if c not in h: h[c] = 0 h[c] = h[c] + 1 ct = 0 h2 = {} for i, c in enumerate(s): if c not in h2: h2[c] = 0 h2[c] = h2[c] + 1 uh = self.sub(h, h2) if len(h2) == len(uh): ct = ct + 1 return ct
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: n = len(s) lc = [1] * n rc = [1] * n ls = set() rs = set() for i in range(n): ls.add(s[i]) rs.add(s[-(i + 1)]) lc[i] = len(ls) rc[-(i + 1)] = len(rs) r = 0 for i in range(n - 1): if lc[i] == rc[i + 1]: r += 1 return r
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: leftMap = {} rightMap = {} for j in range(len(s)): rightMap[s[j]] = rightMap.get(s[j], 0) + 1 i = 0 result = 0 while i < len(s) - 1: leftMap[s[i]] = leftMap.get(s[i], 0) + 1 rightMap[s[i]] = rightMap[s[i]] - 1 if rightMap.get(s[i], 0) <= 0: rightMap.pop(s[i]) if len(leftMap) == len(rightMap): result += 1 i += 1 return result
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: c = Counter(s) res = 0 d = dict() for i, v in enumerate(s): if v not in d: d[v] = 1 else: d[v] += 1 c[v] -= 1 if c[v] == 0: del c[v] if len(c) == len(d): res += 1 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: cnt = 0 l_set = set() d = {c: s.count(c) for c in set(s)} for c in s: l_set.add(c) d[c] -= 1 if d[c] == 0: d.pop(c, None) if len(l_set) == len(d): cnt += 1 return cnt
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NONE IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: uniqueLeft = 0 uniqueRight = 0 ans = 0 leftDict = {} rightDict = {} for i in s: if i in rightDict: rightDict[i] += 1 else: rightDict[i] = 1 uniqueRight += 1 for i in s: if i in leftDict: leftDict[i] += 1 else: leftDict[i] = 1 uniqueLeft += 1 rightDict[i] -= 1 if rightDict[i] == 0: uniqueRight -= 1 if uniqueRight == uniqueLeft: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: right = collections.Counter(s) left = collections.defaultdict(int) res = 0 for i in range(len(s) - 1): c = s[i] right[c] -= 1 if not right[c]: del right[c] left[c] += 1 if len(left) == len(right): res += 1 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: result1 = [[s[0]]] result2 = [[s[-1]]] n = len(s) for i in range(1, len(s)): list1 = list(result1[i - 1]) list2 = list(result2[i - 1]) if s[i] not in list1: list1.append(s[i]) if s[n - 1 - i] not in list2: list2.append(s[n - 1 - i]) result1.append(list1) result2.append(list2) result = 0 result2.reverse() for i in range(len(s) - 1): if len(result1[i]) == len(result2[i + 1]): result += 1 return result
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: if len(s) == 1: return 0 lmap = collections.Counter(s[0:1]) rmap = collections.Counter(s[1:]) ans = 0 for i in range(1, len(s)): if len(lmap) == len(rmap): ans += 1 lmap.update([s[i]]) rmap[s[i]] -= 1 if not rmap[s[i]]: del rmap[s[i]] return ans
CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: s_len = len(s) if s_len == 0 or s_len == 1: return 0 seen = set() dp = [(0) for _ in range(s_len - 1)] dp[0] = 1 seen.add(s[0]) for i in range(1, s_len - 1): if s[i] in seen: dp[i] = dp[i - 1] else: seen.add(s[i]) dp[i] = dp[i - 1] + 1 seen = set() acc = 0 count = 0 for i in range(s_len - 1, 0, -1): if s[i] not in seen: seen.add(s[i]) acc += 1 if acc == dp[i - 1]: count += 1 return count
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: if not s or len(s) < 0: return 0 right = {} left = {s[0]: 1} count = 0 if len(left) == len(right): count += 1 for i in range(1, len(s)): if s[i] in right: right[s[i]] += 1 else: right[s[i]] = 1 if len(left) == len(right): count += 1 for i in range(1, len(s)): right[s[i]] -= 1 if right[s[i]] == 0: del right[s[i]] if s[i] in left: left[s[i]] += 1 else: left[s[i]] = 1 if len(left) == len(right): count += 1 return count
CLASS_DEF FUNC_DEF VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: le_set, ri_set = [s[0]], [s[-1]] le_arr, ri_arr = [1] * len(s), [1] * len(s) for i in range(1, len(s)): le_arr[i] = le_arr[i - 1] if s[i] not in le_set: le_set.append(s[i]) le_arr[i] += 1 for i in range(len(s) - 2, -1, -1): ri_arr[i] = ri_arr[i + 1] if s[i] not in ri_set: ri_set.append(s[i]) ri_arr[i] += 1 cnt = 0 for i in range(0, len(s) - 1): if le_arr[i] == ri_arr[i + 1]: cnt += 1 return cnt
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR LIST VAR NUMBER LIST VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: b_c = collections.defaultdict(lambda: 0) a_c = dict(collections.Counter(list(s))) ans = 0 for i, ch in enumerate(list(s)): b_c[ch] += 1 a_c[ch] -= 1 if len({k for k, v in list(b_c.items()) if v > 0}) == len( {k for k, v in list(a_c.items()) if v > 0} ): ans += 1 return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: fila = {} tot = len(set(s)) x = set() for i in range(len(s)): x.add(s[i]) if len(x) == tot: fa = i break x = set() for i in range(len(s) - 1, -1, -1): x.add(s[i]) if len(x) == tot: la = i break if fa <= la: return la - fa for i, c in enumerate(s): if c not in fila: fila[c] = [i, i] else: fila[c][1] = i firsts, lasts = set(), set() for a, b in fila.values(): firsts.add(a) lasts.add(b) ret = 0 cl, cr = 0, len(fila) for i in range(len(s) - 1): if i in firsts: cl += 1 if i in lasts: cr -= 1 ret += int(cl == cr) if cr < cl: break return ret
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, ss: str) -> int: a = set() k = {} p = {} n = len(ss) def myfunc(s, aa): for i in range(n - 1): if s[i] not in a: a.add(s[i]) aa[i] = 1 else: aa[i] = 0 if i != 0: aa[i] += aa[i - 1] myfunc(ss, k) a.clear() myfunc(ss[::-1], p) ans = 0 for j in range(n - 1): if k[j] == p[n - 1 - (j + 1)]: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: lc, rc = [], [] ls, rs = set(), set() for c in s: ls.add(c) lc.append(len(ls)) for c in reversed(s): rs.add(c) rc.append(len(rs)) res = 0 rc = rc[::-1] for i in range(len(lc) - 1): if lc[i] == rc[i + 1]: res += 1 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: auxDict = {} auxLeftToRight = [0] * len(s) count = 0 for pos in range(len(s)): if s[pos] not in auxDict: auxDict[s[pos]] = 1 count += 1 auxLeftToRight[pos] = count auxDict = {} auxRightToLeft = [0] * len(s) count = 0 for pos in range(len(s) - 1): if s[len(s) - 1 - pos] not in auxDict: auxDict[s[len(s) - 1 - pos]] = 1 count += 1 auxRightToLeft[len(s) - 2 - pos] = count total = 0 for pos in range(len(s)): if auxLeftToRight[pos] == auxRightToLeft[pos]: total += 1 return total
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def count_chars(self, s): count = {} for c in s: if c in count: count[c] += 1 else: count[c] = 1 return count def dict_decr(self, d, c): if c in d: d[c] -= 1 if d[c] <= 0: del d[c] def numSplits(self, s: str) -> int: left, right = s[0], s[1:] left_chars = set([left]) right_chars = self.count_chars(right) num_splits = 0 while right: if len(left_chars) == len(right_chars.keys()): num_splits += 1 c = right[0] self.dict_decr(right_chars, c) left_chars.add(c) left += c right = right[1:] return num_splits
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FUNC_DEF VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def make_hist(self, array: str) -> dict: hist = {} for s in array: if s not in list(hist.keys()): hist[s] = 1 else: hist[s] += 1 return hist def numSplits(self, s: str) -> int: hist_left = self.make_hist(s[:1]) hist_right = self.make_hist(s[1:]) count = 0 for i in range(1, len(s)): v = s[i] if len(list(hist_left.keys())) == len(list(hist_right.keys())): count += 1 if v not in list(hist_left.keys()): hist_left[v] = 1 else: hist_left[v] += 1 if v in list(hist_right.keys()): if hist_right[v] == 1: del hist_right[v] else: hist_right[v] -= 1 return count
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: count = 0 left = set(s[:1]) right = set(s[1:]) if len(left) == len(right): count += 1 k = 0 for i in range(1, len(s)): if s[i] not in left: left.add(s[i]) if s[i] not in s[i + 1 :]: k += 1 diff = len(left) - (len(right) - k) if diff == 0: count += 1 elif diff > 0: break return count
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: left_map = {} right_map = {} unique_right = 0 unique_left = 0 for i in range(len(s)): if s[i] not in right_map: right_map[s[i]] = 1 unique_right += 1 else: right_map[s[i]] += 1 good_split = 0 for i in range(len(s)): if s[i] not in left_map: left_map[s[i]] = 1 unique_left += 1 else: left_map[s[i]] += 1 if right_map[s[i]] == 1: unique_right -= 1 right_map[s[i]] -= 1 if unique_left == unique_right: good_split += 1 return good_split
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplitsBF(self, s: str) -> int: cntr = 0 for i in range(1, len(s)): a, b = collections.Counter(s[:i]), collections.Counter(s[i:]) if len(a) == len(b): cntr += 1 return cntr def numSplits(self, s: str) -> int: a = collections.defaultdict(int) a[s[0]] = 1 b = collections.Counter(s[1:]) pntr = 1 cntr = 0 if len(a) != len(b) else 1 while pntr < len(s): a[s[pntr]] += 1 b[s[pntr]] -= 1 if b[s[pntr]] == 0: del b[s[pntr]] if len(a) == len(b): cntr += 1 pntr += 1 return cntr
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: p = int q = int ll = len(s) if ll < 2: return 0 ns = len(set(s)) np = nq = 0 for ii in range(1, ll): if len(set(s[:ii])) == ns: np = ii break for ii in range(1, ll): if len(set(s[ll - ii :])) == ns: nq = ll - ii break if np <= nq: return nq - np + 1 ans = 0 for ii in range(1, ll): p = len(set(s[:ii])) q = len(set(s[ii:])) if p == q: ans += 1 elif p > q: return ans return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: characters = [] possibleSplits = 0 for i in range(len(s)): if s[i] not in characters: characters.append(s[i]) for i in range(len(s)): leftChars = 0 rightChars = 0 leftSide = s[:i] rightSide = s[i:] for j in range(len(characters)): if characters[j] in leftSide: leftChars += 1 if characters[j] in rightSide: rightChars += 1 if leftChars == rightChars: possibleSplits += 1 return possibleSplits
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: l_count = [] r_count = [] tmp = 0 for i in range(len(s)): if s[i] not in s[:i]: tmp += 1 l_count.append(tmp) tmp = 0 for i in range(len(s))[::-1]: if s[i] not in s[i + 1 :]: tmp += 1 r_count.append(tmp) r_count = r_count[::-1] count = 0 for i in range(len(r_count) - 1): if l_count[i] == r_count[i + 1]: count += 1 return count
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: pre = [0] * len(s) acc = set() cnt = 0 for i in range(len(s) - 1): if s[i] not in acc: cnt += 1 acc.add(s[i]) pre[i] = cnt cnt = 0 acc.clear() res = 0 for i in range(len(s) - 1, 0, -1): if s[i] not in acc: cnt += 1 acc.add(s[i]) if pre[i - 1] == cnt: res += 1 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: if len(s) <= 1: return 0 left = len(s) * [0] right = len(s) * [0] seen = set(s[0]) left[0] = 1 for i in range(1, len(s)): if s[i] not in seen: seen.add(s[i]) left[i] = left[i - 1] + 1 else: left[i] = left[i - 1] seen = set() rtotal = 0 count = 0 for i in range(len(s) - 1, 0, -1): if s[i] not in seen: seen.add(s[i]) rtotal += 1 if rtotal == left[i - 1]: count += 1 elif rtotal > left[i - 1]: break return count
CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: initial = {} for char in s: if char in initial: initial[char] += 1 else: initial[char] = 1 other = {} output = 0 for char in s: initial[char] -= 1 if initial[char] == 0: del initial[char] other[char] = 1 if len(initial) == len(other): output += 1 return output
CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: n = len(s) output = 0 dic = {} for i, x in enumerate(s): if not x in dic: dic[x] = [i, i] else: dic[x][1] = i for i in range(0, n - 1): left = 0 right = 0 for letter, indexes in list(dic.items()): if i >= indexes[0]: left += 1 if i + 1 <= indexes[1]: right += 1 if left == right: output += 1 return output
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: seen_letters = [] preprocess_prefix = [0] for i in range(len(s)): if s[i] not in seen_letters: seen_letters.append(s[i]) preprocess_prefix.append(len(seen_letters)) seen_letters = [] preprocess_postfix = [0] for i in range(len(s) - 1, -1, -1): if s[i] not in seen_letters: seen_letters.append(s[i]) preprocess_postfix.append(len(seen_letters)) preprocess_postfix.reverse() count = 0 for i in range(len(preprocess_postfix)): if preprocess_postfix[i] == preprocess_prefix[i]: count += 1 return count
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: def decorate(a, b): final = {(1): defaultdict(int), (2): defaultdict(int)} count1, count2 = 0, 0 for char in a: final[1][char] += 1 if final[1][char] == 1: count1 += 1 for char in b: final[2][char] += 1 if final[2][char] == 1: count2 += 1 return final, count1, count2 split = None count1, count2 = 0, 0 total = 0 for point in range(1, len(s)): if split == None: split, count1, count2 = decorate(s[:point], s[point:]) else: split[1][s[point - 1]] += 1 if split[1][s[point - 1]] == 1: count1 += 1 split[2][s[point - 1]] -= 1 if split[2][s[point - 1]] == 0: count2 -= 1 if count1 == count2: total += 1 return total
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: p, q, ans = Counter(), Counter(s), 0 for c in s[:-1]: p[c] += 1 q[c] -= 1 if not q[c]: del q[c] ans += len(p) == len(q) return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
You are given a string s, a split is called good if you can split s into 2 non-empty strings p and q where its concatenation is equal to s and the number of distinct letters in p and q are the same. Return the number of good splits you can make in s.   Example 1: Input: s = "aacaba" Output: 2 Explanation: There are 5 ways to split "aacaba" and 2 of them are good. ("a", "acaba") Left string and right string contains 1 and 3 different letters respectively. ("aa", "caba") Left string and right string contains 1 and 3 different letters respectively. ("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split). ("aacab", "a") Left string and right string contains 3 and 1 different letters respectively. Example 2: Input: s = "abcd" Output: 1 Explanation: Split the string as follows ("ab", "cd"). Example 3: Input: s = "aaaaa" Output: 4 Explanation: All possible splits are good. Example 4: Input: s = "acbadbaada" Output: 2   Constraints: s contains only lowercase English letters. 1 <= s.length <= 10^5
class Solution: def numSplits(self, s: str) -> int: N = len(s) left = [0] * (N + 1) seen = set() for i in range(1, N + 1): c = s[i - 1] if c not in seen: left[i] = left[i - 1] + 1 seen.add(c) else: left[i] = left[i - 1] right = [0] * (N + 1) seen.clear() for i in range(N - 1, -1, -1): c = s[i] if c not in seen: right[i] = right[i + 1] + 1 seen.add(c) else: right[i] = right[i + 1] good = 0 for i in range(N + 1): if left[i] == right[i]: good += 1 return good
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
n, K = map(int, input().split()) mod = 998244353 if K == 1: print(2) exit() dp = [([0] * 2**2) for i in range(K + 1)] dp[1][0] = 1 dp[2][1] = 1 dp[2][2] = 1 dp[1][3] = 1 for i in range(1, n): nx = [([0] * 2**2) for i in range(K + 1)] for k in range(K + 1): for j in range(4): if j == 0: nx[k][0] += dp[k][j] % mod if k + 1 <= K: nx[k + 1][1] += dp[k][j] % mod nx[k + 1][2] += dp[k][j] % mod nx[k + 1][3] += dp[k][j] % mod elif j == 1: nx[k][0] += dp[k][j] % mod nx[k][1] += dp[k][j] % mod if k + 2 <= K: nx[k + 2][2] += dp[k][j] % mod nx[k][3] += dp[k][j] % mod elif j == 2: nx[k][0] += dp[k][j] % mod if k + 2 <= K: nx[k + 2][1] += dp[k][j] % mod nx[k][2] += dp[k][j] % mod nx[k][3] += dp[k][j] % mod else: if k + 1 <= K: nx[k + 1][0] += dp[k][j] % mod nx[k + 1][1] += dp[k][j] % mod nx[k + 1][2] += dp[k][j] % mod nx[k][3] += dp[k][j] % mod dp = nx print(sum(dp[K]) % mod)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
import itertools n, k = [int(i) for i in input().split()] kas = [[0, 0, 0, 0], [1, 0, 0, 1], [0, 1, 1, 0]] mmm = 998244353 def count_k(ka, k, t): if t == 0: return ka[k][0] + ka[k][1] + ka[k][2] + ka[k - 1][3] if t == 1: return ka[k - 1][0] + ka[k][1] + ka[k - 2][2] + ka[k - 1][3] if t == 2: return ka[k - 1][0] + ka[k - 2][1] + ka[k][2] + ka[k - 1][3] if t == 3: return ka[k - 1][0] + ka[k][1] + ka[k][2] + ka[k][3] for i in range(1, n): if len(kas) < k + 1: kas.append([0, 0, 0, 0]) kas.append([0, 0, 0, 0]) for kk in range(min(len(kas) - 1, k), 1, -1): kas[kk] = [(count_k(kas, kk, t) % mmm) for t in range(4)] print(sum(kas[k]) % mmm if k < len(kas) else 0)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
ch_0 = {(0): [0, 1, 2], (2): [2], (1): [1], (3): [1, 2, 3]} ch_1 = {(0): [3], (3): [0], (1): [0, 3], (2): [0, 3]} ch_2 = {(0): [], (3): [], (2): [1], (1): [2]} N = 998244353 n, k = map(int, input().strip().split(" ")) dp = [[([0] * 4) for j in range(k + 5)] for i in range(n + 5)] dp[0][1][3] = 1 dp[0][1][0] = 1 dp[0][2][1] = 1 dp[0][2][2] = 1 for i in range(1, n): for j in range(1, k + 1): for mask in range(4): for t in ch_0[mask]: dp[i][j][mask] = (dp[i][j][mask] + dp[i - 1][j][t]) % N if j > 1: for t in ch_1[mask]: dp[i][j][mask] = (dp[i][j][mask] + dp[i - 1][j - 1][t]) % N if j > 2: for t in ch_2[mask]: dp[i][j][mask] = (dp[i][j][mask] + dp[i - 1][j - 2][t]) % N ans = 0 for mask in range(4): ans = (ans + dp[n - 1][k][mask]) % N print(ans)
ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER LIST NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER LIST NUMBER LIST NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER LIST LIST LIST NUMBER LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
n, k = [int(x) for x in input().split()] dp = [[[(0) for _ in range(4)] for _ in range(k + 2)] for _ in range(2)] dp[1][2][0] = 1 dp[1][2][1] = 1 dp[1][1][2] = 1 dp[1][1][3] = 1 for n1 in range(1, n): for k1 in range(1, k + 1): dp[0][k1][0] = dp[1][k1][0] dp[0][k1][1] = dp[1][k1][1] dp[0][k1][2] = dp[1][k1][2] dp[0][k1][3] = dp[1][k1][3] dp[1][k1][0] = ( dp[0][k1][0] + (dp[0][k1 - 2][1] if k1 - 2 >= 0 else 0) + dp[0][k1 - 1][2] + dp[0][k1 - 1][3] ) % 998244353 dp[1][k1][1] = ( dp[0][k1][1] + (dp[0][k1 - 2][0] if k1 - 2 >= 0 else 0) + dp[0][k1 - 1][2] + dp[0][k1 - 1][3] ) % 998244353 dp[1][k1][2] = ( dp[0][k1][2] + dp[0][k1][1] + dp[0][k1][0] + dp[0][k1 - 1][3] ) % 998244353 dp[1][k1][3] = ( dp[0][k1][3] + dp[0][k1][1] + dp[0][k1][0] + dp[0][k1 - 1][2] ) % 998244353 total = 0 for i in range(4): total += dp[1][k][i] % 998244353 print(total % 998244353)
ASSIGN VAR 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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
n, K = map(int, input().split()) dp = [[([0] * 4) for j in range(K + 2)] for i in range(n)] MOD = 998244353 dp[0][1][0] = 1 dp[0][1][1] = 1 dp[0][2][2] = 1 dp[0][2][3] = 1 for i in range(n - 1): for j in range(1, K + 1): if j < K + 1: for k in range(4): dp[i + 1][j][k] += dp[i][j][k] dp[i + 1][j][k] %= MOD for k in range(2): dp[i + 1][j][k] += dp[i][j][2] dp[i + 1][j][k] %= MOD for k in range(2): dp[i + 1][j][k] += dp[i][j][3] dp[i + 1][j][k] %= MOD for j in range(1, K): for k in range(4): if k != 0: dp[i + 1][j + 1][k] += dp[i][j][0] dp[i + 1][j + 1][k] %= MOD for k in range(4): if k != 1: dp[i + 1][j + 1][k] += dp[i][j][1] dp[i + 1][j + 1][k] %= MOD if j + 2 < K + 1: dp[i + 1][j + 2][2] += dp[i][j][3] dp[i + 1][j + 2][2] %= MOD dp[i + 1][j + 2][3] += dp[i][j][2] dp[i + 1][j + 2][3] %= MOD num = 0 for i in range(4): num += dp[n - 1][K][i] num %= MOD print(num)
ASSIGN VAR 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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
n, k = map(int, input().split()) mod = 998244353 dp = [[[0, 0] for j in range(2 * n + 1)] for i in range(n)] dp[0][0][0] = dp[0][1][1] = 1 for i in range(1, n): for j in range(2 * n - 1): dp[i][j][0] += (dp[i - 1][j][0] + dp[i - 1][j][1] + dp[i - 1][j][1]) % mod dp[i][j + 1][0] += dp[i - 1][j][0] % mod dp[i][j + 1][1] += (dp[i - 1][j][0] + dp[i - 1][j][0]) % mod dp[i][j][1] += dp[i - 1][j][1] % mod dp[i][j + 2][1] += dp[i - 1][j][1] % mod print(sum(dp[n - 1][k - 1]) * 2 % mod)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
def main(): n, k = map(int, input().split(" ")) if k > 2 * n: return 0 if k == 2 * n or k == 1: return 2 iguales = [0] * (k + 1) diferentes = [0] * (k + 1) iguales[1] = 2 diferentes[2] = 2 modulo = 998244353 for i in range(1, n): auxigual, auxdiff = [0] * (k + 1), [0] * (k + 1) for j in range(k): auxigual[j + 1] = ( iguales[j + 1] + iguales[j] + 2 * diferentes[j + 1] ) % modulo if j >= 1: auxdiff[j + 1] = ( diferentes[j + 1] + diferentes[j - 1] + 2 * iguales[j] ) % modulo iguales = auxigual diferentes = auxdiff return (iguales[-1] + diferentes[-1]) % modulo print(main())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR BIN_OP NUMBER VAR RETURN NUMBER IF VAR BIN_OP NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
n, k = map(int, input().split()) mod = 998244353 NEXT = {(0, 1): 2, (1, 2): 2} for i in range(1, n): NOW = NEXT NEXT = dict() for key in NOW: if key[0] == 0: if k - (n - i) * 2 <= key[1] <= k: NEXT[key] = NEXT.get(key, 0) + NOW[key] if k - (n - i) * 2 < key[1] + 1 <= k: NEXT[0, key[1] + 1] = NEXT.get((0, key[1] + 1), 0) + NOW[key] NEXT[1, key[1] + 1] = NEXT.get((1, key[1] + 1), 0) + NOW[key] * 2 % mod else: if k - (n - i) * 2 <= key[1] <= k: NEXT[key] = NEXT.get(key, 0) + NOW[key] NEXT[0, key[1]] = NEXT.get((0, key[1]), 0) + NOW[key] * 2 % mod if k - (n - i) * 2 < key[1] + 2 <= k: NEXT[1, key[1] + 2] = NEXT.get((1, key[1] + 2), 0) + NOW[key] ANS = 0 for key in NEXT: if key[1] == k: ANS = (ANS + NEXT[key]) % mod print(ANS)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
from sys import exit, stdin, stdout def sin(): return stdin.readline().rstrip() def listInput(): return list(map(int, sin().split())) def printBS(li): if not li: return for i in range(len(li) - 1): stdout.write("%d " % li[i]) stdout.write("%d\n" % li[-1]) N, K = listInput() MOD = 998244353 dp = [[[(0) for _ in range(K + 1)] for _ in range(4)] for _ in range(N + 1)] dp[1][0][1] = 1 if K > 1: dp[1][1][2] = 1 if K > 1: dp[1][2][2] = 1 dp[1][3][1] = 1 for n in range(2, N + 1): for k in range(1, K + 1): dp[n][0][k] = ( (dp[n - 1][0][k] + dp[n - 1][1][k]) % MOD + (dp[n - 1][2][k] + dp[n - 1][3][k - 1]) % MOD ) % MOD dp[n][1][k] = ( (dp[n - 1][0][k - 1] + dp[n - 1][1][k]) % MOD + ((dp[n - 1][2][k - 2] if k > 1 else 0) + dp[n - 1][3][k - 1]) % MOD ) % MOD dp[n][2][k] = ( (dp[n - 1][0][k - 1] + (dp[n - 1][1][k - 2] if k > 1 else 0)) % MOD + (dp[n - 1][2][k] + dp[n - 1][3][k - 1]) % MOD ) % MOD dp[n][3][k] = ( (dp[n - 1][0][k - 1] + dp[n - 1][1][k]) % MOD + (dp[n - 1][2][k] + dp[n - 1][3][k]) % MOD ) % MOD print(((dp[N][0][K] + dp[N][1][K]) % MOD + (dp[N][2][K] + dp[N][3][K]) % MOD) % MOD)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
mod = 998244353 N, K = map(int, input().split()) dp = [[([0] * (K + 2)) for i in range(2)] for i in range(N)] dp[0][0][0] = 1 dp[0][1][1] = 1 for i in range(1, N): for b in range(K): dp[i][0][b] += dp[i - 1][0][b] dp[i][0][b] += dp[i - 1][1][b] dp[i][0][b] += dp[i - 1][1][b] dp[i][0][b + 1] += dp[i - 1][0][b] dp[i][0][b] %= mod dp[i][1][b + 1] += dp[i - 1][0][b] dp[i][1][b] += dp[i - 1][1][b] dp[i][1][b + 2] += dp[i - 1][1][b] dp[i][1][b + 1] += dp[i - 1][0][b] dp[i][1][b] %= mod ans = 0 for x in range(2): ans += dp[N - 1][x][K - 1] print(ans * 2 % mod)
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
n, k = list(map(int, input().split())) same = [0] * (k + 1) diff = [0] * (k + 1) mod = 998244353 same[1] = 2 if k > 1: diff[2] = 2 for i in range(n - 1): newsame = [0] * (k + 1) newdiff = [0] * (k + 1) for i in range(1, k + 1): newsame[i] = (same[i] + same[i - 1] + 2 * diff[i]) % mod for i in range(2, k + 1): newdiff[i] = (2 * same[i - 1] + diff[i] + diff[i - 2]) % mod same = newsame diff = newdiff print((same[-1] + diff[-1]) % mod)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
n, k = list(map(int, input().split())) limit = 998244353 if k > 2 * n: print(0) elif k == 1 or k == 2 * n: print(2) else: same = [0] * (k + 1) same[1] = 2 diff = [0] * (k + 1) diff[2] = 2 for i in range(2, n + 1): for j in range(min(k, 2 * i), 1, -1): same[j] = same[j] + 2 * diff[j] + same[j - 1] same[j] %= limit diff[j] = diff[j] + 2 * same[j - 1] + diff[j - 2] diff[j] %= limit print((same[k] + diff[k]) % limit)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
n, k = map(int, input().split()) mod = 998244353 dp = [[0, 0, 0, 0] for _ in range(k + 1)] dp[1][0] = dp[1][3] = 1 if k > 1: dp[2][2] = dp[2][1] = 1 for x in range(1, n): g = [[0, 0, 0, 0] for _ in range(k + 1)] g[1][0] = g[1][3] = 1 for i in range(2, k + 1): g[i][0] = (dp[i][0] + dp[i][1] + dp[i][2] + dp[i - 1][3]) % mod g[i][1] = (dp[i - 1][0] + dp[i][1] + dp[i - 2][2] + dp[i - 1][3]) % mod g[i][2] = (dp[i - 1][0] + dp[i - 2][1] + dp[i][2] + dp[i - 1][3]) % mod g[i][3] = (dp[i - 1][0] + dp[i][1] + dp[i][2] + dp[i][3]) % mod dp = g print(sum(dp[-1]) % mod)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
You are given a grid, consisting of $2$ rows and $n$ columns. Each cell of this grid should be colored either black or white. Two cells are considered neighbours if they have a common border and share the same color. Two cells $A$ and $B$ belong to the same component if they are neighbours, or if there is a neighbour of $A$ that belongs to the same component with $B$. Let's call some bicoloring beautiful if it has exactly $k$ components. Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo $998244353$. -----Input----- The only line contains two integers $n$ and $k$ ($1 \le n \le 1000$, $1 \le k \le 2n$) — the number of columns in a grid and the number of components required. -----Output----- Print a single integer — the number of beautiful bicolorings modulo $998244353$. -----Examples----- Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 -----Note----- One of possible bicolorings in sample $1$: [Image]
pri = 998244353 dp = [[[(0) for i in range(2001)] for i in range(1001)] for i in range(2)] n, k = map(int, input().split()) for i in range(1, n + 1): if i == 1: dp[0][i][1] = 2 dp[1][i][2] = 2 continue for j in range(1, 2 * i + 1): dp[0][i][j] = dp[0][i - 1][j] + dp[0][i - 1][j - 1] + 2 * dp[1][i - 1][j] dp[0][i][j] %= pri dp[1][i][j] = 2 * dp[0][i - 1][j - 1] + dp[1][i - 1][j] + dp[1][i - 1][j - 2] dp[0][i][j] %= pri dp[1][i][j] %= pri y = dp[0][n][k] + dp[1][n][k] y %= pri print(y)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. Given a board of N rows and M columns, place the minimum number of knights such that every cell either contains a knight or is attacked by at least one knight. Like in standard chess, a knight attacks every cell that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. ------ Input ------ The first line of input contains one number T, the number of test cases. Each test case contains two integers N and M, the dimensions of the board. ------ Output ------ For each test case print the minimum number of knights required to cover every cell of the board. ------ Constraints ------ 1 ≤ T ≤ 150 1 ≤ N ≤ 3 1 ≤ M ≤ 50 ----- Sample Input 1 ------ 1 2 4 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ One optimal configuration is: Cells (1, 1), (1, 2), (4, 1) and (4, 2) contain knights. Cell (2, 1) is attacked by the knight in cell (4, 2). Cell (2, 2) is attacked by the knight in cell (4, 1). Cell (3, 1) is attacked by the knight in cell (1, 2). And cell (3, 2) is attacked by the knight in cell (1, 1). So every cell either contains a knight or is attacked by at least one knight, hence this is a valid configuration. There is no valid configuration with fewer knights, and so the answer is 4.
for t in range(int(input())): N, M = map(int, input().split()) if M < N: N, M = M, N if N == 1: print(M) else: res = M // 6 * 4 if M % 6 > 0: if M % 6 == 1: res += 2 elif N == 3 and M > 12 and M % 6 == 2: res += 3 else: res += 4 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in mandarin chinese, russian and vietnamese as well. Given a board of N rows and M columns, place the minimum number of knights such that every cell either contains a knight or is attacked by at least one knight. Like in standard chess, a knight attacks every cell that is two squares away horizontally and one square vertically, or two squares vertically and one square horizontally. ------ Input ------ The first line of input contains one number T, the number of test cases. Each test case contains two integers N and M, the dimensions of the board. ------ Output ------ For each test case print the minimum number of knights required to cover every cell of the board. ------ Constraints ------ 1 ≤ T ≤ 150 1 ≤ N ≤ 3 1 ≤ M ≤ 50 ----- Sample Input 1 ------ 1 2 4 ----- Sample Output 1 ------ 4 ----- explanation 1 ------ One optimal configuration is: Cells (1, 1), (1, 2), (4, 1) and (4, 2) contain knights. Cell (2, 1) is attacked by the knight in cell (4, 2). Cell (2, 2) is attacked by the knight in cell (4, 1). Cell (3, 1) is attacked by the knight in cell (1, 2). And cell (3, 2) is attacked by the knight in cell (1, 1). So every cell either contains a knight or is attacked by at least one knight, hence this is a valid configuration. There is no valid configuration with fewer knights, and so the answer is 4.
def knights(n, m): if n == 1: return m elif n == 2: if m == 1: return 2 else: table = [0, 2, 4, 4, 4, 4, 4] if m <= 6: return table[m] else: k = 0 while m > 6: m -= 6 k += 4 return k + table[m] else: table = [0, 3, 4, 4, 4, 4, 4, 6, 8, 8, 8, 8, 8, 10, 11, 12, 12, 12, 12] if m <= 18: return table[m] else: k = 0 while m > 18: k += 4 m -= 6 return k + table[m] t = int(input()) while t > 0: n, m = map(int, input().split()) print(knights(n, m)) t -= 1
FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER
Given an integer n, you must transform it into 0 using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of n. Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. Return the minimum number of operations to transform n into 0.   Example 1: Input: n = 0 Output: 0 Example 2: Input: n = 3 Output: 2 Explanation: The binary representation of 3 is "11". "11" -> "01" with the 2nd operation since the 0th bit is 1. "01" -> "00" with the 1st operation. Example 3: Input: n = 6 Output: 4 Explanation: The binary representation of 6 is "110". "110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0. "010" -> "011" with the 1st operation. "011" -> "001" with the 2nd operation since the 0th bit is 1. "001" -> "000" with the 1st operation. Example 4: Input: n = 9 Output: 14 Example 5: Input: n = 333 Output: 393   Constraints: 0 <= n <= 109
class Solution: def minimumOneBitOperations(self, n: int) -> int: if n <= 1: return n b = int(math.log2(n)) + 1 return (1 << b) - 1 - self.minimumOneBitOperations(n - (1 << b - 1))
CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR
Given an integer n, you must transform it into 0 using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of n. Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. Return the minimum number of operations to transform n into 0.   Example 1: Input: n = 0 Output: 0 Example 2: Input: n = 3 Output: 2 Explanation: The binary representation of 3 is "11". "11" -> "01" with the 2nd operation since the 0th bit is 1. "01" -> "00" with the 1st operation. Example 3: Input: n = 6 Output: 4 Explanation: The binary representation of 6 is "110". "110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0. "010" -> "011" with the 1st operation. "011" -> "001" with the 2nd operation since the 0th bit is 1. "001" -> "000" with the 1st operation. Example 4: Input: n = 9 Output: 14 Example 5: Input: n = 333 Output: 393   Constraints: 0 <= n <= 109
class Solution: def minimumOneBitOperations(self, n: int) -> int: s = 0 m = n while m: s += m & 1 m >>= 1 k = 1 while s: s -= bool(n & k) n ^= s & 1 and k k <<= 1 return n
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR
Given an integer n, you must transform it into 0 using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of n. Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. Return the minimum number of operations to transform n into 0.   Example 1: Input: n = 0 Output: 0 Example 2: Input: n = 3 Output: 2 Explanation: The binary representation of 3 is "11". "11" -> "01" with the 2nd operation since the 0th bit is 1. "01" -> "00" with the 1st operation. Example 3: Input: n = 6 Output: 4 Explanation: The binary representation of 6 is "110". "110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0. "010" -> "011" with the 1st operation. "011" -> "001" with the 2nd operation since the 0th bit is 1. "001" -> "000" with the 1st operation. Example 4: Input: n = 9 Output: 14 Example 5: Input: n = 333 Output: 393   Constraints: 0 <= n <= 109
class Solution: def minimumOneBitOperations(self, n: int) -> int: result = 0 while n: result = ((n & -n) << 1) - 1 - result n -= n & -n return result
CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an integer n, you must transform it into 0 using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of n. Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. Return the minimum number of operations to transform n into 0.   Example 1: Input: n = 0 Output: 0 Example 2: Input: n = 3 Output: 2 Explanation: The binary representation of 3 is "11". "11" -> "01" with the 2nd operation since the 0th bit is 1. "01" -> "00" with the 1st operation. Example 3: Input: n = 6 Output: 4 Explanation: The binary representation of 6 is "110". "110" -> "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0. "010" -> "011" with the 1st operation. "011" -> "001" with the 2nd operation since the 0th bit is 1. "001" -> "000" with the 1st operation. Example 4: Input: n = 9 Output: 14 Example 5: Input: n = 333 Output: 393   Constraints: 0 <= n <= 109
class Solution: def minimumOneBitOperations(self, n: int) -> int: if n == 0: return 0 if n == 1: return 1 tot = 0 b = bin(n)[2:] if b[1] == "0": tot += 2 ** (len(b) - 2) lastChanged = b[1] == "0" for i in range(2, len(b)): if lastChanged and b[i] == "0" or not lastChanged and b[i] == "1": tot += 2 ** (len(b) - 1 - i) lastChanged = True else: lastChanged = False return tot + 1 + self.minimumOneBitOperations(int("1" + "0" * (len(b) - 2), 2))
CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR STRING VAR VAR VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR
Please note the non-standard memory limit. There are n problems numbered with integers from 1 to n. i-th problem has the complexity c_i = 2^i, tag tag_i and score s_i. After solving the problem i it's allowed to solve problem j if and only if IQ < |c_i - c_j| and tag_i ≠ tag_j. After solving it your IQ changes and becomes IQ = |c_i - c_j| and you gain |s_i - s_j| points. Any problem can be the first. You can solve problems in any order and as many times as you want. Initially your IQ = 0. Find the maximum number of points that can be earned. Input The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases. The first line of each test case contains an integer n (1 ≤ n ≤ 5000) — the number of problems. The second line of each test case contains n integers tag_1, tag_2, …, tag_n (1 ≤ tag_i ≤ n) — tags of the problems. The third line of each test case contains n integers s_1, s_2, …, s_n (1 ≤ s_i ≤ 10^9) — scores of the problems. It's guaranteed that sum of n over all test cases does not exceed 5000. Output For each test case print a single integer — the maximum number of points that can be earned. Example Input 5 4 1 2 3 4 5 10 15 20 4 1 2 1 2 5 10 15 20 4 2 2 4 1 2 8 19 1 2 1 1 6 9 1 1 666 Output 35 30 42 0 0 Note In the first test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 1, after that total score is 20 and IQ = 6 4. 1 → 4, after that total score is 35 and IQ = 14 In the second test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 4, after that total score is 15 and IQ = 8 4. 4 → 1, after that total score is 35 and IQ = 14 In the third test case optimal sequence of solving problems is as follows: 1. 1 → 3, after that total score is 17 and IQ = 6 2. 3 → 4, after that total score is 35 and IQ = 8 3. 4 → 2, after that total score is 42 and IQ = 12
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n = int(input()) Tag = list(map(int, input().split())) S = list(map(int, input().split())) DP = [0] * n ANS = 0 for i in range(1, n): MAX = 0 for j in range(i - 1, -1, -1): temp = DP[j] if Tag[j] != Tag[i]: DP[i] = max(DP[i], DP[j] + abs(S[i] - S[j])) DP[j] = max(DP[j], MAX + abs(S[j] - S[i])) MAX = max(MAX, temp + abs(S[i] - S[j])) print(max(DP))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Please note the non-standard memory limit. There are n problems numbered with integers from 1 to n. i-th problem has the complexity c_i = 2^i, tag tag_i and score s_i. After solving the problem i it's allowed to solve problem j if and only if IQ < |c_i - c_j| and tag_i ≠ tag_j. After solving it your IQ changes and becomes IQ = |c_i - c_j| and you gain |s_i - s_j| points. Any problem can be the first. You can solve problems in any order and as many times as you want. Initially your IQ = 0. Find the maximum number of points that can be earned. Input The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases. The first line of each test case contains an integer n (1 ≤ n ≤ 5000) — the number of problems. The second line of each test case contains n integers tag_1, tag_2, …, tag_n (1 ≤ tag_i ≤ n) — tags of the problems. The third line of each test case contains n integers s_1, s_2, …, s_n (1 ≤ s_i ≤ 10^9) — scores of the problems. It's guaranteed that sum of n over all test cases does not exceed 5000. Output For each test case print a single integer — the maximum number of points that can be earned. Example Input 5 4 1 2 3 4 5 10 15 20 4 1 2 1 2 5 10 15 20 4 2 2 4 1 2 8 19 1 2 1 1 6 9 1 1 666 Output 35 30 42 0 0 Note In the first test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 1, after that total score is 20 and IQ = 6 4. 1 → 4, after that total score is 35 and IQ = 14 In the second test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 4, after that total score is 15 and IQ = 8 4. 4 → 1, after that total score is 35 and IQ = 14 In the third test case optimal sequence of solving problems is as follows: 1. 1 → 3, after that total score is 17 and IQ = 6 2. 3 → 4, after that total score is 35 and IQ = 8 3. 4 → 2, after that total score is 42 and IQ = 12
def nr(): return int(input()) def nrs(): return [int(i) for i in input().split()] def f(n, t, s): d = [0] * n for i in range(1, n): for j in range(i - 1, -1, -1): if t[i] == t[j]: continue sc = abs(s[i] - s[j]) d[i], d[j] = max(d[i], d[j] + sc), max(d[j], d[i] + sc) return max(d) for _ in range(nr()): n = nr() t = nrs() s = nrs() print(f(n, t, s))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Please note the non-standard memory limit. There are n problems numbered with integers from 1 to n. i-th problem has the complexity c_i = 2^i, tag tag_i and score s_i. After solving the problem i it's allowed to solve problem j if and only if IQ < |c_i - c_j| and tag_i ≠ tag_j. After solving it your IQ changes and becomes IQ = |c_i - c_j| and you gain |s_i - s_j| points. Any problem can be the first. You can solve problems in any order and as many times as you want. Initially your IQ = 0. Find the maximum number of points that can be earned. Input The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases. The first line of each test case contains an integer n (1 ≤ n ≤ 5000) — the number of problems. The second line of each test case contains n integers tag_1, tag_2, …, tag_n (1 ≤ tag_i ≤ n) — tags of the problems. The third line of each test case contains n integers s_1, s_2, …, s_n (1 ≤ s_i ≤ 10^9) — scores of the problems. It's guaranteed that sum of n over all test cases does not exceed 5000. Output For each test case print a single integer — the maximum number of points that can be earned. Example Input 5 4 1 2 3 4 5 10 15 20 4 1 2 1 2 5 10 15 20 4 2 2 4 1 2 8 19 1 2 1 1 6 9 1 1 666 Output 35 30 42 0 0 Note In the first test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 1, after that total score is 20 and IQ = 6 4. 1 → 4, after that total score is 35 and IQ = 14 In the second test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 4, after that total score is 15 and IQ = 8 4. 4 → 1, after that total score is 35 and IQ = 14 In the third test case optimal sequence of solving problems is as follows: 1. 1 → 3, after that total score is 17 and IQ = 6 2. 3 → 4, after that total score is 35 and IQ = 8 3. 4 → 2, after that total score is 42 and IQ = 12
for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) dp = [0] * n for i in range(n): for j in range(i - 1, -1, -1): if A[i] == A[j]: continue s = abs(B[i] - B[j]) dp[i], dp[j] = max(dp[i], dp[j] + s), max(dp[j], dp[i] + s) print(max(dp))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Please note the non-standard memory limit. There are n problems numbered with integers from 1 to n. i-th problem has the complexity c_i = 2^i, tag tag_i and score s_i. After solving the problem i it's allowed to solve problem j if and only if IQ < |c_i - c_j| and tag_i ≠ tag_j. After solving it your IQ changes and becomes IQ = |c_i - c_j| and you gain |s_i - s_j| points. Any problem can be the first. You can solve problems in any order and as many times as you want. Initially your IQ = 0. Find the maximum number of points that can be earned. Input The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases. The first line of each test case contains an integer n (1 ≤ n ≤ 5000) — the number of problems. The second line of each test case contains n integers tag_1, tag_2, …, tag_n (1 ≤ tag_i ≤ n) — tags of the problems. The third line of each test case contains n integers s_1, s_2, …, s_n (1 ≤ s_i ≤ 10^9) — scores of the problems. It's guaranteed that sum of n over all test cases does not exceed 5000. Output For each test case print a single integer — the maximum number of points that can be earned. Example Input 5 4 1 2 3 4 5 10 15 20 4 1 2 1 2 5 10 15 20 4 2 2 4 1 2 8 19 1 2 1 1 6 9 1 1 666 Output 35 30 42 0 0 Note In the first test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 1, after that total score is 20 and IQ = 6 4. 1 → 4, after that total score is 35 and IQ = 14 In the second test case optimal sequence of solving problems is as follows: 1. 1 → 2, after that total score is 5 and IQ = 2 2. 2 → 3, after that total score is 10 and IQ = 4 3. 3 → 4, after that total score is 15 and IQ = 8 4. 4 → 1, after that total score is 35 and IQ = 14 In the third test case optimal sequence of solving problems is as follows: 1. 1 → 3, after that total score is 17 and IQ = 6 2. 3 → 4, after that total score is 35 and IQ = 8 3. 4 → 2, after that total score is 42 and IQ = 12
def solve(): n = int(input()) c = list(map(int, input().split())) s = list(map(int, input().split())) ans = 0 dp = [0] * n for i in range(n): for j in range(i - 1, -1, -1): if c[i] != c[j]: tmp = dp[j] dp[j] = max(dp[j], dp[i] + abs(s[i] - s[j])) dp[i] = max(dp[i], tmp + abs(s[i] - s[j])) return max(dp) for i in range(int(input())): print(solve())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
It has been truly said that love is not meant for everyone. Let me introduce you to a very sad story of my friend Broken Amit. Yes several times he has been left heartbroken. Looking at his poor condition even God is pity on him. God has given Amit a very nice opportunity to Kiss as many girls as he likes . There are N number of girls each having a "Love value" as well as a decrement value. So, Amit needs to kiss all the N girls exactly once in some order. In one step, he can kiss only a single girl and after every step girl's "Love value" decreases by an amount equal to her decrement value times her index (indexing starts from 0) . But once a girl has been kissed her "Love value" won't decrease in further steps. "Love value" for Amit in a particular step is equal to the sum of "Love values" of all the n girls before the beginning of that step, whereas the "Gross Love Value" of Broken Amit is sum of "Love Values" for Amit in each step. Now Amit not being a good analyzer wants your help to find the maximum "Gross Love Value" he can earn. Input First line contains an integer N - denoting the number of girls. Second line contains N integers denoting the initial "Love value" of the girls (L[i]). Third line contains N integers denoting the "decrement value of the girls (D[i]). Output Single integer denoting maximum "Gross Love Value", Broken Amit can earn. Constraints 1 ≤ n ≤ 17 0 ≤ L[i] < 500000 0 ≤ D[i] < 500000 SAMPLE INPUT 2 2 4 1 2 SAMPLE OUTPUT 12 Explanation If Amit kisses the first girl and then the 2nd one - 1st step - 2 + 4 = 6 2nd step - 2 + (4 - 12) = 4 Gross Love Value - 6+4 = 10 If Amit kisses the 2nd girl than the 1st one. 1st step - 2 + 4 = 6 2nd step - (2 - 01) + 4 = 6 Gross Love Value = 12
def sumlval(lst, n): res = 0 for i in range(n): res += lst[i][0] return res def update(lst, k, n): for i in range(k, n): lst[i][0] -= lst[i][1] * lst[i][2] n = int(input()) lval = list(map(int, input().split())) dval = list(map(int, input().split())) lst = [] for i in range(n): lst.append([lval[i], dval[i], i]) kfunc = lambda x: -(x[1] * x[2]) lst.sort(key=kfunc) ans = 0 for i in range(n): ans += sumlval(lst, n) update(lst, i + 1, n) print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 LIST VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake. Input The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix a. aij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point. Output Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake. Examples Input 2 0 0.5 0.5 0 Output 0.500000 0.500000 Input 5 0 1 1 1 1 0 0 0.5 0.5 0.5 0 0.5 0 0.5 0.5 0 0.5 0.5 0 0.5 0 0.5 0.5 0.5 0 Output 1.000000 0.000000 0.000000 0.000000 0.000000
n = int(input()) b = [] for i in range(n): b.append(list(map(float, input().split()))) ma = 1 << n dp = [(0) for j in range(ma)] dp[0] = 1 for mask in range(1, ma): l = n - bin(mask).count("1") + 1 res = l * (l - 1) // 2 for i in range(n): if mask & 1 << i: for j in range(n): if not mask & 1 << j: dp[mask] += dp[mask ^ 1 << i] * b[j][i] / res ans = [] for i in range(n): ans.append(dp[ma - 1 - (1 << i)]) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake. Input The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix a. aij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point. Output Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake. Examples Input 2 0 0.5 0.5 0 Output 0.500000 0.500000 Input 5 0 1 1 1 1 0 0 0.5 0.5 0.5 0 0.5 0 0.5 0.5 0 0.5 0.5 0 0.5 0 0.5 0.5 0.5 0 Output 1.000000 0.000000 0.000000 0.000000 0.000000
import sys input = sys.stdin.readline def count_bits(x): cnt = 0 for i in range(n): if 1 << i & x: cnt += 1 return cnt n = int(input()) a = [list(map(float, input().split())) for i in range(n)] dp = [(0) for i in range(1 << n)] dp[-1] = 1 for mask in range((1 << n) - 1, -1, -1): val = count_bits(mask) total = val * (val - 1) // 2 for i in range(n): if mask & 1 << i == 0: continue for j in range(n): if mask & 1 << j == 0 or i == j: continue dp[mask ^ 1 << j] += dp[mask] * a[i][j] / total for i in range(n): print(dp[1 << i])
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake. Input The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix a. aij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point. Output Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake. Examples Input 2 0 0.5 0.5 0 Output 0.500000 0.500000 Input 5 0 1 1 1 1 0 0 0.5 0.5 0.5 0 0.5 0 0.5 0.5 0 0.5 0.5 0 0.5 0 0.5 0.5 0.5 0 Output 1.000000 0.000000 0.000000 0.000000 0.000000
n = int(input()) probs = list() for i in range(n): probs.append(list(map(float, input().split()))) dp = [list([(0) for i in range(1 << n)]) for i in range(n)] dp[0][(1 << n) - 1] = 1 ak = [list() for i in range(n + 1)] for i in range(1 << n): ak[bin(i).count("1")].append(i) for k in range(1, n): for ele in ak[n - k + 1]: for j in range(n): if ele & 1 << j: for w in range(n): if ele & 1 << w and j != w: dp[k][ele - (1 << j)] += ( dp[k - 1][ele] * probs[w][j] / ((n - k + 1) * (n - k) / 2) ) for i in range(n): print(dp[n - 1][1 << i], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR STRING EXPR FUNC_CALL VAR
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake. Input The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix a. aij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point. Output Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake. Examples Input 2 0 0.5 0.5 0 Output 0.500000 0.500000 Input 5 0 1 1 1 1 0 0 0.5 0.5 0.5 0 0.5 0 0.5 0.5 0 0.5 0.5 0 0.5 0 0.5 0.5 0.5 0 Output 1.000000 0.000000 0.000000 0.000000 0.000000
from sys import stdin input = stdin.readline def count(n): value = 0 while n: n &= n - 1 value += 1 return value def nc2(n): return n * (n - 1) // 2 def answer(): dp = [0] * (1 << n) dp[(1 << n) - 1] = 1 for mask in range((1 << n) - 1, 0, -1): m = count(mask) if m == 1: continue p = 1 / nc2(m) for i in range(n): for j in range(n): if i == j: continue if mask >> i & 1 and mask >> j & 1: next_mask = mask ^ 1 << j dp[next_mask] += dp[mask] * p * a[i][j] for i in range(n): print(dp[1 << i], end=" ") n = int(input()) a = [list(map(float, input().split())) for i in range(n)] answer() print()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake. Input The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix a. aij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point. Output Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake. Examples Input 2 0 0.5 0.5 0 Output 0.500000 0.500000 Input 5 0 1 1 1 1 0 0 0.5 0.5 0.5 0 0.5 0 0.5 0.5 0 0.5 0.5 0 0.5 0 0.5 0.5 0.5 0 Output 1.000000 0.000000 0.000000 0.000000 0.000000
n = int(input()) p = [] for i in range(n): la = list(map(float, input().split())) p.append(la) full_bit = (1 << n) - 1 dp = [0] * full_bit + [1] for i in range(full_bit, 0, -1): cunt = bin(i)[2:].count("1") if cunt == 1 or dp[i] == 0: continue mul = 1 / (cunt * (cunt - 1) >> 1) for x in range(n): if i & 1 << x == 0: continue for y in range(x + 1, n): if i & 1 << y == 0: continue dp[i - (1 << y)] += dp[i] * p[x][y] * mul dp[i - (1 << x)] += dp[i] * p[y][x] * mul ans = [] for i in range(n): ans.append(dp[1 << i]) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER STRING IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
import sys def get_ints(): return list(map(int, sys.stdin.readline().strip().split())) N = int(input()) nums = get_ints() s = sum(nums) dp = [False] * (s + 1) dp[0] = True for a in nums: for b in range(s - a, -1, -1): if dp[b]: dp[a + b] = True if s & 1 or not dp[s // 2]: print(0) else: res = -1 norm = 30 for i in range(N): n = nums[i] cnt = 0 while not n & 1: cnt += 1 n >>= 1 if cnt < norm: norm = cnt res = i print(1) print(res + 1)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def partition(arr): n = len(arr) k = sum(arr) m = k // 2 p = [([False] * (n + 1)) for i in range(m + 1)] p[0] = [True] * (n + 1) for i in range(1, m + 1): for j in range(1, n + 1): x = arr[j - 1] if i - x >= 0: p[i][j] = p[i][j - 1] or p[i - x][j - 1] else: p[i][j] = p[i][j - 1] return p[m][n] n = int(input()) a = [int(x) for x in input().split()] while all(x % 2 == 0 for x in a): a = [(x // 2) for x in a] k = sum(a) if k % 2 == 1: result = 0 elif not partition(a): result = 0 else: result = 1 for i in range(n): if a[i] % 2 == 1: break print(result) if result == 1: print(i + 1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
import sys def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int, sys.stdin.readline().rstrip().split()) def LI(): return list(map(int, sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int, sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) n = I() A = LI() X = [] for a in A: x = a count = 0 while x % 2 == 0: x //= 2 count += 1 X.append(count) m = min(X) for i in range(n): A[i] //= 2**m s = sum(A) if s % 2 == 1: print(0) exit() dp = [([0] * (s // 2 + 1)) for _ in range(n + 1)] dp[0][0] = 1 for i in range(1, n + 1): a = A[i - 1] for j in range(s // 2 + 1): if dp[i - 1][j]: dp[i][j] = 1 if j >= a and dp[i - 1][j - a]: dp[i][j] = 1 if dp[-1][-1]: print(1) for i in range(n): if A[i] % 2 == 1: print(i + 1) break else: print(0)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR 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 ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def possible(nums, target): reach = {0} for num in nums: reach |= {(i + num) for i in reach if i + num <= target} if target in reach: return True return False n = int(input()) arr = list(map(int, input().split())) s = sum(arr) if s % 2 == 1 or not possible(arr, s // 2): print(0) else: print(1) mi = 100 res = -1 for i in range(n): A = arr[i] & -arr[i] cnt = 0 while A: cnt += 1 A >>= 1 if cnt < mi: mi = cnt res = i + 1 print(res)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
n = int(input()) a = list(map(int, input().split())) S = sum(a) dp = [False] * (S + 1) dp[0] = True for x in a: for y in range(S - x, -1, -1): dp[x + y] = dp[x + y] or dp[y] if S % 2 == 1 or not dp[S // 2]: print(0) exit() else: norm = 30 res = -1 for i, x in enumerate(a): cnt = 0 while x & 1 == 0: cnt += 1 x >>= 1 if cnt < norm: norm = cnt res = i print(1) print(res + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
import sys input = sys.stdin.readline n = int(input()) l = list(map(int, input().split())) dp = [([-1] * 200003) for _ in range(103)] def fun(i, s): if s == 0: return 1 if s < 0 or i >= n: return 0 if dp[i][s] != -1: return dp[i][s] v = fun(i + 1, s - l[i]) u = fun(i + 1, s) dp[i][s] = max(u, v) return dp[i][s] su = sum(l) if su % 2 == 1: print(0) else: a = fun(0, sum(l) // 2) if a == 0: print(0) else: print(1) ma = 100 ans = 1 for i in range(len(l)): ss = 0 while l[i] % 2 == 0: ss += 1 l[i] //= 2 ma = min(ma, ss) if ma == ss: ans = i + 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def isSubsetSum(arr, n, sm): subset = [[(False) for j in range(sm + 1)] for i in range(3)] for i in range(n + 1): for j in range(sm + 1): if j == 0: subset[i % 2][j] = True elif i == 0: subset[i % 2][j] = False elif arr[i - 1] <= j: subset[i % 2][j] = ( subset[(i + 1) % 2][j - arr[i - 1]] or subset[(i + 1) % 2][j] ) else: subset[i % 2][j] = subset[(i + 1) % 2][j] return subset[n % 2][sm] def solve(n, arr): s = sum(arr) if s % 2 != 0: print(0) return sn = s // 2 if isSubsetSum(arr, n, sn) == False: print(0) return for i in range(n): if arr[i] % 2 != 0: print(1) print(i + 1) return while True: for i in range(n): arr[i] = arr[i] // 2 if arr[i] % 2 != 0: print(1) print(i + 1) return n = int(input()) arr = list(map(int, input().split(" "))) solve(n, arr)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR RETURN VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN WHILE NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def highestPowerOf2(n): return n & ~(n - 1) def solve(a): s = sum(a) q, r = divmod(s, 2) if r == 1: return 0, 0 else: x = 1 for ai in a: x |= x << ai if not x >> q & 1: return 0, 0 else: powers2 = [highestPowerOf2(ai) for ai in a] return 1, powers2.index(min(powers2)) n = int(input()) a = list(map(int, input().split())) ans = solve(a) print(ans[0]) if ans[0]: print(ans[1] + 1)
FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def answer(): s = sum(a) dp = [[(False) for i in range(s + 1)] for j in range(n + 1)] hs = False dp[0][0] = True for i in range(n): for j in range(s + 1): dp[i + 1][j] |= dp[i][j] if dp[i][j]: dp[i + 1][j + a[i]] = True if j == s - j: hs = True break if not hs: print(0) return print(1) for i in range(30): for j in range(n): if a[j] >> i & 1: print(j + 1) return n = int(input()) a = list(map(int, input().split())) answer()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
I = input IN = lambda x: map(int, x.split()) L = lambda x: list(IN(x)) n = int(I()) a = L(I()) def find(a, n): s = sum(a) if s % 2 == 1: print(0) return t = 1 for i in a: t |= t << i if t & 1 << s // 2: pass else: print(0) return z = 0 p = 1024 for i in range(n): t = a[i] & -a[i] if t < p: z = i p = t if p == 1: break print(1) print(z + 1) find(a, n)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
import sys def zip_sorted(a, b): a, b = zip(*sorted(zip(a, b))) sorted(zip(a, b), key=lambda x: x[1]) return a, b def number_to_list(a): b = [] while a >= 1: c = a % 10 a = int(a / 10) b.append(c) return list(reversed(b)) def str_list_to_int_list(a): a = list(map(int, a)) return a def make_1d_array(n, inti=0): a = [inti for _ in range(n)] return a def make_2d_array(n, m, inti=0): a = [[inti for _ in range(m)] for _ in range(n)] return a def make_3d_array(n, m, k, inti=0): a = [[[inti for _ in range(k)] for _ in range(m)] for _ in range(n)] return a def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) input = sys.stdin.readline I = lambda: list(map(int, input().split())) S = lambda: list(map(str, input())) (n,) = I() a = I() sum1 = sum(a) if sum1 % 2 == 0: sum1 = sum1 // 2 prev = [False] * (sum1 + 1) prev[0] = True prev_a = [[] for j in range(sum1 + 1)] for i in range(n): dp = prev.copy() dp_a = [prev_a[j] for j in range(len(prev_a))] for j in range(sum1 + 1): if j - a[i] >= 0: if prev[j - a[i]]: if dp[j]: if len(dp_a[j]) > len(prev_a[j - a[i]]) + 1: dp_a[j] = prev_a[j - a[i]] + [i + 1] else: dp_a[j] = prev_a[j - a[i]] + [i + 1] dp[j] = dp[j] or prev[j - a[i]] prev = dp.copy() prev_a = [dp_a[j] for j in range(len(dp_a))] idx = -1 if dp[sum1]: min1 = 10**10 for i in range(sum1 + 1): if dp[i] and (i % 2 != 0 or dp[(2 * sum1 - i) // 2] == False): if len(dp_a[i]) < min1: min1 = len(dp_a[i]) idx = i print(len(dp_a[idx])) print(*dp_a[idx]) else: print(0) else: print(0)
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def can1timsum(nums, i, val, memo={}): if val in memo: print(i, val, "in memo") return memo[val] if val == 0: return True if val < 0 or i < 0: return False result = can1timsum(nums, i - 1, val - nums[i], memo) or can1timsum( nums, i - 1, val, memo ) memo[val] = result return result def cansum(nums, i, val, memo={}, numeros=[]): if val in memo: posible, i2 = memo[val] if posible: if i2 < i: print("actualiza", i, end="") memo[val] = True, i2 return True if i2 >= i: return False if val == 0: return True if val < 0 or i < 0: return False result = cansum(nums, i - 1, val - nums[i], memo, []) or cansum( nums, i - 1, val, memo, [] ) memo[val] = result, i return result def solvecaso(): n = int(input()) nums = list(map(int, input().split())) nones = 0 while True: for num in nums: if num % 2 == 1: nones += 1 if nones != 0: break for i in range(n): nums[i] //= 2 suma = 0 for num in nums: suma += num if suma % 2 == 1: return "0" if not cansum(nums, n - 1, suma // 2, {}): return "0" index = 0 for num in nums: if num % 2 == 1: break index += 1 return f"1\n{index + 1}" print(solvecaso())
FUNC_DEF DICT IF VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF DICT LIST IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR IF VAR VAR EXPR FUNC_CALL VAR STRING VAR STRING ASSIGN VAR VAR NUMBER VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR LIST ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN STRING IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER DICT RETURN STRING ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
n = int(input()) a = list(map(int, input().split())) s = sum(a) if s % 2 == 1: print(0) exit() dp = [([False] * (s // 2 + 1)) for _ in range(n + 1)] dp[0][0] = True for i in range(n): for j in range(s // 2 + 1): if j < a[i]: dp[i + 1][j] = dp[i][j] else: dp[i + 1][j] |= dp[i][j - a[i]] | dp[i][j] if not dp[n][s // 2]: print(0) else: l = [] for i in a: cnt = 0 while i % 2 == 0: i //= 2 cnt += 1 l.append(cnt) print(1) print(l.index(min(l)) + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def check(l): for v in l: if v % 2 == 1: return False else: return True def findTargetSumWays(nums): sumAll = sum(nums) if 0 > sumAll or (0 + sumAll) % 2: return 0 target = (0 + sumAll) // 2 dp = [0] * (target + 1) dp[0] = 1 for num in nums: for j in range(target, num - 1, -1): dp[j] = dp[j] + dp[j - num] return dp[-1] def getResult(n, a_list): while check(a_list): a_list = [(v // 2) for v in a_list] if findTargetSumWays(a_list) == 0: return 0, None for i in range(n): if a_list[i] % 2 == 1: return 1, i + 1 n = int(input()) a_list = [int(tt) for tt in input().split()] r1, r2 = getResult(n, a_list) print(r1) if r2: print(r2)
FUNC_DEF FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER FUNC_DEF WHILE FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER NONE FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
maxn = 200700 n = int(input()) a = list(map(int, input().split())) dp = [0] * (2 * maxn) dp[0] = 1 s = sum(a) if s & 1: print(0) exit() for v in a: for i in range(s, -1, -1): dp[i + v] |= dp[i] if dp[s // 2] == 0: print(0) else: for i, v in enumerate(a, 1): if v & 1 or dp[(s - v) // 2] == 0: print(1) print(i) break else: print(0)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n = int(input()) lst = list(map(int, input().split())) def can_partition(num): s = sum(num) if s % 2 != 0: return False dp = [[(-1) for x in range(int(s / 2) + 1)] for y in range(len(num))] return True if can_partition_recursive(dp, num, int(s / 2), 0) == 1 else False def can_partition_recursive(dp, num, sum, currentIndex): if sum == 0: return 1 n = len(num) if n == 0 or currentIndex >= n: return 0 if dp[currentIndex][sum] == -1: if num[currentIndex] <= sum: if ( can_partition_recursive( dp, num, sum - num[currentIndex], currentIndex + 1 ) == 1 ): dp[currentIndex][sum] = 1 return 1 dp[currentIndex][sum] = can_partition_recursive(dp, num, sum, currentIndex + 1) return dp[currentIndex][sum] sumN = sum(lst) if sumN % 2: print(0) else: ans = 0 if not can_partition(lst): print(0) else: print(1) while True: odd = None for i in range(n): if lst[i] % 2 == 1: print(i + 1) sys.exit() lst[i] //= 2
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
input() aa = list(map(int, input().split())) ans = [] n = len(aa) saa = sum(aa) if saa & 1 == 0: k = saa // 2 dp = [([False] * (k + 1)) for _ in range(n + 1)] for i in range(n + 1): dp[i][0] = True for i in range(1, n + 1): for cs in range(1, k + 1): dp[i][cs] = dp[i - 1][cs] if aa[i - 1] <= cs: dp[i][cs] |= dp[i - 1][cs - aa[i - 1]] if dp[n][k]: x = 2 while not ans: for i, a in enumerate(aa): if a % x != 0: ans = [i] break x *= 2 print(len(ans)) if ans: ans[0] += 1 print(" ".join(map(str, ans)))
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
from sys import stdin, stdout get_string = lambda: stdin.readline().strip(" ") get_intmap = lambda: map(int, get_string().split(" ")) def testcase(): n = int(input()) l = list(get_intmap()) suml = sum(l) if suml % 2 == 1: print(0) return dp = 1 for i in l: dp |= dp << i if dp & 1 << suml // 2: print(1) min_lsb_pos = 32 min_ind = -1 for ind, i in enumerate(l): lsb_pos = 0 while i & 1 << lsb_pos == 0: lsb_pos += 1 if min_lsb_pos > lsb_pos: min_lsb_pos = lsb_pos min_ind = ind print(min_ind + 1) else: print(0) testcase() quit() for t in range(int(input())): testcase()
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def good(a): s = sum(a) if s % 2 == 1: return False can = [False] * (s // 2 + 1) can[0] = True for e in a: for i in range(len(can) - 1 - e, -1, -1): if can[i]: can[i + e] = True return can[-1] def calc(n): r = 0 while n % 2 == 0: n //= 2 r += 1 return r def solve(n, a): if not good(a): return [] return [min(range(n), key=lambda i: calc(a[i])) + 1] n = int(input()) a = list(map(int, input().split())) res = solve(n, a) print(len(res)) if res: print(*res)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR RETURN LIST RETURN LIST BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
for _ in range(1): n = int(input()) a = list(map(int, input().split())) b = a[:] a.sort() a = a if sum(a) % 2 == 1: print(0) continue add = sum(a) arr1 = [0] * (add + 2) for i in range(n - 1): arr1[a[i]] = 1 for j in range(add, -1, -1): if arr1[j] > 0: arr1[j + a[i + 1]] = 1 arr1[a[i + 1]] = 1 if arr1[add // 2] == 0: print(0) continue indx = -1 mini = 99999999999 for i in range(n): j = 0 while b[i] % 2**j == 0: j += 1 if j < mini: mini = j indx = i print(1) print(indx + 1)
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
n = int(input()) a = list(map(int, input().split())) s = sum(a) if s % 2: print(0) exit() dp = [[(0) for i in range(s + 1)] for j in range(n)] for i in range(n): dp[i][0] = 1 dp[0][a[0]] = 1 for i in range(1, n): for j in range(1, s + 1): if j > a[i]: dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i]]) dp[i][j] = max(dp[i][j], dp[i - 1][j]) if dp[-1][s // 2] == 0: print(0) exit() m = 10**18 for i in range(n): b = bin(a[i])[2:][::-1] ind = b.index("1") if ind < m: m = ind ans = i print(1) print(ans + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def bad(v): global n s = 0 for i in v: s += i if s % 2 != 0: return 0 b = 2**200005 for i in v: b |= b >> i r = b // 2 ** (200005 - s // 2) % 2 return r pw = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] n = int(input()) m = [int(i) for i in input().split()] if bad(m): mj = len(pw) for i in range(len(m)): if m[i] % 2 != 0: t = i + 1 break for j in range(mj): if m[i] % pw[j] != 0: if mj > j: mj = j t = i + 1 print(1, t, sep="\n") else: print(0)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR STRING EXPR FUNC_CALL VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
N = int(input()) A = list(map(int, input().split())) S = sum(A) if S % 2: print(0) exit() val = S // 2 num = [False] * 200001 num[0] = True for i in A: tmp = num[:] for j in range(200001): if tmp[j]: if j + i <= 200000: num[i + j] = True if not num[val]: print(0) exit() while True: odd = [] even = [] for i in A: if i % 2: odd.append(i) else: even.append(i) if not odd: for i in range(N): A[i] = A[i] // 2 S = sum(A) continue elif odd: print(1) print(A.index(odd[0]) + 1) exit()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def inp(): return [int(a) for a in input().split()] def gcd(x, y): if y: return gcd(y, x % y) else: return x def bad(array): st = set() st.add(0) for i in array: st1 = set() for j in st: st1.add(i + j) st = st.union(st1) if sum(array) // 2 in st: return True return False ind = [] n = int(input()) a = inp() g = a[0] for i in range(1, n): g = gcd(g, a[i]) for i in range(n): a[i] //= g if sum(a) % 2 == 1: print(0) elif bad(a): for i in range(n): if a[i] % 2: print(1) print(i + 1) break else: print(0)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER 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 IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
import time def main(): n = i_input() a = li_input() p = [0] * n s = 0 for i, aa in enumerate(a): s += aa c = 0 while not aa % 2: aa //= 2 c += 1 p[i] = c if s % 2: print(0) return a.sort() limit = s // 2 l = [0] for aa in a: b = [] p1 = p2 = 0 while p1 < len(l): while p2 < len(l) and l[p1] + aa >= l[p2] and l[p2] <= limit: b.append(l[p2]) p2 += 1 if l[p1] + aa != b[-1] and l[p1] + aa <= limit: b.append(l[p1] + aa) p1 += 1 l = b if l[-1] != limit: print(0) return minp = 100, -1 for i in range(n): minp = min(minp, (p[i], i + 1)) print(1) print(minp[1]) def i_input(): return int(input()) def l_input(): return input().split() def li_input(): return list(map(int, l_input())) TT = time.time() main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
import sys def findMin(S, n): total = sum(S) T = [([False] * (total + 1)) for _ in range(len(S) + 1)] for i in range(len(S) + 1): T[i][0] = True j = 1 while i > 0 and j <= total: T[i][j] = T[i - 1][j] if S[i - 1] <= j: T[i][j] |= T[i - 1][j - S[i - 1]] j = j + 1 j = total // 2 while j >= 0 and not T[len(S)][j]: j = j - 1 return total - 2 * j n = int(input()) a = list(map(int, input().split())) cou = 1 c = a[0] for i in range(1, n): if a[i] == c: cou += 1 if sum(a) % 2 != 0: print(0) elif cou == n: if cou % 2 == 0: print(1) print(1) else: print(0) elif findMin(a, n) > 0: print(0) else: min = 10000 ans = -1 for i in range(n): num = a[i] count = 0 while num % 2 == 0: num = num // 2 count += 1 if count < min: min = count ans = i print(1) print(ans + 1)
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
from sys import stdin def read_int(): return int(stdin.readline()) def read_ints(): return map(int, stdin.readline().split(" ")) n = read_int() a = list(read_ints()) s = sum(a) if s % 2 == 1: print(0) exit(0) dp = [False] * (s // 2 + 1) dp[0] = True for ai in a: for j in range(s // 2, ai - 1, -1): dp[j] = dp[j] or dp[j - ai] if not dp[s // 2]: print(0) exit(0) while True: if sum(a) % 2 == 1: print(0) exit(0) else: odd = -1 for i in range(n): if a[i] % 2 == 1: print(1) print(i + 1) exit(0) a = [(x // 2) for x in a]
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
N = int(input()) aa = [int(x) for x in input().split()] def get_max_pow2_factor(x): result = 0 cur_a = x while cur_a > 0 and cur_a % 2 == 0: cur_a //= 2 result += 1 return result shared_pow2_factor = min([get_max_pow2_factor(x) for x in aa]) if shared_pow2_factor > 0: aa = [(x // 2**shared_pow2_factor) for x in aa] odd_a = None odd_a_index = None for i in range(len(aa)): if aa[i] % 2 != 0: odd_a = aa[i] odd_a_index = i break if sum(aa) % 2 == 1: print(0) else: sum_a = sum(aa) target = sum_a // 2 aa = sorted(aa, key=lambda x: -x) dp = set() have_solution = False for a in aa: new_add = set() for num in dp: new_add.add(num + a) new_add.add(a) dp = dp | new_add if target in dp: have_solution = True break if have_solution: print(1) print(odd_a_index + 1) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
n = int(input()) a = list(map(int, input().split())) s = sum(a) if s % 2 == 1: print(0) else: s = s // 2 dp = [[(False) for i in range(s + 1)] for _ in range(len(a) + 1)] for i in range(len(dp)): dp[i][0] = True for i in range(1, len(a) + 1): for j in range(1, s + 1): if j >= a[i - 1]: dp[i][j] = dp[i - 1][j] or dp[i - 1][j - a[i - 1]] else: dp[i][j] = dp[i - 1][j] if not dp[n][s]: print(0) else: f = 0 for i in range(len(a)): if a[i] % 2 == 1: print(1) print(i + 1) f = 1 break if f == 0: f = 1 while f: for i in range(len(a)): if a[i] % 2 == 1: print(1) print(i + 1) f = 0 break a[i] = a[i] // 2
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP 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 VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
import sys n = int(input()) a = [int(x) for x in input().split()] z = sum(a) if z % 2: print(0) sys.exit() s = set([0]) for x in a: s.update([(t + x) for t in s]) if z // 2 not in s: print(0) sys.exit() b = [(x & -x) for x in a] i = b.index(min(b)) print(f"1\n{i + 1}")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
import sys n = int(input()) array = [int(x) for x in input().split()] if sum(array) % 2 == 0: s = 1 for i in array: s |= s << i if s & 1 << sum(array) // 2: b = [(x & -x) for x in array] i = b.index(min(b)) print(f"1\n{i + 1}") sys.exit() print(0)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
def solve(arr): s = sum(arr) if s & 1: print(0) return half = s // 2 dp = [False] * (half + 1) dp[0] = True for a in arr: if a > half: print(0) return for j in range(half, a - 1, -1): dp[j] = dp[j] or dp[j - a] if not dp[-1]: print(0) return found = [None] * 13 for idx, a in enumerate(arr): for i in range(13): if not found[i] and a >> i & 1: found[i] = idx + 1 for i in range(13): if found[i] is not None: print(1) print(found[i]) return n = int(input()) arr = [int(x) for x in input().split()] solve(arr)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP LIST NONE NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NONE EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
n = int(input()) a = list(map(int, input().split())) sums = sum(a) b = [] for i in range(n): b.append([i + 1]) dp = dict(zip(a, b)) for i in range(n): for j in range(sums // 2, 0, -1): if j - a[i] in dp and i + 1 not in dp[j - a[i]]: dp[j] = list(dp[j - a[i]]) dp[j].append(i + 1) if sums % 2 == 1 or sums // 2 not in dp: print(0) else: flag = True while flag: for i in range(n): if a[i] % 2 == 1: print(1) print(i + 1) flag = False break else: a[i] = a[i] // 2 if flag: sums = sums // 2 if sums % 2 == 1: print(0) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER
Baby Ehab was toying around with arrays. He has an array $a$ of length $n$. He defines an array to be good if there's no way to partition it into $2$ subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in $a$ so that it becomes a good array. Can you help him? A sequence $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into $2$ subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements. -----Input----- The first line contains an integer $n$ ($2 \le n \le 100$) — the length of the array $a$. The second line contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 2000$) — the elements of the array $a$. -----Output----- The first line should contain the minimum number of elements you need to remove. The second line should contain the indices of the elements you're removing, separated by spaces. We can show that an answer always exists. If there are multiple solutions, you can print any. -----Examples----- Input 4 6 3 9 12 Output 1 2 Input 2 1 2 Output 0 -----Note----- In the first example, you can partition the array into $[6,9]$ and $[3,12]$, so you must remove at least $1$ element. Removing $3$ is sufficient. In the second example, the array is already good, so you don't need to remove any elements.
N = int(input()) A = list(map(int, input().split())) def findPartiion(arr, n): Sum = 0 for i in range(n): Sum += arr[i] if Sum % 2 != 0: return 0 part = [0] * (Sum // 2 + 1) for i in range(Sum // 2 + 1): part[i] = 0 for i in range(n): for j in range(Sum // 2, arr[i] - 1, -1): if part[j - arr[i]] == 1 or j == arr[i]: part[j] = 1 return part[Sum // 2] if not findPartiion(A, N): print(0) else: mod = 2 Done = False while 1: for i, elt in enumerate(A): if elt % mod != 0: print(1) print(i + 1) Done = True break if Done: break mod *= 2
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER