description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): ans = set() def f(ip, op=[]): if ip < 0: return if ip == 0: ans.add(tuple(sorted(op[:], reverse=True))) return for d in range(ip, 0, -1): if len(op) == 0 or op[-1] >= d: op.append(d) f(ip - d, op) op.remove(d) f(n) return sorted(ans, reverse=True)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF LIST IF VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def memoed(self, n, memo): if n == 0: return [] if n == 1: return [[1]] if n in memo: return memo[n] res = [[n]] for i in range(n, 0, -1): for j in self.memoed(n - i, memo): if i >= j[0]: res.append([i] + j) memo[n] = res return memo[n] def UniquePartitions(self, n): return self.memoed(n, {})
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN LIST IF VAR NUMBER RETURN LIST LIST NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST LIST VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR DICT
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): output = [[0]] for i in range(n, 0, -1): temp = [] for item in output: j = 1 temp2 = [] while item[0] + j * i <= n: temp2.append(i) temp.append([item[0] + j * i] + item[1:].copy() + temp2.copy()) j += 1 output += temp return sorted([item[1:] for item in output if item[0] == n], reverse=True)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: count = 0 def UniquePartitions(self, n): table = [[[1]]] + [None] * (n - 1) for i in range(1, n): table[i] = [[i + 1]] for k in range(i): table[i].extend([([i - k] + l) for l in table[k] if i - k >= l[0]]) return table[-1]
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST LIST LIST NUMBER BIN_OP LIST NONE BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR LIST LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP LIST BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR NUMBER
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): ans = [] def printArray(p, n, ans): res = [] for i in range(0, n): res.append(p[i]) ans.append(res) return ans p = [0] * n k = 0 p[k] = n while True: ans = printArray(p, k + 1, ans) rem_val = 0 while k >= 0 and p[k] == 1: rem_val += p[k] k -= 1 if k < 0: break p[k] -= 1 rem_val += 1 while rem_val > p[k]: p[k + 1] = p[k] rem_val = rem_val - p[k] k += 1 p[k + 1] = rem_val k += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def __init__(self): self.ans = [] def fun(self, arr, i, target): if not target: self.ans.append(arr) return if i == 0: return if i <= target: self.fun(arr + [i], i, target - i) self.fun(arr, i - 1, target) return def UniquePartitions(self, n): i = n self.fun([], i, n) return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): res = [[n]] arr = [(n - i) for i in range(n)] def helper(res, index, temp, _sum, arr): if _sum > n or index == n: return if sum(temp) == n: res.append(temp) return helper(res, index, temp + [arr[index]], _sum + arr[index], arr) helper(res, index + 1, temp, _sum, arr) helper(res, 1, [], 0, arr) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER LIST NUMBER VAR RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): ans = [] self.dfs(n, 0, [], ans) return ans def dfs(self, n, s, path, ans): if s == n: ans.append(path) return if s > n: return for i in range(n, 0, -1): if len(path) == 0 or path[-1] >= i: self.dfs(n, s + i, path + [i], ans)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER LIST VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR LIST VAR VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def __init__(self): self.res = [] self.sum1 = None def solve(self, n, pat, sum1): if sum1 > self.sum1 or n <= 0: return if sum1 == self.sum1: self.res.append(pat) return self.solve(n, pat + [n], sum1 + n) self.solve(n - 1, pat, sum1) def UniquePartitions(self, n): self.sum1 = n self.solve(n, [], 0) return self.res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NONE FUNC_DEF IF VAR VAR VAR NUMBER RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR LIST NUMBER RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): curr = [] a = [] for i in range(n, 0, -1): a.append(i) res = [] def backtr(i, su): if su == n: res.append(curr.copy()) return if i >= n or su > n: return curr.append(a[i]) backtr(i, su + a[i]) curr.pop() backtr(i + 1, su) return res return backtr(0, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): res = [] array = [(n - i) for i in range(n)] def helper(arr, target, res, temp, index): if target == 0: res.append(temp) return if target < 0 or index == n: return helper(arr, target - arr[index], res, temp + [arr[index]], index) helper(arr, target, res, temp, index + 1) helper(array, n, res, [], 0) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR LIST NUMBER RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): partitions = [[n]] copier = [n] while copier[0] != 1: remainder = 0 while copier[-1] == 1: remainder += copier.pop(-1) copier[-1] -= 1 remainder += 1 while remainder >= copier[-1]: copier.append(copier[-1]) remainder -= copier[-1] if remainder != 0: copier.append(remainder) partitions.append(copier[:]) return partitions
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST VAR ASSIGN VAR LIST VAR WHILE VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def __init__(self): self.unique_parts = [] def UniquePartitions(self, n): q = self.partitions(n, n, []) return self.unique_parts def partitions(self, curr_num, curr_sum, curr_part): if curr_sum < 0 or curr_num <= 0: return -1 curr_part.append(curr_num) if curr_sum - curr_num == 0: self.unique_parts.append(curr_part[:]) q = self.partitions(curr_num, curr_sum - curr_num, curr_part) curr_part.pop() q = self.partitions(curr_num - 1, curr_sum, curr_part)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR LIST RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def back_track(self, res, nums, i, target, found): if target < 0: return elif target == 0: res.append(found.copy()) else: for j in range(i, n, 1): found.append(nums[j]) self.back_track(res, nums, j, target - nums[j], found) found.remove(nums[j]) return res def UniquePartitions(self, n): res = [] nums = [i for i in range(n, 0, -1)] target = n self.back_track(res, nums, 0, target, []) return res
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR LIST RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def __init__(self): self.ans = None def UniquePartitions(self, n): self.ans = [] self.findAns(n, [], n) return self.ans def findAns(self, n, cur_list, mx): if n == 0: self.ans.append(cur_list.copy()) return for v in range(min(mx, n), 0, -1): cur_list.append(v) self.findAns(n - v, cur_list, v) cur_list.pop()
CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
a = [] def getArray(n, z, val): if n == 0: a.append(z) elif val <= 0 or n < 0: return -1 else: getArray(n - val, z + [val], val) getArray(n, z, val - 1) class Solution: def UniquePartitions(self, n): global a a = [] getArray(n, [], n) return a
ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): r = [] def gorec(n, curr): if n == 0: r.append(curr.copy()) return for i in range(n, 0, -1): if curr and curr[-1] < i: continue curr.append(i) gorec(n - i, curr) curr.pop() gorec(n, []) return r
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): a = [] for j in range(1, n + 1): a.append(j) a = a[::-1] ans = [] def sub(i, s, su): if s == n: ans.append(su.copy()) return if i >= n or s > n: return su.append(a[i]) sub(i, s + a[i], su) su.pop() sub(i + 1, s, su) sub(0, 0, []) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER LIST RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): def fun(n, arr=[], sum=0, partition=[]): if sum == n: arr.append(partition) return if sum > n: return for i in range(n, 0, -1): if len(partition) == 0 or partition[-1] >= i: fun(n, arr, sum + i, partition + [i]) return arr return fun(n)
CLASS_DEF FUNC_DEF FUNC_DEF LIST NUMBER LIST IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR LIST VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): res = [] curr = [] def dfs(n, prev): nonlocal res, curr if n == 0: res.append(curr.copy()) return for i in range(min(prev, n), 0, -1): curr.append(i) dfs(n - i, i) curr.pop() dfs(n, n) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, k): dp = [[[] for j in range(k + 1)] for i in range(k + 1)] for i in range(k + 1): dp[0][i] = [[]] for i in range(1, k + 1): for j in range(1, k + 1): for g in dp[i][j - 1]: dp[i][j].append(g) if j <= i: for g in dp[i - j][j]: g_clone = g.copy() g_clone.append(j) dp[i][j].append(g_clone) for i in dp[-1][-1]: i.reverse() return dp[-1][-1][::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR FOR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR RETURN VAR NUMBER NUMBER NUMBER
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def util(self, a, n, S, s): global ans if S == 0: s = s[:-1] s = s.split(",") ans.append(s) return if n == 0: return if a[n - 1] <= S: self.util(a, n, S - a[n - 1], s + str(a[n - 1]) + ",") self.util(a, n - 1, S, s) else: self.util(a, n - 1, S, s) def UniquePartitions(self, n): global ans ans = [] a = [i for i in range(1, n + 1)] self.util(a, n, n, "") return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): uparts = self.UniquePartitionsLow(n) upl = list(uparts) upl.sort(reverse=True) return upl def UniquePartitionsLow(self, n, memo={}): if 0 == n: return set() if n in memo: return memo[n] uparts = set() uparts.add((n,)) if 1 == n: return uparts for k in range(1, n // 2 + 1): for upcl in self.UniquePartitionsLow(n - k, memo=memo): upl = [k] upl.extend(list(upcl)) upl.sort(reverse=True) ups = tuple(upl) uparts.add(ups) memo.setdefault(n, uparts) return uparts
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF DICT IF NUMBER VAR RETURN FUNC_CALL VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): def func(ind, tar, li, ds, ans): if ind == len(li): if tar == 0: ans.append(ds.copy()) return if li[ind] <= tar: ds.append(li[ind]) func(ind, tar - li[ind], li, ds, ans) ds.pop() func(ind + 1, tar, li, ds, ans) li = [i for i in range(n, 0, -1)] ds = [] ans = [] func(0, n, li, ds, ans) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def UniquePartitions(self, n): dp = {(1): ((1,),)} def recur(num): for i in range(2, num + 1): cur = [[i]] for j in range(1, i // 2 + 1): for k in dp[i - j]: cur.append(sorted(list(k) + [j], reverse=True)) dp[i] = tuple(set(map(tuple, cur))) recur(n) return sorted(dp[n], reverse=True)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR LIST VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER
Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. Example 1: Input: n = 3 Output: 3 2 1 1 1 1 Explanation: For n = 3, {3}, {2, 1} and {1, 1, 1}. Example 2: Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Explanation: For n = 4, {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. Your Task: You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. Expected Time Complexity: O(2^n) Expected Space Complexity: O(n) Constraints: 1 <= n <= 25
class Solution: def knapsack(self, n, val, i, arr, res, ans): if i >= n: if sum(ans) == n: j = ans.copy() j.sort(reverse=True) res.add(tuple(j)) return if arr[i] <= val: self.knapsack(n, val - arr[i], i, arr, res, ans + [arr[i]]) self.knapsack(n, val, i + 1, arr, res, ans) else: self.knapsack(n, val, i + 1, arr, res, ans) def UniquePartitions(self, n): arr = [x for x in range(1, n + 1)] res = set() self.knapsack(len(arr), n, 0, arr, res, []) res = sorted(list(res), reverse=True) return res
CLASS_DEF FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: sΒ could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters likeΒ .Β orΒ *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "a*" Output: true Explanation:Β '*' means zero or more of the precedengΒ element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example 3: Input: s = "ab" p = ".*" Output: true Explanation:Β ".*" means "zero or more (*) of any character (.)". Example 4: Input: s = "aab" p = "c*a*b" Output: true Explanation:Β c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". Example 5: Input: s = "mississippi" p = "mis*is*p*." Output: false
class Solution: cache = {} def isMatch(self, s, p): if (s, p) in self.cache: return self.cache[s, p] if not p: return not s if p[-1] == "*": if self.isMatch(s, p[:-2]): self.cache[s, p] = True return True if s and (s[-1] == p[-2] or p[-2] == ".") and self.isMatch(s[:-1], p): self.cache[s, p] = True return True if s and (p[-1] == s[-1] or p[-1] == ".") and self.isMatch(s[:-1], p[:-1]): self.cache[s, p] = True return True self.cache[s, p] = False return False
CLASS_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR RETURN VAR IF VAR NUMBER STRING IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: sΒ could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters likeΒ .Β orΒ *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "a*" Output: true Explanation:Β '*' means zero or more of the precedengΒ element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example 3: Input: s = "ab" p = ".*" Output: true Explanation:Β ".*" means "zero or more (*) of any character (.)". Example 4: Input: s = "aab" p = "c*a*b" Output: true Explanation:Β c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". Example 5: Input: s = "mississippi" p = "mis*is*p*." Output: false
class Solution: def isMatch(self, s, p): self.mem = {} self.t = s self.p = p return self.match(0, 0) def match(self, i, j): if (i, j) not in self.mem: if j == len(self.p): res = i == len(self.t) elif j + 1 < len(self.p) and self.p[j + 1] == "*": res = ( self.match(i, j + 2) or i < len(self.t) and self.p[j] in {self.t[i], "."} and self.match(i + 1, j) ) else: res = ( i < len(self.t) and self.p[j] in {self.t[i], "."} and self.match(i + 1, j + 1) ) self.mem[i, j] = res return self.mem[i, j]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER FUNC_DEF IF VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: sΒ could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters likeΒ .Β orΒ *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "a*" Output: true Explanation:Β '*' means zero or more of the precedengΒ element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example 3: Input: s = "ab" p = ".*" Output: true Explanation:Β ".*" means "zero or more (*) of any character (.)". Example 4: Input: s = "aab" p = "c*a*b" Output: true Explanation:Β c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". Example 5: Input: s = "mississippi" p = "mis*is*p*." Output: false
class Solution: def isMatch(self, s, p): self.maps = dict() return self.dfs(s, p) def dfs(self, s, p): key = s, p if key in self.maps: return self.maps[key] if not s and not p: return True if s and not p: return False if len(p) > 1 and p[1] == "*": if s and s[0] == p[0] or p[0] == "." and s: self.maps[key] = ( self.dfs(s[1:], p[2:]) or self.dfs(s[1:], p) or self.dfs(s, p[2:]) ) else: self.maps[key] = self.dfs(s, p[2:]) elif s and s[0] == p[0] or p[0] == "." and s: self.maps[key] = self.dfs(s[1:], p[1:]) return self.maps[key] if key in self.maps else False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING IF VAR VAR NUMBER VAR NUMBER VAR NUMBER STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER STRING VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR NUMBER
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: sΒ could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters likeΒ .Β orΒ *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "a*" Output: true Explanation:Β '*' means zero or more of the precedengΒ element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example 3: Input: s = "ab" p = ".*" Output: true Explanation:Β ".*" means "zero or more (*) of any character (.)". Example 4: Input: s = "aab" p = "c*a*b" Output: true Explanation:Β c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". Example 5: Input: s = "mississippi" p = "mis*is*p*." Output: false
class Solution: def isMatch(self, s, p): m, n = len(s), len(p) dp = [([False] * (n + 1)) for _ in range(m + 1)] dp[0][0] = True for j in range(2, n + 1): if p[j - 1] == "*": dp[0][j] = dp[0][j - 2] for i in range(1, m + 1): for j in range(1, n + 1): if p[j - 1] == "*": if p[j - 2] == s[i - 1] or p[j - 2] == ".": dp[i][j] = dp[i - 1][j] or dp[i][j - 1] or dp[i][j - 2] elif p[j - 2] != s[i - 1]: dp[i][j] = dp[i][j - 2] elif p[j - 1] == s[i - 1] or p[j - 1] == ".": dp[i][j] = dp[i - 1][j - 1] return dp[m][n]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER VAR VAR 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 IF VAR BIN_OP VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: sΒ could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters likeΒ .Β orΒ *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "a*" Output: true Explanation:Β '*' means zero or more of the precedengΒ element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example 3: Input: s = "ab" p = ".*" Output: true Explanation:Β ".*" means "zero or more (*) of any character (.)". Example 4: Input: s = "aab" p = "c*a*b" Output: true Explanation:Β c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". Example 5: Input: s = "mississippi" p = "mis*is*p*." Output: false
class Solution: def __init__(self): self.dp = {} def isMatch(self, text, pattern): if not pattern: return not text if (text, pattern) in self.dp: return self.dp[text, pattern] first_match = bool(text) and pattern[0] in {text[0], "."} if len(pattern) >= 2 and pattern[1] == "*": self.dp[text, pattern] = ( self.isMatch(text, pattern[2:]) or first_match and self.isMatch(text[1:], pattern) ) else: self.dp[text, pattern] = first_match and self.isMatch(text[1:], pattern[1:]) return self.dp[text, pattern]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: sΒ could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters likeΒ .Β orΒ *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "a*" Output: true Explanation:Β '*' means zero or more of the precedengΒ element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example 3: Input: s = "ab" p = ".*" Output: true Explanation:Β ".*" means "zero or more (*) of any character (.)". Example 4: Input: s = "aab" p = "c*a*b" Output: true Explanation:Β c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". Example 5: Input: s = "mississippi" p = "mis*is*p*." Output: false
class Solution: def isMatch(self, s, p): dp = [([False] * (len(p) + 1)) for _ in range(len(s) + 1)] dp[-1][-1] = True for i in range(len(s), -1, -1): for j in range(len(p) - 1, -1, -1): first_match = i < len(s) and p[j] in {s[i], "."} if j + 1 < len(p) and p[j + 1] is "*": dp[i][j] = dp[i][j + 2] or first_match and dp[i + 1][j] else: dp[i][j] = first_match and dp[i + 1][j + 1] return dp[0][0]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: sΒ could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters likeΒ .Β orΒ *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "a*" Output: true Explanation:Β '*' means zero or more of the precedengΒ element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example 3: Input: s = "ab" p = ".*" Output: true Explanation:Β ".*" means "zero or more (*) of any character (.)". Example 4: Input: s = "aab" p = "c*a*b" Output: true Explanation:Β c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". Example 5: Input: s = "mississippi" p = "mis*is*p*." Output: false
class Solution(object): def isMatch(self, string, expr): self.memo = {(0, 0): True} self.string = string self.expr = expr N = len(string) M = len(expr) return self.get(N, M) def get(self, i, j): if i < 0 or j < 0: return False if (i, j) not in self.memo: self.memo[i, j] = self.eval(i, j) return self.memo[i, j] def eval(self, i, j): if j <= 0: return False if self.expr[j - 1] == "*": prev_char = self.expr[j - 2] if prev_char == "." or i >= 1 and prev_char == self.string[i - 1]: if self.get(i - 1, j): return True return self.get(i, j - 2) else: if ( self.expr[j - 1] == "." or i >= 1 and self.expr[j - 1] == self.string[i - 1] ): return self.get(i - 1, j - 1) return False
CLASS_DEF VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER
Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: sΒ could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and characters likeΒ .Β orΒ *. Example 1: Input: s = "aa" p = "a" Output: false Explanation: "a" does not match the entire string "aa". Example 2: Input: s = "aa" p = "a*" Output: true Explanation:Β '*' means zero or more of the precedengΒ element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". Example 3: Input: s = "ab" p = ".*" Output: true Explanation:Β ".*" means "zero or more (*) of any character (.)". Example 4: Input: s = "aab" p = "c*a*b" Output: true Explanation:Β c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab". Example 5: Input: s = "mississippi" p = "mis*is*p*." Output: false
class Solution: def isMatch(self, s, p): m = {} def dp(i, j): print((i, j)) if (i, j) not in m: if j == len(p): ans = i == len(s) else: first_match = i < len(s) and p[j] in {s[i], "."} if j + 1 < len(p) and p[j + 1] == "*": ans = dp(i, j + 2) or first_match and dp(i + 1, j) else: ans = first_match and dp(i + 1, j + 1) m[i, j] = ans return m[i, j] return dp(0, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER
While playing yet another strategy game, Mans has recruited $n$ Swedish heroes, whose powers which can be represented as an array $a$. Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $a_i$ and $a_{i+1}$, remove them and insert a hero with power $-(a_i+a_{i+1})$ back in the same position. For example if the array contains the elements $[5, 6, 7, 8]$, he can pick $6$ and $7$ and get $[5, -(6+7), 8] = [5, -13, 8]$. After he will perform this operation $n-1$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 200000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$)Β β€” powers of the heroes. -----Output----- Print the largest possible power he can achieve after $n-1$ operations. -----Examples----- Input 4 5 6 7 8 Output 26 Input 5 4 -5 9 -2 1 Output 15 -----Note----- Suitable list of operations for the first sample: $[5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]$
n = int(input()) a = list(map(int, input().split())) INF = 10**20 DP = [-INF] * 12 DP[1] = a[0] DP[5] = -a[0] for elem in a[1:]: newDP = [] newDP.append(DP[5] + elem) newDP.append(DP[3] + elem) newDP.append(DP[4] + elem) newDP.append(DP[1] - elem) newDP.append(DP[2] - elem) newDP.append(DP[0] - elem) newDP.append(max(DP[2] + elem, DP[8] + elem, DP[11] + elem)) newDP.append(max(DP[0] + elem, DP[6] + elem, DP[9] + elem)) newDP.append(max(DP[1] + elem, DP[7] + elem, DP[10] + elem)) newDP.append(max(DP[4] - elem, DP[7] - elem, DP[10] - elem)) newDP.append(max(DP[5] - elem, DP[8] - elem, DP[11] - elem)) newDP.append(max(DP[3] - elem, DP[6] - elem, DP[9] - elem)) DP = newDP if n == 1: print(a[0]) else: print(max(DP[7], DP[10]))
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 NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
While playing yet another strategy game, Mans has recruited $n$ Swedish heroes, whose powers which can be represented as an array $a$. Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $a_i$ and $a_{i+1}$, remove them and insert a hero with power $-(a_i+a_{i+1})$ back in the same position. For example if the array contains the elements $[5, 6, 7, 8]$, he can pick $6$ and $7$ and get $[5, -(6+7), 8] = [5, -13, 8]$. After he will perform this operation $n-1$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 200000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$)Β β€” powers of the heroes. -----Output----- Print the largest possible power he can achieve after $n-1$ operations. -----Examples----- Input 4 5 6 7 8 Output 26 Input 5 4 -5 9 -2 1 Output 15 -----Note----- Suitable list of operations for the first sample: $[5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]$
N = int(input()) a, b, c, d, e = float("-inf"), float("-inf"), float("-inf"), 0, 1 for x in map(int, input().split()): a, b, c, d, e = ( max(c + x, b - x), max(a + x, c - x), max(b + x, a - x, d - e * x), d + e * x, -e, ) print([d, b][N > 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER
While playing yet another strategy game, Mans has recruited $n$ Swedish heroes, whose powers which can be represented as an array $a$. Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $a_i$ and $a_{i+1}$, remove them and insert a hero with power $-(a_i+a_{i+1})$ back in the same position. For example if the array contains the elements $[5, 6, 7, 8]$, he can pick $6$ and $7$ and get $[5, -(6+7), 8] = [5, -13, 8]$. After he will perform this operation $n-1$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 200000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$)Β β€” powers of the heroes. -----Output----- Print the largest possible power he can achieve after $n-1$ operations. -----Examples----- Input 4 5 6 7 8 Output 26 Input 5 4 -5 9 -2 1 Output 15 -----Note----- Suitable list of operations for the first sample: $[5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]$
n = int(input()) a = [int(i) for i in input().split()] if n == 1: print(a[0]) else: dp = [dict(), dict()] dp[0][0, 0, 0] = a[0] dp[0][1, 0, 1] = -a[0] for i in range(1, n): _i = i & 1 dp[_i].clear() for j in range(3): dp[_i][j, 0, 0] = dp[1 - _i].get((j, 0, 1), -200000000000000.0 - 5) + a[i] dp[_i][j, 0, 1] = ( dp[1 - _i].get(((j + 2) % 3, 0, 0), -200000000000000.0 - 5) - a[i] ) dp[_i][j, 1, 0] = max( dp[1 - _i].get((j, 0, 0), -200000000000000.0 - 5) + a[i], dp[1 - _i].get((j, 1, 0), -200000000000000.0 - 5) + a[i], dp[1 - _i].get((j, 1, 1), -200000000000000.0 - 5) + a[i], ) dp[_i][j, 1, 1] = max( dp[1 - _i].get(((j + 2) % 3, 0, 1), -200000000000000.0 - 5) - a[i], dp[1 - _i].get(((j + 2) % 3, 1, 0), -200000000000000.0 - 5) - a[i], dp[1 - _i].get(((j + 2) % 3, 1, 1), -200000000000000.0 - 5) - a[i], ) print( max( dp[n - 1 & 1][(3 - (n + 2) % 3) % 3, 1, 0], dp[n - 1 & 1][(3 - (n + 2) % 3) % 3, 1, 1], ) )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER NUMBER BIN_OP NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER NUMBER BIN_OP NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER NUMBER BIN_OP NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER NUMBER BIN_OP NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER BIN_OP NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER
While playing yet another strategy game, Mans has recruited $n$ Swedish heroes, whose powers which can be represented as an array $a$. Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $a_i$ and $a_{i+1}$, remove them and insert a hero with power $-(a_i+a_{i+1})$ back in the same position. For example if the array contains the elements $[5, 6, 7, 8]$, he can pick $6$ and $7$ and get $[5, -(6+7), 8] = [5, -13, 8]$. After he will perform this operation $n-1$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 200000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$)Β β€” powers of the heroes. -----Output----- Print the largest possible power he can achieve after $n-1$ operations. -----Examples----- Input 4 5 6 7 8 Output 26 Input 5 4 -5 9 -2 1 Output 15 -----Note----- Suitable list of operations for the first sample: $[5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]$
n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) exit() inf = 1002003004005006007 dp = [[[-inf, -inf, -inf] for _ in range(2)] for _ in range(n + 1)] dp[0][0][0] = 0 for i in range(n): for j in range(2): for k in range(3): dp[i + 1][j][(k + 1) % 3] = max( dp[i + 1][j][(k + 1) % 3], dp[i][j][k] + a[i] ) dp[i + 1][j][(k - 1) % 3] = max( dp[i + 1][j][(k - 1) % 3], dp[i][j][k] - a[i] ) if i + 1 < n: for k in range(3): dp[i + 2][1][(k + 2) % 3] = max( dp[i + 2][1][(k + 2) % 3], dp[i][0][k] + a[i] + a[i + 1] ) dp[i + 2][1][(k - 2) % 3] = max( dp[i + 2][1][(k - 2) % 3], dp[i][0][k] - a[i] - a[i + 1] ) ans = dp[n][1][1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
While playing yet another strategy game, Mans has recruited $n$ Swedish heroes, whose powers which can be represented as an array $a$. Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $a_i$ and $a_{i+1}$, remove them and insert a hero with power $-(a_i+a_{i+1})$ back in the same position. For example if the array contains the elements $[5, 6, 7, 8]$, he can pick $6$ and $7$ and get $[5, -(6+7), 8] = [5, -13, 8]$. After he will perform this operation $n-1$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 200000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$)Β β€” powers of the heroes. -----Output----- Print the largest possible power he can achieve after $n-1$ operations. -----Examples----- Input 4 5 6 7 8 Output 26 Input 5 4 -5 9 -2 1 Output 15 -----Note----- Suitable list of operations for the first sample: $[5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]$
from sys import stdin input = stdin.readline def solve(): n = int(input()) a = list(map(int, input().split())) if n == 1: print(max(a[0], -a[0])) return NINF = -(10**15) dp = [[[([NINF] * 2) for i in range(2)] for j in range(3)] for k in range(n)] dp[0][n % 3][0][1] = a[0] dp[0][(n + 1) % 3][0][0] = -a[0] for i in range(n - 1): for j in range(3): for k in range(2): for l in range(2): if dp[i][j][k][l] > NINF: dp[i + 1][j][k or l == 1][1] = max( dp[i + 1][j][k or l == 1][1], dp[i][j][k][l] + a[i + 1] ) dp[i + 1][(j + 1) % 3][k or l == 0][0] = max( dp[i + 1][(j + 1) % 3][k or l == 0][0], dp[i][j][k][l] - a[i + 1], ) print(max(dp[n - 1][1][1][1], dp[n - 1][1][1][0])) def __starting_point(): solve() __starting_point()
ASSIGN VAR 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 IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
While playing yet another strategy game, Mans has recruited $n$ Swedish heroes, whose powers which can be represented as an array $a$. Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $a_i$ and $a_{i+1}$, remove them and insert a hero with power $-(a_i+a_{i+1})$ back in the same position. For example if the array contains the elements $[5, 6, 7, 8]$, he can pick $6$ and $7$ and get $[5, -(6+7), 8] = [5, -13, 8]$. After he will perform this operation $n-1$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve? -----Input----- The first line contains a single integer $n$ ($1 \le n \le 200000$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \le a_i \le 10^9$)Β β€” powers of the heroes. -----Output----- Print the largest possible power he can achieve after $n-1$ operations. -----Examples----- Input 4 5 6 7 8 Output 26 Input 5 4 -5 9 -2 1 Output 15 -----Note----- Suitable list of operations for the first sample: $[5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]$
N, x, y, z, v, w = input(), -9000000000.0, -9000000000.0, -9000000000.0, 0, 1 for A in map(int, input().split()): x, y, z, v, w = ( max(z + A, y - A), max(x + A, z - A), max(y + A, x - A, v - w * A), v + w * A, -w, ) print([v, y][N > "1"])
ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR STRING
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def ans(x): print(x) def gcd(a, b): while b: a, b = b, a % b return a t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = list(input()) alphcnt = [0] * 26 for i in s: alphcnt[ord(i) - 97] += 1 alphcnt.sort(reverse=True) end = 1 for i in range(n, -1, -1): g = gcd(i, k) if g == 1: if alphcnt[0] >= i: ans(i) end = 0 else: cnt = 0 p = i // g for j in alphcnt: cnt += j // p if cnt >= g: ans(i) end = 0 if end == 0: break
FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
PI = 3.141592653589793 INF = float("inf") MOD = 1000000007 def add(x, y): return (x + y) % MOD def sub(x, y): return (x - y + MOD) % MOD def mul(x, y): return x * y % MOD def gcd(x, y): if y == 0: return x return gcd(y, x % y) def lcm(x, y): return x * y // gcd(x, y) def power(x, y): res = 1 x %= MOD while y != 0: if y & 1: res = mul(res, x) y >>= 1 x = mul(x, x) return res def mod_inv(n): return power(n, MOD - 2) def prob(p, q): return mul(p, power(q, MOD - 2)) def ii(): return int(input()) def li(): return [int(i) for i in input().split()] def ls(): return [i for i in input().split()] def fun(): for i in vis: if i == -1: return 1 return 0 for t in range(ii()): t += 1 n, k = li() s = input() f = [(0) for i in range(26)] store = [] for i in s: f[ord(i) - 97] += 1 for i in f: store.append(i) store.sort() store.reverse() ans = 1 for i in range(2, n + 1): x = k % i if x == 0: ans = i else: need = i // gcd(i, x) cnt = 0 for j in store: d = j // need cnt += d * need if cnt >= i: ans = i print(ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
for _ in range(int(input())): n, k = map(int, input().split()) s = input() l = set() dic = {} for i in s: dic[i] = dic.get(i, 0) + 1 a = dic.values() ans = 0 for i in range(1, int(k**0.5) + 1): if k % i == 0: l.add(i) l.add(k // i) for fact in l: for j in range(fact, n + 1, fact): c = 0 temp = j // fact for i in a: c += i // temp if c >= fact: ans = max(ans, j) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def binary_search(item, start, end, l): if start > end: return max(end, 0) if start > len(l) - 1: return len(l) - 1 mid = (start + end) // 2 if l[mid] > item: return binary_search(item, start, mid - 1, l) elif l[mid] < item: return binary_search(item, mid + 1, end, l) else: return mid def f(): n, k = map(int, input().split(" ")) multiplesofk = [] for i in range(1, k + 1): if k % i == 0: multiplesofk.append(i) string = input() counter = [0] * 200 for i in range(0, len(string)): counter[ord(string[i])] += 1 listofcount = [] for i in range(ord("a"), ord("z") + 1): if counter[i] != 0: listofcount.append(counter[i]) maximum = 0 for i in range(1, max(listofcount) + 1): s = 0 for j in range(0, len(listofcount)): s += listofcount[j] // i element = binary_search(s, 0, len(multiplesofk) - 1, multiplesofk) maximum = max(maximum, multiplesofk[element] * i) print(maximum) for i in range(int(input())): f()
FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def check(x): add = 0 for i in range(26): add += alpha[i] // x return add def gcd(a, b): if b == 0: return a return gcd(b, a % b) for t in range(int(input())): n, m = map(int, input().split()) alpha = [(0) for i in range(26)] st = input() for i in range(n): alpha[ord(st[i]) - 97] += 1 maxi = 0 for i in range(1, n + 1): if i <= m: if m % i == 0: j = 1 maxi = max(maxi, i) while True: q = check(j) if q < i: break maxi = max(maxi, i * j) j += 1 else: r = gcd(i, m) if r >= 1: s = max(m, i) // r p = check(s) if p >= r: maxi = max(maxi, i) print(maxi)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = list(input().rstrip()) l = [] for i in range(1, int(k**0.5) + 1): if k % i == 0: if i <= n: l.append(i) if k // i <= n: l.append(k // i) l = set(l) c = [0] * 26 for ss in s: c[ord(ss) - ord("a")] += 1 ans = 0 for kk in l: for x in range(1, n + 1): can = 0 for i in range(26): can += c[i] // x if can > 0 and kk % can == 0: ans = max(ans, x * can) if can >= kk: ans = max(ans, x * kk) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
from sys import stdin, stdout def necklace_assembly(n, k, s): res = 0 cna = [(0) for i in range(26)] for i in range(len(s)): cna[ord(s[i]) - ord("a")] += 1 for i in range(n, 0, -1): l = k % i if l == 0: res = i break g = gcd(i, l) if g != 0: d = 0 need = i // g for v in cna: d += v // need * need if d >= i: res = i break return res def gcd(a, b): if b == 0: return a return gcd(b, a % b) t = int(stdin.readline()) for i in range(t): n, k = list(map(int, stdin.readline().split())) s = stdin.readline().strip() stdout.write(str(necklace_assembly(n, k, s)) + "\n")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def good(n, k, letters): if letters[-1] >= n: return True necklace = [-1] * n letters_copy = letters[:] i = 25 index = 0 visited = set() while i >= 0: indices = set() while index not in indices: indices.add(index) index += k index %= n if letters_copy[i] >= len(indices): letters_copy[i] -= len(indices) for j in indices: necklace[j] = i visited.add(j) for j in range(n): if j not in visited: index = j break else: i -= 1 if len(visited) == n: break for j in range(n): if necklace[j] == -1: return False if necklace[j] != necklace[(j + k) % n]: return False return True def solve(s, n, k, ans): letters = [0] * 26 for i in s: letters[ord(i) - ord("a")] += 1 letters.sort() max_n = 0 for i in range(1, n + 1): if good(i, k, letters): max_n = i ans.append(max_n) def main(): t = int(input()) ans = [] for i in range(t): n, k = map(int, input().split()) s = input() solve(s, n, k, ans) for i in ans: print(i) main()
FUNC_DEF IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def main(): t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() d = {} M = 0 for i in s: if i in d: d[i] += 1 else: d[i] = 1 M = max(M, d[i]) s = sorted(d.keys(), key=lambda x: d[x], reverse=True) qs = [k] for j in range(2, int(k**0.5) + 2): if k % j == 0: qs.append(j) qs.append(k // j) L = M for q in qs: max_reps = n // q for reps in range(max_reps, 0, -1): total_contribs = 0 for potential_contributor in s: given_contrib = d[potential_contributor] // reps if given_contrib == 0: break total_contribs += given_contrib if total_contribs < q: continue L = max(L, q * reps) break print(L) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def HOD(a, b): while a != 0 and b != 0: if a > b: a %= b else: b %= a return a + b for _ in range(int(input())): n, k = map(int, input().split()) s = list(input()) d = {} for i in s: if i not in d: d[i] = 1 else: d[i] += 1 a = sorted(d.values())[::-1] for i in range(n, 0, -1): x = k % i if x == 0: break types = HOD(i, x) same = i // types c = 0 for j in a: c += j // same if c >= types: break print(i)
FUNC_DEF WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
import sys inp = [x for x in sys.stdin.read().split()] ii = 0 ttt = int(inp[ii]) ii += 1 for _ in range(ttt): n, k, s = int(inp[ii]), int(inp[ii + 1]), inp[ii + 2] ii += 3 cnt = [0] * 26 for c in s: cnt[ord(c) - ord("a")] += 1 res = 1 for d in range(1, k + 1): if k % d == 0: for l in range(d, n + 1, d): q = l // d counter = 0 for x in cnt: counter += x // q if counter >= d: res = max(res, l) print(res)
IMPORT ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
for i in range(int(input())): n, k = list(map(int, input().split())) s = input() a = [0] * 26 for i in s: a["abcdefghijklmnopqrstuvwxyz".find(i)] += 1 z = 1 a = sorted(a)[::-1] a0 = list(a) for i in range(1, n + 1): b = [] c = [0] * i d = 0 for j in range(i): if c[j] == 0: d += 1 b.append(1) c[j] = d j0 = j j = (j - k) % i while j != j0: b[-1] += 1 c[j] = d j = (j - k) % i for j in b: for l in range(26): if a[l] >= j: a[l] -= j break else: break else: z = i a = list(a0) print(z)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
t = int(input()) def gcd(a, b): if b == 0: return a return gcd(b, a % b) for _ in range(t): n, k = map(int, input().split()) s = input() occ = [0] * 26 for i in s: occ[ord(i) - 97] += 1 occ.sort() occ.reverse() for l in range(1, n + 1): cycle = gcd(l, k) need = l // cycle su = 0 for i in occ: su += i // need if su * need >= l: wyn = l print(wyn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def make_divisors(n): lower_divisors, upper_divisors = [], [] i = 1 while i * i <= n: if n % i == 0: lower_divisors.append(i) if i != n // i: upper_divisors.append(n // i) i += 1 return lower_divisors + upper_divisors[::-1] def main(): q = int(input()) for _ in range(q): n, k = map(int, input().split()) s = input() while n > 0: if k % n == 0: ans = n break n -= 1 alphabet = [0] * 26 n = len(s) for i in range(n): alphabet[ord(s[i]) - 97] += 1 KL = make_divisors(k) for nk in KL: ok = 0 ng = pow(10, 18) def check(x, k): if x == 0: return True if k * x > n: return False val = 0 for num in alphabet: temp = num // x * x val += temp if val >= x * k: return True else: return False while abs(ok - ng) > 1: mid = (ok + ng) // 2 if check(mid, nk): ok = mid else: ng = mid ans = max(ans, ok * nk) print(ans) main()
FUNC_DEF ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
t = int(input()) while t: n, k = [int(i) for i in input().split()] s = input() mem = {i: (0) for i in s} for c in s: mem[c] += 1 chars = sorted(list(mem.values())) for i in range(n, 0, -1): p = [-1] * i for j in range(i): p[j] = (j + k) % i cycles = [] unused = set([x for x in range(i)]) while unused: curr = unused.pop() unused.add(curr) l = 0 while curr in unused: l += 1 unused.remove(curr) curr = p[curr] cycles.append(l) cycles.sort() x = 0 y = 0 char_counts = chars.copy() while x < len(cycles) and y < len(char_counts): if char_counts[y] >= cycles[x]: char_counts[y] -= cycles[x] x += 1 if char_counts[y] != 0: y -= 1 y += 1 if x >= len(cycles): print(i) break t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def gcd(a, b): return gcd(b, a % b) if b else a t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() elements = {} for i in s: if i in elements.keys(): elements[i] += 1 else: elements[i] = 1 for i in range(n, 0, -1): every_nth = gcd(k, i) can_give = 0 for j in elements.keys(): can_give += elements[j] // (i // every_nth) if can_give >= every_nth: print(i) break
FUNC_DEF RETURN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split()] s = input() dict = {} ans = 0 for i in range(len(s)): if dict.get(ord(s[i]) - 97) is None: dict[ord(s[i]) - 97] = 0 dict[ord(s[i]) - 97] += 1 for i in range(1, k + 1): if k % i == 0: for j in range(i, n + 1, i): lol = j // i count = 0 for p in range(26): if dict.get(p) is None: continue count += dict[p] // lol if count >= i: ans = max(ans, j) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NONE VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def possible(n, brr, k): chk = [] visited = [0] * n for i in range(n): cnt = 0 if visited[i] == 0: visited[i] = 1 j = (i + k) % n cnt += 1 while j != i: visited[j] = 1 cnt += 1 j = (j + k) % n chk.append(cnt) i = 0 j = 0 while i < len(chk) and j < len(brr): if chk[i] <= brr[j]: brr[j] -= chk[i] i += 1 else: j += 1 if i != len(chk): return False return True t = int(input()) for i in range(t): n, k = map(int, input().split()) s = input() d = dict() for i in s: d[i] = d.get(i, 0) + 1 arr = list(d.values()) arr.sort(reverse=True) ans = -1 for i in range(n, 0, -1): brr = arr.copy() if possible(i, brr, k): ans = i break print(ans)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def solve(N, k, s): count = [0] * 26 for c in s: count[ord(c) - 97] += 1 ans = 0 for n in range(1, N + 1): X = 0 for c in count: X += c // n for x in range(X, 0, -1): if k % x == 0: ans = max(ans, x * n) break return ans t = int(input()) for _ in range(t): n, k = input().split() n, k = int(n), int(k) s = input() print(solve(n, k, s))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def gcd(n, k): if n % k == 0: return k return gcd(k, n % k) t = int(input()) for you in range(t): l = input().split() n = int(l[0]) k = int(l[1]) s = input() freq = [(0) for i in range(26)] for i in s: freq[ord(i) - 97] += 1 mina = [] for i in range(1, n + 1): x = gcd(i, k) sumi = 0 for j in range(26): sumi = sumi + freq[j] // (i // x) if sumi >= x: mina.append(i) print(max(mina))
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() m = [0] * 26 for a in s: m[ord(a) - ord("a")] += 1 t = max(m) x = t * k while True: b = x // gcd(x, k) count = 0 for a in m: count += a // b if count * b >= x: break x -= 1 print(x)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
from sys import stdin, stdout rr = lambda: input().strip() rri = lambda: int(rr()) rrm = lambda: [int(x) for x in rr().split()] def sol(): n, k = rrm() s = input().strip() fr = [(0) for i in range(26)] for c in s: fr[ord(c) - ord("a")] += 1 res = 1 for i in range(1, k + 1): if k % i == 0: x = 1 while True: cn = x * i z = 0 for j in fr: z += j // x if z < i: break x += 1 x -= 1 res = max(res, x * i) print(res) return T = rri() for t in range(1, T + 1): ans = sol()
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
for _ in range(int(input())): n, k = map(int, input().split()) s = input() qu = list(0 for i in range(26)) qu.sort() ans = 0 for m in range(1, n + 1): qu = list(0 for i in range(26)) for i in s: qu[ord(i) - ord("a")] += 1 p = list(0 for i in range(m)) used = list(False for i in range(m)) for i in range(m): p[i] = (i + k) % m cycle = list() for i in range(m): if not used[i]: h = i an = 0 while not used[h]: used[h] = True h = p[h] an += 1 cycle.append(an) cycle.sort() index = 0 i = 0 while True: if index == 26: break if i == len(cycle): ans = m break if cycle[i] > qu[index]: index += 1 continue qu[index] -= cycle[i] i += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def gcd(a, b): return a if b == 0 else gcd(b, a % b) def feasible(n, k, beads, lng, maxcnt): g = gcd(k, lng) h = lng // g if g == 1: return lng <= maxcnt g0 = 0 for _, c in beads.items(): g0 += c // h return g <= g0 def solve(n, k, beads): maxcnt = 0 for _, c in beads.items(): if c > maxcnt: maxcnt = c for lng in range(n, 1, -1): if feasible(n, k, beads, lng, maxcnt): return lng return 1 cases = int(input().strip()) for _ in range(cases): n, k = tuple(map(int, input().strip().split())) store = input().strip() beads = {} for b in store: if b in beads: beads[b] += 1 else: beads[b] = 1 print(solve(n, k, beads))
FUNC_DEF RETURN VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) t = int(input()) for test in range(0, t): n, k = input().split() n = int(n) k = int(k) s = input() dic = {} b = [] for i in range(0, n): if s[i] in dic: b[dic[s[i]]] += 1 else: dic[s[i]] = len(b) b.append(1) kk = k b.sort() for l in range(n, 0, -1): k = kk % l if k == 0: print(l) break s = k * l // gcd(l, k) // k ll = l for i in range(len(b) - 1, -1, -1): ll -= b[i] // s * s if ll <= 0: print(l) break if ll <= 0: break
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, input().split()) def find(cnt, k, h): l = 0 r = 2001 while r - l > 1: now = 0 c = (l + r) // 2 for j in cnt: now += j // c if now >= h: l = c else: r = c return l * h for i in range(mint()): n, k = map(int, input().split()) cnt = [0] * 26 for i in minp(): cnt[ord(i) - ord("a")] += 1 cnt.sort(reverse=True) ans = 0 for i in range(1, k + 1): if k % i == 0: ans = max(ans, find(cnt, k, i)) if k // i != i: ans = max(ans, find(cnt, k, k // i)) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
letters = 26 for t in range(int(input())): n, k = map(int, input().split(" ")) s = input() counts = [0] * (letters + 1) for c in s: counts[ord(c) - ord("a")] += 1 counts.sort(reverse=True) zeroi = counts.index(0) if zeroi >= 0: counts = counts[:zeroi] ans = 1 for d in range(1, min(k, n) + 1): if k % d != 0: continue l = 1 r = n // d while l < r: m = (l + r + 1) // 2 inGroup = 0 for count in counts: inGroup += count // m if inGroup >= d: l = m else: r = m - 1 ans = max(ans, l * d) print(ans)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def find(cnt, k, h): l = 0 r = 2001 while r - l > 1: now = 0 c = (l + r) // 2 for j in cnt: now += j // c if now >= h: l = c else: r = c return l * h for i in range(int(input())): n, k = map(int, input().split()) cnt = [0] * 26 for i in input(): cnt[ord(i) - ord("a")] += 1 cnt.sort(reverse=True) ans = 0 for i in range(1, k + 1): if k % i == 0: ans = max(ans, find(cnt, k, i)) if k // i != i: ans = max(ans, find(cnt, k, k // i)) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
t = int(input()) for i in range(t): [n, k] = list(map(int, input().split())) s = input() l = [s.count(a) for a in set(s)] M = 0 for j in range(1, n + 1): MM = sum(map(lambda x: x // j, l)) if MM == 0: continue while k % MM: MM -= 1 M = max(M, MM * j) print(M)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
from sys import stdin, stdout input = stdin.readline for t in range(int(input())): n, k = map(int, input().split()) s = input().strip() a = [(0) for i in range(26)] for i in s: a[ord(i) - 97] += 1 x = 0 for i in a: if i == n: x = i break if x != 0: stdout.write(str(n) + "\n") continue for z in range(n, -1, -1): q = a[:] visited = [(0) for i in range(z)] cycle = [] for i in range(z): if visited[i] == 0: j = i l = 0 while visited[j] == 0: visited[j] = 1 j = (j + k) % z l += 1 cycle.append(l) flag = 0 for i in cycle: flag = 0 for j in range(26): if q[j] >= i: flag = 1 q[j] = q[j] - i break if flag == 0: break if flag == 1: stdout.write(str(z) + "\n") break
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
def gcd(a, b): if a < b: return gcd(b, a) if b == 0: return a return gcd(b, a % b) for _ in range(int(input())): n, k = list(map(int, input().split())) s = input() if s[0] * n == s: print(n) continue cnt = {} for c in s: if c not in cnt: cnt[c] = 0 cnt[c] += 1 cnt = [cnt[c] for c in cnt] ans = 1 for x in range(2, n + 1): tempK = k % x if tempK == 0: ans = max(ans, x) continue mod = x % tempK if mod != 0: mod = -(mod - tempK) times = tempK // gcd(tempK, mod) needed = x * times // tempK usedBeads = 0 for num in cnt: usedBeads += num - num % needed if usedBeads >= x: ans = max(ans, x) print(ans)
FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The store sells $n$ beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them. A necklace is a set of beads connected in a circle. For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemble the following necklaces (these are not all possible options): [Image] And the following necklaces cannot be assembled from beads sold in the store: [Image] The first necklace cannot be assembled because it has three beads "a" (of the two available). The second necklace cannot be assembled because it contains a bead "d", which is not sold in the store. We call a necklace $k$-beautiful if, when it is turned clockwise by $k$ beads, the necklace remains unchanged. For example, here is a sequence of three turns of a necklace. [Image] As you can see, this necklace is, for example, $3$-beautiful, $6$-beautiful, $9$-beautiful, and so on, but it is not $1$-beautiful or $2$-beautiful. In particular, a necklace of length $1$ is $k$-beautiful for any integer $k$. A necklace that consists of beads of the same color is also beautiful for any $k$. You are given the integers $n$ and $k$, and also the string $s$ containing $n$ lowercase letters of the English alphabetΒ β€” each letter defines a bead in the store. You can buy any subset of beads and connect them in any order. Find the maximum length of a $k$-beautiful necklace you can assemble. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$)Β β€” the number of test cases in the test. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 2000$). The second line of each test case contains the string $s$ containing $n$ lowercase English lettersΒ β€” the beads in the store. It is guaranteed that the sum of $n$ for all test cases does not exceed $2000$. -----Output----- Output $t$ answers to the test cases. Each answer is a positive integerΒ β€” the maximum length of the $k$-beautiful necklace you can assemble. -----Example----- Input 6 6 3 abcbac 3 6 aaa 7 1000 abczgyo 5 4 ababa 20 10 aaebdbabdbbddaadaadc 20 5 ecbedececacbcbccbdec Output 6 3 5 4 15 10 -----Note----- The first test case is explained in the statement. In the second test case, a $6$-beautiful necklace can be assembled from all the letters. In the third test case, a $1000$-beautiful necklace can be assembled, for example, from beads "abzyo".
T = int(input()) def solve(): [N, K] = list(map(int, input().split())) count = {} S = input() for c in S: if c not in count: count[c] = 0 count[c] += 1 def necklace_size(m): count_ = count.copy() ans = [None for _ in range(m)] for i in range(len(ans)): if ans[i] != None: continue cyclic_group = set() j = i while j not in cyclic_group: cyclic_group.add(j) j += K j %= m key = None val = -1 for k in count_: if count_[k] >= len(cyclic_group): if key == None: key = k val = count_[k] elif count_[k] < val: key = k val = count_[k] if key == None: return False for i in cyclic_group: ans[i] = key count_[key] -= 1 return "".join(ans) for i in range(len(S), -1, -1): foo = necklace_size(i) if foo: return len(foo) return -1 for _ in range(T): print(solve())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NONE RETURN NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
rri = lambda: int(input()) rrm = lambda: list(map(int, input().split(" "))) def solve(N, K, A): oo = 0 for j in range(n): if A[j] == 1: oo += 1 ans = oo for mod in range(k): delta = 0 for j in range(mod, n, k): if A[j] == 1: delta -= 1 else: delta += 1 delta = min(0, delta) ans = min(ans, oo + delta) return ans for _ in range(int(input())): n, k = [int(i) for i in input().split(" ")] ll = [int(i) for i in list(input())] print(solve(n, k, ll))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
import sys input = sys.stdin.readline t = int(input()) out = [] for i in range(t): n, k = map(int, input().split()) s = list(input()) count = [0] for j in s: if j == "1": count.append(count[-1] + 1) else: count.append(count[-1]) ans = count[-1] dp = [] for j in range(n): cost = count[j] if j >= k: cost = min(cost, dp[j - k] + count[j] - count[j - k + 1]) if s[j] == "0": cost += 1 dp.append(cost) ans = min(ans, cost + count[-1] - count[j + 1]) out.append(ans) for i in out: print(i)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
T = int(input()) for test in range(T): n, k = list(map(int, input().split())) a = [[] for _ in range(n)] s = input() count = 0 for i in range(n): if s[i] == "1": count += 1 ans = (i + 1) % k a[ans].append(i + 1) if count == 0: print(0) continue result = int(1000000000.0) for x in a: if len(x) == 0: continue dp = [0] for i in range(1, len(x)): ans = int((x[i] - x[i - 1]) / k - 1) dp.append(min(dp[-1] + ans, i)) res = int(1000000000.0) for i in range(len(dp)): res = min(res, dp[i] + len(dp) - i - 1) result = min(result, res + count - len(x)) print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
INF = int(100000000.0) for _ in [0] * int(input()): n, k = map(int, input().split()) a = [0] + [*map(int, input())] b = [0] * (n + 1) for i in range(1, n + 1): b[i] += b[i - 1] + a[i] r = [INF] * (n + 1) r[0] = 0 for i in range(1, n + 1): r[i] = min(r[i], b[i - 1] + (a[i] < 1)) if i > k: r[i] = min(r[i], r[i - k] + b[i - 1] - b[i - k] + (a[i] < 1)) c = b[n] for i in range(1, n + 1): c = min(c, r[i] + b[n] - b[i]) print(c)
ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER LIST FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
tcs = int(input()) for tc in range(tcs): n, k = map(int, input().split()) s = input() total = 0 for i in range(n): if s[i] == "1": total += 1 ans = total for i in range(k): dif = 0 for j in range(i, n, k): if s[j] == "1": dif += 1 else: dif -= 1 dif = max(dif, 0) ans = min(ans, total - dif) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
import sys input = sys.stdin.readline def print(val): sys.stdout.write(str(val) + "\n") def prog(): for _ in range(int(input().strip())): n, k = map(int, input().split()) garland = input().strip() modi = [] for i in range(k): summ = [0] for j in range(i, n, k): summ.append((garland[j] == "1") + summ[-1]) modi.append(summ) total = sum([val[-1] for val in modi]) modchanges = [] for mod in modi: i_mins = [mod[-1]] for i in range(1, len(mod)): i_mins.append( min( i_mins[-1] - (mod[i] - mod[i - 1] == 1) + (mod[i] - mod[i - 1] == 0), mod[-1] - (mod[i] - mod[i - 1] == 1), ) ) modchanges.append(min(i_mins) + total - mod[-1]) print(min(modchanges)) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
for _ in range(int(input())): n, k = map(int, input().split()) data = input() total = 0 splited = [[] for _ in range(k)] for i in range(n): splited[i % k].append(ord(data[i]) - ord("0")) total += ord(data[i]) - ord("0") result = total for array in splited: dp = [0] * len(array) dp[0] = array[0] for i in range(1, len(array)): if array[i] == 1: dp[i] = dp[i - 1] + 1 elif array[i] == 0: dp[i] = max(dp[i - 1] - 1, 0) result = min(result, total - max(dp)) print(result)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) for i in range(t): n, k = [int(x) for x in input().split()] s = [int(x) for x in list(input())] ln = len(s) mp = ln for j in range(k): sm, mn, md = 0, 0, 0 for c in range(j, ln, k): sm += 2 * s[c] - 1 if sm <= mn: mn = sm if sm - mn > md: md = sm - mn mp = min(mp, -md) print(sum(s) + mp)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
import sys input = sys.stdin.readline t = int(input()) ans = [] for _ in range(t): n, k = map(int, input().split()) s = list(map(int, list(input().rsplit()[0]))) tot = sum(s) ret = 1 << 100 for i in range(k): D = [0] * 3 cnt = 0 for j in range(i, n, k): cnt += s[j] P = [0] * 3 if s[j]: P[0] = D[0] + 1 P[1] = min(D[0], D[1]) P[2] = min(D[1], D[2]) + 1 else: P[0] = D[0] P[1] = min(D[0], D[1]) + 1 P[2] = min(D[1], D[2]) D = P[:] ret = min(ret, tot - cnt + min(D)) ans.append(ret) print("\n".join(map(str, ans)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) for tt in range(t): n, k = map(int, input().split()) s = input() ones = best = 0 for i in range(k): cnt = 0 for j in range(i, n, k): if s[j] == "1": cnt += 1 ones += 1 else: cnt = max(0, cnt - 1) best = max(cnt, best) print(ones - best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) for i in range(t): n, k = map(int, input().split()) s = input() pre = [0] * n if s[0] == "1": pre[0] = 1 for i in range(1, n): pre[i] = pre[i - 1] if s[i] == "1": pre[i] += 1 dp = [0] * n dp1 = [0] * n for i in range(k): atleast = 0 count = 0 for j in range(i, n, k): if s[j] == "1": count += 1 atleast += pre[n - 1] - count count = 0 for j in range(i, n, k): if j >= k: if s[j] == "0": dp[j] += 1 dp[j] += atleast + min(dp[j - k], count) elif s[j] == "0": dp[j] = atleast + 1 else: dp[j] = atleast if s[j] == "1": count += 1 last = j count = 0 for j in range(last, -1, -k): if j + k < n: dp1[j] = dp1[j + k] if s[j + k] == "0": dp1[j] += 1 dp1[j] = min(dp1[j], count) if s[j] == "1": count += 1 fa = pre[n - 1] for i in range(n): fa = min(fa, dp[i] + dp1[i]) print(fa)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR STRING VAR VAR NUMBER VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR STRING VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
def max_subarray(s): best_sum = 0 best_start = best_end = 0 current_sum = 0 for current_end, x in enumerate(s): x = 1 if x == "1" else -1 if current_sum <= 0: current_start = current_end current_sum = x else: current_sum += x if current_sum > best_sum: best_sum = current_sum best_start = current_start best_end = current_end + 1 return best_sum, best_start, best_end def min_changes(s): best_sum, best_start, best_end = max_subarray(s) changes = ( s[best_start:best_end].count("0") + s[:best_start].count("1") + s[best_end:].count("1") ) return changes t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = str(input()) ones = s.count("1") min_moves = n for i in range(k): substring = s[i::k] substring_ones = substring.count("1") changes = min_changes(substring) cost = changes + ones - substring_ones if cost < min_moves: min_moves = cost print(min_moves)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) while t > 0: t -= 1 n, k = map(int, input().split()) s = input() cnt, v = [0] * k, [list() for i in range(k)] ans = 0 for i in range(n): if s[i] == "1": cnt[i % k] += 1 ans += 1 v[i % k].append(int(s[i])) tot = ans for i in range(k): curAns = tot - cnt[i] tot0, tot1 = 0, 0 mnsum = 0 for j in range(len(v[i])): if v[i][j] == 0: tot0 += 1 else: tot1 += 1 ans = min(ans, curAns + mnsum + tot0 + (cnt[i] - tot1)) mnsum = min(mnsum, tot1 - tot0) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) def solve(N, K, A): P = [0] for x in A: P.append(P[-1] + x) for _ in range(K + 1): P.append(P[-1]) ans = sum(A) if ans > 0: ans -= 1 INF = float("inf") dp = [INF] * (N + 1) for i in range(N - 1, -1, -1): cost = A[i] ^ 1 dp[i] = basecost = cost + P[-1] - P[i + 1] if i + K < N: cand = cost + P[i + K] - P[i + 1] + dp[i + K] if cand < basecost: dp[i] = cand s = 0 for i, x in enumerate(A): dp[i] += s s += x ans2 = min(dp) if ans2 < ans: ans = ans2 return ans for _ in range(t): n, k = list(map(int, input().split())) s = list(map(int, list(input()))) print(solve(n, k, s))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
from sys import stdin, stdout for _ in range(int(stdin.readline())): n, k = map(int, stdin.readline().split()) s = input() if n == 1: print(0) continue c1 = s.count("1") ma = c1 for i in range(k): l = 0 for j in range(i, n, k): if s[j] == "1": l = l + 1 else: l = l - 1 l = max(l, 0) ma = min(ma, c1 - l) print(ma)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) for _ in range(t): n, k = map(int, input().split(" ")) s = input() y = [] for i in s: y.append(i) totalones = y.count("1") i = 0 m = 0 countone = 0 while i < k: m = 0 for j in range(i, n, k): if y[j] == "1": m += 1 else: m -= 1 if m < 0: m = 0 countone = max(m, countone) i += 1 print(totalones - countone)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
for t in range(int(input())): n, k = map(int, input().split()) s = input() count_1_l = [0] * n count_1_r = [0] * n for i in range(n): if i == 0: if s[i] == "1": count_1_l[i] = 1 else: count_1_l[i] = count_1_l[i - 1] if s[i] == "1": count_1_l[i] += 1 for i in range(n): j = n - 1 - i if j == n - 1: if s[j] == "1": count_1_r[j] = 1 else: count_1_r[j] = count_1_r[j + 1] if s[j] == "1": count_1_r[j] += 1 fl_on = [0] * n if s[0] == "0": fl_on[0] = 1 for i in range(1, n): if i < k: fl_on[i] = count_1_l[i - 1] if s[i] == "0": fl_on[i] += 1 else: j = i - k fl_on[i] = min(fl_on[j], count_1_l[j]) + count_1_l[i - 1] - count_1_l[j] if s[i] == "0": fl_on[i] += 1 fr_on = [0] * n if s[n - 1] == "0": fr_on[n - 1] = 1 for h in range(1, n): i = n - 1 - h if i > n - 1 - k: fr_on[i] = count_1_r[i + 1] if s[i] == "0": fr_on[i] += 1 else: j = i + k fr_on[i] = min(fr_on[j], count_1_r[j]) + count_1_r[i + 1] - count_1_r[j] if s[i] == "0": fr_on[i] += 1 result = count_1_l[n - 1] for i in range(n): result = min(result, fl_on[i] + fr_on[i]) print(result)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) s = list(map(int, list(input().strip()))) dp = [[0, 0] for i in range(n)] dp[0] = [s[0], 1 - s[0]] p = [s[0]] for i in range(1, n): p.append(p[-1] + s[i]) for i in range(1, n): dp[i][0] = s[i] + min(dp[i - 1]) if i - k < 0: dp[i][1] = 1 - s[i] + p[i - 1] else: dp[i][1] = 1 - s[i] + min(dp[i - k][1] + p[i - 1] - p[i - k], p[i - 1]) print(min(dp[-1]))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER LIST VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
T = int(input()) while T != 0: T -= 1 ans = 10**9 n, k = map(int, input().split()) s = input() tot = s.count("1") for i in range(k): sum = 0 maxn = 0 for j in range(i, n, k): sum = max(0, sum - 1) if s[j] == "0" else sum + 1 maxn = max(maxn, sum) ans = min(ans, tot - maxn) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR STRING FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
for _ in range(int(input())): n, k = map(int, input().split()) s = input() pre = [(0) for i in range(n)] steps = [(0) for i in range(n)] su = 0 for i in range(1, n): su += int(s[i - 1]) pre[i] = su sol = steps[n - 1] + pre[n - 1] for i in range(n - 2, -1, -1): if i >= n - k: steps[i] = pre[n - 1] + int(s[n - 1]) - pre[i + 1] else: x = pre[i + k] - pre[i + 1] + 1 - int(s[i]) + steps[i + k] y = pre[n - 1] - pre[i + 1] + int(s[i] + s[n - 1]) steps[i] = min(x, y) sol = min(sol, steps[i] + pre[i]) print(sol)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) for tt in range(t): n, k = map(int, input().split()) s = input().strip() arr = [] for i in range(k + 1): arr.append([]) tot = 0 for i in range(len(s)): if s[i] == "1": tot += 1 for i in range(n): if s[i] == "1": arr[i % k].append(1) else: arr[i % k].append(-1) ans = 0 for i in range(k): cur = 0 for j in arr[i]: cur += j if cur < 0: cur = 0 ans = max(ans, cur) print(tot - ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) for _ in range(t): n, k = [int(i) for i in input().split()] s = input() all = s.count("1") s = s.strip("0") new_len = len(s) ans = new_len for i in range(k): cnt0 = 0 cnt1 = 0 succ = 0 for j in range(i, new_len, k): if s[j] == "0": cnt0 += 1 succ = max(succ - 1, 0) else: succ += 1 cnt1 += 1 ans = min(ans, all - cnt1 + cnt0, all - succ) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
ord0 = b"0"[0] qqq = int(input()) ans = [] for _ in range(qqq): n, k = [int(x) for x in input().split()] s = [(c - ord0) for c in input().encode("ascii")] res = 10**9 tot = sum(s) for rem in range(k): c = s[rem:n:k] m = len(c) cnt = sum(c) x0, x1, x2 = 0, 0, 0 for x in c: if x == 0: x1, x2 = min(x0, x1) + 1, min(x0, x1, x2) else: x0, x1, x2 = x0 + 1, min(x0, x1), min(x0, x1, x2) + 1 res = min(res, tot - cnt + min(x0, x1, x2)) ans.append(res) print("\n".join(str(x) for x in ans))
ASSIGN VAR UNKNOWN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
from sys import stdin for _ in range(int(stdin.readline())): n, k = map(int, stdin.readline().split()) s = stdin.readline() x = s.count("1") ans = x for i in range(k): arr = [(int(s[i]) ^ 1) - (int(s[i]) ^ 0)] for j in range(i + k, n, k): y = int(s[j]) arr.append(arr[-1] + (y ^ 1) - (y ^ 0)) mini = 10**10 max_so_far = su = 0 for b in arr: mini = min(mini, b - max_so_far) max_so_far = max(max_so_far, b) ans = min(ans, mini + x) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
from sys import stdin, stdout int_in = lambda: int(stdin.readline()) arr_in = lambda: [int(x) for x in stdin.readline().split()] mat_in = lambda rows: [arr_in() for y in range(rows)] str_in = lambda: stdin.readline().strip() out = lambda o: stdout.write("{}\n".format(o)) arr_out = lambda o: out(" ".join(map(str, o))) bool_out = lambda o: out("YES" if o else "NO") def solve(n, k, s): result = float("inf") ones = s.count(1) for i in range(k): prefix_sum = 0 for item in s[i::k]: prefix_sum = max(0, prefix_sum + item) result = min(result, ones - prefix_sum) return result for i in range(int_in()): n, k = arr_in() s = [(1 if x == "1" else -1) for x in str_in()] out(solve(n, k, s))
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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) for _ in range(t): n, k = list(map(int, input().split(" "))) s = input() a = [0] * k total = 0 for i, ch in enumerate(s): if ch == "1": a[i % k] += 1 total += 1 res = n for start in range(k): last = -1 b = [0, n, n] c = [0, 0, 0] for j in range(start, n, k): if s[j] == "1": c[0] = b[0] + 1 c[1] = min(b[0], b[1]) c[2] = min(b[1], b[2]) + 1 else: c[0] = b[0] c[1] = min(b[0], b[1]) + 1 c[2] = min(b[1], b[2]) tmp = b b = c c = tmp res = min(res, total - a[start] + min(b[0], b[1], b[2])) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
t = int(input()) answers = [] for _ in range(t): n, k = [int(i) for i in input().split()] s = input() vals = [(0 if i == "0" else 1) for i in s] total_on = sum(vals) ans = n for j in range(k): counter = j sub_arr = [] while counter < n: sub_arr.append(vals[counter]) counter += k last = sub_arr[0] already_on = sum(sub_arr) compulsory = total_on - already_on if compulsory > ans: continue num_lamps = len(sub_arr) sub_sub_arr = [] s = 0 mult = 1 if last == 1 else -1 for i in range(len(sub_arr)): if last == sub_arr[i]: s += 1 else: sub_sub_arr.append(mult * s) mult = -1 * mult s = 1 if i == len(sub_arr) - 1: sub_sub_arr.append(mult * s) last = sub_arr[i] curr_best = 0 best = 0 for i in range(len(sub_sub_arr)): if sub_sub_arr[i] > 0: curr_best += sub_sub_arr[i] else: el = sub_sub_arr[i] if curr_best + el < 0: curr_best = 0 else: curr_best = curr_best + el best = max(curr_best, best) option = min(num_lamps - already_on, already_on, already_on - best) ans = min(compulsory + option, ans) answers.append(ans) print(*answers, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given a garland consisting of $n$ lamps. States of the lamps are represented by the string $s$ of length $n$. The $i$-th character of the string $s_i$ equals '0' if the $i$-th lamp is turned off or '1' if the $i$-th lamp is turned on. You are also given a positive integer $k$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $k$-periodic if the distance between each pair of adjacent turned on lamps is exactly $k$. Consider the case $k=3$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 25~ 000$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 10^6; 1 \le k \le n$) β€” the length of $s$ and the required period. The second line of the test case contains the string $s$ consisting of $n$ characters '0' and '1'. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ ($\sum n \le 10^6$). -----Output----- For each test case, print the answer β€” the minimum number of moves you need to make to obtain $k$-periodic garland from the given one. -----Example----- Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 Output 1 2 5 4 0 0
import sys readline = sys.stdin.readline readall = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") def solve(): n, k = nm() s = list(map(int, list(ns()))) w = sum(s) sc = n for i in range(k): t = s[i::k] m = len(t) dp = [([0] * 3) for j in range(m + 1)] for j in range(m): dp[j + 1][0] = dp[j][0] + t[j] dp[j + 1][1] = min(dp[j][0], dp[j][1]) + 1 - t[j] dp[j + 1][2] = min(dp[j][1], dp[j][2]) + t[j] sc = min(sc, min(dp[m]) + w - sum(t)) print(sc) return T = ni() for _ in range(T): solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR