description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given 2n balls of k distinct colors. You will be given an integer array balls of size k where balls[i] is the number of balls of color i.  All the balls will be shuffled uniformly at random, then we will distribute the first n balls to the first box and the remaining n balls to the other box (Please read the explanation of the second example carefully). Please note that the two boxes are considered different. For example, if we have two balls of colors a and b, and two boxes [] and (), then the distribution [a] (b) is considered different than the distribution [b] (a) (Please read the explanation of the first example carefully). We want to calculate the probability that the two boxes have the same number of distinct balls.   Example 1: Input: balls = [1,1] Output: 1.00000 Explanation: Only 2 ways to divide the balls equally: - A ball of color 1 to box 1 and a ball of color 2 to box 2 - A ball of color 2 to box 1 and a ball of color 1 to box 2 In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1 Example 2: Input: balls = [2,1,1] Output: 0.66667 Explanation: We have the set of balls [1, 1, 2, 3] This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equale probability (i.e. 1/12): [1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1] After that we add the first two balls to the first box and the second two balls to the second box. We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box. Probability is 8/12 = 0.66667 Example 3: Input: balls = [1,2,1,2] Output: 0.60000 Explanation: The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box. Probability = 108 / 180 = 0.6 Example 4: Input: balls = [3,2,1] Output: 0.30000 Explanation: The set of balls is [1, 1, 1, 2, 2, 3]. It is hard to display all the 60 possible random shuffles of this set but it is easy to check that 18 of them will have the same number of distinct colors in each box. Probability = 18 / 60 = 0.3 Example 5: Input: balls = [6,6,6,6,6,6] Output: 0.90327   Constraints: 1 <= balls.length <= 8 1 <= balls[i] <= 6 sum(balls) is even. Answers within 10^-5 of the actual value will be accepted as correct.
class Solution: def getProbability(self, balls: List[int]) -> float: M = len(balls) N = sum(balls) F = [math.factorial(n) for n in range(N // 2 + 1)] s1 = [0] * M s2 = [0] * M def find(i): if i == M: if sum(s1) == sum(s2) and len([n for n in s1 if n]) == len( [n for n in s2 if n] ): base1 = F[N // 2] for n in s1: base1 //= F[n] base2 = F[N // 2] for n in s2: base2 //= F[n] self.ans += base1 * base2 return for n in range(balls[i] + 1): s1[i] = n s2[i] = balls[i] - n find(i + 1) self.ans = 0 find(0) base = math.factorial(N) for n in balls: base //= math.factorial(n) return self.ans / base
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers equal to target S. Example 1: Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5 Explanation: -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3 There are 5 ways to assign symbols to make the sum of nums be target 3. Note: The length of the given array is positive and will not exceed 20. The sum of elements in the given array will not exceed 1000. Your output answer is guaranteed to be fitted in a 32-bit integer.
class Solution: def findTargetSumWays(self, nums, S): total = sum(nums) if total < S or total + S & 1: return 0 target = (S + total) // 2 dp = [0] * (target + 1) dp[0] = 1 for n in nums: for i in range(target, n - 1, -1): dp[i] += dp[i - n] return dp[target]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers equal to target S. Example 1: Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5 Explanation: -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3 There are 5 ways to assign symbols to make the sum of nums be target 3. Note: The length of the given array is positive and will not exceed 20. The sum of elements in the given array will not exceed 1000. Your output answer is guaranteed to be fitted in a 32-bit integer.
class Solution: def findTargetSumWays(self, nums, S): def findTargetNumber(nums, target): print(nums, target) dp = [(0) for _ in range(target + 1)] dp[0] = 1 for n in nums: for i in range(target, n - 1, -1): dp[i] += dp[i - n] return dp[-1] sumResult = sum(nums) if (S + sumResult) % 2 != 0 or sumResult < S: return 0 else: return findTargetNumber(nums, int((sumResult + S) / 2))
CLASS_DEF FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers equal to target S. Example 1: Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5 Explanation: -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3 There are 5 ways to assign symbols to make the sum of nums be target 3. Note: The length of the given array is positive and will not exceed 20. The sum of elements in the given array will not exceed 1000. Your output answer is guaranteed to be fitted in a 32-bit integer.
class Solution: def findTargetSumWays(self, nums, S): target = S + sum(nums) if target % 2 == 1 or sum(nums) < S: return 0 target //= 2 dp = [(0) for i in range(target + 1)] dp[0] = 1 for curNum in nums: for cap in range(len(dp) - 1, -1, -1): if cap >= curNum: dp[cap] += dp[cap - curNum] return dp[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers equal to target S. Example 1: Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5 Explanation: -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3 There are 5 ways to assign symbols to make the sum of nums be target 3. Note: The length of the given array is positive and will not exceed 20. The sum of elements in the given array will not exceed 1000. Your output answer is guaranteed to be fitted in a 32-bit integer.
class Solution: def findTargetSumWays(self, nums, S): sum_ = sum(nums) if S > sum_: return 0 target = sum_ - S if target % 2: return 0 target = target // 2 dp = [1] + [0] * target for n in nums: i = target while i >= n: dp[i] += dp[i - n] i -= 1 return dp[target]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers equal to target S. Example 1: Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5 Explanation: -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3 There are 5 ways to assign symbols to make the sum of nums be target 3. Note: The length of the given array is positive and will not exceed 20. The sum of elements in the given array will not exceed 1000. Your output answer is guaranteed to be fitted in a 32-bit integer.
class Solution: def findTargetSumWays(self, nums, S): c = [0] * 1001 c[0] = 1 T = sum(nums) A = T + S if T < S or A & 1: return 0 A >>= 1 nums = sorted(nums) temp = 0 for ind, v in enumerate(nums): temp += v for i in range(min(temp, A), v - 1, -1): c[i] += c[i - v] return c[A]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, s): res = [] part = [] def ispalin(i, j): while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True def dfs(i): if i == len(s): res.append(part.copy()) for j in range(i, len(s)): if ispalin(i, j): part.append(s[i : j + 1]) dfs(j + 1) part.pop() dfs(0) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): def helper(s, v): if s == n: ans.append(v) return for i in range(s, n): if S[s : i + 1] == S[s : i + 1][::-1]: helper(i + 1, v + [S[s : i + 1]]) ans = [] n = len(S) helper(0, []) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): ans = [] a = [] n = len(S) def check(s): return s == s[::-1] def sol(i, n): if i == n: ans.append(a.copy()) return for j in range(n - i): if check(S[i : i + j + 1]): a.append(S[i : i + j + 1]) sol(i + j + 1, n) a.pop() sol(0, n) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): def helper(index, s, temp, ans): if index == len(s): ans.append([ele for ele in temp]) return for i in range(index, len(s)): if isPalindrome(s, index, i): temp.append(s[index : i + 1]) helper(i + 1, s, temp, ans) temp.pop() def isPalindrome(s, start, end): while start <= end: if s[start] != s[end]: return False start += 1 end -= 1 return True ans = [] temp = [] helper(0, S, temp, ans) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): ans = [] part = [] def dfs(i=0): if i == len(S): ans.append(part[:]) return for j in range(i, len(S)): if self.checkPal(S[i : j + 1]): part.append(S[i : j + 1]) dfs(j + 1) part.pop() dfs() return ans def checkPal(self, str): return str == str[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF RETURN VAR VAR NUMBER
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): n = len(S) ans = [] res = [] def isPali(s): n = len(s) for i in range(n // 2): if s[i] != s[n - i - 1]: return False return True def bts(i): if i == n: res.append(ans[:]) return for index in range(i, n): if isPali(S[i : index + 1]): ans.append(S[i : index + 1]) bts(index + 1) ans.pop() bts(0) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): n = len(S) res = [] def subs(start, out): if start == n: res.append(out) return for i in range(start, n): if S[start : i + 1] == S[start : i + 1][::-1]: subs(i + 1, out + [S[start : i + 1]]) subs(0, []) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, s): l = [] def backtrack(a, s): if s is "": l.append(a[:]) return n = len(s) x = "" for i in range(n): x += s[i] if x == x[::-1]: a.append(x) backtrack(a, s[i + 1 :]) a.pop(-1) backtrack([], s) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR STRING EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
def palindromic_partition_helper(input_string: str, output_string: str, result_arr: []): if len(input_string) == 0: result_arr.append(output_string) return n = len(input_string) for i in range(0, n): word = input_string[0 : i + 1] ros = input_string[i + 1 :] if is_palindrome(word): output_string = output_string + word + "-" palindromic_partition_helper(ros, output_string, result_arr) output_string = output_string[: -(len(word) + 1)] def is_palindrome(word): start = 0 end = len(word) - 1 while start < end: if word[start] != word[end]: return False start = start + 1 end = end - 1 return True def print_partition(input_word): result_arr = [] input_dict = {} palindromic_partition_helper(input_word, "", result_arr) final_arr = [] for data in result_arr: sub_string = data.split("-")[:-1] final_arr.append(sub_string) return final_arr class Solution: def allPalindromicPerms(self, S): return print_partition(S)
FUNC_DEF VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): result = [] temp = [] index = 0 self.solve(index, S, temp, result) return result def solve(self, index, s, temp, result): if index == len(s): result.append(temp[:]) return for i in range(index, len(s)): if self.checkPal(index, i, s): temp.append(s[index : i + 1]) self.solve(i + 1, s, temp, result) temp.pop() def checkPal(self, s, e, S): while s <= e: if S[s] != S[e]: return False s += 1 e -= 1 return True
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): def isPalindrom(si, ei): nonlocal S for i in range((ei - si + 1) // 2): if S[si + i] != S[ei - i]: return False return True ans = [] n = len(S) def backtrack(starti, arr): nonlocal S, ans, n if starti == n: ans.append(arr.copy()) else: for endi in range(starti, n): if isPalindrom(starti, endi): arr.append(S[starti : endi + 1]) backtrack(endi + 1, arr) arr.pop() backtrack(0, []) return ans
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def IsPalindrome(self, start, end, s): while start <= end: if s[start] != s[end]: return False start += 1 end -= 1 return True def helper(self, ind, n, s, ans, temp): if ind == n: ans.append(temp[:]) return for i in range(ind, n): if self.IsPalindrome(ind, i, s): temp.append(s[ind : i + 1]) self.helper(i + 1, n, s, ans, temp) temp.pop() def allPalindromicPerms(self, S): ans = [] temp = [] self.helper(0, len(S), S, ans, temp) return ans
CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, s): m = len(s) r = [] a = [] def check(string, i, j): x = j for y in range(i, x): if string[y] != string[x]: return False x -= 1 return True def allPalPartUtil( allPart: list, currPart: list, start: int, n: int, string: str ): if start >= n: x = currPart.copy() allPart.append(x) return for i in range(start, n): if check(string, start, i): currPart.append(string[start : i + 1]) allPalPartUtil(allPart, currPart, i + 1, n, string) currPart.pop() allPalPartUtil(r, a, 0, m, s) return r
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, s): l = [] def dfs(x, n, a): if x is "": l.append(a[:]) return y = "" for i in range(n): y += x[i] if y != y[::-1]: continue a.append(y) dfs(x[i + 1 :], n - i - 1, a) a.pop(-1) dfs(s, len(s), []) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR STRING EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def isPalindrome(self, s): n = len(s) for i in range((n + 1) // 2): if s[i] != s[n - 1 - i]: return 0 return 1 def util(self, s, ans): global a if len(s) == 0: a[ans] = 1 print(ans) return for k in range(1, len(s) + 1): if self.isPalindrome(s[:k]) == 1: if len(ans) == 0: self.util(s[k:], ans + s[:k]) else: self.util(s[k:], ans + " " + s[:k]) def allPalindromicPerms(self, S): global a a = {} self.util(S, "") return []
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR STRING VAR VAR FUNC_DEF ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR STRING RETURN LIST
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, s): def func(ind, n, res, ans, s): if ind == n: res.append(ans.copy()) return for i in range(ind, n): kk = s[ind : i + 1] if kk == kk[::-1]: ans.append(kk) func(i + 1, n, res, ans, s) ans.pop() ans = [] res = [] n = len(s) func(0, n, res, ans, s) return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def is_palindrome(self, string): j = len(string) - 1 i = 0 while i < j: if string[i] != string[j]: return False i += 1 j -= 1 return True def partition(self, string, ans, res): if len(string) == 0: res.add(" ".join(ans)) return for i in range(len(string)): if self.is_palindrome(string[: i + 1]): self.partition(string[i + 1 :], ans + [string[: i + 1]], res) def allPalindromicPerms(self, S): res = set() self.partition(S, [], res) res = [[x] for x in res] res = sorted(res) return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def __init__(self): self.dp = dict() def isPalin(self, s): return s == s[::-1] def allPalindromicPerms(self, s, i=0): if len(s) == 1: return [s] if self.dp.get(s) != None: return self.dp[s] till = "" way = [] if self.isPalin(s): way.append([s]) for x in range(1, len(s)): rem = s[:x] if not self.isPalin(rem): continue for _ in self.allPalindromicPerms(s[x:], i + 1): way.append([rem, *_]) self.dp[s] = way if i == 0: return sorted(way) return way
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR NUMBER FUNC_DEF NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN LIST VAR IF FUNC_CALL VAR VAR NONE RETURN VAR VAR ASSIGN VAR STRING ASSIGN VAR LIST IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def is_palindrome(self, S): low = 0 high = len(S) - 1 while low < high: if S[low] != S[high]: return False low += 1 high -= 1 return True def solve(self, S, part, res): for i in range(len(S)): prefix = S[: i + 1] if self.is_palindrome(prefix): rest = S[i + 1 :] if len(rest) == 0: part.append(prefix) res.append(part[:]) part.pop() return part.append(prefix) self.solve(rest, part, res) part.pop() def allPalindromicPerms(self, S): res = [] self.solve(S, [], res) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
def ispalindrome(s, ind, i): x = ind y = i while x < y: if s[x] != s[y]: return False x += 1 y -= 1 return True def palinpart(s, ind, n, ans, subs): if ind == n: ans.append(subs[:]) return temp = "" for i in range(ind, n): temp += s[i] if ispalindrome(s, ind, i): subs.append(temp) palinpart(s, i + 1, n, ans, subs) subs.pop() class Solution: def allPalindromicPerms(self, s): ans = [] subs = [] n = len(s) i = 0 palinpart(s, i, n, ans, subs) return ans
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def isPalin(self, s): n = len(s) i = 0 j = n - 1 while i <= j: if s[i] != s[j]: return 0 i += 1 j -= 1 return 1 def solve(self, inp, out, ans): if len(inp) == 0: ans.append(out.copy()) return n = len(inp) for i in range(n): if self.isPalin(inp[: i + 1]): out.append(inp[: i + 1]) self.solve(inp[i + 1 :], out, ans) out.pop() def allPalindromicPerms(self, S): n = len(S) ans = [] inp = S out = [] self.solve(inp, out, ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, s): def check(strs): return strs == strs[::-1] ans = [] def fun(temp, t): nonlocal ans if len(t) == 0: ans.append(temp) return for gap in range(1, len(t) + 1): sub_t = t[:gap] if check(sub_t): fun(temp + [sub_t], t[gap:]) else: continue fun([], s) return ans
CLASS_DEF FUNC_DEF FUNC_DEF RETURN VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def ispal(self, s): if s == s[::-1]: return True return False def helper(self, allpart, curpart, start, end, s): if start >= end: x = curpart.copy() allpart.append(x) return for i in range(start, end): if self.ispal(s[start : i + 1]): curpart.append(s[start : i + 1]) self.helper(allpart, curpart, i + 1, end, s) curpart.pop() return allpart def allPalindromicPerms(self, S): ans = self.helper([], [], 0, len(S), S) return ans
CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST LIST NUMBER FUNC_CALL VAR VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
def expandByCenter(S, n, c1, c2, graph): while c1 >= 0 and c2 < n and S[c1] == S[c2]: graph[c1].add(c2) c1 -= 1 c2 += 1 def util(graph, S, n, u, ans, path): if u == n: path.append(n) li = [] for i in range(len(path) - 1): li.append(S[path[i] : path[i + 1]]) ans.append(li) path.pop() return path.append(u) for v in sorted(graph[u]): util(graph, S, n, v + 1, ans, path) path.pop() class Solution: def allPalindromicPerms(self, S): n = len(S) graph = {u: set() for u in range(n)} for c1 in range(n): expandByCenter(S, n, c1, c1, graph) expandByCenter(S, n, c1, c1 + 1, graph) ans = [] util(graph, S, n, 0, ans, []) return ans
FUNC_DEF WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR LIST RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def is_palindrome(self, data): i, j = 0, len(data) - 1 while i < j: if data[i] != data[j]: return False i += 1 j -= 1 return True def solve(self, start, S, palindromes, ans): if start >= len(S): ans.append(palindromes[:]) return for i in range(start, len(S)): if self.is_palindrome(S[start : i + 1]): palindromes.append(S[start : i + 1]) self.solve(i + 1, S, palindromes, ans) palindromes.pop() def allPalindromicPerms(self, S): ans = [] palindromes = [] self.solve(0, S, palindromes, ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): self.res = set() self.solve(list(S)) return sorted(list(self.res)) def solve(self, arr): self.res.add(tuple(arr)) if len(arr) <= 1: return for i in range(1, len(arr)): if arr[i - 1] == arr[i][::-1]: brr = arr[: i - 1] + [arr[i - 1] + arr[i]] + arr[i + 1 :] self.solve(brr) if i + 1 < len(arr) and arr[i - 1] == arr[i + 1][::-1]: brr = arr[: i - 1] + [arr[i - 1] + arr[i] + arr[i + 1]] + arr[i + 2 :] self.solve(brr)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER LIST BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def is_palindrome(self, start, end, nums): while start <= end: if nums[start] != nums[end]: return False start = start + 1 end = end - 1 return True def helper(self, ind, nums, path, ans): if ind == len(nums): ans.append(list(path)) return for i in range(ind, len(nums)): if self.is_palindrome(ind, i, nums): path.append("".join(nums[ind : i + 1])) self.helper(i + 1, nums, path, ans) path.pop() def allPalindromicPerms(self, S): ans = [] self.helper(0, list(S), [], ans) return ans
CLASS_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR LIST VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def isPali(self, str): i = 0 j = len(str) - 1 while i < j: if str[i] != str[j]: return False i += 1 j -= 1 return True def solve(self, str): if len(str) == 1: return [[str[0]]] curr = [] for i in range(1, len(str)): if not self.isPali(str[:i]): continue prev = self.solve(str[i:]) for p in prev: p.insert(0, str[:i]) curr.append(p) if self.isPali(str): curr.append([str]) return curr def allPalindromicPerms(self, str): return self.solve(str)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): ans = [] temp = [] def palli(S, start, end): sub = S[start:end] return sub == sub[::-1] def func(i, S, temp, ans): if i == len(S): ans.append(temp[:]) for j in range(i, len(S)): if palli(S, i, j + 1): temp.append(S[i : j + 1]) func(j + 1, S, temp, ans) temp.pop() func(0, S, temp, ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR VAR VAR RETURN VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): res = [] n = len(S) def dfs(index, l): if index == n: res.append(l.copy()) return for i in range(index, n): s1 = S[index : i + 1] if s1 == s1[::-1]: l.append(s1) dfs(i + 1, l) l.pop() return res res = dfs(0, []) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER LIST RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): res = [] perm = [] def dfs(i): if i >= len(S): res.append(perm.copy()) return for j in range(i, len(S)): if self.isPali(S, i, j): perm.append(S[i : j + 1]) dfs(j + 1) perm.pop() dfs(0) return res def isPali(self, s, l, r): while l < r: if s[l] != s[r]: return False l = l + 1 r = r - 1 return True
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): def go(ind): if ind == len(S): ans.append(p[:]) return for i in range(ind, len(S)): if isP(S[ind : i + 1]): p.append(S[ind : i + 1]) go(i + 1) p.pop(-1) def isP(x): l = 0 r = len(x) - 1 while l < r: if x[l] != x[r]: return False l += 1 r -= 1 return True p = [] ans = [] go(0) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): def isPalindrome(i, j, S): while i <= j: if S[i] != S[j]: return False i += 1 j -= 1 return True def helper(i, arr, ans): if i == len(S): ans.append(arr[:]) return for j in range(i, len(S)): if isPalindrome(i, j, S): arr.append(S[i : j + 1]) helper(j + 1, arr, ans) arr.pop() arr, ans = [], [] helper(0, arr, ans) return ans
CLASS_DEF FUNC_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): s = S def palindrome(sub): lt = 0 rt = len(sub) - 1 while lt < rt: if sub[lt] != sub[rt]: return False lt += 1 rt -= 1 return True def part(index, s, temp, ans): if index == len(s): ans.append(temp.copy()) return else: for i in range(index, len(s)): sub = s[index : i + 1] if palindrome(sub): temp.append(sub) part(i + 1, s, temp, ans) temp.pop() ans = [] part(0, s, [], ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR LIST VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): self.s = S self.n = len(S) self.palindromes = [] self.findPalindromes(0, "", []) return self.palindromes def findPalindromes(self, index, word, palindrome): if index >= self.n: if word == "": self.palindromes.append(palindrome.copy()) return word += self.s[index] if word == word[::-1]: self.findPalindromes(index + 1, "", palindrome + [word]) self.findPalindromes(index + 1, word, palindrome)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER STRING LIST RETURN VAR FUNC_DEF IF VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
def isPalindrome(string: str, low: int, high: int): while low < high: if string[low] != string[high]: return False low += 1 high -= 1 return True def allPalPartUtil(allPart: list, currPart: list, start: int, n: int, string: str): if start >= n: x = currPart.copy() allPart.append(x) return for i in range(start, n): if isPalindrome(string, start, i): currPart.append(string[start : i + 1]) allPalPartUtil(allPart, currPart, i + 1, n, string) currPart.pop() class Solution: def allPalindromicPerms(self, S): n = len(S) allPart = [] currPart = [] allPalPartUtil(allPart, currPart, 0, n, S) return allPart
FUNC_DEF VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, s): res = [] part = [] def dfs(i): if i >= len(s): res.append(part[:]) return for j in range(i, len(s)): if self.isPalin(s, i, j): part.append(s[i : j + 1]) dfs(j + 1) part.pop() return res return dfs(0) def isPalin(self, s, i, j): if s[i : j + 1] == s[i : j + 1][::-1]: return True
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): def palind(f): l = len(f) for i in range(l // 2): if f[i] != f[l - i - 1]: return 0 return 1 def manu(i, n, f, s): if i == n: x = [] for i in range(len(f)): x.append(f[i]) a.append(x) return 0 x = "" for j in range(i, n): x += s[j] if palind(x): f.append(x) manu(j + 1, n, f, s) f.pop() return a = [] manu(0, len(S), [], S) return a
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR LIST VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): self.res = [] self.n = len(S) self.pals = self.palindrome(S) curr = [] self.palsTmp(curr, 0, S) return self.res def palsTmp(self, curr, i, S): if i >= self.n: self.res.append(list(curr)) else: for j in range(i, self.n): if self.pals[i][j]: curr.append(S[i : j + 1]) self.palsTmp(curr, j + 1, S) curr.pop() def palindrome(self, s): n = len(s) tb = [[(False) for j in range(n)] for i in range(n)] for i in range(n): tb[i][i] = True for i in range(n - 1): if s[i] == s[i + 1]: tb[i][i + 1] = True for gap in range(2, n): for i in range(n - gap): j = i + gap if s[i] == s[j] and tb[i + 1][j - 1]: tb[i][j] = True return tb
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): def dfs(i, l): if i == len(S): ans.append(l[:]) return for ind in range(i, len(S)): if S[i : ind + 1] == S[i : ind + 1][::-1]: l.append(S[i : ind + 1]) dfs(ind + 1, l) l.pop() ans = [] dfs(0, []) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def isPalindrome(self, s): i = 0 j = len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True def backtrack(self, i, n, temp, l, S): if i >= n: l.append(temp[:]) p = "" for j in range(i, n): p += S[j] if self.isPalindrome(p): temp.append(p) self.backtrack(j + 1, n, temp, l, S) temp.pop() def allPalindromicPerms(self, S): l = [] self.backtrack(0, len(S), [], l, S) return l
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR LIST VAR VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): s = S n = len(s) ds, ans = [], [] def ispalindrome(start, end, s): while start <= end: if s[start] != s[end]: return False start += 1 end -= 1 return True def rec(idx, s): if idx == n: ans.append(list(ds)) return ans for i in range(idx, n): if ispalindrome(idx, i, s): ds.append(s[idx : i + 1]) rec(i + 1, s) ds.pop() return ans return rec(0, s)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
def func(s, temp, ans): if not s: ans.append(temp[:]) return for i in range(1, len(s) + 1): t = s[:i] if t == t[::-1]: func(s[i:], temp + [t], ans) class Solution: def allPalindromicPerms(self, s): ans = [] func(s, [], ans) return ans
FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR LIST VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR LIST VAR RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
def rec(S, ind, res, string): if ind == len(S): res.append(list(string.split())) ans = 0 for i in range(ind + 1, len(S) + 1): temp = S[ind:i] if temp == temp[::-1]: rec(S, i, res, string + " " + temp) class Solution: def allPalindromicPerms(self, S): res = [] rec(S, 0, res, "") return res
FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR STRING VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR STRING RETURN VAR
Given a String S, Find all possible Palindromic partitions of the given String. Example 1: Input: S = "geeks" Output: g e e k s g ee k s Explanation: All possible palindromic partitions are printed. Example 2: Input: S = "madam" Output: m a d a m m ada m madam Your Task: You don't need to read input or print anything. Your task is to complete the function allPalindromicPerms() which takes a String S as input parameter and returns a list of lists denoting all the possible palindromic partitions in the order of their appearance in the original string. Expected Time Complexity: O(N*2^{N}) Expected Auxiliary Space: O(N^{2}), where N is the length of the String Constraints: 1 <= |S| <= 20
class Solution: def allPalindromicPerms(self, S): n = len(S) res = [] curr = [] self.allpartitions(S, res, curr, 0) return res def allpartitions(self, s, res, curr, start): n = len(s) if start >= n: x = curr.copy() res.append(x) return for i in range(start, n): if self.ispalindrome(s, start, i): curr.append(s[start : i + 1]) self.allpartitions(s, res, curr, i + 1) curr.pop() def ispalindrome(self, string, low, high): while low < high: if string[low] != string[high]: return False low += 1 high -= 1 return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER
Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of n vertices numbered from 1 to n. The i-th vertex has m_i outgoing edges that are labeled as e_i[0], e_i[1], …, e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The i-th vertex also has an integer k_i written on itself. A travel on this graph works as follows. 1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable c to this integer. 2. After arriving at the vertex i, or when Gildong begins the travel at some vertex i, add k_i to c. 3. The next vertex is e_i[x] where x is an integer 0 ≤ x ≤ m_i-1 satisfying x ≡ c \pmod {m_i}. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly. For example, assume that Gildong starts at vertex 1 with c = 5, and m_1 = 2, e_1[0] = 1, e_1[1] = 2, k_1 = -3. Right after he starts at vertex 1, c becomes 2. Since the only integer x (0 ≤ x ≤ 1) where x ≡ c \pmod {m_i} is 0, Gildong goes to vertex e_1[0] = 1. After arriving at vertex 1 again, c becomes -1. The only integer x satisfying the conditions is 1, so he goes to vertex e_1[1] = 2, and so on. Since Gildong is quite inquisitive, he's going to ask you q queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of c. Note that you should not count the vertices that will be visited only finite times. Input The first line of the input contains an integer n (1 ≤ n ≤ 1000), the number of vertices in the graph. The second line contains n integers. The i-th integer is k_i (-10^9 ≤ k_i ≤ 10^9), the integer written on the i-th vertex. Next 2 ⋅ n lines describe the edges of each vertex. The (2 ⋅ i + 1)-st line contains an integer m_i (1 ≤ m_i ≤ 10), the number of outgoing edges of the i-th vertex. The (2 ⋅ i + 2)-nd line contains m_i integers e_i[0], e_i[1], …, e_i[m_i-1], each having an integer value between 1 and n, inclusive. Next line contains an integer q (1 ≤ q ≤ 10^5), the number of queries Gildong wants to ask. Next q lines contains two integers x and y (1 ≤ x ≤ n, -10^9 ≤ y ≤ 10^9) each, which mean that the start vertex is x and the starting value of c is y. Output For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex x with starting integer y. Examples Input 4 0 0 0 0 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 2 1 3 2 Input 4 4 -5 -3 -1 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Output 1 1 1 3 1 1 Note The first example can be shown like the following image: <image> Three integers are marked on i-th vertex: i, k_i, and m_i respectively. The outgoing edges are labeled with an integer representing the edge number of i-th vertex. The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (c after k_i added)". * 1(0) → 2(0) → 2(0) → … * 2(0) → 2(0) → … * 3(-1) → 1(-1) → 3(-1) → … * 4(-2) → 2(-2) → 2(-2) → … * 1(1) → 3(1) → 4(1) → 1(1) → … * 1(5) → 3(5) → 1(5) → … The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example. <image> The queries for the second example works as follows: * 1(4) → 2(-1) → 2(-6) → … * 2(-5) → 2(-10) → … * 3(-4) → 1(0) → 2(-5) → 2(-10) → … * 4(-3) → 1(1) → 3(-2) → 4(-3) → … * 1(5) → 3(2) → 1(6) → 2(1) → 2(-4) → … * 1(9) → 3(6) → 2(1) → 2(-4) → …
import sys from sys import stdin def solve(v, c): if d[v][c] != None: return d[v][c] vnum = 0 visits = set() while True: if c * n + v in visits: last = d[v][c] vs = set() for u in visits: if d[u % n][u // n] <= last: vs.add(u % n) nans = len(vs) for u in visits: d[u % n][u // n] = nans return nans elif d[v][c] != None and d[v][c] > 0: nans = d[v][c] for u in visits: d[u % n][u // n] = nans return nans visits.add(c * n + v) d[v][c] = vnum vnum -= 1 c = (c + k[v]) % mod v = lis[v][c % len(lis[v])] n = int(stdin.readline()) mod = 2520 d = [([None] * mod) for i in range(n)] k = list(map(int, stdin.readline().split())) lis = [] for i in range(n): m = int(stdin.readline()) lis.append(list(map(int, stdin.readline().split()))) for j in range(m): lis[i][j] -= 1 q = int(stdin.readline()) ANS = [] for loop in range(q): x, y = map(int, stdin.readline().split()) x -= 1 ANS.append(solve(x, y % mod)) print("\n".join(map(str, ANS)))
IMPORT FUNC_DEF IF VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR IF VAR VAR VAR NONE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given an array of n integer numbers. Let sum(l, r) be the sum of all numbers on positions from l to r non-inclusive (l-th element is counted, r-th element is not counted). For indices l and r holds 0 ≤ l ≤ r ≤ n. Indices in array are numbered from 0. For example, if a = [ - 5, 3, 9, 4], then sum(0, 1) = - 5, sum(0, 2) = - 2, sum(1, 4) = 16 and sum(i, i) = 0 for each i from 0 to 4. Choose the indices of three delimiters delim0, delim1, delim2 (0 ≤ delim0 ≤ delim1 ≤ delim2 ≤ n) and divide the array in such a way that the value of res = sum(0, delim0) - sum(delim0, delim1) + sum(delim1, delim2) - sum(delim2, n) is maximal. Note that some of the expressions sum(l, r) can correspond to empty segments (if l = r for some segment). Input The first line contains one integer number n (1 ≤ n ≤ 5000). The second line contains n numbers a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109). Output Choose three indices so that the value of res is maximal. If there are multiple answers, print any of them. Examples Input 3 -1 2 3 Output 0 1 3 Input 4 0 0 -1 0 Output 0 0 0 Input 1 10000 Output 1 1 1
from itertools import accumulate, chain class MaxSegTree: def __init__(self, zero, data=None, size=None): if data is None and size is None: raise Exception("data and size both cannot be None simultaneously") if size is None: size = len(data) depth = (size - 1).bit_length() self.size = 1 << depth self.zero = zero if data is None: self.data = [zero] * (2 * self.size) else: self.data = [zero] * self.size + data + [zero] * (self.size - size) for d in reversed(range(depth)): t = 1 << d for i in range(t, 2 * t): self.data[i] = max(self.data[2 * i], self.data[2 * i + 1]) def _max_interval(self, a, b): result = self.zero a += self.size b += self.size while a < b: if a & 1: result = max(result, self.data[a]) a += 1 if b & 1: b -= 1 result = max(result, self.data[b]) a >>= 1 b >>= 1 return result def _set_val(self, a, val): a += self.size while self.data[a] != val and a > 0: self.data[a] = val val = max(val, self.data[a ^ 1]) a >>= 1 def __getitem__(self, i): if isinstance(i, slice): return self._max_interval( 0 if i.start is None else i.start, self.size if i.stop is None else i.stop, ) elif isinstance(i, int): return self.data[i + self.size] def __setitem__(self, i, x): self._set_val(i, x) def __iter__(self): return iter(self.data[self.size :]) n = int(input()) C = list( (v, i) for i, v in enumerate(chain((0,), accumulate(map(int, input().split())))) ) mst = MaxSegTree((-float("inf"), 0), C) a, b, c = None, None, None best = -float("inf") for v2, j in C: v1, i = mst[: j + 1] v3, k = mst[j:] if v1 - v2 + v3 > best: best = v1 - v2 + v3 a, b, c = i, j, k print(a, b, c)
CLASS_DEF FUNC_DEF NONE NONE IF VAR NONE VAR NONE FUNC_CALL VAR STRING IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NONE NUMBER VAR VAR NONE VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER VAR ASSIGN VAR VAR VAR NONE NONE NONE ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
You are given an array of n integer numbers. Let sum(l, r) be the sum of all numbers on positions from l to r non-inclusive (l-th element is counted, r-th element is not counted). For indices l and r holds 0 ≤ l ≤ r ≤ n. Indices in array are numbered from 0. For example, if a = [ - 5, 3, 9, 4], then sum(0, 1) = - 5, sum(0, 2) = - 2, sum(1, 4) = 16 and sum(i, i) = 0 for each i from 0 to 4. Choose the indices of three delimiters delim0, delim1, delim2 (0 ≤ delim0 ≤ delim1 ≤ delim2 ≤ n) and divide the array in such a way that the value of res = sum(0, delim0) - sum(delim0, delim1) + sum(delim1, delim2) - sum(delim2, n) is maximal. Note that some of the expressions sum(l, r) can correspond to empty segments (if l = r for some segment). Input The first line contains one integer number n (1 ≤ n ≤ 5000). The second line contains n numbers a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109). Output Choose three indices so that the value of res is maximal. If there are multiple answers, print any of them. Examples Input 3 -1 2 3 Output 0 1 3 Input 4 0 0 -1 0 Output 0 0 0 Input 1 10000 Output 1 1 1
n = int(input()) a = [5] + list(map(int, input().split())) dp = [[[i, 0] for i in range(n + 1)] for j in range(4)] end = n indx_a, indx_b, indx_c = 0, 0, 0 for i in range(1, n + 1): dp[0][i][1] = dp[0][i - 1][1] + a[i] dp[1][i][1] = max(dp[0][i - 1][1], dp[1][i - 1][1]) - a[i] dp[2][i][1] = max(dp[1][i - 1][1], dp[2][i - 1][1]) + a[i] dp[3][i][1] = max(dp[2][i - 1][1], dp[3][i - 1][1]) - a[i] dp_indx = [0, 0, 0, 0] indx = 0 pt = 3 for i in range(n, 0, -1): if dp[pt][i][1] < dp[pt - 1][i][1]: pt -= 1 dp_indx[pt] = i if pt == 0: break print(dp_indx[0], dp_indx[1], dp_indx[2])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER
You are given an array of n integer numbers. Let sum(l, r) be the sum of all numbers on positions from l to r non-inclusive (l-th element is counted, r-th element is not counted). For indices l and r holds 0 ≤ l ≤ r ≤ n. Indices in array are numbered from 0. For example, if a = [ - 5, 3, 9, 4], then sum(0, 1) = - 5, sum(0, 2) = - 2, sum(1, 4) = 16 and sum(i, i) = 0 for each i from 0 to 4. Choose the indices of three delimiters delim0, delim1, delim2 (0 ≤ delim0 ≤ delim1 ≤ delim2 ≤ n) and divide the array in such a way that the value of res = sum(0, delim0) - sum(delim0, delim1) + sum(delim1, delim2) - sum(delim2, n) is maximal. Note that some of the expressions sum(l, r) can correspond to empty segments (if l = r for some segment). Input The first line contains one integer number n (1 ≤ n ≤ 5000). The second line contains n numbers a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109). Output Choose three indices so that the value of res is maximal. If there are multiple answers, print any of them. Examples Input 3 -1 2 3 Output 0 1 3 Input 4 0 0 -1 0 Output 0 0 0 Input 1 10000 Output 1 1 1
n = int(input()) nums = [int(x) for x in input().split()] best = [None for x in range(len(nums))] way = [None for x in range(len(nums))] def find_best_way(): global best, n, nums, way for j in range(n - 1, -1, -1): best[j] = dict() way[j] = dict() previous = {"s1": 0, "s2": 0, "s3": 0, "s4": 0} if j + 1 == n else best[j + 1] best[j]["s1"] = -nums[j] + previous["s1"] way[j]["s1"] = "s1" for i in range(2, 5): changed = (1 if i % 2 == 0 else -1) * nums[j] + previous[f"s{i}"] unchanged = (-1 if i % 2 == 0 else 1) * nums[j] + previous[f"s{i - 1}"] if unchanged >= changed: best[j][f"s{i}"] = unchanged way[j][f"s{i}"] = f"s{i - 1}" continue best[j][f"s{i}"] = changed way[j][f"s{i}"] = f"s{i}" def get_delimiters(): global way, nums, n qnt = {"s1": 0, "s2": 0, "s3": 0, "s4": 0} s = "s4" for i in range(n): qnt[way[i][s]] += 1 s = way[i][s] d1 = qnt["s4"] d2 = d1 + qnt["s3"] d3 = d2 + qnt["s2"] return d1, d2, d3 find_best_way() print(*get_delimiters())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR STRING BIN_OP VAR VAR VAR STRING ASSIGN VAR VAR STRING STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR VAR STRING BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR STRING VAR VAR ASSIGN VAR VAR STRING VAR STRING VAR FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR STRING RETURN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
from sys import stdin input = stdin.readline MOD = 1000000007 t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split()] dp = [([0] * (n + 1)) for _ in range(k)] for r in range(1, k): for c in range(1, n + 1): dp[r][c] = (dp[r][c - 1] + dp[r - 1][n - c] + 1) % MOD ans = (dp[-1][-1] + 1) % MOD print(ans)
ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys MOD = 1000000007 def read_ints(): return [int(i) for i in sys.stdin.readline().strip().split()] def read_int(): return int(sys.stdin.readline().strip()) def solve(n, k): if k == 1: return 1 particles = 1 dp = [1] * n while k > 1: particles += sum(dp) particles %= MOD k -= 1 tot = 0 dp2 = [0] * n for i in range(n): dp2[i] = tot tot += dp[i] tot %= MOD dp2.reverse() dp = dp2[:] return particles t = read_int() for i in range(t): n, k = read_ints() print(solve(n, k))
IMPORT ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys sys.setrecursionlimit(10000) mod = 10**9 + 7 def solve(curr, left): if dp[curr][left] != -1: return dp[curr][left] if left == 1: return 1 if curr == 0: if dp[curr][left] != -1: return dp[curr][left] dp[curr][left] = (1 + solve(curr + 1, left)) % mod return dp[curr][left] elif curr == n: return 1 ans1 = solve(n - curr, left - 1) % mod ans2 = solve(curr + 1, left) % mod dp[curr][left] = (ans1 + ans2) % mod return dp[curr][left] for _ in range(int(input())): n, k = map(int, input().split()) dp = [[(-1) for i in range(k + 1)] for j in range(n + 1)] ans = solve(0, k) print(ans)
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = sys.stdin.readline def fun(x, y): rem = 1000000007 return (x % rem + y % rem) % rem for _ in range(int(input())): n, k = map(int, input().split()) if n == 1: if k > 1: print(2) else: print(1) else: left = [0] * (n - 1) right = [0] * (n - 1) left[0] = 1 count = 0 ans = 0 for i in range(k): if count % 2 == 0: tem = 0 for j in range(n - 1): tem = fun(left[j], tem) right[j] = tem ans = fun(ans, tem) else: tem = 0 for j in range(n - 2, -1, -1): tem = fun(right[j], tem) left[j] = tem ans = fun(ans, tem) count += 1 if k > 1: ans += 1 print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP 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 IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
from sys import stdin MOD = int(1000000000.0 + 7) def solve(tc): n, k = map(int, stdin.readline().split()) if n == 1: if k == 1: print(1) else: print(2) return mem = [(0) for i in range(k + 1)] planes = [(1) for i in range(n)] mem[1] = 1 for i in range(2, k + 1): mem[i] = mem[i - 1] for j in range(n): mem[i] += planes[j] mem[i] %= MOD if i & 1: cum = planes[0] planes[0] = 0 for j in range(1, n): tmp = planes[j] planes[j] = cum cum += tmp cum %= MOD else: cum = planes[n - 1] planes[n - 1] = 0 for j in range(n - 2, -1, -1): tmp = planes[j] planes[j] = cum cum += tmp cum %= MOD print(mem[k]) tcs = 1 tcs = int(stdin.readline().strip()) tc = 1 while tc <= tcs: solve(tc) tc += 1
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
from sys import stdin modulus = int(10**9 + 7) def resultat(max_decay_age, width): if max_decay_age == 1: return 1 current_layer = [1] * (width - 1) for thing in range(max_decay_age - 2): current_layer = list(reversed(current_layer)) new_layer = [0] * (width - 1) total = 1 for i in range(width - 1): total = (total + current_layer[i]) % modulus new_layer[i] = total current_layer = new_layer return (sum(current_layer) + 2) % modulus count = int(stdin.readline()) for _ in range(count): width, max_decay_age = (int(x) for x in stdin.readline().split()) print(resultat(max_decay_age, width))
ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
modulo = 10**9 + 7 for ct in range(int(input())): n, k = map(int, input().split()) dp = [[(0) for j in range(n + 1)] for i in range(k + 1)] for i in range(k + 1): for j in range(n + 1): if i == 1 or j == 0: dp[i][j] = 1 else: dp[i][j] = (dp[i][j - 1] + dp[i - 1][n - j]) % modulo print(dp[-1][-1])
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
MOD = 10**9 + 7 for _ in range(int(input())): n, k = map(int, input().split()) if k == 1: print(1) elif k == 2: print(n + 1) elif n == 1: print(2) else: cur = [i for i in range(1, n)] k -= 3 res = 1 + n + sum(cur) while k: k -= 1 nxt = [cur[-1]] for i in range(len(cur) - 2, -1, -1): nxt.append((nxt[-1] + cur[i]) % MOD) res = (res + sum(nxt)) % MOD cur = nxt print(res)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
mod = int(1000000000.0 + 7) for _ in range(int(input())): n, k = map(int, input().split()) if k == 1: print(1) continue if n == 1: print(2) continue ans = 1 n -= 1 a = [(1) for _ in range(n)] while True: cursum = 0 for i in range(n): a[i] += cursum cursum = a[i] ans += cursum k -= 1 if k == 1: break cursum = 0 for i in range(n - 1, -1, -1): a[i] += cursum cursum = a[i] ans += cursum k -= 1 if k == 1: break print((ans + 1) % mod)
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) dp = [[[(-1) for i in range(2)] for j in range(k + 1)] for l in range(n + 1)] for i in range(1, n + 1): dp[i][1][0], dp[i][1][1] = 1, 1 for i in range(2, k + 1): for j in range(n, 0, -1): loc = 2 if j < n: loc += dp[j + 1][i][0] - 1 if j > 1: loc += dp[j - 1][i - 1][1] - 1 dp[j][i][0] = loc % (10**9 + 7) for j in range(1, n + 1): loc = 2 if j < n: loc += dp[j + 1][i - 1][0] - 1 if j > 1: loc += dp[j - 1][i][1] - 1 dp[j][i][1] = loc % (10**9 + 7) print(dp[1][k][0])
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
t = int(input()) for test in range(1, t + 1): n, k = list(map(int, input().split())) if k == 1: print(1) elif n == 1: print(2) else: res = 2 ls = [(1) for _ in range(n - 1)] for i in range(k - 1): new_ls = [] curr_sum = 0 for j in range(n - 1): res = (res + ls[j]) % (10**9 + 7) curr_sum += ls[j] % (10**9 + 7) new_ls.append(curr_sum) ls = new_ls[::-1] print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
MOD = 10**9 + 7 def solve(N, K): last_dp = [1] * (N + 1) ans = 1 turn = 0 for _ in range(K - 1, 0, -1): dp = [0] * (N + 1) if turn == 0: for j in range(N - 1, -1, -1): dp[j] = last_dp[j] + dp[j + 1] ans += dp[0] else: for j in range(1, N + 1, 1): dp[j] = last_dp[j] + dp[j - 1] ans += dp[N] turn = 1 - turn last_dp = dp return ans % MOD T = int(input()) for _ in range(T): N, K = map(int, input().split()) print(solve(N, K))
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys mod = int(10**9 + 7) for _ in range(int(sys.stdin.readline())): n, k = map(int, sys.stdin.readline().split()) if k == 1: print(1) continue if n == 1: print(2) continue now = [0] * (n - 1) now[0], ans, direction = 1, 0, 1 for i in range(k): temp, total = [0] * (n - 1), 0 if direction == 1: for j in range(n - 1): total += now[j] % mod temp[j] = total direction = -direction else: for j in range(n - 2, -1, -1): total += now[j] % mod temp[j] = total direction = -direction now = temp ans += total print((ans + 1) % mod)
IMPORT ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = sys.stdin.readline mod = 10**9 + 7 t = int(input()) for tests in range(t): n, k = map(int, input().split()) if k == 1: print(1) continue if n == 1: print(2) continue ANS = 1 DP = [1] * n for i in range(k - 1): NDP = [0] * n if i % 2 == 0: S = 0 for j in range(n): S += DP[j] S %= mod if j != n - 1: NDP[j + 1] = S ANS += S else: S = 0 for j in range(n - 1, -1, -1): S += DP[j] S %= mod if j != 0: NDP[j - 1] = S ANS += S DP = NDP print(ANS % mod)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
for _ in range(int(input())): n, k = map(int, input().split()) MOD = 1000000007 dp = [[(0) for x in range(1010)] for y in range(1010)] for l in range(k + 1): for m in range(n + 1): if l == 1: dp[l][m] = 1 elif m == 0: dp[l][m] = 1 else: dp[l][m] = (dp[l][m - 1] + dp[l - 1][n - m]) % MOD print(dp[k][n])
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 NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) md = 10**9 + 7 (t,) = I() for _ in range(t): n, k = I() if n == 1: print(1 if k == 1 else 2) elif k == 1: print(1) else: an = 1 arr = [0] * n di = 1 arr[0] = 1 while k > 1: pos = [0] * n an = (an + sum(arr)) % md if di: for i in range(1, n): pos[i] += arr[i - 1] + pos[i - 1] pos[i] %= md arr = list(pos) di = 0 else: for i in range(n - 2, -1, -1): pos[i] += arr[i + 1] + pos[i + 1] pos[i] %= md arr = list(pos) di = 1 k -= 1 an = (an + sum(arr)) % md print(an)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
MAX_N = 1001 MOD_N = 1000000007 t = int(input()) ans = [[(-1) for _ in range(MAX_N)] for _ in range(MAX_N)] def dp(n, k, total_k): if n == 1 or k == 0: ans[n][k] = 1 return ans[n][k] if ans[n][k] != -1: return ans[n][k] ans[n][k] = (dp(n - 1, total_k - k, total_k) + dp(n, k - 1, total_k)) % MOD_N return ans[n][k] while t > 0: n, k = map(int, input().split()) ans = [[1] * (n + 1)] + [[1] * (n + 1)] + [([1] + [0] * n) for _ in range(k)] for i in range(2, k + 1): for j in range(1, n + 1): ans[i][j] = (ans[i - 1][n - j] + ans[i][j - 1]) % MOD_N print(ans[k][n]) t -= 1
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
from itertools import accumulate t = int(input()) mod = 10**9 + 7 for _ in range(t): n, k = map(int, input().split()) now = [0] * (n + 1) now[0] = 1 now[1] = 1 ans = 0 for i in range(k): if i & 1: for j in reversed(range(n - 1)): now[j] += now[j + 1] now[j] %= mod ans += now[0] ans %= mod now[0] = 0 else: for j in range(1, n): now[j + 1] += now[j] now[j] %= mod ans += now[-1] ans %= mod now[-1] = 0 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
for _ in range(int(input())): planes, k = (int(x) for x in input().split()) if k == 1: print(1) continue if planes == 1: print(2) continue d = {} d[k] = 1 ans = 0 temp = k dp = {} planes -= 1 mod = 1000000007 for i in range(k, -1, -1): dp[i] = [] for i in range(k, -1, -1): for j in range(planes + 1): dp[i].append(0) dp[k][planes] = 1 for i in range(k - 1, -1, -1): par = 0 for j in range(planes, 0, -1): par = (par % mod + dp[i + 1][j] % mod) % mod dp[i][planes - j + 1] = par for i in range(k, -1, -1): ans = (ans % mod + dp[i][planes] % mod) % mod print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys def main(): allans = [] t = int(input()) for _ in range(t): n, k = readIntArr() dp = makeArr(0, [k + 1, n + 1]) for i in range(1, k + 1): dp[i][0] = 1 for j in range(n + 1): dp[1][j] = 1 for i in range(2, k + 1): for j in range(1, n + 1): dp[i][j] = (dp[i][j - 1] + dp[i - 1][n - j]) % MOD ans = dp[k][n] allans.append(ans) multiLineArrayPrint(allans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(defaultVal, dimensionArr): dv = defaultVal da = dimensionArr if len(da) == 1: return [dv for _ in range(da[0])] else: return [makeArr(dv, da[1:]) for _ in range(da[0])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = lambda: sys.stdin.readline().rstrip() MOD = 1000000000.0 + 7 class Problem: def __init__(self): pass def solve(self): ans = 0 N, K = map(int, input().split()) dp = [[(0) for j in range(N + 1)] for i in range(K + 1)] for i in range(1, K + 1): for j in range(N + 1): if i == 1 or j == 0: dp[i][j] = 1 continue dp[i][j] = int((dp[i][j - 1] + dp[i - 1][N - j]) % MOD) ans = dp[K][N] print(ans) def main(): p = Problem() T = int(input()) while T: p.solve() T -= 1 main()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
from itertools import accumulate rn = lambda: int(input()) rns = lambda: map(int, input().split()) rl = lambda: list(map(int, input().split())) rs = lambda: input() YN = lambda x: print("YES") if x else print("NO") mod = 10**9 + 7 def pre(a): return list(accumulate(a, lambda a, b: (a + b) % mod)) def suff(a): return list(reversed(list(accumulate(a[::-1], lambda a, b: (a + b) % mod)))) for _ in range(rn()): n, k = rns() ans = 1 nums = (n - 1) * [1] for i in range(k - 1): ans += sum(nums) if i % 2 == 0: nums = suff(nums) else: nums = pre(nums) ans %= mod if k > 1: ans += 1 ans %= mod print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
for u in range(int(input())): n, k = map(int, input().split()) x = [1] * n ans = 1 mod = 10**9 + 7 if k == 1: print(1) elif n == 1: print(2) else: k -= 2 ans += n x = [1] * (n - 1) while k > 0: s = 0 if k % 2 == 0: x[0] = x[0] % mod s += x[0] % mod for i in range(1, n - 1): x[i] = (x[i] + x[i - 1]) % mod s += x[i] % mod else: x[n - 2] = x[n - 2] % mod s += x[n - 2] % mod for i in range(n - 3, -1, -1): x[i] = (x[i] + x[i + 1]) % mod s += x[i] % mod ans += s % mod k -= 1 print(ans % mod)
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 VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
for _ in range(int(input())): n, k = list(map(int, input().split())) if k == 1: print(1) elif k == 2: print(n + 1) else: a = [i for i in range(n - 1, 0, -1)] s = n + 1 for i in a: s += i d, k = 1, k - 2 while k > 1: if d: for i in range(1, len(a)): a[i] = (a[i] + a[i - 1]) % 1000000007 else: for i in range(len(a) - 2, -1, -1): a[i] = (a[i] + a[i + 1]) % 1000000007 k -= 1 d = 1 - d for i in a: s += i if s > 1000000007: s = s % 1000000007 print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
t = int(input()) MOD = 10**9 + 7 for _ in range(t): n, k = list(map(int, input().split())) if k == 1: print(1) continue elif n == 1: print(2) continue counts = [1] * (n - 1) size = n - 1 ans = 2 left_to_right = True for age in range(k - 1): ans += sum(counts) if left_to_right: for i in range(1, size): counts[i] = (counts[i - 1] + counts[i]) % MOD else: for i in range(size - 2, -1, -1): counts[i] = (counts[i + 1] + counts[i]) % MOD left_to_right = not left_to_right print(ans % MOD)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") inp = lambda: list(map(int, sys.stdin.readline().rstrip("\r\n").split())) mod = 10**9 + 7 Mod = 998244353 INF = float("inf") sys.setrecursionlimit(3 * 10000 + 10) tc = 1 tc = int(input()) for test in range(1, tc + 1): N, K = inp() dp = [[[0, 0] for i in range(N + 1)] for j in range(K + 1)] for i in range(1, N + 1): dp[1][i][0] = dp[1][i][1] = 1 for k in range(2, K + 1): for n in range(N, 0, -1): ans = 2 if n < N: ans += dp[k][n + 1][0] - 1 if n > 1: ans += dp[k - 1][n - 1][1] - 1 dp[k][n][0] = ans % mod for n in range(1, N + 1): ans = 2 if n < N: ans += dp[k - 1][n + 1][0] - 1 if n > 1: ans += dp[k][n - 1][1] - 1 dp[k][n][1] = ans % mod print(dp[K][1][0])
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys input = sys.stdin.buffer.readline mod = 10**9 + 7 for t in range(int(input())): N, K = map(int, input().split()) DP = [[[-1, -1, -1] for j in range(N + 2)] for i in range(K + 2)] for k in range(1, K + 1): for x in range(N, -2, -1): y = 1 if x == N or k == 1 or x == -1: DP[k][x][y] = 1 continue DP[k][x][y] = DP[k][x + y][y] + DP[k - 1][x - y][-y] if DP[k][x][y] >= mod: DP[k][x][y] -= mod for x in range(-1, N + 1): y = -1 if x == N or k == 1 or x == -1: DP[k][x][y] = 1 continue DP[k][x][y] = DP[k][x + y][y] + DP[k - 1][x - y][-y] if DP[k][x][y] >= mod: DP[k][x][y] -= mod print(DP[K][0][1])
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
def putin(): return map(int, input().split()) def sol(): C = 10**9 + 7 n, k = putin() if k == 1: print(1) return if n == 1: print(2) return D = [] for i in range(k): D.append([0] * n) for i in range(n): D[0][i] = 1 for i in range(1, k): D[i][n - 1] = D[i - 1][1] + 1 D[i][n - 1] %= C for j in range(n - 2, 0, -1): D[i][j] = D[i][j + 1] + D[i - 1][n - j] D[i][j] %= C D[i][0] = 1 + D[i][1] D[i][0] %= C print(D[k - 1][0]) for iter in range(int(input())): sol()
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
MOD = 10**9 + 7 t = int(input()) for _ in range(t): n, k = map(int, input().split()) dp = [([0] * (n + 1)) for _ in range(k)] for i in range(n + 1): dp[0][i] = 1 for b in range(1, k): cur = 0 for i in range(n + 1): dp[b][i] = cur + 1 if i < n: cur = (cur + dp[b - 1][n - 1 - i]) % MOD print(dp[k - 1][n])
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
R = range for s in [*open(0)][1:]: n, k = map(int, s.split()) D = [[1] * (n + 1)] + [([1] + n * [0]) for _ in R(k)] for d in R(1, k): for i in R(n): D[d][i + 1] = (D[d][i] + D[d - 1][n - i - 1]) % (10**9 + 7) print(D[k - 1][n])
ASSIGN VAR VAR FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR LIST NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
mod = 10**9 + 7 def abhi(n, k): if k <= 0: return 0 elif n <= 0 and k > 0: return 1 elif dp[n][k] != -1: return dp[n][k] % mod else: a = abhi(s - n, k - 1) % mod b = abhi(n - 1, k) % mod if s - n >= 0: dp[s - n][k - 1] = a % mod if n >= 1: dp[n - 1][k] = b % mod return (a + b) % mod for i in range(int(input())): s, k = map(int, input().split()) dp = [[(-1) for i in range(1005)] for j in range(1005)] print(abhi(s, k) % mod)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP 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 VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys def load_sys(): return sys.stdin.readlines() def load_local(): with open("input.txt", "r") as f: input = f.readlines() return input def f(n, k): MOD_NUM = 10**9 + 7 dp = [[(0) for _ in range(n + 2)] for _ in range(k + 1)] for age in range(1, k + 1): dp[age][0] = 1 for age in range(k + 1): for idx in range(1, n + 1): dp[age][idx] = (dp[age][idx - 1] + dp[age - 1][n - idx]) % MOD_NUM return dp[k][n] input = load_sys() for i in range(1, len(input)): arr = input[i].split() n, k = int(arr[0]), int(arr[1]) print(f(n, k))
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR STRING STRING VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
for _ in range(int(input())): n, k = map(int, input().split()) dp = [] for i in range(0, 1005): dp.append([0] * 1005) ans = 1 for i in range(1, k + 1): for j in range(0, n + 1): if j == 0 or i == 1: dp[i][j] = 1 else: dp[i][j] = (dp[i - 1][n - j] + dp[i][j - 1]) % 1000000007 print(dp[k][n])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
T = int(input()) M = 10**9 + 7 while T: n, k = map(int, input().split()) X = [1] * (n - 1) z = 1 + min(k - 1, 1) * n for i in range(k - 2): for j in range(1, n - 1): X[j] = (X[j] + X[j - 1]) % M z = (z + sum(X)) % M X = X[::-1] print(z % M) T = T - 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE 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 BIN_OP NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
t = int(input()) inf = 10**9 + 7 for i in range(t): n, k = map(int, input().split()) a = [0] * (n + 1) a[1] = 1 ans = 1 if k > 1: ans += 1 for i in range(k - 1): pref = [0] * (n + 1) if i % 2 == 0: for j in range(1, n): pref[j] = (a[j] + pref[j - 1]) % inf ans = (ans + pref[j]) % inf else: for j in range(n - 1, 0, -1): pref[j] = (a[j] + pref[j + 1]) % inf ans = (ans + pref[j]) % inf a = pref print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import time t = int(input()) for _ in range(t): n, k = list(map(int, input().split())) if k == 1: print("1") else: m = 1000000007 s = 1 + n k = k - 1 l = [(1) for i in range(n - 1)] a = 0 while k > 1: if k % 2 == 0: for i in range(n - 1): if i == 0: a = l[i] l[i] = a s = (s % m + a % m) % m else: a = (a % m + l[i] % m) % m l[i] = a s = (s % m + a % m) % m else: for i in range(-1, -n, -1): if i == -1: a = l[i] l[i] = a s = (s % m + a % m) % m else: a = (a % m + l[i] % m) % m l[i] = a s = (s % m + a % m) % m k -= 1 print(s % m)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
from itertools import accumulate DIV = 10**9 + 7 def cum_sum(in_list, forward=True): if forward: for i in range(1, len(in_list)): in_list[i] += in_list[i - 1] in_list[i] %= DIV else: for i in range(len(in_list) - 2, -1, -1): in_list[i] += in_list[i + 1] in_list[i] %= DIV def count_planar(n, k): if k == 1: print(1) elif n == 1: print(2) else: cnt = 2 mem = [1] * (n - 1) for i in range(k - 1): if i % 2 == 0: cum_sum(mem, forward=True) cnt += mem[-1] else: cum_sum(mem, forward=False) cnt += mem[0] print(cnt % (10**9 + 7)) num = int(input()) for i in range(num): n, k = [int(el) for el in input().split()] count_planar(n, k)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF NUMBER IF VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
module = 10**9 + 7 for _ in range(int(input())): n, k = map(int, input().split()) if k == 1: print(1) elif n == 1: print(2) else: ans = 2 table = [[1] * (n - 1)] left = 1 while k != 1: if left: table.append([table[-1][-1]]) for num in list(reversed(table[-2]))[1:]: table[-1].append(table[-1][-1] + num) table[-1].reverse() for ind in range(n - 1): table[-1][ind] %= module left = 0 else: table.append([table[-1][0]]) for num in table[-2][1:]: table[-1].append(table[-1][-1] + num) for ind in range(n - 1): table[-1][ind] %= module left = 1 k -= 1 if left: start = -2 ans += table[-1][-1] else: start = -1 for num, ind in enumerate(range(start, -len(table), -1)): ans = (ans + table[ind][0 if num % 2 == 0 else -1]) % module print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER FOR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
def ss(n, k): dp = [[(0) for _ in range(n + 1)] for _ in range(k + 1)] for i in range(n + 1): dp[0][i] = 1 for i in range(k + 1): dp[i][0] = 1 for d in range(1, k + 1): for i in range(n): dp[d][i + 1] = (dp[d][i] + dp[d - 1][n - i - 1]) % 1000000007 return dp[k - 1][n] for _ in range(int(input())): n, k = map(int, input().split()) print(ss(n, k))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER 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
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
def fog(): s = list(map(int, input().split())) n = s[0] k = s[1] lst = [] sm = 0 for i in range(n - 1): lst.append(1) if k == 1: print(1) elif n == 1: print(2) elif k == 2: print(n + 1) elif n == 2: print(k + 1) else: for i in range(k - 2): lst1 = [sum(lst)] for i in range(n - 2): lst1.append((lst1[i] - lst[-i - 1]) % (10**9 + 7)) sm += sum(lst1) % (10**9 + 7) sm = sm % (10**9 + 7) lst = lst1 print(sm + 1 + n) t = int(input()) while t > 0: t -= 1 fog()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
MOD = int(1000000000.0 + 7) def solve(k, n): global dp for i in range(1, k + 1): for j in range(1, n + 1): dp[i][j] = (dp[i][j - 1] + dp[i - 1][n - j]) % MOD return dp[k][n] for _ in range(int(input())): n_, k_ = map(int, input().split()) dp = [[(0) for i in range(n_ + 1)] for j in range(k_ + 1)] for i in range(k_ + 1): dp[i][0] = 1 dp[0][0] = 0 print(solve(k_, n_))
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
import sys mod = int(1000000000.0 + 7) input = sys.stdin.readline def iter_solver(N, K): dp = [[[(-1) for _ in range(2)] for __ in range(K + 1)] for ___ in range(N + 1)] for i in range(1, N + 1): dp[i][1][0] = dp[i][1][1] = 1 for k in range(2, K + 1): for n in range(N, 0, -1): ans = 2 if n < N: ans += dp[n + 1][k][0] - 1 ans %= mod if n > 1: ans += dp[n - 1][k - 1][1] - 1 ans %= mod dp[n][k][0] = ans for n in range(1, N + 1): ans = 2 if n < N: ans += dp[n + 1][k - 1][0] - 1 ans %= mod if n > 1: ans += dp[n - 1][k][1] - 1 ans %= mod dp[n][k][1] = ans return dp[1][K][0] def solve(curr, k, dir): global dp if k == 1: return 1 if dp[curr][k][dir] != -1: return dp[curr][k][dir] ans = 2 if dir == 1: if curr < n: ans += solve(curr + 1, k, dir) - 1 ans %= mod if curr > 1: ans += solve(curr - 1, k - 1, 1 - dir) - 1 ans %= mod else: if curr > 1: ans += solve(curr - 1, k, dir) - 1 ans %= mod if curr < n: ans += solve(curr + 1, k - 1, 1 - dir) - 1 ans %= mod dp[curr][k][dir] = ans return ans t = int(input()) while t > 0: t -= 1 n, k = list(map(int, input().split())) print(iter_solver(n, k))
IMPORT ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR RETURN VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
MOD = 10**9 + 7 t = int(input()) for test in range(t): n, k = [int(i) for i in input().split()] if k == 1: print(1) elif k == 2: print(n + 1) elif n == 1: print(2) else: ans = n + 1 v = [(1) for i in range(n - 1)] for my_iter in range(k - 2): v_new = [(0) for i in range(n - 1)] v_new[-1] = v[0] for it in range(1, n - 1): v_new[-it - 1] = (v_new[-it] + v[it]) % MOD ans += sum(v_new) % MOD v = v_new print(ans % MOD)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
t = int(input()) for i in range(t): n, k = map(int, input().split()) sum = [] if k == 1: print(1) elif n == 1: print(2) elif k == 2: print(n + 1) else: for i in range(k): tmp = [0] * (n + 1) sum.append(tmp) for i in range(n + 1): sum[0][i] = 1 for i in range(1, k): for j in range(n + 1): if j == 0: sum[i][j] = 1 else: sum[i][j] = (sum[i][j - 1] + sum[i - 1][n - j]) % (10**9 + 7) print(sum[-1][-1])
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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Gaurang has grown up in a mystical universe. He is faced by $n$ consecutive 2D planes. He shoots a particle of decay age $k$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $k-1$. If a particle has decay age equal to $1$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $3$ (towards the right), the process is as follows: (here, $D(x)$ refers to a single particle with decay age $x$) the first plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the second plane produces a $D(2)$ to the left and lets $D(3)$ continue on to the right; the first plane lets $D(2)$ continue on to the left and produces a $D(1)$ to the right; the second plane lets $D(1)$ continue on to the right ($D(1)$ cannot produce any copies). In total, the final multiset $S$ of particles is $\{D(3), D(2), D(2), D(1)\}$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $S$, given $n$ and $k$. Since the size of the multiset can be very large, you have to output it modulo $10^9+7$. Note: Particles can go back and forth between the planes without colliding with each other. -----Input----- The first line of the input contains the number of test cases $t$ ($1 \le t \le 100$). Then, $t$ lines follow, each containing two integers $n$ and $k$ ($1 \le n, k \le 1000$). Additionally, the sum of $n$ over all test cases will not exceed $1000$, and the sum of $k$ over all test cases will not exceed $1000$. All test cases in one test are different. -----Output----- Output $t$ integers. The $i$-th of them should be equal to the answer to the $i$-th test case. -----Examples----- Input 4 2 3 2 2 3 1 1 3 Output 4 3 1 2 Input 3 1 1 1 500 500 250 Output 1 2 257950823 -----Note----- Let us explain the first example with four test cases. Test case 1: ($n = 2$, $k = 3$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($n = 2$, $k = 2$) is explained as follows: the first plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the second plane produces a $D(1)$ to the left and lets $D(2)$ continue on to the right; the first plane lets $D(1)$ continue on to the left ($D(1)$ cannot produce any copies). Total size of multiset obtained $\{D(1), D(1), D(2)\}$ is equal to three. Test case 3: ($n = 3$, $k = 1$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($n = 1$, $k = 3$) there is only one plane. The particle produces a new copy to the left. The multiset $\{D(2), D(3)\}$ is of size two.
from sys import stdin, stdout input = stdin.readline mod = 10**9 + 7 def f(l, r, k): assert l + r == n if r == 0: return 1 if k == 1: return 1 if dp[l][k]: return dp[l][k] ans = f(r, l, k - 1) + f(l + 1, r - 1, k) ans %= mod dp[l][k] = ans return ans t = int(input()) for _ in range(t): n, k = map(int, input().split()) dp = [[(0) for i in range(k + 1)] for j in range(n + 1)] print(f(0, n, k))
ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR