description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): def backtrack(first, curr): if tuple(curr) not in tracker: output.append(curr[:]) tracker[tuple(curr)] = 1 for i in range(first, n): curr.append(arr[i]) backtrack(i + 1, curr) curr.pop() arr.sort() tracker = {} output = [] backtrack(0, []) return output
CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, nums, n): arr = {} self.subset(arr, [], 0, nums) temp = arr.keys() return sorted(temp) def subset(self, arr, temp, start, nums): if start == len(nums): tempo = sorted(temp) if tuple(tempo) not in arr: arr[tuple(tempo)] = 1 return temp.append(nums[start]) self.subset(arr, temp, start + 1, nums) temp.pop() self.subset(arr, temp, start + 1, nums)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
def solve(arr, i, ans, temp): if i >= len(arr): ans.append(sorted(temp)) return temp.append(arr[i]) solve(arr, i + 1, ans, temp) temp.pop() solve(arr, i + 1, ans, temp) class Solution: def AllSubsets(self, arr, n): ans = [] solve(arr, 0, ans, []) f = {} for i in ans: f[tuple(i)] = 1 l = sorted(list(f.keys())) return l
FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR LIST ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): res = [] def solve(i, ans): if i == n: res.append(ans.copy()) return ans.append(arr[i]) solve(i + 1, ans) ans.pop() solve(i + 1, ans) arr.sort() solve(0, []) s = set() for item in res: s.add(tuple(item)) ans = list(s) ans.sort() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): ls = {()} tot = n temp = sorted(arr) def sol(subs, k, temp, tot): if k == tot: ls.add(tuple(subs.copy())) return idx = k + 1 while idx < tot and temp[idx] == temp[k]: idx += 1 count = idx - k for j in range(count + 1): for i in range(j): subs.append(temp[k]) sol(subs, idx, temp, tot) for i in range(j): subs.pop() subs = [] sol(subs, 0, temp, tot) ls = list(ls) ls = list(map(list, ls)) ls.sort() return ls
CLASS_DEF FUNC_DEF ASSIGN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): op = [] arr.sort() n = len(arr) res = {()} self.solve(arr, op, 0, n, res) res = list(res) res = list(map(list, res)) res.sort() return res def solve(self, arr, op, i, n, res): if i == n: res.add(tuple(op)) return op1 = op op2 = op self.solve(arr, op1, i + 1, n, res) self.solve(arr, op2 + [arr[i]], i + 1, n, res)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): ans = set() def fun(ip, op): if len(ip) == 0: ans.add(tuple(op)) return op.append(ip[0]) fun(ip[1:], op) op.pop() fun(ip[1:], op) arr.sort() fun(arr, []) ans = list(map(list, ans)) ans.sort() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr.sort() res = [] def recur(arr, start, path): res.append(path.copy()) for i in range(start, len(arr)): if i > start and arr[i - 1] == arr[i]: continue path.append(arr[i]) recur(arr, i + 1, path) path.pop() recur(arr, 0, []) return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER LIST RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, nums, n): def backtrack(first=0, curr=[]): if len(curr) <= len(nums): output.append(list(curr)) for i in range(first, len(nums)): if i > first and nums[i] == nums[i - 1]: continue curr.append(nums[i]) backtrack(i + 1, curr) curr.pop() nums.sort() output = [] backtrack() return output
CLASS_DEF FUNC_DEF FUNC_DEF NUMBER LIST IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): def backtrack(start, end, tmp): ans.append(tmp[:]) for i in range(start, end): if i > start and arr[i] == arr[i - 1]: continue tmp.append(arr[i]) backtrack(i + 1, end, tmp) tmp.pop() ans = [] arr.sort() backtrack(0, len(arr), []) return ans
CLASS_DEF FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR LIST RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, array, n): array.sort() result = [] def solve(idx, cur): if idx == len(array): result.append(cur[:]) return else: cur.append(array[idx]) solve(idx + 1, cur) cur.pop() while idx < len(array) - 1 and array[idx] == array[idx + 1]: idx += 1 solve(idx + 1, cur) solve(0, []) result.sort() return result
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER LIST EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def solve(self, s, ou, res): if len(s) == 0: res.add(tuple(ou)) return ou1 = ou[:] ou2 = ou[:] ou2.append(s[0]) s = s[1:] self.solve(s, ou1, res) self.solve(s, ou2, res) def AllSubsets(self, arr, n): ou = [] res = set() arr.sort() self.solve(arr, ou, res) res = list(res) res.sort() return res
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): ans = {()} arr.sort() def subset(arr, index, n, temp): if index >= n: ans.add(tuple(temp.copy())) return else: temp.append(arr[index]) subset(arr, index + 1, n, temp) temp.pop() subset(arr, index + 1, n, temp) return subset(arr, 0, n, []) ans = list(ans) ans = list(map(list, ans)) ans.sort() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN EXPR FUNC_CALL VAR VAR NUMBER VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr = sorted(arr) res = [] def solve(i, subset): if i >= len(arr): res.append(subset) return sub1 = subset[:] subset.append(arr[i]) solve(i + 1, subset) while i + 1 < len(arr) and arr[i] == arr[i + 1]: i += 1 solve(i + 1, sub1) solve(0, []) return sorted(res)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER LIST RETURN FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, array, n): array.sort() self.result = list() i = 0 temps = list() self.solve(i, temps, array, n) return self.result def solve(self, i, temps, array, n): self.result.append(temps) flag = None for i in range(i, n): if flag == None or array[i] != flag: flag = array[i] self.solve(i + 1, temps + [flag], array, n) return None
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR VAR IF VAR NONE VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR RETURN NONE
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr = sorted(arr) def util(currentList, resultSet, currentIndex): resultSet.add(tuple(currentList)) if currentIndex < len(arr): resultSet = util(currentList, resultSet, currentIndex + 1) nextList = currentList + [arr[currentIndex]] resultSet.add(tuple(nextList)) resultSet = util(nextList, resultSet, currentIndex + 1) return resultSet return sorted(list(util([], set(), 0)))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST FUNC_CALL VAR NUMBER
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): if not arr: return res = [] arr.sort() self.dfs(arr, 0, res, []) return res def dfs(self, arr, index, res, path): res.append(path) if index == len(arr): return for i in range(index, len(arr)): if i > index and arr[i] == arr[i - 1]: continue self.dfs(arr, i + 1, res, path + [arr[i]])
CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR LIST RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def solve(self, inp, out, res): if len(inp) == 0: res.add(tuple(out)) return out2 = out.copy() out2.append(inp[0]) inp = inp[1:] self.solve(inp, out, res) self.solve(inp, out2, res) def AllSubsets(self, arr, n): subset = set() arr.sort() op = [] self.solve(arr, op, subset) return sorted(list(subset))
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): def f(i, n, arr, l, ans): if i == n: ans.add(tuple(l)) return f(i + 1, n, arr, l, ans) f(i + 1, n, arr, l + [arr[i]], ans) return arr = sorted(arr) ans = set() f(0, n, arr, [], ans) return sorted(list(ans))
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR LIST VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR VAR LIST VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr.sort() curSet, subsets = [], [] self.helper(0, arr, n, curSet, subsets) subsets.sort() return subsets def helper(self, i, arr, n, curSet, subsets): if i >= n: subsets.append(curSet.copy()) return curSet.append(arr[i]) self.helper(i + 1, arr, n, curSet, subsets) curSet.pop() while i + 1 < n and arr[i] == arr[i + 1]: i += 1 self.helper(i + 1, arr, n, curSet, subsets)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): def sol(arr, idx, n, result, ans): if idx == n: ans.append(sorted(result)) return sol(arr, idx + 1, n, result, ans) result.append(arr[idx]) sol(arr, idx + 1, n, result, ans) result.pop() return ans, result = [], [] sol(arr, 0, n, result, ans) ans = set(tuple(ele) for ele in ans) ans = list(ans) return sorted(ans)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR RETURN ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): s = {()} arr.sort() def fun(i, arr, l): if i == len(arr): s.add(tuple(l.copy())) return l.append(arr[i]) fun(i + 1, arr, l) l.pop() fun(i + 1, arr, l) fun(0, arr, []) a = list(s) a.sort() return a if __name__ == "__main__": test_cases = int(input()) for cases in range(test_cases): n = int(input()) a = list(map(int, input().strip().split())) obj = Solution() result = obj.AllSubsets(a, n) for i in range(len(result)): print("(", end="") size = len(result[i]) for j in range(size - 1): print(result[i][j], end=" ") if size: print(result[i][size - 1], end=")") else: print(")", end="") print()
CLASS_DEF FUNC_DEF ASSIGN VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR STRING IF VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def subset(self, arr, i, curr, ans): if i == len(arr): ans.append(curr) return self.subset(arr, i + 1, curr + arr[i], ans) self.subset(arr, i + 1, curr, ans) def AllSubsets(self, arr, n): arr.sort() a = "" for i in arr: a = a + str(i) ans = [] self.subset(a, 0, "", ans) val = set(ans) v2 = list(val) v2.sort() return v2
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def solve(self, arr, out, i, ans): if i >= len(arr): ans.add(tuple(out.copy())) return ele = arr[i] out.append(ele) self.solve(arr, out, i + 1, ans) out.pop() self.solve(arr, out, i + 1, ans) def AllSubsets(self, arr, n): arr.sort() ans = {()} i = 0 self.solve(arr, [], i, ans) ans = list(ans) ans = list(map(list, ans)) ans.sort() return ans
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr.sort() res = [tuple()] for i in arr: temp = [tuple()] for j in res: temp.append(j) temp.append(j + (i,)) res = temp res = set(res) res = list(res) res.sort() return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): res = [] dp = {} def recurse(arr1, k1): key = arr1[:] key.sort() if tuple(key) in dp: return res.append(key) dp[tuple(key)] = 1 if k1 >= len(arr): return for i in range(k1, len(arr)): arr1.append(arr[i]) recurse(arr1, i + 1) arr1.pop() recurse([], 0) res.sort() return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST NUMBER EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, nums, n): def find(cur, nums): result.append(cur) for i in range(len(nums)): if not i or nums[i] != nums[i - 1]: find(cur + [nums[i]], nums[i + 1 :]) nums.sort() result = [] find([], nums) return result
CLASS_DEF FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): ans = [] ans_set = set() ans.append(()) idx = 0 cur_subset = [] self.allSubsetsUtil(arr, n, idx, cur_subset, ans, ans_set) ans.sort() return ans def allSubsetsUtil(self, arr, n, idx, cur_subset, ans, ans_set): for i in range(idx, n): cur_subset.append(arr[i]) to_be_inserted = sorted(cur_subset) tup = tuple(to_be_inserted) if tup not in ans_set: ans.append(tup) ans_set.add(tup) self.allSubsetsUtil(arr, n, i + 1, cur_subset, ans, ans_set) cur_subset.pop()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): def backtrack(start=0, cur=[]): result.append(cur) if start >= len(arr): return for i in range(start, len(arr)): if i > start and arr[i - 1] == arr[i]: continue backtrack(i + 1, cur + [arr[i]]) result = [] arr.sort() backtrack() return result
CLASS_DEF FUNC_DEF FUNC_DEF NUMBER LIST EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
def rec(arr, res, i, ans): if i == len(arr): ans.add(tuple(res)) return rec(arr, res, i + 1, ans) res.append(arr[i]) rec(arr, res, i + 1, ans) res.pop() class Solution: def AllSubsets(self, arr, n): arr.sort() ans = set() rec(arr, [], 0, ans) l = list(ans) l.sort() return l
FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): res = set() def solve(arr, i, q): if i >= len(arr): q.sort() q = tuple(q) res.add(q) return solve(arr, i + 1, q[:]) q.append(arr[i]) solve(arr, i + 1, q[:]) k = [] solve(arr, 0, k) res = list(res) for i in range(len(res)): res[i] = list(res[i]) return sorted(res)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, a, n): a.sort() l, d = [], {} def backtrack(i, b, f): if i == n: if f: if not d.get(tuple(b), False): l.append(b[:]) d[tuple(b)] = True return if f: if not d.get(tuple(b), False): l.append(b[:]) d[tuple(b)] = True b.append(a[i]) backtrack(i + 1, b, True) b.pop(-1) backtrack(i + 1, b, False) backtrack(0, [], True) return l
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST DICT FUNC_DEF IF VAR VAR IF VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN IF VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr.sort() b = [] m = {} def rec(i, l): if i == n: e = tuple(l) if e not in m: m[e] = 1 b.append(e) return rec(i + 1, l[:]) rec(i + 1, l[:] + [arr[i]]) rec(0, []) b.sort() return b
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR EXPR FUNC_CALL VAR NUMBER LIST EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): def dfs(s): nonlocal temp if s == []: return for i in range(len(s)): temp += s[i] if temp not in res: res.add(temp) dfs(s[i + 1 :]) temp = temp[:-1] temp = "" res = set() dfs("".join(sorted(list(map(str, arr))))) res = list(res) ans = [[]] for i in range(len(res)): lst = [] lst[0:] = res[i] ans.append(list(map(int, lst))) return sorted(ans)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR LIST RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): def solve(ip, op, res): if len(ip) == 0: op = tuple(op) res.add(op) return op1 = op[:] op2 = op[:] op2.append(ip[0]) ip.remove(ip[0]) solve(ip[:], op1, res) solve(ip[:], op2, res) res = set() ip = arr[:] ip.sort() solve(ip, [], res) res = list(res) for i in range(len(res)): res[i] = list(res[i]) return sorted(res)
CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): res = [] m = {} arr.sort() def backtrack(ind, curr): if ind == len(arr): l = tuple(curr) if l not in m: m[l] = 1 res.append(l) return backtrack(ind + 1, curr) curr.append(arr[ind]) backtrack(ind + 1, curr) curr.pop() backtrack(0, []) res.sort() return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT EXPR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER LIST EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): def func(arr, li, res, n, ind, ss): if ind == n: tt = tuple(li) if tt not in ss: res.append(li.copy()) ss.add(tt) return li.append(arr[ind]) func(arr, li, res, n, ind + 1, ss) li.pop() func(arr, li, res, n, ind + 1, ss) arr.sort() li = [] res = [] ss = set() func(arr, li, res, n, 0, ss) return sorted(res)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): res = [] subset = [] arr.sort() def helper(i): if i >= n: res.append(subset[:]) return subset.append(arr[i]) helper(i + 1) while i + 1 < n and arr[i] == arr[i + 1]: i += 1 subset.pop() helper(i + 1) helper(0) res.sort() return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def solve(self, arr, n, idx, output, ans): if idx == n: ans.append(output) return self.solve(arr, n, idx + 1, output, ans) self.solve(arr, n, idx + 1, output + [arr[idx]], ans) def AllSubsets(self, arr, n): arr.sort() ans = [] output = [] brr = [] idx = 0 self.solve(arr, n, idx, output, ans) ans.sort() brr.append(ans[0]) for i in range(len(ans)): if ans[i] != brr[-1]: brr.append(ans[i]) return brr
CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): dic = {} def subs(idx, arr, lst, tmp): if idx >= len(arr): if str(tmp) not in dic: lst.append(tmp[:]) dic[str(tmp)] = 1 return tmp.append(arr[idx]) subs(idx + 1, arr, lst, tmp) tmp.pop() subs(idx + 1, arr, lst, tmp) lst = [] tmp = [] arr.sort() subs(0, arr, lst, tmp) return sorted(lst)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def util(self, arr, n, res, ans): if n == 0: res.add(tuple(sorted(ans.copy()))) return self.util(arr, n - 1, res, ans + [arr[n - 1]]) self.util(arr, n - 1, res, ans) def AllSubsets(self, arr, n): res = set() self.util(arr, n, res, []) val = list() for x in res: val.append(x) val = sorted(val) return val
CLASS_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): res = [] arr.sort() self.rec(arr, 0, [], res) return res def rec(self, arr, indx, cur_arr, res): res.append(cur_arr[:]) last_num = -1 for i in range(indx, len(arr)): if last_num == arr[i]: continue else: last_num = arr[i] cur_arr.append(last_num) self.rec(arr, i + 1, cur_arr, res) cur_arr.pop()
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER LIST VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): def dfs(ind, path): if ind == n: res.add(path) return dfs(ind + 1, path + (arr[ind],)) dfs(ind + 1, path) res = set() x = tuple() arr.sort() dfs(0, x) res = sorted(list(res)) return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
def subse(arr, ind, ans, res, n): if ind >= n: res.add(tuple(ans)) return subse(arr, ind + 1, ans.copy(), res, n) ans.append(arr[ind]) subse(arr, ind + 1, ans.copy(), res, n) class Solution: def AllSubsets(self, arr, n): ans = [] res = set() arr.sort() subse(arr, 0, ans, res, n) res = list(res) res.sort() return res
FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): dic = {} arr.sort() self.solve(arr, (), dic) return sorted(dic.keys()) def solve(self, ip, op, dic): if len(ip) == 0: if op not in dic: dic[op] = 1 return else: op1 = op + (ip[0],) op2 = op ip = self.erase(ip) self.solve(ip, op1, dic) self.solve(ip, op2, dic) return def erase(self, ip): if len(ip) <= 1: return "" else: return ip[1:]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN VAR NUMBER
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def subsetsHelper(self, arr, i, n, cur, res): if i >= n: res.append(cur[:]) return curInd = i + 1 while curInd < n and arr[curInd] == arr[i]: curInd += 1 count = curInd - i for j in range(0, count + 1): for k in range(j): cur.append(arr[i]) self.subsetsHelper(arr, curInd, n, cur, res) for k in range(j): cur.pop() def AllSubsets(self, arr, n): res = [] cur = [] arr.sort() self.subsetsHelper(arr, 0, n, cur, res) res.sort() return res
CLASS_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr.sort() subsets = [] subsets.append([]) start, end = 0, 0 for i in range(n): start = 0 if i > 0 and arr[i] == arr[i - 1]: start = end + 1 end = len(subsets) - 1 for j in range(start, end + 1): newSet = list(subsets[j]) newSet.append(arr[i]) subsets.append(newSet) subsets.sort() return subsets
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, A, n): ans = set() def rec(tem, A, ind, n): if ind == n: ans.add(tuple(sorted(tem))) return tem.append(A[ind]) rec(tem, A, ind + 1, n) tem.pop(-1) rec(tem, A, ind + 1, n) rec([], A, 0, len(A)) ans = list(ans) ans.sort() return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
from itertools import chain, combinations def powerset(iterable): s = list(iterable) return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1)) class Solution: def AllSubsets(self, arr, n): return sorted(set(powerset(sorted(arr))))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr = sorted(arr) ans = [] def solve(arr, n, idx, curr, ans): if idx >= n: ans.append(curr[:]) return curr.append(arr[idx]) solve(arr, n, idx + 1, curr, ans) curr.pop() while idx < n - 1 and arr[idx] == arr[idx + 1]: idx += 1 solve(arr, n, idx + 1, curr, ans) return solve(arr, n, 0, [], ans) return sorted(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR NUMBER LIST VAR RETURN FUNC_CALL VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr.sort() subsets = [] self.util(arr, 0, n, "", subsets) subsets.sort() res = [] for i, s in enumerate(subsets): if i == 0: res.append([]) elif i > 0 and subsets[i] != subsets[i - 1]: li = s.split(",") res.append(list(map(int, li))) return res def util(self, arr, i, n, sub, subsets): if i == n: subsets.append(sub) return if sub == "": s_cpy = str(arr[i]) else: s_cpy = sub + "," + str(arr[i]) self.util(arr, i + 1, n, s_cpy, subsets) self.util(arr, i + 1, n, sub, subsets)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def solve(self, index, nums, op, res): if index >= len(nums): res.add(tuple(op)) return op.append(nums[index]) self.solve(index + 1, nums, op, res) op.pop() self.solve(index + 1, nums, op, res) return def AllSubsets(self, arr, n): arr.sort() op = [] res = set() self.solve(0, arr, op, res) res = list(res) res.sort() return res
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr = sorted(arr) def getSubset(index, temp, result): result.append(list(temp)) if index == len(arr): return for i in range(index, len(arr)): if i != index and arr[i] == arr[i - 1]: continue temp.append(arr[i]) getSubset(i + 1, temp, result) del temp[-1] result = [] getSubset(0, [], result) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr.sort() res = [] V = set() def bt(path, i): if tuple(path) not in V: res.append(list(path)) V.add(tuple(path)) for j in range(i, n): path.append(arr[j]) bt(path, j + 1) path.pop() bt([], 0) return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST NUMBER RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def rec(self, arr, n, i, sets, curr_set): sets.add(tuple(curr_set.copy())) for j in range(i, n): curr_set.append(arr[j]) self.rec(arr, n, j + 1, sets, curr_set) curr_set.pop() def AllSubsets(self, arr, n): arr.sort() sets = set() self.rec(arr, n, 0, sets, []) new_sets = [list(i) for i in sets] new_sets.sort() return new_sets
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
class Solution: def AllSubsets(self, arr, n): arr.sort() res = [] def solve(idx, ans): res.append(ans.copy()) prev = -1 for i in range(idx, n): if arr[i] != prev: ans.append(arr[i]) solve(i + 1, ans) ans.pop() prev = arr[i] solve(0, []) return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets. Note: Each subset should be sorted. Example 1: Input: N = 3, arr[] = {2,1,2} Output:(),(1),(1 2),(1 2 2),(2),(2 2) Explanation: All possible subsets = (),(2),(1),(1,2),(2),(2,2),(2,1),(2,1,2) After Sorting each subset = (),(2),(1),(1,2),(2),(2,2),(1,2),(1,2,2) Unique Susbsets in Lexicographical order = (),(1),(1,2),(1,2,2),(2),(2,2) Example 2: Input: N = 4, arr[] = {1,2,3,3} Output: (),(1),(1 2),(1 2 3) (1 2 3 3),(1 3),(1 3 3),(2),(2 3) (2 3 3),(3),(3 3) Your Task: Your task is to complete the function AllSubsets() which takes the array arr[] and N as input parameters and returns list of all possible unique subsets in lexicographical order. Expected Time Complexity: O(2^{N}). Expected Auxiliary Space: O(2^{N} * X), X = Length of each subset. Constraints: 1 ≀ N ≀ 12 1 ≀ arr[i] ≀ 9
def solve(arr, index, output, ans): if index >= len(arr): ans.append(output[:]) return output.append(arr[index]) solve(arr, index + 1, output, ans) output.remove(arr[index]) while index + 1 < len(arr) and arr[index] == arr[index + 1]: index += 1 solve(arr, index + 1, output, ans) def subset(arr): ans = [] output = [] index = 0 solve(arr, index, output, ans) ans.sort() return ans class Solution: def AllSubsets(self, arr, n): arr.sort() return subset(arr)
FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. College admissions are starting and Chef has also recently completed his high school. There are $N$ colleges, numbered from $1$ to $N$, and they are ranked from $1$ to $N$. Each college has only a single seat left. To apply for the colleges, every student has to give a common exam, the result of which decides their destiny, i.e, the student having best rank (scoring max marks) gets to choose the college of their preference first. Each student has a list of colleges that they like. They will want to get to the best-ranked college from their list. If they can't get into it, they will want to get into the second-ranked college from among their list, and so on. If the single vacant seat of a college gets filled, the admission for that year is closed in that college. Then, the people who have applied for that college have to look for a lower-ranked college in their preference list provided that it still has vacancies. Given information about $M$ students, about their marks scored in the exam (given as ranks) and the id’s of the college they are applying for, find which college Chef will land into given that he is the student with $id = 1$. Every student tries to get into the highest-ranked college on his list which has a vacancy and if not possible to get into any of the colleges on his list, he won’t join any college this year. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $M + 3$ lines of input. First line contains $2$ space separated integers $N$, $M$, the total number of colleges, and total number of students respectively. Second line contain $N$ distinct space separated integers $R_{i}$, rank of the $i^{th}$ college. Here $1$ denotes a higher rank and $N$ denotes a lower rank. Third line contains $M$ distinct space separated integers $S_{i}$, the rank of the $i^{th}$ student in the exam. Here $1$ denotes a higher rank, i.e, maximum marks scored and $M$ denotes a lower rank, i.e, minimum marks scored. Next $M$ lines contain $K + 1$ space separated integers which the first integer $K$ is the number of colleges $i^{th}$ student will apply to and the next $K$ integers describe the same. ------ Output: ------ For each testcase, output in a single line the college in which Chef applies and $0$ if he won't be joining any college this year. ------ Constraints ------ $1 ≀ N, M ≀ 5*10^{4}$ $R$ is the permutation of integers from $[1, N]$ $S$ is the permutation of integers from $[1, M]$ Sum of $N$ over all tests is atmost $3* 10^{5}$ Sum of $M$ over all tests is atmost $3.5 * 10^{5}$ Sum of $K$ over all students over all tests is atmost $1.5*10^{6}$ Every student applies to atleast $1$ college and all the applied colleges are different. Note: Language multiplier for $Java$ is $3$ and for $python$ is $5$ and its recommended to use Fast Input-Output for this problem. ----- Sample Input 1 ------ 3 2 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 1 1 1 1 1 2 1 1 2 1 2 2 1 ----- Sample Output 1 ------ 2 0 1 ----- explanation 1 ------ Case 1: Here since Chef has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 2$. Case 2: Here since there is only $1$ college and Chef stood second, he won't be able to apply to any of the colleges this year, so we print $id = 0$. Case 3: Here since Chef is the only student and thus has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 1$.
for i in range(int(input())): N, M = map(int, input().split()) cRank = list(map(int, input().split())) sRank = list(map(int, input().split())) students = [] for i in range(M): c = list(map(int, input().split())) _c = sorted(c[1:], key=lambda x: cRank[x - 1]) students.append([i, c[0], _c]) sort_students = sorted(students, key=lambda x: sRank[x[0]]) clgs_filed = [False] * N choice = 0 for i in sort_students: ch = 0 for j in i[2]: if clgs_filed[j - 1] == False: clgs_filed[j - 1] = True ch = j break if i[0] == 0: choice = ch break print(choice)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. College admissions are starting and Chef has also recently completed his high school. There are $N$ colleges, numbered from $1$ to $N$, and they are ranked from $1$ to $N$. Each college has only a single seat left. To apply for the colleges, every student has to give a common exam, the result of which decides their destiny, i.e, the student having best rank (scoring max marks) gets to choose the college of their preference first. Each student has a list of colleges that they like. They will want to get to the best-ranked college from their list. If they can't get into it, they will want to get into the second-ranked college from among their list, and so on. If the single vacant seat of a college gets filled, the admission for that year is closed in that college. Then, the people who have applied for that college have to look for a lower-ranked college in their preference list provided that it still has vacancies. Given information about $M$ students, about their marks scored in the exam (given as ranks) and the id’s of the college they are applying for, find which college Chef will land into given that he is the student with $id = 1$. Every student tries to get into the highest-ranked college on his list which has a vacancy and if not possible to get into any of the colleges on his list, he won’t join any college this year. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $M + 3$ lines of input. First line contains $2$ space separated integers $N$, $M$, the total number of colleges, and total number of students respectively. Second line contain $N$ distinct space separated integers $R_{i}$, rank of the $i^{th}$ college. Here $1$ denotes a higher rank and $N$ denotes a lower rank. Third line contains $M$ distinct space separated integers $S_{i}$, the rank of the $i^{th}$ student in the exam. Here $1$ denotes a higher rank, i.e, maximum marks scored and $M$ denotes a lower rank, i.e, minimum marks scored. Next $M$ lines contain $K + 1$ space separated integers which the first integer $K$ is the number of colleges $i^{th}$ student will apply to and the next $K$ integers describe the same. ------ Output: ------ For each testcase, output in a single line the college in which Chef applies and $0$ if he won't be joining any college this year. ------ Constraints ------ $1 ≀ N, M ≀ 5*10^{4}$ $R$ is the permutation of integers from $[1, N]$ $S$ is the permutation of integers from $[1, M]$ Sum of $N$ over all tests is atmost $3* 10^{5}$ Sum of $M$ over all tests is atmost $3.5 * 10^{5}$ Sum of $K$ over all students over all tests is atmost $1.5*10^{6}$ Every student applies to atleast $1$ college and all the applied colleges are different. Note: Language multiplier for $Java$ is $3$ and for $python$ is $5$ and its recommended to use Fast Input-Output for this problem. ----- Sample Input 1 ------ 3 2 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 1 1 1 1 1 2 1 1 2 1 2 2 1 ----- Sample Output 1 ------ 2 0 1 ----- explanation 1 ------ Case 1: Here since Chef has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 2$. Case 2: Here since there is only $1$ college and Chef stood second, he won't be able to apply to any of the colleges this year, so we print $id = 0$. Case 3: Here since Chef is the only student and thus has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 1$.
t = int(input()) while t != 0: n, m = list(map(int, input().split())) coll = list(map(int, input().split())) stud = list(map(int, input().split())) pref = [] for i in range(m): c = list(map(int, input().split())) _c = sorted(c[1:], key=lambda x: coll[x - 1]) pref.append(_c) allot = [0] * n top_stud = [] for i in range(m): top_stud.append(i) top_stud = sorted(top_stud, key=lambda x: stud[x]) for i in top_stud: flag = 0 j = 0 while j < len(pref[i]): if allot[pref[i][j] - 1] == 0: flag = 1 allot[pref[i][j] - 1] = 1 if i == 0: print(pref[i][j]) break else: j += 1 if i == 0: if flag == 0: print(0) break t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. College admissions are starting and Chef has also recently completed his high school. There are $N$ colleges, numbered from $1$ to $N$, and they are ranked from $1$ to $N$. Each college has only a single seat left. To apply for the colleges, every student has to give a common exam, the result of which decides their destiny, i.e, the student having best rank (scoring max marks) gets to choose the college of their preference first. Each student has a list of colleges that they like. They will want to get to the best-ranked college from their list. If they can't get into it, they will want to get into the second-ranked college from among their list, and so on. If the single vacant seat of a college gets filled, the admission for that year is closed in that college. Then, the people who have applied for that college have to look for a lower-ranked college in their preference list provided that it still has vacancies. Given information about $M$ students, about their marks scored in the exam (given as ranks) and the id’s of the college they are applying for, find which college Chef will land into given that he is the student with $id = 1$. Every student tries to get into the highest-ranked college on his list which has a vacancy and if not possible to get into any of the colleges on his list, he won’t join any college this year. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $M + 3$ lines of input. First line contains $2$ space separated integers $N$, $M$, the total number of colleges, and total number of students respectively. Second line contain $N$ distinct space separated integers $R_{i}$, rank of the $i^{th}$ college. Here $1$ denotes a higher rank and $N$ denotes a lower rank. Third line contains $M$ distinct space separated integers $S_{i}$, the rank of the $i^{th}$ student in the exam. Here $1$ denotes a higher rank, i.e, maximum marks scored and $M$ denotes a lower rank, i.e, minimum marks scored. Next $M$ lines contain $K + 1$ space separated integers which the first integer $K$ is the number of colleges $i^{th}$ student will apply to and the next $K$ integers describe the same. ------ Output: ------ For each testcase, output in a single line the college in which Chef applies and $0$ if he won't be joining any college this year. ------ Constraints ------ $1 ≀ N, M ≀ 5*10^{4}$ $R$ is the permutation of integers from $[1, N]$ $S$ is the permutation of integers from $[1, M]$ Sum of $N$ over all tests is atmost $3* 10^{5}$ Sum of $M$ over all tests is atmost $3.5 * 10^{5}$ Sum of $K$ over all students over all tests is atmost $1.5*10^{6}$ Every student applies to atleast $1$ college and all the applied colleges are different. Note: Language multiplier for $Java$ is $3$ and for $python$ is $5$ and its recommended to use Fast Input-Output for this problem. ----- Sample Input 1 ------ 3 2 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 1 1 1 1 1 2 1 1 2 1 2 2 1 ----- Sample Output 1 ------ 2 0 1 ----- explanation 1 ------ Case 1: Here since Chef has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 2$. Case 2: Here since there is only $1$ college and Chef stood second, he won't be able to apply to any of the colleges this year, so we print $id = 0$. Case 3: Here since Chef is the only student and thus has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 1$.
T = int(input()) for _ in range(T): N, M = map(int, input().split()) R = list(map(int, input().split())) S = list(map(int, input().split())) l = [] h = {} for i in range(M): K = list(map(int, input().split())) K = K[1:] K.sort(key=lambda x: R[x - 1]) l.append([K, i + 1]) l.sort(key=lambda x: S[x[1] - 1]) a = 0 for i in range(M): x = 0 for j in l[i][0]: if h.get(j): continue else: h[j] = 1 x = j break if l[i][1] == 1: a = x break print(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. College admissions are starting and Chef has also recently completed his high school. There are $N$ colleges, numbered from $1$ to $N$, and they are ranked from $1$ to $N$. Each college has only a single seat left. To apply for the colleges, every student has to give a common exam, the result of which decides their destiny, i.e, the student having best rank (scoring max marks) gets to choose the college of their preference first. Each student has a list of colleges that they like. They will want to get to the best-ranked college from their list. If they can't get into it, they will want to get into the second-ranked college from among their list, and so on. If the single vacant seat of a college gets filled, the admission for that year is closed in that college. Then, the people who have applied for that college have to look for a lower-ranked college in their preference list provided that it still has vacancies. Given information about $M$ students, about their marks scored in the exam (given as ranks) and the id’s of the college they are applying for, find which college Chef will land into given that he is the student with $id = 1$. Every student tries to get into the highest-ranked college on his list which has a vacancy and if not possible to get into any of the colleges on his list, he won’t join any college this year. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $M + 3$ lines of input. First line contains $2$ space separated integers $N$, $M$, the total number of colleges, and total number of students respectively. Second line contain $N$ distinct space separated integers $R_{i}$, rank of the $i^{th}$ college. Here $1$ denotes a higher rank and $N$ denotes a lower rank. Third line contains $M$ distinct space separated integers $S_{i}$, the rank of the $i^{th}$ student in the exam. Here $1$ denotes a higher rank, i.e, maximum marks scored and $M$ denotes a lower rank, i.e, minimum marks scored. Next $M$ lines contain $K + 1$ space separated integers which the first integer $K$ is the number of colleges $i^{th}$ student will apply to and the next $K$ integers describe the same. ------ Output: ------ For each testcase, output in a single line the college in which Chef applies and $0$ if he won't be joining any college this year. ------ Constraints ------ $1 ≀ N, M ≀ 5*10^{4}$ $R$ is the permutation of integers from $[1, N]$ $S$ is the permutation of integers from $[1, M]$ Sum of $N$ over all tests is atmost $3* 10^{5}$ Sum of $M$ over all tests is atmost $3.5 * 10^{5}$ Sum of $K$ over all students over all tests is atmost $1.5*10^{6}$ Every student applies to atleast $1$ college and all the applied colleges are different. Note: Language multiplier for $Java$ is $3$ and for $python$ is $5$ and its recommended to use Fast Input-Output for this problem. ----- Sample Input 1 ------ 3 2 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 1 1 1 1 1 2 1 1 2 1 2 2 1 ----- Sample Output 1 ------ 2 0 1 ----- explanation 1 ------ Case 1: Here since Chef has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 2$. Case 2: Here since there is only $1$ college and Chef stood second, he won't be able to apply to any of the colleges this year, so we print $id = 0$. Case 3: Here since Chef is the only student and thus has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 1$.
t = int(input()) while t != 0: n, m = list(map(int, input().split())) collegeRank = list(map(int, input().split())) studentsRank = list(map(int, input().split())) chefsRank = studentsRank[0] studentPreferences = [[-1]] * (chefsRank + 1) for i in range(m): CurrentStudentRank = studentsRank[i] studentsPreference = list(map(int, input().split())) if CurrentStudentRank <= chefsRank: studentPreferences[CurrentStudentRank] = studentsPreference studentsRank.sort() if n < chefsRank: print("0") else: chefsPreferences = studentPreferences[chefsRank] chefsPreferences = chefsPreferences[1:] j = 1 while j <= chefsRank: currentPreference = studentPreferences[j] currentPreference = currentPreference[1:] commonPreferences = list(set(chefsPreferences) & set(currentPreference)) maxRank = 0 if len(commonPreferences) > 0: ranking = [] for i in range(len(commonPreferences)): currentCollageRank = collegeRank[commonPreferences[i] - 1] if currentCollageRank != -1: ranking.append(currentCollageRank) if len(ranking) > 0: maxRank = min(ranking) if j != chefsRank: collegeRank[collegeRank.index(maxRank)] = -1 j = j + 1 if maxRank == -1 or maxRank == 0: print("0") else: print(collegeRank.index(maxRank) + 1) t = t - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. College admissions are starting and Chef has also recently completed his high school. There are $N$ colleges, numbered from $1$ to $N$, and they are ranked from $1$ to $N$. Each college has only a single seat left. To apply for the colleges, every student has to give a common exam, the result of which decides their destiny, i.e, the student having best rank (scoring max marks) gets to choose the college of their preference first. Each student has a list of colleges that they like. They will want to get to the best-ranked college from their list. If they can't get into it, they will want to get into the second-ranked college from among their list, and so on. If the single vacant seat of a college gets filled, the admission for that year is closed in that college. Then, the people who have applied for that college have to look for a lower-ranked college in their preference list provided that it still has vacancies. Given information about $M$ students, about their marks scored in the exam (given as ranks) and the id’s of the college they are applying for, find which college Chef will land into given that he is the student with $id = 1$. Every student tries to get into the highest-ranked college on his list which has a vacancy and if not possible to get into any of the colleges on his list, he won’t join any college this year. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $M + 3$ lines of input. First line contains $2$ space separated integers $N$, $M$, the total number of colleges, and total number of students respectively. Second line contain $N$ distinct space separated integers $R_{i}$, rank of the $i^{th}$ college. Here $1$ denotes a higher rank and $N$ denotes a lower rank. Third line contains $M$ distinct space separated integers $S_{i}$, the rank of the $i^{th}$ student in the exam. Here $1$ denotes a higher rank, i.e, maximum marks scored and $M$ denotes a lower rank, i.e, minimum marks scored. Next $M$ lines contain $K + 1$ space separated integers which the first integer $K$ is the number of colleges $i^{th}$ student will apply to and the next $K$ integers describe the same. ------ Output: ------ For each testcase, output in a single line the college in which Chef applies and $0$ if he won't be joining any college this year. ------ Constraints ------ $1 ≀ N, M ≀ 5*10^{4}$ $R$ is the permutation of integers from $[1, N]$ $S$ is the permutation of integers from $[1, M]$ Sum of $N$ over all tests is atmost $3* 10^{5}$ Sum of $M$ over all tests is atmost $3.5 * 10^{5}$ Sum of $K$ over all students over all tests is atmost $1.5*10^{6}$ Every student applies to atleast $1$ college and all the applied colleges are different. Note: Language multiplier for $Java$ is $3$ and for $python$ is $5$ and its recommended to use Fast Input-Output for this problem. ----- Sample Input 1 ------ 3 2 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 1 1 1 1 1 2 1 1 2 1 2 2 1 ----- Sample Output 1 ------ 2 0 1 ----- explanation 1 ------ Case 1: Here since Chef has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 2$. Case 2: Here since there is only $1$ college and Chef stood second, he won't be able to apply to any of the colleges this year, so we print $id = 0$. Case 3: Here since Chef is the only student and thus has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 1$.
clg_choice = [] clg_rank_new = [] testcases = int(input()) for i in range(testcases): clg_choice.clear() clg_rank_new.clear() no_clg = int(0) N_of_clg, M_of_students = map(int, input().split()) clg_rank = list(map(int, input().split())) clg_rank = [[clg_rank[i], i + 1, 0] for i in range(N_of_clg)] students_rank = list(map(int, input().split())) chef_rank = students_rank[0] students_rank = [[students_rank[i], i + 1, 0] for i in range(M_of_students)] for i in range(M_of_students): clg_choice.append(list(map(int, input().split()))) clg_choice[i][0] = 0 students_rank.sort() for j in range(1, len(clg_choice[0])): clg_rank[clg_choice[0][j] - 1][2] = 1 clg_rank_new.append(clg_rank[clg_choice[0][j] - 1]) clg_rank_new.sort() for i in range(len(clg_choice[0]) - 1): no_clg = clg_rank_new[i][1] for j in range(chef_rank - 1): if students_rank[j][2] == 0: if clg_rank_new[i][1] in clg_choice[students_rank[j][1] - 1]: students_rank[j][2] = 1 if i + 1 == len(clg_choice[0]) - 1: no_clg = 0 else: no_clg = clg_rank_new[i + 1][1] break if no_clg == clg_rank_new[i][1]: break print(no_clg) students_rank.clear() clg_rank.clear()
ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. College admissions are starting and Chef has also recently completed his high school. There are $N$ colleges, numbered from $1$ to $N$, and they are ranked from $1$ to $N$. Each college has only a single seat left. To apply for the colleges, every student has to give a common exam, the result of which decides their destiny, i.e, the student having best rank (scoring max marks) gets to choose the college of their preference first. Each student has a list of colleges that they like. They will want to get to the best-ranked college from their list. If they can't get into it, they will want to get into the second-ranked college from among their list, and so on. If the single vacant seat of a college gets filled, the admission for that year is closed in that college. Then, the people who have applied for that college have to look for a lower-ranked college in their preference list provided that it still has vacancies. Given information about $M$ students, about their marks scored in the exam (given as ranks) and the id’s of the college they are applying for, find which college Chef will land into given that he is the student with $id = 1$. Every student tries to get into the highest-ranked college on his list which has a vacancy and if not possible to get into any of the colleges on his list, he won’t join any college this year. ------ Input: ------ First line will contain $T$, number of testcases. Then the testcases follow. Each testcase contains $M + 3$ lines of input. First line contains $2$ space separated integers $N$, $M$, the total number of colleges, and total number of students respectively. Second line contain $N$ distinct space separated integers $R_{i}$, rank of the $i^{th}$ college. Here $1$ denotes a higher rank and $N$ denotes a lower rank. Third line contains $M$ distinct space separated integers $S_{i}$, the rank of the $i^{th}$ student in the exam. Here $1$ denotes a higher rank, i.e, maximum marks scored and $M$ denotes a lower rank, i.e, minimum marks scored. Next $M$ lines contain $K + 1$ space separated integers which the first integer $K$ is the number of colleges $i^{th}$ student will apply to and the next $K$ integers describe the same. ------ Output: ------ For each testcase, output in a single line the college in which Chef applies and $0$ if he won't be joining any college this year. ------ Constraints ------ $1 ≀ N, M ≀ 5*10^{4}$ $R$ is the permutation of integers from $[1, N]$ $S$ is the permutation of integers from $[1, M]$ Sum of $N$ over all tests is atmost $3* 10^{5}$ Sum of $M$ over all tests is atmost $3.5 * 10^{5}$ Sum of $K$ over all students over all tests is atmost $1.5*10^{6}$ Every student applies to atleast $1$ college and all the applied colleges are different. Note: Language multiplier for $Java$ is $3$ and for $python$ is $5$ and its recommended to use Fast Input-Output for this problem. ----- Sample Input 1 ------ 3 2 2 2 1 1 2 2 1 2 2 1 2 1 2 1 2 1 1 1 1 1 2 1 1 2 1 2 2 1 ----- Sample Output 1 ------ 2 0 1 ----- explanation 1 ------ Case 1: Here since Chef has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 2$. Case 2: Here since there is only $1$ college and Chef stood second, he won't be able to apply to any of the colleges this year, so we print $id = 0$. Case 3: Here since Chef is the only student and thus has the highest rank, he gets to choose the highest rank college in his preference list first, that is college with $id = 1$.
tot = int(input()) for _ in range(tot): ans = None N, M = list(map(int, input().split())) CR = input() SR = list(map(int, input().split())) SP = [] filled = [0] * (N + 1) for _ in range(M): sp_temp = list(map(int, input().split())) SP.append(sp_temp) chefRank = SR[0] SR, SP = [list(t) for t in zip(*sorted(zip(SR, SP)))] for i, rank in enumerate(SR): if rank != chefRank: for pref in SP[i][1:]: if filled[pref] == 0: filled[pref] = 1 break else: for pref in SP[i][1:]: if filled[pref] == 0: ans = pref print(ans) break if ans == None: print(0) break break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER
There is an infinite board of square tiles. Initially all tiles are white. Vova has a red marker and a blue marker. Red marker can color $a$ tiles. Blue marker can color $b$ tiles. If some tile isn't white then you can't use marker of any color on it. Each marker must be drained completely, so at the end there should be exactly $a$ red tiles and exactly $b$ blue tiles across the board. Vova wants to color such a set of tiles that: they would form a rectangle, consisting of exactly $a+b$ colored tiles; all tiles of at least one color would also form a rectangle. Here are some examples of correct colorings: [Image] Here are some examples of incorrect colorings: [Image] Among all correct colorings Vova wants to choose the one with the minimal perimeter. What is the minimal perimeter Vova can obtain? It is guaranteed that there exists at least one correct coloring. -----Input----- A single line contains two integers $a$ and $b$ ($1 \le a, b \le 10^{14}$) β€” the number of tiles red marker should color and the number of tiles blue marker should color, respectively. -----Output----- Print a single integer β€” the minimal perimeter of a colored rectangle Vova can obtain by coloring exactly $a$ tiles red and exactly $b$ tiles blue. It is guaranteed that there exists at least one correct coloring. -----Examples----- Input 4 4 Output 12 Input 3 9 Output 14 Input 9 3 Output 14 Input 3 6 Output 12 Input 506 2708 Output 3218 -----Note----- The first four examples correspond to the first picture of the statement. Note that for there exist multiple correct colorings for all of the examples. In the first example you can also make a rectangle with sides $1$ and $8$, though its perimeter will be $18$ which is greater than $8$. In the second example you can make the same resulting rectangle with sides $3$ and $4$, but red tiles will form the rectangle with sides $1$ and $3$ and blue tiles will form the rectangle with sides $3$ and $3$.
a, b = map(int, input().split()) s1, s2, s3 = a**0.5, b**0.5, (a + b) ** 0.5 m = a + b ans = 0 for i in range(1, int(s3) + 1): if i <= s1 and a % i == 0: m = min(m, a // i) if i <= s2 and b % i == 0: m = min(m, b // i) if (a + b) % i == 0: d = (a + b) // i if d >= m: ans = (i + d) * 2 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There is an infinite board of square tiles. Initially all tiles are white. Vova has a red marker and a blue marker. Red marker can color $a$ tiles. Blue marker can color $b$ tiles. If some tile isn't white then you can't use marker of any color on it. Each marker must be drained completely, so at the end there should be exactly $a$ red tiles and exactly $b$ blue tiles across the board. Vova wants to color such a set of tiles that: they would form a rectangle, consisting of exactly $a+b$ colored tiles; all tiles of at least one color would also form a rectangle. Here are some examples of correct colorings: [Image] Here are some examples of incorrect colorings: [Image] Among all correct colorings Vova wants to choose the one with the minimal perimeter. What is the minimal perimeter Vova can obtain? It is guaranteed that there exists at least one correct coloring. -----Input----- A single line contains two integers $a$ and $b$ ($1 \le a, b \le 10^{14}$) β€” the number of tiles red marker should color and the number of tiles blue marker should color, respectively. -----Output----- Print a single integer β€” the minimal perimeter of a colored rectangle Vova can obtain by coloring exactly $a$ tiles red and exactly $b$ tiles blue. It is guaranteed that there exists at least one correct coloring. -----Examples----- Input 4 4 Output 12 Input 3 9 Output 14 Input 9 3 Output 14 Input 3 6 Output 12 Input 506 2708 Output 3218 -----Note----- The first four examples correspond to the first picture of the statement. Note that for there exist multiple correct colorings for all of the examples. In the first example you can also make a rectangle with sides $1$ and $8$, though its perimeter will be $18$ which is greater than $8$. In the second example you can make the same resulting rectangle with sides $3$ and $4$, but red tiles will form the rectangle with sides $1$ and $3$ and blue tiles will form the rectangle with sides $3$ and $3$.
inp = input() inp = inp.split() a = int(inp[0]) b = int(inp[1]) ma = a + b mx = ans = 1e19 k = int(ma**0.5) i = 1 while i <= k: if a % i == 0: mx = min(mx, a / i) if b % i == 0: mx = min(mx, b / i) if ma % i == 0 and ma / i >= mx: ans = min(ans, 2 * (i + ma / i)) i += 1 print(int(ans))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There is an infinite board of square tiles. Initially all tiles are white. Vova has a red marker and a blue marker. Red marker can color $a$ tiles. Blue marker can color $b$ tiles. If some tile isn't white then you can't use marker of any color on it. Each marker must be drained completely, so at the end there should be exactly $a$ red tiles and exactly $b$ blue tiles across the board. Vova wants to color such a set of tiles that: they would form a rectangle, consisting of exactly $a+b$ colored tiles; all tiles of at least one color would also form a rectangle. Here are some examples of correct colorings: [Image] Here are some examples of incorrect colorings: [Image] Among all correct colorings Vova wants to choose the one with the minimal perimeter. What is the minimal perimeter Vova can obtain? It is guaranteed that there exists at least one correct coloring. -----Input----- A single line contains two integers $a$ and $b$ ($1 \le a, b \le 10^{14}$) β€” the number of tiles red marker should color and the number of tiles blue marker should color, respectively. -----Output----- Print a single integer β€” the minimal perimeter of a colored rectangle Vova can obtain by coloring exactly $a$ tiles red and exactly $b$ tiles blue. It is guaranteed that there exists at least one correct coloring. -----Examples----- Input 4 4 Output 12 Input 3 9 Output 14 Input 9 3 Output 14 Input 3 6 Output 12 Input 506 2708 Output 3218 -----Note----- The first four examples correspond to the first picture of the statement. Note that for there exist multiple correct colorings for all of the examples. In the first example you can also make a rectangle with sides $1$ and $8$, though its perimeter will be $18$ which is greater than $8$. In the second example you can make the same resulting rectangle with sides $3$ and $4$, but red tiles will form the rectangle with sides $1$ and $3$ and blue tiles will form the rectangle with sides $3$ and $3$.
read_numbers = lambda: map(int, input().split()) INF = 1 << 64 a, b = read_numbers() ans, n, m = INF, 1, 1 for i in range(1, int((a + b) ** 0.5 + 1)): if a % i == 0: n = i if b % i == 0: m = i if (a + b) % i == 0: second = (a + b) // i if a // n <= second or b // m <= second: ans = min(ans, 2 * (second + i)) print(ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There is an infinite board of square tiles. Initially all tiles are white. Vova has a red marker and a blue marker. Red marker can color $a$ tiles. Blue marker can color $b$ tiles. If some tile isn't white then you can't use marker of any color on it. Each marker must be drained completely, so at the end there should be exactly $a$ red tiles and exactly $b$ blue tiles across the board. Vova wants to color such a set of tiles that: they would form a rectangle, consisting of exactly $a+b$ colored tiles; all tiles of at least one color would also form a rectangle. Here are some examples of correct colorings: [Image] Here are some examples of incorrect colorings: [Image] Among all correct colorings Vova wants to choose the one with the minimal perimeter. What is the minimal perimeter Vova can obtain? It is guaranteed that there exists at least one correct coloring. -----Input----- A single line contains two integers $a$ and $b$ ($1 \le a, b \le 10^{14}$) β€” the number of tiles red marker should color and the number of tiles blue marker should color, respectively. -----Output----- Print a single integer β€” the minimal perimeter of a colored rectangle Vova can obtain by coloring exactly $a$ tiles red and exactly $b$ tiles blue. It is guaranteed that there exists at least one correct coloring. -----Examples----- Input 4 4 Output 12 Input 3 9 Output 14 Input 9 3 Output 14 Input 3 6 Output 12 Input 506 2708 Output 3218 -----Note----- The first four examples correspond to the first picture of the statement. Note that for there exist multiple correct colorings for all of the examples. In the first example you can also make a rectangle with sides $1$ and $8$, though its perimeter will be $18$ which is greater than $8$. In the second example you can make the same resulting rectangle with sides $3$ and $4$, but red tiles will form the rectangle with sides $1$ and $3$ and blue tiles will form the rectangle with sides $3$ and $3$.
a, b = map(int, input().split()) ad = [] for i in range(1, a + 1): if i * i > a: break if a % i == 0: ad.append(i) if i * i != a: ad.append(a // i) bd = [] for i in range(1, b + 1): if i * i > b: break if b % i == 0: bd.append(i) if i * i != b: bd.append(b // i) ab = a + b abd = [] for i in range(1, ab + 1): if i * i > ab: break if ab % i == 0: abd.append(i) if i * i != ab: abd.append(ab // i) ad.sort() bd.sort() abd.sort() ai = -1 bi = -1 r = int(400000000000000.0) for d in abd: while ai + 1 < len(ad) and ad[ai + 1] <= d: ai += 1 while bi + 1 < len(bd) and bd[bi + 1] <= d: bi += 1 h = ab // d if ai >= 0 and a // ad[ai] <= h or bi >= 0 and b // bd[bi] <= h: r = min(r, h + d) print(r * 2)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
def solve(n, m): va = 0 arr = [(1) for i in range(n + 1)] for i in range(2, n + 1): a = m % i va += arr[a] for j in range(a, n + 1, i): arr[j] += 1 return va for i in range(int(input())): n, m = map(int, input().split()) print(solve(n, m))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
import sys input = sys.stdin.readline T = int(input()) for j in range(T): N, M = map(int, input().split()) result = 0 arr = [1] * (N + 1) for i in range(2, N + 1): x = M % i result += arr[x] for j in range(x, N + 1, i): arr[j] += 1 print(result)
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 NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
M = 500000.0 + 5 factor = {} for i in range(1, int(M)): for j in range(i, int(M), i): factor.setdefault(j, []).append(i) t = int(input()) for _ in range(t): N, M = map(int, input().split()) ans = 0 for i in range(2, N + 1): X = M - M % i if X != 0: ans += factor[X].index(i) else: ans += i - 1 print(ans)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR LIST 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
M = int(500000.0 + 5) divisors = [[] for i in range(0, M + 1)] def findDivisiors(): for i in range(1, M + 1): for j in range(i, M + 1, i): divisors[j].append(i) def binarySearch(numbers, start, end, num): if start <= end: mid = (start + end) // 2 if numbers[mid] == num: return mid if num < numbers[mid]: return binarySearch(numbers, start, mid - 1, num) else: return binarySearch(numbers, mid + 1, end, num) return -1 t = int(input()) findDivisiors() for i in range(t): n, m = map(int, input().split()) count = 0 for b in range(2, n + 1): x = m - m % b if x > 0: count += binarySearch(divisors[x], 0, len(divisors[x]) - 1, b) else: count += b - 1 print(count)
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
t = int(input()) for test in range(t): n, m = map(int, input().split()) c = 0 f = 0 if n > m: x = n - m c += x * m c += x * (x - 1) // 2 n = m c += n - 1 d = {} for i in range(n, 1, -1): x = m % i f = max(f, x) try: d[x] += 1 c += d[x] - 1 p = 1 q = 0 z = 0 while z < f: z = x + p * i try: q += d[z] p += 1 except: p += 1 c += q except: d[x] = 1 p = 1 q = 0 z = 0 while z < f: z = x + p * i try: q += d[z] p += 1 except: p += 1 c += q print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
t = int(input()) while t > 0: nm = list(map(int, input().strip().split())) n, m = nm[0], nm[1] ans = 0 li = [1] * (n + 1) for i in range(2, n + 1): a = m % i ans += li[a] for j in range(a, n + 1, i): li[j] += 1 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
for _ in range(int(input())): n, m = map(int, input().split()) arr = [1] * (n + 1) k = 0 for i in range(2, n + 1): md = m % i k += arr[md] p = md while p <= n: arr[p] += 1 p += i print(k)
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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
t = int(input()) for iii in range(t): n, m = map(int, input().split()) l = [] for i in range(n + 1): l.append(int(1)) ans = 0 for a in range(2, n + 1): x = m % a ans += l[x] for b in range(x, n + 1, a): l[b] += 1 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 LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
def preCalcMod(): factors = [] M = 500000 factors.insert(0, []) for i in range(1, M + 1): j = i while j <= M: if i == 1: factors.insert(j, []) factors[j].append(i) j += i return factors def modularEquation(): factors = preCalcMod() T = int(input()) for i in range(0, T): N, M = map(int, input().split()) count = 0 for b in range(2, min(N, M) + 1): T = M - M % b if T == 0: break for key in factors[T]: if key < b: count += 1 else: break if N > M: a = M lastTerm = N - 1 nBy2 = (N - M) / 2 sumIs = nBy2 * (a + lastTerm) count += int(sumIs) print(count) modularEquation()
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
mx = 500015 a = [list() for x in range(mx)] def PreCompute(): for i in range(1, mx): for j in range(i, mx, i): a[j].append(i) def Lower_bound(fr, value): l, r = [0, len(a[fr]) - 1] ans = 0 while l <= r: mid = l + r >> 1 if a[fr][mid] < value: ans = mid l = mid + 1 else: r = mid - 1 return ans + 1 PreCompute() t = int(input()) while t: t -= 1 cnt = 0 n, m = [int(x) for x in input().split()] for b in range(2, n + 1): x = m - m % b if x > 0: cnt += Lower_bound(x, b) else: cnt += b - 1 print(cnt)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR LIST NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
test = int(input()) mex = 500001 div = [[] for _ in range(mex)] for i in range(1, mex): for j in range(i, mex, i): div[j].append(i) for i in range(test): fans = 0 n, m = map(int, input().split()) mx = min(n, m) for b in range(2, mx + 1): amul = m - m % b s, e = 0, len(div[amul]) - 1 ans = -1 while s <= e: md = (s + e) // 2 if div[amul][md] < b: ans = md s = md + 1 else: e = md - 1 fans += ans + 1 for b in range(mx + 1, n + 1): fans += b - 1 print(fans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
from sys import stdin def fact(z, b): c = 0 l = [] for i in range(1, int(z**0.5) + 1): if z % i == 0: if i != z // i: l.append(i) l.append(z // i) else: l.append(i) for i in l: if i < b: c += 1 return c t = int(stdin.readline()) for e in range(t): n, m = stdin.readline().split() n = int(n) m = int(m) co = 0 l = [1] * (n + 1) for i in range(2, n + 1): a = m % i co += l[a] for j in range(a, n + 1, i): l[j] += 1 print(co)
FUNC_DEF ASSIGN VAR NUMBER 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 BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
MAX_M = 5 * 10**5 factors = [[] for i in range(MAX_M + 1)] for i in range(1, MAX_M + 1): for j in range(i, MAX_M + 1, i): factors[j].append(i) def lowerBound(b, T): left, right = 0, len(factors[T]) - 1 while left < right: mid = (left + right) // 2 if factors[T][mid] >= b: right = mid else: left = mid + 1 return left T = int(input()) for _ in range(T): N, M = map(int, input().split()) ans = 0 for b in range(1, N + 1): x = M - M % b k = lowerBound(b, x) if b <= x else b - 1 ans += k print(ans)
ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
for i in range(int(input())): x = [int(y) for y in input().split()] n, m = x[0], x[1] l = [1] * (n + 1) ans = 0 for i in range(2, n + 1): a = m % i ans += l[a] for j in range(a, n + 1, i): l[j] += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
def modEquation(n, m, fact): ans = 0 for b in range(2, n + 1): t = m - m % b if b > m: ans += b - 1 else: f = fact[t] st = 0 en = len(f) - 1 while st <= en: mid = (st + en) // 2 if f[mid] < b: st = mid + 1 else: en = mid - 1 ans += st return ans def main(): fact_s = int(500000.0) + 1 fact = [([] * fact_s) for i in range(fact_s)] for i in range(1, fact_s): for j in range(i, fact_s, i): fact[j].append(i) for i in range(int(input().strip())): n, m = map(int, input().strip().split(" ")) print("{0}".format(modEquation(n, m, fact))) main()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
def solve(): t = int(input()) for i in range(t): N, M = map(int, input().split()) a = [1] * (N + 1) cnt = 0 for j in range(2, N + 1): c = M % j cnt += a[c] for k in range(c, N + 1, j): a[k] = a[k] + 1 print(cnt) def main(): solve() 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
def lower_bound(arr, val): n = len(arr) low = 0 high = n while low < high: mid = (high + low) // 2 if arr[mid] >= val: high = mid else: low = mid + 1 if arr[low] < val: low = low + 1 return low maxm = int(500000.0 + 1) div = [[] for i in range(maxm)] for i in range(1, maxm): for j in range(i, maxm, i): div[j].append(i) for _ in range(int(input())): n, m = map(int, input().split()) ans = 0 for b in range(2, n + 1): x = m - m % b if x > 0: ans += lower_bound(div[x], b) else: ans += b - 1 print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
for i in range(int(input())): N, M = map(int, input().split()) count = 0 arr = [(1) for i in range(N + 1)] for a in range(2, N + 1): count += arr[M % a] for b in range(M % a, N + 1, a): arr[b] += 1 print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Given integers $N$ and $M$, find the number of ordered pairs $(a, b)$ such that $1 ≀ a < b ≀ N$ and $((M\ \mathrm{mod}\ a)\ \mathrm{mod}\ b) = ((M\ \mathrm{mod}\ b)\ \mathrm{mod}\ a)$. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. The only line of each test case contains two integers $N$, $M$. ------ Output ------ For each testcase, output in a single line the answer to the problem. ------ Constraints ------ $1 ≀ T ≀ 1000$ $2 ≀ N ≀ 10^{6}$ $1 ≀ M ≀ 5\cdot 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Note: Multiplier for JAVA for this problem is reduced to $1.25$ instead of usual $2$. ------ Subtasks ------ Subtask #1 (10 points): $1 ≀ T ≀ 10$ $2 ≀ N ≀ 10^{3}$ $1 ≀ M ≀ 10^{5}$ Subtask #2 (40 points): $1 ≀ T ≀ 100$ $2 ≀ N ≀ 10^{5}$ $1 ≀ M ≀ 10^{5}$ The sum of $N$ over all test cases does not exceed $10^{6}$. Subtask #3 (50 points): Original Constraints ----- Sample Input 1 ------ 3 3 5 3 6 3 10 ----- Sample Output 1 ------ 2 3 2 ----- explanation 1 ------ Test Case $1$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 5$, $((5 \%1) \%2) = (0\%2) = 0$. Also, $((5 \%2) \%1) = (1\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 5$, $((5 \%1) \%3) = (0\%3) = 0$. Also, $((5 \%3) \%1) = (2\%1) = 0$. Test Case $2$: There are $3$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 6$, $((6 \%1) \%2) = (0\%2) = 0$. Also, $((6 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 6$, $((6 \%1) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%1) = (0\%1) = 0$. - $(2, 3)$: $(1 ≀ 2 < 3 ≀ N)$. Given $M = 6$, $((6 \%2) \%3) = (0\%3) = 0$. Also, $((6 \%3) \%2) = (0\%2) = 0$. Test Case $3$: There are $2$ valid pairs satisfying the conditions. These are: - $(1, 2)$: $(1 ≀ 1 < 2 ≀ N)$. Given $M = 10$, $((10 \%1) \%2) = (0\%2) = 0$. Also, $((10 \%2) \%1) = (0\%1) = 0$. - $(1, 3)$: $(1 ≀ 1 < 3 ≀ N)$. Given $M = 10$, $((10 \%1) \%3) = (0\%3) = 0$. Also, $((10 \%3) \%1) = (1\%1) = 0$.
def divisor(n): ans = [[1] for i in range(n + 1)] ans[0].pop() for i in range(2, n + 1): for j in range(i, n + 1, i): ans[j].append(i) return ans def count(n, p, div): if n >= len(div): return 0 size = len(div[n]) st, en = 0, size - 1 check = 0 while st <= en: mid = (st + en) // 2 temp = div[n][mid] if temp == p: check = mid - 1 break elif temp > p: en = mid - 1 elif temp < p: check = mid st = mid + 1 return check t = int(input()) div = divisor(6 * 10**5) for _ in range(t): n, m = map(int, input().split()) ans = 0 for i in range(2, n + 1): temp = m // i * i if temp == 0: ans += i - 1 continue c = count(temp, i, div) + 1 ans += c print(ans)
FUNC_DEF ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def cached(func): _cache = {} def wrapped(*args): nonlocal _cache if args not in _cache: _cache[args] = func(*args) return _cache[args] return wrapped def len_num(l): return 10**l - 10 ** (l - 1) if l > 0 else 0 @cached def len_sum(l): return len_sum(l - 1) + len_num(l - 1) * (l - 1) if l > 1 else 0 def block_len(block_num): l = len(str(block_num)) return len_sum(l) + (block_num - 10 ** (l - 1) + 1) * l def arith_sum(n): return n * (n + 1) // 2 def block_len_sum(block_num): l = len(str(block_num)) result = 0 for i in range(1, l + 1): ls = len_sum(i) if i < l: ln = len_num(i) else: ln = block_num - 10 ** (l - 1) + 1 result += ls * ln + i * arith_sum(ln) return result def block(n): return "".join(str(i) for i in range(1, n + 1)) def blocks(n): return "".join(block(i) for i in range(1, n + 1)) def binary_search(call, val): start = 1 end = 1 while call(end) <= val: end *= 2 result = start while start <= end: mid = (start + end) // 2 if call(mid) <= val: start = mid + 1 result = start else: end = mid - 1 return result cases = int(input()) for _ in range(cases): index = int(input()) - 1 block_num = binary_search(block_len_sum, index) rel_index = index - block_len_sum(block_num - 1) number = binary_search(block_len, rel_index) digit = rel_index - block_len(number - 1) print(str(number)[digit])
FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN VAR FUNC_DEF RETURN VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
for _ in range(int(input())): n = int(input()) u = 0 prev = 0 for i in range(1, 50005): if u + prev + len(str(i)) >= n: for k in range(1, i + 1): if u + len(str(k)) >= n: for j in str(k): if u + 1 == n: print(j) break u += 1 break else: u += len(str(k)) break u = u + prev + len(str(i)) prev += len(str(i))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
import sys input = sys.stdin.readline bsc = {} def digs(x): s = 0 while x > 0: s += 1 x //= 10 return s def tenp(x): t = 1 for i in range(x): t *= 10 return t def blocksize(b): if b in bsc: return bsc[b] d = digs(b) tp = tenp(d - 1) sz = d * (b - tp + 1) while d > 1: d -= 1 tp //= 10 sz += d * tp * 9 bsc[b] = sz return sz def dec(x): l = [] while x > 0: l.append(x % 10) x //= 10 return l[::-1] def dig_in_block(mx, k): i, d, nd = 1, 1, 10 while True: if d >= k: return dec(i)[k - 1] k -= d i += 1 if i == nd: d += 1 nd *= 10 def dig(k): b = 0 while True: b += 1 nsz = blocksize(b) if nsz >= k: break k -= nsz return dig_in_block(b, k) q = int(input()) for _ in range(q): k = int(input()) print(dig(k))
IMPORT ASSIGN VAR VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
import sys input = sys.stdin.readline def preprocess(): num_len = [0] s_num_len = [0] cur_len = 1 next_num = 10 for i in range(1, 50000): if i == next_num: cur_len += 1 next_num *= 10 num_len.append(num_len[-1] + cur_len) s_num_len.append(cur_len) return num_len, s_num_len def query(num, num_len, s_num_len): i = 1 while num_len[i] <= num: num -= num_len[i] i += 1 if num == 0: return str(i - 1)[-1] i = 1 while s_num_len[i] <= num: num -= s_num_len[i] i += 1 if num == 0: return str(i - 1)[-1] return str(i)[num - 1] num_len, s_num_len = preprocess() n = int(input()) ans = [] for i in range(n): num = int(input()) ans.append(query(num, num_len, s_num_len)) print("\n".join(ans))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
word = "" arr = [0] for i in range(1, 22000): word = word + str(i) arr.append(arr[-1] + len(word)) def sol(k): d = 0 for i in range(1, 22000): if arr[i] > k: d = i - 1 break k = k - arr[d] if k == 0: return str(d)[-1] else: return word[k - 1] for i in range(int(input())): print(sol(int(input())))
ASSIGN VAR STRING ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def solve(k): s = 0 cur = 0 j = -1 for i in range(1, 19): p = 9 * 10 ** (i - 1) q = cur * p + i * p * (p + 1) // 2 if k < s + q: j = i break s += q cur += i * p if s > 10**18: break k -= s left = 0 right = 9 * 10 ** (j - 1) + 1 while left + 1 < right: mid = (left + right) // 2 if cur * mid + mid * (mid + 1) // 2 * j <= k: left = mid else: right = mid k -= cur * left + left * (left + 1) // 2 * j i = 1 while 1: p = 9 * 10 ** (i - 1) if k <= i * p: l = k // i v = 10 ** (i - 1) + l return str(v)[k % i] k -= i * p i += 1 q = int(input()) for i in range(q): print(solve(int(input()) - 1))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
q = int(input()) def f(n): take = 0 ret = 0 while True: if n - take <= 0: break ret += (n - take) * (n - take + 1) // 2 take = take * 10 + 9 return ret def g(n): take = 0 ret = 0 while True: if n - take <= 0: break ret += n - take take = take * 10 + 9 return ret for _ in range(q): k = int(input()) low, high = 1, k ans = -1 while low < high: mid = low + high + 1 >> 1 if f(mid) == k: ans = mid % 10 break if f(mid) < k: low = mid else: high = mid - 1 if f(low) == k: ans = low % 10 if ans != -1: print(ans) continue k -= f(low) next = low + 1 low, high = 1, next while low < high: mid = (low + high + 1) // 2 if g(mid) == k: ans = mid % 10 break if g(mid) < k: low = mid else: high = mid - 1 if g(low) == k: ans = low % 10 if ans != -1: print(ans) continue h = g(low) assert h < k m = str(low + 1) print(m[k - h - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def num_of_digits(number): if number <= 0: return 0 soma = 0 start = 0 l = 1 r = 9 digit = 1 past_value = 0 while True: if r > number: r = number times = r - l + 1 left_mapped = past_value + digit right_mapped = past_value + times * digit soma = soma + (left_mapped + right_mapped) * times // 2 if r == number: break l *= 10 r = r * 10 + 9 digit += 1 past_value = right_mapped return soma q = int(input("")) while q: q -= 1 digit = int(input("")) l = 0 r = int(10000000000.0) while r - l > 1: m = (r + l) // 2 if num_of_digits(m) < digit: l = m else: r = m left_soma = digit - num_of_digits(r - 1) l1 = 1 r1 = 9 prev_soma = 0 soma = r1 - l1 + 1 n_digit = 1 while left_soma > soma: l1 *= 10 r1 = r1 * 10 + 9 n_digit += 1 prev_soma = soma soma = soma + (r1 - l1 + 1) * n_digit left_soma -= prev_soma k = left_soma // n_digit left_digit = l1 - 1 + k left_digits = left_soma % n_digit if left_digits == 0: print(str(left_digit)[-1]) else: left_digit += 1 left_digits -= 1 left_digit = str(left_digit)[left_digits] print(left_digit)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE 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 BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def calc(k): num, ret, pre = len(str(k)), 0, 0 for i in range(1, num): cnt = pow(10, i - 1) * 9 ret += pre * cnt + i * (1 + cnt) * cnt // 2 pre += i * cnt extra = k - pow(10, num - 1) + 1 ret += pre * extra ret += num * (1 + extra) * extra // 2 return int(ret) def ds(k): num, _sum = 1, 0 while _sum + num * pow(10, num - 1) * 9 < k: _sum += num * pow(10, num - 1) * 9 num += 1 k -= _sum val = pow(10, num - 1) + k // num if k % num is 0: return (val - 1) % 10 k = num - k % num for i in range(k): val = val // 10 return val % 10 def solve(k): l, r, ans = 0, 1000000000.0 + 7, 0 while l <= r: mid = int((l + r) // 2) if calc(mid) >= k: ans, r = mid, mid - 1 else: l = mid + 1 k = k - calc(ans - 1) return ds(k) q = int(input()) for i in range(q): k = int(input()) print(solve(k))
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def s(i): n = 1 result = i while i >= n * 9: i -= 9 * n result += i n *= 10 return result def n(i): result = 0 while True: i //= 10 result += 1 if i == 0: break return result def get_char_from_block(i, k): k = k if k != 0 else s(i) c = 1 m = 1 if k < 10: return k while k > 9 * m * c: k -= 9 * m * c m *= 10 c += 1 ost = (k + c - 1) % c num = (k + c - 1) // c + m - 1 while ost < c - 1: num //= 10 ost += 1 return num % 10 def main(): q = int(input()) for i in range(q): k = int(input()) l, r = int(1), int(10**9) res = 0 h = 1 while l + 1 < r: m = (l + r) // 2 i = m c = 10 ** (n(i) - 1) p = 0 while c != 0: p += (s(c) + s(i)) * (i - c + 1) // 2 i = c - 1 c //= 10 if p - s(m) >= k: r = m else: h = p l = m res = get_char_from_block(l, k - (h - s(l))) print(res) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR WHILE VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
t = int(input()) def getDigits(quadrant): return len(str(quadrant)) for i in range(t): quadrant = 1 digits = 1 remainingDigits = int(input()) while remainingDigits > digits: remainingDigits -= digits quadrant += 1 digits += getDigits(quadrant) i = 1 digit = 0 while remainingDigits > 0: if remainingDigits <= getDigits(i): digit = int(str(i)[remainingDigits - 1]) break else: remainingDigits -= getDigits(i) i += 1 print(digit)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
q = int(input()) s = "" l = [0] * 30001 for i in range(1, 30000): s += str(i) l[i] = len(s) for i in range(q): k = int(input()) n = 1 t = 2 * k while n * (n + 1) < t: n += 1 k -= 1 for i in range(1, n + 1): if k >= l[i]: k -= l[i] else: print(s[k]) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
q = int(input()) def sumto(r, need): pw = 1 sum = 0 add = 0 len = 1 while 1: if pw * 10 - 1 < r: cnt = pw * 10 - pw if need: sum += add * cnt + (cnt + 1) * cnt // 2 * len add += cnt * len else: sum += cnt * len else: cnt = r - pw + 1 if need: sum += cnt * add + (cnt + 1) * cnt // 2 * len else: sum += cnt * len break pw *= 10 len += 1 return sum for qi in range(q): k = int(input()) k -= 1 l = 1 r = 1000000000 res = -1 while r - l >= 0: mid = l + r >> 1 if sumto(mid, 1) > k: res = mid r = mid - 1 else: l = mid + 1 k -= sumto(res - 1, 1) l = 1 r = res num = -1 while r - l >= 0: mid = l + r >> 1 if sumto(mid, 0) > k: num = mid r = mid - 1 else: l = mid + 1 print(str(num)[k - sumto(num - 1, 0)])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER
The only difference between the easy and the hard versions is the maximum value of $k$. You are given an infinite sequence of form "112123123412345$\dots$" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from $1$ to $1$, the second one β€” from $1$ to $2$, the third one β€” from $1$ to $3$, $\dots$, the $i$-th block consists of all numbers from $1$ to $i$. So the first $56$ elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the $1$-st element of the sequence is $1$, the $3$-rd element of the sequence is $2$, the $20$-th element of the sequence is $5$, the $38$-th element is $2$, the $56$-th element of the sequence is $0$. Your task is to answer $q$ independent queries. In the $i$-th query you are given one integer $k_i$. Calculate the digit at the position $k_i$ of the sequence. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 500$) β€” the number of queries. The $i$-th of the following $q$ lines contains one integer $k_i$ $(1 \le k_i \le 10^9)$ β€” the description of the corresponding query. -----Output----- Print $q$ lines. In the $i$-th line print one digit $x_i$ $(0 \le x_i \le 9)$ β€” the answer to the query $i$, i.e. $x_i$ should be equal to the element at the position $k_i$ of the sequence. -----Examples----- Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999 1000000000 Output 8 2 9 8 -----Note----- Answers on queries from the first example are described in the problem statement.
def foo(x: int): if x <= 0: return 0 a = len(str(x)) ret = a * (x * x + x) // 2 t = 1 cur, prv = 10, 1 while t < a: l = a - t freq = cur - prv ret -= l * (freq * (x - prv + 1 + (x - (cur - 1) + 1)) // 2) cur, prv = 10 * cur, 10 * prv t += 1 return ret def bar(x: int): if x <= 0: return 0 a = len(str(x)) ret = a * x t = 1 cur, prv = 10, 1 while t < a: l = a - t freq = cur - prv ret -= l * freq cur, prv = 10 * cur, 10 * prv t += 1 return ret def solve(k): lo, hi = 0, k while lo < hi: mid = lo + 1 + hi >> 1 L = foo(mid) lo, hi = (mid, hi) if L < k else (lo, mid - 1) L = foo(lo) x, y = 0, lo + 1 while x < y: mid = x + 1 + y >> 1 T = bar(mid) x, y = (mid, y) if L + T < k else (x, mid - 1) T = bar(x) return str(x + 1)[k - L - T - 1] q = int(input()) for test_case in range(q): k = int(input()) print(solve(k))
FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR