description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
class CodeforcesTask209ASolution: def __init__(self): self.result = "" self.n = 0 def read_input(self): self.n = int(input()) def process_task(self): ways = [0] * (self.n + 1) ways[0] = 1 ways[1] = 2 md = 1000000007 for x in range(2, self.n + 1): ways[x] = (1 + ways[x - 1] + ways[x - 2]) % md self.result = str(ways[self.n] - 1) def get_result(self): return self.result Solution = CodeforcesTask209ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
R = lambda: map(int, input().split()) n = int(input()) arr = [1, 2] + [0] * n for i in range(2, n): arr[i] = (arr[i - 1] + arr[i - 2]) % 1000000007 for i in range(n): arr[i] = (arr[i] + arr[i - 1]) % 1000000007 print(arr[n - 1])
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) a = [0] * n b = 0 a[0] = 1 if n > 1: a[1] = 2 for i in range(2, n): a[i] = (a[i - 2] + a[i - 1] + 1) % (10**9 + 7) if n > 1: b = a[n - 2] print((a[n - 1] + b) % (10**9 + 7))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) if n != 1: array = [(1) for k in range(n + 1)] array[1] = 1 array[2] = 2 sumi = 3 for k in range(3, n + 1): array[k] = array[k - 1] + array[k - 2] % int(1000000000.0 + 7) sumi += array[k] % int(1000000000.0 + 7) print(sumi % int(1000000000.0 + 7)) else: print(1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) mod = 1000000007 red, blue = 0, 0 for i in range(n): if i % 2 == 0: red += blue + 1 red %= mod else: blue += red + 1 blue %= mod print((red + blue) % mod)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) dp = [1] * n sb = 0 sr = 0 j = 0 ans = 0 while j < n: if j % 2 == 0: dp[j] += sb dp[j] = dp[j] % 1000000007 sr += dp[j] else: dp[j] += sr dp[j] = dp[j] % 1000000007 sb += dp[j] ans += dp[j] ans = ans % 1000000007 j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) a, b = 0, 0 for i in range(n): if i % 2 == 0: a += b + 1 else: b += a + 1 a, b = a % 1000000007, b % 1000000007 print((a + b) % 1000000007)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) mod = 10**9 + 7 if n >= 2: dp = [(0) for i in range(n)] dp[0], dp[1] = 1, 2 ans = 3 for i in range(2, n): dp[i] = (dp[i - 1] + dp[i - 2]) % mod ans = (ans + dp[i]) % mod print(ans) else: print(1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid. A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7). Input The first line contains a single integer n (1 ≀ n ≀ 106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: * pick the first marble; * pick the second marble; * pick the third marble; * pick the first and second marbles; * pick the second and third marbles; * pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
n = int(input()) if n == 1: print(1) elif n == 2: print(3) else: a = [1, 3] i = 2 while i < n: a.append((a[0] + a[1] + 2) % 1000000007) i += 1 a.pop(0) print(a[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. Example 1: Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Note: The n will be in the range [1, 1000].
class Solution: def minSteps(self, n): res = 0 p = 2 while n > 1: while n % p == 0: res += p n /= p p += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. Example 1: Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Note: The n will be in the range [1, 1000].
class Solution: def minSteps(self, n): if n == 1: return 0 return self.calcsteps(0, n) def isprime(self, n): if n == 2 or n == 3: return True if n % 2 == 0 or n % 3 == 0: return False i = 5 w = 2 while i * i <= n: if n % i == 0: return False i += w w = 6 - w return True def primes(self, n): primfac = [] d = 2 while d * d <= n: while n % d == 0: primfac.append(d) n //= d d += 1 if n > 1: primfac.append(n) return primfac def calcsteps(self, steps, divs): if self.isprime(divs): return divs + steps primes = self.primes(divs) return steps + sum(primes)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. Example 1: Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Note: The n will be in the range [1, 1000].
class Solution: def minSteps(self, n): f = 2 s = 0 while f * f <= n: while n % f == 0: s += f n = int(n / f) f += 1 if n > 1: s += n return int(s)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. Example 1: Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Note: The n will be in the range [1, 1000].
class Solution: def minSteps(self, n): def factors(n): d = 2 while d * d <= n: while n % d == 0: n /= d yield d d += 1 if n > 1: yield n return int(sum(factors(n)))
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR VAR VAR NUMBER IF VAR NUMBER EXPR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. Example 1: Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Note: The n will be in the range [1, 1000].
class Solution: def minSteps(self, n): m = n sum = 0 tmp = 2 if m == 1: return 0 if m == 2: return 2 else: while m > tmp: k = m % tmp if k == 0: m = m / tmp sum += tmp else: tmp = tmp + 1 sum = sum + tmp return sum
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. Example 1: Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Note: The n will be in the range [1, 1000].
class Solution: def minSteps(self, n): d = 2 ans = 0 while n > 1: while n % d == 0: ans += d n = n / d d = d + 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. Example 1: Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Note: The n will be in the range [1, 1000].
class Solution: def minSteps(self, n): if n == 1: return 0 isPrime = [(True) for _ in range(n + 1)] isPrime[1] = False prime = [] for i in range(2, n + 1): if isPrime[i] == True: prime.append(i) k = 1 while (k + 1) * i <= n: isPrime[(k + 1) * i] = False k += 1 if isPrime[n]: return n cnt, i = 0, 0 while n > 1: while n % prime[i] == 0: cnt += prime[i] n = n // prime[i] if isPrime[n]: return cnt + n i += 1 return cnt
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR VAR NUMBER RETURN VAR
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. Example 1: Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Note: The n will be in the range [1, 1000].
class Solution: def minSteps(self, n): primeFactors = [] for i in range(2, int(n**0.5) + 1): while n % i == 0: primeFactors.append(i) n = n // i if n > 1: primeFactors.append(n) return sum(primeFactors)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial copy is not allowed). Paste: You can paste the characters which are copied last time. Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'. Example 1: Input: 3 Output: 3 Explanation: Intitally, we have one character 'A'. In step 1, we use Copy All operation. In step 2, we use Paste operation to get 'AA'. In step 3, we use Paste operation to get 'AAA'. Note: The n will be in the range [1, 1000].
class Solution: def minSteps(self, n): primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] ret = 0 for p in primes: while not n % p: n /= p ret += p return int(ret + n * (n > 1))
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): if N == 1: return 0 else: result = 0 for i in range(2, N + 1): if i % 2 == 0: result = (3 * result + 3) % (10**9 + 7) else: result = (3 * result - 3) % (10**9 + 7) return result % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): mod = 10**9 + 7 if N == 1: return 0 a, b = 1, 0 for i in range(2, N): b, a = a, (2 * a + 3 * b) % mod return 3 * a % mod
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): dp = [(0) for i in range(N + 1)] prev = pres = 0 for i in range(2, N + 1): if i % 2 == 0: pres = (prev * 3 + 3) % (10**9 + 7) else: pres = prev * 3 - 3 % (10**9 + 7) prev = pres return pres % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, n): if n < 2: return 0 m = 10**9 m += 7 t = 0 i = 2 while i <= n: t = t * 3 % m if i % 2: t -= 3 else: t = (t + 3) % m i += 1 return t
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
MODULO = 10**9 + 7 class Solution: def countPaths(self, N): dp = [0] * max(N + 1, 3) dp[0] = 1 dp[1] = 0 dp[2] = 3 pow3 = 3 for i in range(3, N + 1): pow3 *= 3 pow3 %= MODULO dp[i] = (pow3 - dp[i - 1]) % MODULO return dp[N]
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
MOD = 1000000007 class Solution: def countPaths(self, N): if N == 1: return 0 res = 0 for i in range(2, N + 1): if i & 1: res = (res * 3 - 3) % MOD else: res = (res * 3 + 3) % MOD return res % MOD
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR RETURN BIN_OP VAR VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): if N == 1: return 0 mod = 10**9 + 7 t1 = 1 t2 = 0 for i in range(2, N): t2, t1 = t1, (2 * t1 + 3 * t2) % mod ans = 3 * t1 % mod return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): if N < 2: return 0 dp = [(0) for _ in range(N + 1)] dp[2] = 3 mod = 1000000007 num = 3 for i in range(3, N + 1): num = num * 3 % mod dp[i] = (num - dp[i - 1] + mod) % mod return dp[N] % mod
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): M = 1000000000.0 + 7 if N <= 1: return 0 prev0 = 2 prev1 = 3 for i in range(3, N + 1): temp1 = 3 * prev0 % M temp0 = (2 * prev0 + prev1) % M prev0 = temp0 prev1 = temp1 return int(prev1)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): bigNum = 10**9 + 7 if N <= 1: return 0 if N == 2: return 3 ans = 0 prev = 3 for k in range(2, N): if k % 2 == 0: ans = 3 * prev - 3 else: ans = 3 * prev + 3 prev = ans % bigNum return prev
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): a = 1 M = 1000000000 + 7 for i in range(N): if i % 2 == 0: a = a * 3 - 3 else: a = a * 3 + 3 a = a % M return a
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): ans = [] if N == 0 or N == 1: return 0 ans.append(0) ans.append(3) for i in range(3, N + 1): ans.append((ans[-1] * 2 + ans[-2] * 3) % 1000000007) return ans[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): mod = 1000000007 low, high = 1, 0 tlow, thigh = 1, 0 for i in range(2, N + 1): high = 3 * tlow % mod low = (2 * tlow % mod + thigh) % mod tlow = low thigh = high return high
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): if N < 2: return 0 res = 3 k = 3 for temp in range(1, N - 1): if temp % 2 == 0: res = (res * 3 + k) % (10**9 + 7) else: res = (res * 3 - k) % (10**9 + 7) return res
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, n): add = True ans = 0 for i in range(n - 1): if add: ans = ans * 3 + 3 else: ans = ans * 3 - 3 add = not add ans = ans % 1000000007 return ans % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): a, b = 0, 3 if N == 1: return 0 for i in range(N - 2): if i & 1: b = (b * 3 + 3) % 1000000007 else: b = (b * 3 - 3) % 1000000007 return b
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): if N == 1: return 0 ans = 0 for i in range(2, N + 1): if i % 2 == 0: ans = 3 * (ans + 1) else: ans = 3 * (ans - 1) ans = ans % (1000000000.0 + 7) return int(ans)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER RETURN FUNC_CALL VAR VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): mod = 10**9 + 7 if N == 1: return 0 if N == 2: return 3 dp = [0] * (N + 1) dp[1] = 1 dp[2] = 3 cur = 3 for i in range(3, N + 1): cur = cur * 3 % mod dp[i] = (cur - dp[i - 1]) % mod return dp[i]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths_v1(self, N): prevo = 1 prevA = 0 prevB = 0 prevC = 0 curro = 0 currA = 0 currB = 0 currC = 0 modv = 10**9 + 7 for i in range(1, N + 1): curro = (prevA + prevB + prevC) % modv currA = (prevo + prevB + prevC) % modv currB = (prevo + prevA + prevC) % modv currC = (prevo + prevA + prevB) % modv prevo = curro prevA = currA prevB = currB prevC = currC return prevo def countPaths(self, N): if N == 1: return 0 ans = 0 for i in range(2, N + 1): if i % 2 == 0: ans = 3 * (ans + 1) else: ans = 3 * (ans - 1) ans = ans % (1000000000.0 + 7) return int(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER NUMBER RETURN FUNC_CALL VAR VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, N): if N == 1: return 0 val, curr, mod = 0, 2, 10**9 + 7 while curr <= N: if curr % 2 == 0: val = (val + 1) * 3 % mod else: val = (val - 1) * 3 % mod curr += 1 return val
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR
Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. Example 1: Input: N = 1 Output: 0 Explanation: The minimum length of a cyclic path is 2. Example 2: Input: N = 2 Output: 3 Explanation: The three paths are : O-A-O, O-B-O, O-C-O Your Task: You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{6}
class Solution: def countPaths(self, n): mod = int(1000000000.0 + 7) pre0 = 0 pre1 = 1 for i in range(1, n): dp1 = 3 * pre1 % mod dp2 = (2 * pre1 + pre0) % mod pre0 = dp1 pre1 = dp2 return pre0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There is a 2D matrix of N rows and M columns. Rows are number 1 to N from top to bottom and columns 1 to M from left to right. You are standing at (1,1). From, A [ i ] [ j ] you can move to A [ i + 1 ] [ j ] if A [ i + 1 ] [ j ] > A [ i ] [ j ]. Or, from, A [ i ] [ j ] you can move to A [ i ] [ j + 1 ] if A [ i ] [ j + 1 ] > A [ i ] [ j ]. Moving from (1,1), what is the longest path that you can travel? Input: First line contains, T, the number of testcases. Each testcase consists of N, M. Each of the next N lines contain M integers each. Output: For each testcase, print the length of the longest path from (1,1). Constraints: 1 ≀ T ≀ 100 1 ≀ N, M ≀ 100 1 ≀ A[i][j] ≀ 10^6 SAMPLE INPUT 3 1 1 1 4 4 1 2 3 4 2 2 3 4 3 2 3 4 4 5 6 7 2 2 1 2 3 4 SAMPLE OUTPUT 1 7 3 Explanation In first testcase the path is of 1 length only i.e. (1,1). In second testcase, the path of length 7 is (1,1) , (2,1), (3,1), (4,1), (4,2), (4,3), (4,4). In third case, any of the following paths can be taken. (1,1), (1,2), (2,2) or (1,1), (1,2), (2,2). Both are of length 3.
def longestPath(matrix): path_matrix = [] temp = [] for i in range(len(matrix)): for j in range(len(matrix[0])): temp.append(0) path_matrix.append(temp) temp = [] for i in range(len(matrix)): for j in range(len(matrix[0])): if i == 0 and j == 0: path_matrix[i][j] = 1 elif i == 0: if matrix[i][j] > matrix[i][j - 1] and path_matrix[i][j - 1] != 0: path_matrix[i][j] = path_matrix[i][j - 1] + 1 elif j == 0: if matrix[i][j] > matrix[i - 1][j] and path_matrix[i - 1][j] != 0: path_matrix[i][j] = path_matrix[i - 1][j] + 1 elif ( matrix[i - 1][j] < matrix[i][j] and matrix[i][j - 1] < matrix[i][j] and (path_matrix[i - 1][j] != 0 or path_matrix[i][j - 1] != 0) ): path_matrix[i][j] = ( max(path_matrix[i - 1][j], path_matrix[i][j - 1]) + 1 ) elif matrix[i][j - 1] < matrix[i][j] and path_matrix[i][j - 1] != 0: path_matrix[i][j] = path_matrix[i][j - 1] + 1 elif matrix[i - 1][j] < matrix[i][j] and path_matrix[i - 1][j] != 0: path_matrix[i][j] = path_matrix[i - 1][j] + 1 max_val = -1 for row in path_matrix: max_val = max(max_val, max(row)) return max_val t = eval(input()) for i in range(t): s = input().split() n = int(s[0]) m = int(s[1]) matrix = [] tmp = [] for i in range(n): temp = input().split() for x in temp: tmp.append(int(x)) matrix.append(tmp) tmp = [] print(longestPath(matrix))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There is a 2D matrix of N rows and M columns. Rows are number 1 to N from top to bottom and columns 1 to M from left to right. You are standing at (1,1). From, A [ i ] [ j ] you can move to A [ i + 1 ] [ j ] if A [ i + 1 ] [ j ] > A [ i ] [ j ]. Or, from, A [ i ] [ j ] you can move to A [ i ] [ j + 1 ] if A [ i ] [ j + 1 ] > A [ i ] [ j ]. Moving from (1,1), what is the longest path that you can travel? Input: First line contains, T, the number of testcases. Each testcase consists of N, M. Each of the next N lines contain M integers each. Output: For each testcase, print the length of the longest path from (1,1). Constraints: 1 ≀ T ≀ 100 1 ≀ N, M ≀ 100 1 ≀ A[i][j] ≀ 10^6 SAMPLE INPUT 3 1 1 1 4 4 1 2 3 4 2 2 3 4 3 2 3 4 4 5 6 7 2 2 1 2 3 4 SAMPLE OUTPUT 1 7 3 Explanation In first testcase the path is of 1 length only i.e. (1,1). In second testcase, the path of length 7 is (1,1) , (2,1), (3,1), (4,1), (4,2), (4,3), (4,4). In third case, any of the following paths can be taken. (1,1), (1,2), (2,2) or (1,1), (1,2), (2,2). Both are of length 3.
def adjList(i, j, N, M): l = [] if i + 1 < N and A[i + 1][j] > A[i][j]: l.append((i + 1, j)) if j + 1 < M and A[i][j + 1] > A[i][j]: l.append((i, j + 1)) return l def dfs(li, curlen): global length, A, visit, N, M if length < curlen: length = curlen l = adjList(li[0], li[1], N, M) for i in range(len(l)): if visit[l[i][0]][l[i][1]] != 1: visit[l[i][0]][l[i][1]] = 1 dfs(l[i], curlen + 1) for _ in range(eval(input())): temp = list(map(int, input().split())) N, M = temp A = [] visit = [] for _ in range(N): temp = list(map(int, input().split())) visit.append([0] * M) A.append(temp) visit[0][0] = 1 for i in range(1, N): if A[i - 1][0] < A[i][0]: visit[i][0] = visit[i - 1][0] + 1 for j in range(1, M): if A[0][j - 1] < A[0][j]: visit[0][j] = visit[0][j - 1] + 1 for i in range(1, N): for j in range(1, M): if A[i][j] > A[i - 1][j] and visit[i][j] < visit[i - 1][j] + 1: visit[i][j] = visit[i - 1][j] + 1 if A[i][j] > A[i][j - 1] and visit[i][j] < visit[i][j - 1] + 1: visit[i][j] = visit[i][j - 1] + 1 r = 0 for i in range(0, N): for j in range(0, M): if visit[i][j] == i + j + 1: r = max(visit[i][j], r) if r == 0: r = r + 1 print(r)
FUNC_DEF ASSIGN VAR LIST IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There is a 2D matrix of N rows and M columns. Rows are number 1 to N from top to bottom and columns 1 to M from left to right. You are standing at (1,1). From, A [ i ] [ j ] you can move to A [ i + 1 ] [ j ] if A [ i + 1 ] [ j ] > A [ i ] [ j ]. Or, from, A [ i ] [ j ] you can move to A [ i ] [ j + 1 ] if A [ i ] [ j + 1 ] > A [ i ] [ j ]. Moving from (1,1), what is the longest path that you can travel? Input: First line contains, T, the number of testcases. Each testcase consists of N, M. Each of the next N lines contain M integers each. Output: For each testcase, print the length of the longest path from (1,1). Constraints: 1 ≀ T ≀ 100 1 ≀ N, M ≀ 100 1 ≀ A[i][j] ≀ 10^6 SAMPLE INPUT 3 1 1 1 4 4 1 2 3 4 2 2 3 4 3 2 3 4 4 5 6 7 2 2 1 2 3 4 SAMPLE OUTPUT 1 7 3 Explanation In first testcase the path is of 1 length only i.e. (1,1). In second testcase, the path of length 7 is (1,1) , (2,1), (3,1), (4,1), (4,2), (4,3), (4,4). In third case, any of the following paths can be taken. (1,1), (1,2), (2,2) or (1,1), (1,2), (2,2). Both are of length 3.
tc = eval(input()) while tc > 0: n, m = list(map(int, input().split())) ar = [0] * n for i in range(n): ar[i] = list(map(int, input().split())) c = [(0) for i in range(m)] ml = 0 for i in range(0, n): for j in range(0, m): idx = j if j > 0 and c[j - 1] != 0 and ar[i][idx - 1] < ar[i][idx]: l = c[j - 1] + 1 else: l = 0 if i > 0 and c[j] != 0 and ar[i - 1][idx] < ar[i][idx]: r = c[j] + 1 else: r = 0 if i == 0 and j == 0: c[j] = 1 else: c[j] = max(l, r) ml = max(ml, max(c)) if sum(c) == 0: break print(ml) tc -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
There is a 2D matrix of N rows and M columns. Rows are number 1 to N from top to bottom and columns 1 to M from left to right. You are standing at (1,1). From, A [ i ] [ j ] you can move to A [ i + 1 ] [ j ] if A [ i + 1 ] [ j ] > A [ i ] [ j ]. Or, from, A [ i ] [ j ] you can move to A [ i ] [ j + 1 ] if A [ i ] [ j + 1 ] > A [ i ] [ j ]. Moving from (1,1), what is the longest path that you can travel? Input: First line contains, T, the number of testcases. Each testcase consists of N, M. Each of the next N lines contain M integers each. Output: For each testcase, print the length of the longest path from (1,1). Constraints: 1 ≀ T ≀ 100 1 ≀ N, M ≀ 100 1 ≀ A[i][j] ≀ 10^6 SAMPLE INPUT 3 1 1 1 4 4 1 2 3 4 2 2 3 4 3 2 3 4 4 5 6 7 2 2 1 2 3 4 SAMPLE OUTPUT 1 7 3 Explanation In first testcase the path is of 1 length only i.e. (1,1). In second testcase, the path of length 7 is (1,1) , (2,1), (3,1), (4,1), (4,2), (4,3), (4,4). In third case, any of the following paths can be taken. (1,1), (1,2), (2,2) or (1,1), (1,2), (2,2). Both are of length 3.
def process(mat, n, m): sol = [[(1) for j in range(m)] for i in range(n)] for i in reversed(list(range(n - 1))): if mat[i + 1][m - 1] > mat[i][m - 1]: sol[i][m - 1] = sol[i + 1][m - 1] + 1 for j in reversed(list(range(m - 1))): if mat[n - 1][j + 1] > mat[n - 1][j]: sol[n - 1][j] = sol[n - 1][j + 1] + 1 for i in reversed(list(range(n - 1))): for j in reversed(list(range(m - 1))): if mat[i + 1][j] > mat[i][j]: sol[i][j] = sol[i + 1][j] + 1 if mat[i][j + 1] > mat[i][j]: sol[i][j] = max(sol[i][j + 1] + 1, sol[i][j]) return sol[0][0] T = int(input()) for i in range(T): H = [int(x) for x in input().split()] N = H[0] M = H[1] mat = [[] for i in range(N)] for i in range(N): mat[i] = [int(x) for x in input().split()] print(process(mat, N, M))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR RETURN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR 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 VAR
There is a 2D matrix of N rows and M columns. Rows are number 1 to N from top to bottom and columns 1 to M from left to right. You are standing at (1,1). From, A [ i ] [ j ] you can move to A [ i + 1 ] [ j ] if A [ i + 1 ] [ j ] > A [ i ] [ j ]. Or, from, A [ i ] [ j ] you can move to A [ i ] [ j + 1 ] if A [ i ] [ j + 1 ] > A [ i ] [ j ]. Moving from (1,1), what is the longest path that you can travel? Input: First line contains, T, the number of testcases. Each testcase consists of N, M. Each of the next N lines contain M integers each. Output: For each testcase, print the length of the longest path from (1,1). Constraints: 1 ≀ T ≀ 100 1 ≀ N, M ≀ 100 1 ≀ A[i][j] ≀ 10^6 SAMPLE INPUT 3 1 1 1 4 4 1 2 3 4 2 2 3 4 3 2 3 4 4 5 6 7 2 2 1 2 3 4 SAMPLE OUTPUT 1 7 3 Explanation In first testcase the path is of 1 length only i.e. (1,1). In second testcase, the path of length 7 is (1,1) , (2,1), (3,1), (4,1), (4,2), (4,3), (4,4). In third case, any of the following paths can be taken. (1,1), (1,2), (2,2) or (1,1), (1,2), (2,2). Both are of length 3.
test_cases = eval(input()) for i in range(test_cases): n, m = list(map(int, input().split())) a = [] for i in range(n): a.append(list(map(int, input().split()))) lis = [[(1) for i in range(m)] for j in range(n)] lis[n - 1][m - 1] = 1 for i in range(n - 1): if a[n - i - 1][m - 1] >= a[n - i - 2][m - 1]: lis[n - i - 2][m - 1] = lis[n - i - 1][m - 1] + 1 for j in range(m - 1): if a[n - 1][m - j - 1] >= a[n - 1][m - j - 2]: lis[n - 1][m - j - 2] = lis[n - 1][m - j - 1] + 1 for i in range(n - 2, -1, -1): for j in range(m - 2, -1, -1): if a[i][j] < a[i + 1][j] and a[i][j] >= a[i][j + 1]: lis[i][j] = lis[i + 1][j] + 1 elif a[i][j] >= a[i + 1][j] and a[i][j] < a[i][j + 1]: lis[i][j] = lis[i][j + 1] + 1 elif a[i][j] < a[i + 1][j] and a[i][j] < a[i][j + 1]: lis[i][j] = max(lis[i + 1][j], lis[i][j + 1]) + 1 maxi = lis[0][0] print(maxi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
There is a 2D matrix of N rows and M columns. Rows are number 1 to N from top to bottom and columns 1 to M from left to right. You are standing at (1,1). From, A [ i ] [ j ] you can move to A [ i + 1 ] [ j ] if A [ i + 1 ] [ j ] > A [ i ] [ j ]. Or, from, A [ i ] [ j ] you can move to A [ i ] [ j + 1 ] if A [ i ] [ j + 1 ] > A [ i ] [ j ]. Moving from (1,1), what is the longest path that you can travel? Input: First line contains, T, the number of testcases. Each testcase consists of N, M. Each of the next N lines contain M integers each. Output: For each testcase, print the length of the longest path from (1,1). Constraints: 1 ≀ T ≀ 100 1 ≀ N, M ≀ 100 1 ≀ A[i][j] ≀ 10^6 SAMPLE INPUT 3 1 1 1 4 4 1 2 3 4 2 2 3 4 3 2 3 4 4 5 6 7 2 2 1 2 3 4 SAMPLE OUTPUT 1 7 3 Explanation In first testcase the path is of 1 length only i.e. (1,1). In second testcase, the path of length 7 is (1,1) , (2,1), (3,1), (4,1), (4,2), (4,3), (4,4). In third case, any of the following paths can be taken. (1,1), (1,2), (2,2) or (1,1), (1,2), (2,2). Both are of length 3.
for _ in range(int(input())): N, M = list(map(int, input().strip().split())) arr = [[(0) for i in range(M + 1)] for j in range(N + 1)] dp = [[(0) for i in range(M)] for j in range(N)] for i in range(N): arr[i][:M] = list(map(int, input().strip().split())) for i in range(N - 1, -1, -1): for j in range(M - 1, -1, -1): if arr[i][j] < arr[i][j + 1] and arr[i][j] < arr[i + 1][j]: dp[i][j] = max(dp[i][j + 1], dp[i + 1][j]) + 1 elif arr[i][j] < arr[i][j + 1]: dp[i][j] = dp[i][j + 1] + 1 elif arr[i][j] < arr[i + 1][j]: dp[i][j] = dp[i + 1][j] + 1 else: dp[i][j] = 1 print(dp[0][0])
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 FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Reziba has many magic gems. Each magic gem can be split into $M$ normal gems. The amount of space each magic (and normal) gem takes is $1$ unit. A normal gem cannot be split. Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $N$ units. If a magic gem is chosen and split, it takes $M$ units of space (since it is split into $M$ gems); if a magic gem is not split, it takes $1$ unit. How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $N$ units? Print the answer modulo $1000000007$ ($10^9+7$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ. -----Input----- The input contains a single line consisting of $2$ integers $N$ and $M$ ($1 \le N \le 10^{18}$, $2 \le M \le 100$). -----Output----- Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $N$ units. Print the answer modulo $1000000007$ ($10^9+7$). -----Examples----- Input 4 2 Output 5 Input 3 2 Output 3 -----Note----- In the first example each magic gem can split into $2$ normal gems, and we know that the total amount of gems are $4$. Let $1$ denote a magic gem, and $0$ denote a normal gem. The total configurations you can have is: $1 1 1 1$ (None of the gems split); $0 0 1 1$ (First magic gem splits into $2$ normal gems); $1 0 0 1$ (Second magic gem splits into $2$ normal gems); $1 1 0 0$ (Third magic gem splits into $2$ normal gems); $0 0 0 0$ (First and second magic gems split into total $4$ normal gems). Hence, answer is $5$.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") def mat_mul_mod(A, B, mod): n, p = len(A), len(B[0]) fmod = float(mod) float_prec = float(1 << 51) B = [[((Bij & (1 << 16) - 1) - 1.0j * (Bij >> 16)) for Bij in Bi] for Bi in B] C = [([0] * p) for _ in range(n)] for i, Ai in enumerate(A): row = [0.0] * p for j, Bj in enumerate(B): Aij = Ai[j] + 1.0j * (Ai[j] * 65536.0 % mod) for k, Bjk in enumerate(Bj): row[k] += (Aij * Bjk).real if row[k] > float_prec: row[k] %= fmod C[i] = [int(r % fmod) for r in row] return C def main(): MOD = 10**9 + 7 n, m = map(int, input().split()) mat = [([0] * m) for _ in range(m)] mat[0][0] = 1 mat[0][-1] = 1 for i in range(m - 1): mat[i + 1][i] = 1 vec = [1] * m for i in bin(n)[:1:-1]: if i == "1": vec = [(sum(a * b for a, b in zip(row, vec)) % MOD) for row in mat] mat = mat_mul_mod(mat, mat, MOD) print(int(vec[-1])) main()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
Reziba has many magic gems. Each magic gem can be split into $M$ normal gems. The amount of space each magic (and normal) gem takes is $1$ unit. A normal gem cannot be split. Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $N$ units. If a magic gem is chosen and split, it takes $M$ units of space (since it is split into $M$ gems); if a magic gem is not split, it takes $1$ unit. How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $N$ units? Print the answer modulo $1000000007$ ($10^9+7$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ. -----Input----- The input contains a single line consisting of $2$ integers $N$ and $M$ ($1 \le N \le 10^{18}$, $2 \le M \le 100$). -----Output----- Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $N$ units. Print the answer modulo $1000000007$ ($10^9+7$). -----Examples----- Input 4 2 Output 5 Input 3 2 Output 3 -----Note----- In the first example each magic gem can split into $2$ normal gems, and we know that the total amount of gems are $4$. Let $1$ denote a magic gem, and $0$ denote a normal gem. The total configurations you can have is: $1 1 1 1$ (None of the gems split); $0 0 1 1$ (First magic gem splits into $2$ normal gems); $1 0 0 1$ (Second magic gem splits into $2$ normal gems); $1 1 0 0$ (Third magic gem splits into $2$ normal gems); $0 0 0 0$ (First and second magic gems split into total $4$ normal gems). Hence, answer is $5$.
import sys MOD = 10**9 + 7 double_prec = float(2**51) MODF = float(MOD) def mult(A, B): n = len(A) m = len(B[0]) C = [([0] * n) for _ in range(m)] A = [[((Aij & 2**16 - 1) - 1.0j * (Aij >> 16)) for Aij in A_j] for A_j in A] for j, B_j in enumerate(B): buffer = [0.0] * n for k, A_k in enumerate(A): Bkj = B_j[k] + 1.0j * ((B_j[k] << 16) % MOD) for i, Aik in enumerate(A_k): buffer[i] += (Aik * Bkj).real C_j = C[j] for i, b in enumerate(buffer): C_j[i] = int(b % MODF) return C def vecmult(A, B): n = len(A) C = [0] * n for k, A_k in enumerate(A): Bk = B[k] for i, Aik in enumerate(A_k): C[i] += Aik * Bk for i in range(n): C[i] %= MOD return C n, m = [int(x) for x in input().split()] mapper = [([0] * m) for _ in range(m)] mapper[0][0] = 1 mapper[-1][0] = 1 for i in range(m - 1): mapper[i][i + 1] = 1 def power(A, B, m): n = len(A) if m == 0: return B while m > 16: if m % 2 == 1: B = vecmult(A, B) A = mult(A, A) m //= 2 while m > 0: B = vecmult(A, B) m -= 1 return B vec = [1] * m vec = power(mapper, vec, n) print(vec[-1])
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Reziba has many magic gems. Each magic gem can be split into $M$ normal gems. The amount of space each magic (and normal) gem takes is $1$ unit. A normal gem cannot be split. Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $N$ units. If a magic gem is chosen and split, it takes $M$ units of space (since it is split into $M$ gems); if a magic gem is not split, it takes $1$ unit. How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $N$ units? Print the answer modulo $1000000007$ ($10^9+7$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ. -----Input----- The input contains a single line consisting of $2$ integers $N$ and $M$ ($1 \le N \le 10^{18}$, $2 \le M \le 100$). -----Output----- Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $N$ units. Print the answer modulo $1000000007$ ($10^9+7$). -----Examples----- Input 4 2 Output 5 Input 3 2 Output 3 -----Note----- In the first example each magic gem can split into $2$ normal gems, and we know that the total amount of gems are $4$. Let $1$ denote a magic gem, and $0$ denote a normal gem. The total configurations you can have is: $1 1 1 1$ (None of the gems split); $0 0 1 1$ (First magic gem splits into $2$ normal gems); $1 0 0 1$ (Second magic gem splits into $2$ normal gems); $1 1 0 0$ (Third magic gem splits into $2$ normal gems); $0 0 0 0$ (First and second magic gems split into total $4$ normal gems). Hence, answer is $5$.
import sys MOD = 10**9 + 7 def mult(A, B): n = len(A) C = [] for x in range(n): c = [0] * n for z in range(n): b = B[x][z] a = A[z] for y in range(n): c[y] += a[y] * b C.append(c) for x in range(n): c = C[x] for y in range(n): c[y] %= MOD return C def vecmult(A, B): n = len(A) C = [0] * n for z in range(n): b = B[z] a = A[z] for y in range(n): C[y] += a[y] * b return [(c % MOD) for c in C] n, m = [int(x) for x in input().split()] mapper = [([0] * m) for _ in range(m)] mapper[0][0] = 1 mapper[-1][0] = 1 for i in range(m - 1): mapper[i][i + 1] = 1 def power(A, B, m): n = len(A) if m == 0: return B while m > 1: if m % 2 == 1: B = vecmult(A, B) A = mult(A, A) m //= 2 return vecmult(A, B) vec = [1] * m vec = power(mapper, vec, n) print(vec[-1] % MOD)
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Reziba has many magic gems. Each magic gem can be split into $M$ normal gems. The amount of space each magic (and normal) gem takes is $1$ unit. A normal gem cannot be split. Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $N$ units. If a magic gem is chosen and split, it takes $M$ units of space (since it is split into $M$ gems); if a magic gem is not split, it takes $1$ unit. How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $N$ units? Print the answer modulo $1000000007$ ($10^9+7$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ. -----Input----- The input contains a single line consisting of $2$ integers $N$ and $M$ ($1 \le N \le 10^{18}$, $2 \le M \le 100$). -----Output----- Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $N$ units. Print the answer modulo $1000000007$ ($10^9+7$). -----Examples----- Input 4 2 Output 5 Input 3 2 Output 3 -----Note----- In the first example each magic gem can split into $2$ normal gems, and we know that the total amount of gems are $4$. Let $1$ denote a magic gem, and $0$ denote a normal gem. The total configurations you can have is: $1 1 1 1$ (None of the gems split); $0 0 1 1$ (First magic gem splits into $2$ normal gems); $1 0 0 1$ (Second magic gem splits into $2$ normal gems); $1 1 0 0$ (Third magic gem splits into $2$ normal gems); $0 0 0 0$ (First and second magic gems split into total $4$ normal gems). Hence, answer is $5$.
import sys MOD = 10**9 + 7 MODF = 1.0 * MOD MODF_inv = 1.0 / MODF SHRT = 1.0 * (1 << 16) SHRT_inv = 1.0 / SHRT MAGIC = float(2**52 + 2**51) fround = lambda x: x + MAGIC - MAGIC modder = lambda a: a - MODF * fround(a * MODF_inv) mod_prod = lambda a, b, c=0.0: modder( (b - SHRT * fround(SHRT_inv * b)) * a + c + modder(a * SHRT) * fround(b * SHRT_inv) ) def mult(A, B): n, m = len(A[0]), len(B) C = [([0.0] * n) for _ in range(m)] for j, B_j in enumerate(B): C_j = C[j] for k, A_k in enumerate(A): Bkj = B_j[k] for i, Aik in enumerate(A_k): C_j[i] = mod_prod(Aik, Bkj, C_j[i]) return C def power(A, B, n, mult_func): if n == 0: return B while n > 1: if n % 2 == 1: B = mult_func(A, B) A = mult_func(A, A) n //= 2 return mult_func(A, B) n, m = [int(x) for x in input().split()] mat = [([0.0] * m) for _ in range(m)] mat[0][0] = 1.0 mat[-1][0] = 1.0 for i in range(m - 1): mat[i][i + 1] = 1.0 vec = [[1.0] * m] vec = power(mat, vec, n, mult) print(int(vec[0][-1]) % MOD)
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR
Reziba has many magic gems. Each magic gem can be split into $M$ normal gems. The amount of space each magic (and normal) gem takes is $1$ unit. A normal gem cannot be split. Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $N$ units. If a magic gem is chosen and split, it takes $M$ units of space (since it is split into $M$ gems); if a magic gem is not split, it takes $1$ unit. How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $N$ units? Print the answer modulo $1000000007$ ($10^9+7$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ. -----Input----- The input contains a single line consisting of $2$ integers $N$ and $M$ ($1 \le N \le 10^{18}$, $2 \le M \le 100$). -----Output----- Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $N$ units. Print the answer modulo $1000000007$ ($10^9+7$). -----Examples----- Input 4 2 Output 5 Input 3 2 Output 3 -----Note----- In the first example each magic gem can split into $2$ normal gems, and we know that the total amount of gems are $4$. Let $1$ denote a magic gem, and $0$ denote a normal gem. The total configurations you can have is: $1 1 1 1$ (None of the gems split); $0 0 1 1$ (First magic gem splits into $2$ normal gems); $1 0 0 1$ (Second magic gem splits into $2$ normal gems); $1 1 0 0$ (Third magic gem splits into $2$ normal gems); $0 0 0 0$ (First and second magic gems split into total $4$ normal gems). Hence, answer is $5$.
import sys MOD = 10**9 + 7 def mult(A, B): n = len(A) cut = 2**16 cutmask = cut - 1 C = [([0.0] * n) for _ in range(n)] tmp1 = [[float(aa & cutmask) for aa in a] for a in A] tmp2 = [[float(aa >> 16) for aa in a] for a in A] for x in range(n): c = C[x] for z in range(n): b = float(B[x][z]) a = tmp1[z] for y in range(n): c[y] += a[y] * b b = float((B[x][z] << 16) % MOD) a = tmp2[z] for y in range(n): c[y] += a[y] * b for y in range(n): c[y] = int(c[y]) % MOD return C def vecmult(A, B): n = len(A) C = [0] * n for z in range(n): b = B[z] a = A[z] for y in range(n): C[y] += a[y] * b return [(c % MOD) for c in C] n, m = [int(x) for x in input().split()] mapper = [([0] * m) for _ in range(m)] mapper[0][0] = 1 mapper[-1][0] = 1 for i in range(m - 1): mapper[i][i + 1] = 1 def power(A, B, m): n = len(A) if m == 0: return B while m > 8: if m % 2 == 1: B = vecmult(A, B) A = mult(A, A) m //= 2 while m > 0: B = vecmult(A, B) m -= 1 return B vec = [1] * m vec = power(mapper, vec, n) print(vec[-1] % MOD)
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Reziba has many magic gems. Each magic gem can be split into $M$ normal gems. The amount of space each magic (and normal) gem takes is $1$ unit. A normal gem cannot be split. Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $N$ units. If a magic gem is chosen and split, it takes $M$ units of space (since it is split into $M$ gems); if a magic gem is not split, it takes $1$ unit. How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $N$ units? Print the answer modulo $1000000007$ ($10^9+7$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ. -----Input----- The input contains a single line consisting of $2$ integers $N$ and $M$ ($1 \le N \le 10^{18}$, $2 \le M \le 100$). -----Output----- Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $N$ units. Print the answer modulo $1000000007$ ($10^9+7$). -----Examples----- Input 4 2 Output 5 Input 3 2 Output 3 -----Note----- In the first example each magic gem can split into $2$ normal gems, and we know that the total amount of gems are $4$. Let $1$ denote a magic gem, and $0$ denote a normal gem. The total configurations you can have is: $1 1 1 1$ (None of the gems split); $0 0 1 1$ (First magic gem splits into $2$ normal gems); $1 0 0 1$ (Second magic gem splits into $2$ normal gems); $1 1 0 0$ (Third magic gem splits into $2$ normal gems); $0 0 0 0$ (First and second magic gems split into total $4$ normal gems). Hence, answer is $5$.
from sys import stdin def _solve(n, m, memo): if n <= 0: return 0 if n < m: return 1 if n == 1: return 2 if (n, m) in memo: return memo[n, m] out = 0 mid = n // 2 out = (out + _solve(mid, m, memo) * _solve(n - mid, m, memo)) % 1000000007 for i in range(1, m): if mid - i < 0 or n - mid + i - m < 0: continue n1 = 1 if mid - i == 0 else _solve(mid - i, m, memo) n2 = 1 if n - mid + i - m == 0 else _solve(n - mid + i - m, m, memo) out = (out + n1 * n2) % 1000000007 memo[n, m] = out return out def solve(n, m): return _solve(n, m, {}) def main(): from sys import stdin n, m = list(map(int, stdin.readline().strip().split())) out = solve(n, m) print("{}".format(out)) main()
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR DICT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Reziba has many magic gems. Each magic gem can be split into $M$ normal gems. The amount of space each magic (and normal) gem takes is $1$ unit. A normal gem cannot be split. Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $N$ units. If a magic gem is chosen and split, it takes $M$ units of space (since it is split into $M$ gems); if a magic gem is not split, it takes $1$ unit. How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $N$ units? Print the answer modulo $1000000007$ ($10^9+7$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ. -----Input----- The input contains a single line consisting of $2$ integers $N$ and $M$ ($1 \le N \le 10^{18}$, $2 \le M \le 100$). -----Output----- Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $N$ units. Print the answer modulo $1000000007$ ($10^9+7$). -----Examples----- Input 4 2 Output 5 Input 3 2 Output 3 -----Note----- In the first example each magic gem can split into $2$ normal gems, and we know that the total amount of gems are $4$. Let $1$ denote a magic gem, and $0$ denote a normal gem. The total configurations you can have is: $1 1 1 1$ (None of the gems split); $0 0 1 1$ (First magic gem splits into $2$ normal gems); $1 0 0 1$ (Second magic gem splits into $2$ normal gems); $1 1 0 0$ (Third magic gem splits into $2$ normal gems); $0 0 0 0$ (First and second magic gems split into total $4$ normal gems). Hence, answer is $5$.
import sys MOD = 10**9 + 7 def polymod(P, Q): assert Q[-1] == 1 n = len(Q) while len(P) >= n: p = P[-1] for i in range(n): P[-i - 1] -= p * Q[-i - 1] assert P[-1] == 0 P.pop() return P def polyprod(P, Q): n = len(P) m = len(Q) W = [0] * (n + m - 1) for i in range(n): for j in range(m): W[i + j] += P[i] * Q[j] return [(w % MOD) for w in W] def power(A, B, m, mult): if m == 0: return B while m > 1: if m % 2 == 1: B = mult(A, B) A = mult(A, A) m //= 2 return mult(A, B) def calc_nth_term(init, linear_coeff, n): def mult(A, B): return polymod(polyprod(A, B), linear_coeff) ans = power([0, 1], [1], n, mult) return sum(ans[i] * init[i] for i in range(len(ans))) n, m = [int(x) for x in input().split()] linear_rec = [0] * (m + 1) linear_rec[0] = -1 linear_rec[m - 1] = -1 linear_rec[m] = 1 print(calc_nth_term([1] * m, linear_rec, n) % MOD)
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST NUMBER NUMBER LIST NUMBER VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL 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 ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP LIST NUMBER VAR VAR VAR VAR
Given an array arr of size N, the task is to make strictly increasing and strictly decreasing subsequences from the array such that each array element belongs to increasing subsequence or decreasing subsequence, but not both, or can be part of none of the subsequence. Minimize the number of elements that are not part of any of the subsequences and find the count of such elements. Example 1: Input: N = 12, arr[] = {7, 8, 1, 2, 4, 6, 3, 5, 2, 1, 8, 7} Output: 2 Explanation: Increasing sequence can be : {1, 2, 4, 5, 8}. Decreasing sequence can be : {7, 6, 3, 2, 1}. So, only 2 (8, 7) elements are left which are not part of either of the subsequences. Example 2: Input: N = 7, arr[] = {1, 4, 2, 3, 3, 2, 4} Output: 0 Explanation: Increasing sequence can be : {1, 2, 3, 4}. Decreasing sequence can be : {4, 3, 2}. Your Task: You don't need to read input or print anything. Complete the function minCount() which takes N and array arr as input parameters and returns the integer value Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{3}) Constraints: 1 ≀ N ≀ 10^{2}
class Solution: def minCount(self, arr, n): dp = [ [[float("inf") for k in range(n + 1)] for j in range(n + 1)] for i in range(n + 1) ] val = self.mindp(arr, n, 1, float("-inf"), float("inf"), dp, 0, 0) return val def mindp(self, arr, n, st, incm, decmin, dp, incind, decind): if st == n + 1: return 0 if not dp[st][incind][decind] == float("inf"): return dp[st][incind][decind] val = 1 + self.mindp(arr, n, st + 1, incm, decmin, dp, incind, decind) if incm < arr[st - 1]: val = min( val, self.mindp(arr, n, st + 1, arr[st - 1], decmin, dp, st, decind) ) if decmin > arr[st - 1]: val = min( val, self.mindp(arr, n, st + 1, incm, arr[st - 1], dp, incind, st) ) dp[st][incind][decind] = val return val
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR
Given an array arr of size N, the task is to make strictly increasing and strictly decreasing subsequences from the array such that each array element belongs to increasing subsequence or decreasing subsequence, but not both, or can be part of none of the subsequence. Minimize the number of elements that are not part of any of the subsequences and find the count of such elements. Example 1: Input: N = 12, arr[] = {7, 8, 1, 2, 4, 6, 3, 5, 2, 1, 8, 7} Output: 2 Explanation: Increasing sequence can be : {1, 2, 4, 5, 8}. Decreasing sequence can be : {7, 6, 3, 2, 1}. So, only 2 (8, 7) elements are left which are not part of either of the subsequences. Example 2: Input: N = 7, arr[] = {1, 4, 2, 3, 3, 2, 4} Output: 0 Explanation: Increasing sequence can be : {1, 2, 3, 4}. Decreasing sequence can be : {4, 3, 2}. Your Task: You don't need to read input or print anything. Complete the function minCount() which takes N and array arr as input parameters and returns the integer value Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{3}) Constraints: 1 ≀ N ≀ 10^{2}
class Solution: def minCount(self, nums, n): dp = [ [[(0) for _ in range(n + 2)] for __ in range(n + 2)] for ___ in range(n + 1) ] for i in range(n + 1): for j in range(1, n + 2): for k in range(1, n + 2): if i > 0: dp[i][j][k] = dp[i - 1][j][k] if j == n + 1 or nums[i - 1] < nums[j - 1]: dp[i][j][k] = max(dp[i][j][k], dp[i - 1][i][k] + 1) if k == n + 1 or nums[i - 1] > nums[k - 1]: dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][i] + 1) return n - dp[n][n + 1][n + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP 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 NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Given an array arr of size N, the task is to make strictly increasing and strictly decreasing subsequences from the array such that each array element belongs to increasing subsequence or decreasing subsequence, but not both, or can be part of none of the subsequence. Minimize the number of elements that are not part of any of the subsequences and find the count of such elements. Example 1: Input: N = 12, arr[] = {7, 8, 1, 2, 4, 6, 3, 5, 2, 1, 8, 7} Output: 2 Explanation: Increasing sequence can be : {1, 2, 4, 5, 8}. Decreasing sequence can be : {7, 6, 3, 2, 1}. So, only 2 (8, 7) elements are left which are not part of either of the subsequences. Example 2: Input: N = 7, arr[] = {1, 4, 2, 3, 3, 2, 4} Output: 0 Explanation: Increasing sequence can be : {1, 2, 3, 4}. Decreasing sequence can be : {4, 3, 2}. Your Task: You don't need to read input or print anything. Complete the function minCount() which takes N and array arr as input parameters and returns the integer value Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{3}) Constraints: 1 ≀ N ≀ 10^{2}
class Solution: def minCount(self, l, n): l.insert(0, float("inf")) l.insert(0, -float("inf")) dp = [ [[(-1) for _ in range(n + 1)] for _ in range(n + 1)] for _ in range(n + 1) ] def ss(i, j, k): if k - 2 >= n: return 0 if dp[i][j - 1][k - 2] != -1: return dp[i][j - 1][k - 2] if l[k] > l[i]: dp[i][j - 1][k - 2] = ss(k, j, k + 1) if l[k] < l[j]: if dp[i][j - 1][k - 2] == -1: dp[i][j - 1][k - 2] = ss(i, k, k + 1) else: dp[i][j - 1][k - 2] = min(dp[i][j - 1][k - 2], ss(i, k, k + 1)) if dp[i][j - 1][k - 2] == -1: dp[i][j - 1][k - 2] = ss(i, j, k + 1) + 1 else: dp[i][j - 1][k - 2] = min(dp[i][j - 1][k - 2], ss(i, j, k + 1) + 1) return dp[i][j - 1][k - 2] return ss(0, 1, 2)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER
Given an array arr of size N, the task is to make strictly increasing and strictly decreasing subsequences from the array such that each array element belongs to increasing subsequence or decreasing subsequence, but not both, or can be part of none of the subsequence. Minimize the number of elements that are not part of any of the subsequences and find the count of such elements. Example 1: Input: N = 12, arr[] = {7, 8, 1, 2, 4, 6, 3, 5, 2, 1, 8, 7} Output: 2 Explanation: Increasing sequence can be : {1, 2, 4, 5, 8}. Decreasing sequence can be : {7, 6, 3, 2, 1}. So, only 2 (8, 7) elements are left which are not part of either of the subsequences. Example 2: Input: N = 7, arr[] = {1, 4, 2, 3, 3, 2, 4} Output: 0 Explanation: Increasing sequence can be : {1, 2, 3, 4}. Decreasing sequence can be : {4, 3, 2}. Your Task: You don't need to read input or print anything. Complete the function minCount() which takes N and array arr as input parameters and returns the integer value Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{3}) Constraints: 1 ≀ N ≀ 10^{2}
MAX = 102 def countMin(arr, dp, n, dec, inc, i): if dp[dec][inc][i] != -1: return dp[dec][inc][i] if i == n: return 0 if arr[i] < arr[dec]: dp[dec][inc][i] = countMin(arr, dp, n, i, inc, i + 1) if arr[i] > arr[inc]: if dp[dec][inc][i] == -1: dp[dec][inc][i] = countMin(arr, dp, n, dec, i, i + 1) else: dp[dec][inc][i] = min(countMin(arr, dp, n, dec, i, i + 1), dp[dec][inc][i]) if dp[dec][inc][i] == -1: dp[dec][inc][i] = 1 + countMin(arr, dp, n, dec, inc, i + 1) else: dp[dec][inc][i] = min( 1 + countMin(arr, dp, n, dec, inc, i + 1), dp[dec][inc][i] ) return dp[dec][inc][i] def wrapper(arr, n): arr[MAX - 2] = 1000000000 arr[MAX - 1] = -1000000000 dp = [[[(-1) for i in range(MAX)] for i in range(MAX)] for i in range(MAX)] return countMin(arr, dp, n, MAX - 2, MAX - 1, 0) class Solution: def minCount(self, arr, n): for i in range(MAX): arr.append(0) return wrapper(arr, n)
ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an array arr of size N, the task is to make strictly increasing and strictly decreasing subsequences from the array such that each array element belongs to increasing subsequence or decreasing subsequence, but not both, or can be part of none of the subsequence. Minimize the number of elements that are not part of any of the subsequences and find the count of such elements. Example 1: Input: N = 12, arr[] = {7, 8, 1, 2, 4, 6, 3, 5, 2, 1, 8, 7} Output: 2 Explanation: Increasing sequence can be : {1, 2, 4, 5, 8}. Decreasing sequence can be : {7, 6, 3, 2, 1}. So, only 2 (8, 7) elements are left which are not part of either of the subsequences. Example 2: Input: N = 7, arr[] = {1, 4, 2, 3, 3, 2, 4} Output: 0 Explanation: Increasing sequence can be : {1, 2, 3, 4}. Decreasing sequence can be : {4, 3, 2}. Your Task: You don't need to read input or print anything. Complete the function minCount() which takes N and array arr as input parameters and returns the integer value Expected Time Complexity: O(N^{3}) Expected Auxiliary Space: O(N^{3}) Constraints: 1 ≀ N ≀ 10^{2}
class Solution: def minCount(self, arr, n): inf = float("inf") dic = {} def dfs(mn, mx, i): if (mn, mx, i + 1) in dic: return dic[mn, mx, i + 1] if i >= n: return 0 ans = inf if arr[i] < mx: ans = min(ans, dfs(mn, arr[i], i + 1)) if arr[i] > mn: ans = min(ans, dfs(arr[i], mx, i + 1)) ans = min(ans, 1 + dfs(mn, mx, i + 1)) dic[mn, mx, i + 1] = ans return ans return dfs(-inf, inf, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FUNC_DEF IF VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER
Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≀ n ≀ 3000). The second line contains a string T of length m (1 ≀ m ≀ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer β€” the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6Γ—2=12.
import sys mod = 998244353 eps = 10**-9 def main(): import sys input = sys.stdin.readline S = input().rstrip("\n") S = S[::-1] T = input().rstrip("\n") NS = len(S) NT = len(T) dp = [([0] * (NT + 1)) for _ in range(NS + 1)] dp[0][0] = 1 for i in range(NS): s = S[i] for j in range(NT + 1): if j == 0: if i != NS - 1: if T[0] == s and dp[i][0]: dp[i + 1][1] = (dp[i + 1][1] + dp[i][0] + min(i, NS - NT)) % mod elif T[0] == s and dp[i][0]: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][0] + NS - NT) % mod elif j == NT: dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod elif i != NS - 1: if s == T[j]: dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j]) % mod elif s == T[j]: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod k = NS - (i - j) - 1 if k >= NT: dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod elif T[k] == s: if i != NS - 1: dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod else: dp[i + 1][-1] = (dp[i + 1][-1] + dp[i][j]) % mod if k == 0 and dp[i][j]: dp[i + 1][-1] = (dp[i + 1][-1] + (NS - NT)) % mod print(dp[-1][-1] % mod) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR
Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≀ n ≀ 3000). The second line contains a string T of length m (1 ≀ m ≀ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer β€” the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6Γ—2=12.
s = input() t = input() n, m = len(s), len(t) dp = [[(0) for _ in range(n + 1)] for _ in range(n + 1)] for i in range(n + 1): for j in range(n + 1): if i == j: dp[i][j] = 1 for i in range(n - 1, -1, -1): for j in range(i + 1, n + 1): if i >= m or t[i] == s[j - i - 1]: dp[i][j] += dp[i + 1][j] % 998244353 if j - 1 >= m or t[j - 1] == s[j - i - 1]: dp[i][j] += dp[i][j - 1] % 998244353 total = 0 for i in range(m, n + 1): total += dp[0][i] % 998244353 print(total % 998244353)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR 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 VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≀ n ≀ 3000). The second line contains a string T of length m (1 ≀ m ≀ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer β€” the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6Γ—2=12.
from sys import stdin, stdout def kaavi_and_magic_spell(S, T): MOD = 998244353 n = len(S) m = len(T) dp = [[(0) for _ in range(n)] for _ in range(n)] for i in range(n): if comp(S, T, 0, i): dp[i][i] = 1 for l in range(2, n + 1): for i in range(n - l + 1): if comp(S, T, l - 1, i): dp[i][i + l - 1] += dp[i + 1][i + l - 1] dp[i][i + l - 1] %= MOD if comp(S, T, l - 1, i + l - 1): dp[i][i + l - 1] += dp[i][i + l - 2] dp[i][i + l - 1] %= MOD r = 0 for j in range(m - 1, n): r += dp[0][j] r %= MOD r *= 2 r %= MOD return r def comp(S, T, i, j): if j >= len(T): return True return S[i] == T[j] S = stdin.readline().strip() T = stdin.readline().strip() r = kaavi_and_magic_spell(S, T) stdout.write(str(r) + "\n")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 IF FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future. <image> Kaavi has a string T of length m and all the strings with the prefix T are magic spells. Kaavi also has a string S of length n and an empty string A. During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: * Delete the first character of S and add it at the front of A. * Delete the first character of S and add it at the back of A. Kaavi can perform no more than n operations. To finish the divination, she wants to know the number of different operation sequences to make A a magic spell (i.e. with the prefix T). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo 998 244 353. Two operation sequences are considered different if they are different in length or there exists an i that their i-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string S is a substring of S that occurs at the beginning of S. Input The first line contains a string S of length n (1 ≀ n ≀ 3000). The second line contains a string T of length m (1 ≀ m ≀ n). Both strings contain only lowercase Latin letters. Output The output contains only one integer β€” the answer modulo 998 244 353. Examples Input abab ba Output 12 Input defineintlonglong signedmain Output 0 Input rotator rotator Output 4 Input cacdcdbbbb bdcaccdbbb Output 24 Note The first test: <image> The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of A, although the results are the same, they are considered as different operations. So the answer is 6Γ—2=12.
import sys input = sys.stdin.readline sys.setrecursionlimit(10**5) s = input()[:-1] t = input()[:-1] MOD = 998244353 r_lim = len(t) n = len(s) dp = [([0] * (n + 1)) for i in range(n + 1)] for length in range(1, n + 1): for l in range(n + 1): r = l + length if r > n: break if length == 1: if l >= r_lim or s[0] == t[l]: dp[l][r] = 2 else: dp[l][r] = 0 continue if l >= r_lim or s[length - 1] == t[l]: dp[l][r] += dp[l + 1][r] dp[l][r] %= MOD if r - 1 >= r_lim or s[length - 1] == t[r - 1]: dp[l][r] += dp[l][r - 1] dp[l][r] %= MOD ans = 0 for i in range(r_lim, n + 1): ans += dp[0][i] ans %= MOD print(ans)
IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER 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 ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. Initially, there are n stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first. One move happens as follows. Lets say there are m β‰₯ 2 stickers on the wall. The player, who makes the current move, picks some integer k from 2 to m and takes k leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move. Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent. Given the integer n and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya's and Gena's score if both players play optimally. -----Input----- The first line of input contains a single integer n (2 ≀ n ≀ 200 000)Β β€” the number of stickers, initially located on the wall. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10 000 ≀ a_{i} ≀ 10 000)Β β€” the numbers on stickers in order from left to right. -----Output----- Print one integerΒ β€” the difference between the Petya's score and Gena's score at the end of the game if both players play optimally. -----Examples----- Input 3 2 4 8 Output 14 Input 4 1 -7 -2 3 Output -3 -----Note----- In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0. In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value - 8. On the second move Gena will take the remaining two stickers. The Petya's score is 1 + ( - 7) + ( - 2) = - 8, Gena's score is ( - 8) + 3 = - 5, i.e. the score difference will be - 3.
n = int(input()) a = [0] + list(map(int, input().split())) dp = [0] * (n + 1) s = [0] * (n + 1) for i in range(1, n + 1): s[i] = s[i - 1] + a[i] dp[n] = 0 cur = s[n] for i in range(n - 1, 0, -1): dp[i] = cur cur = max(cur, s[i] - dp[i]) print(dp[1])
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. Initially, there are n stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first. One move happens as follows. Lets say there are m β‰₯ 2 stickers on the wall. The player, who makes the current move, picks some integer k from 2 to m and takes k leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move. Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent. Given the integer n and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya's and Gena's score if both players play optimally. -----Input----- The first line of input contains a single integer n (2 ≀ n ≀ 200 000)Β β€” the number of stickers, initially located on the wall. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10 000 ≀ a_{i} ≀ 10 000)Β β€” the numbers on stickers in order from left to right. -----Output----- Print one integerΒ β€” the difference between the Petya's score and Gena's score at the end of the game if both players play optimally. -----Examples----- Input 3 2 4 8 Output 14 Input 4 1 -7 -2 3 Output -3 -----Note----- In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0. In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value - 8. On the second move Gena will take the remaining two stickers. The Petya's score is 1 + ( - 7) + ( - 2) = - 8, Gena's score is ( - 8) + 3 = - 5, i.e. the score difference will be - 3.
n = int(input()) a = list(map(int, input().split())) p = s = sum(a) for i in range(n - 2, 0, -1): s -= a[i + 1] p = max(p, s - p) print(p)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. Initially, there are n stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first. One move happens as follows. Lets say there are m β‰₯ 2 stickers on the wall. The player, who makes the current move, picks some integer k from 2 to m and takes k leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move. Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent. Given the integer n and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya's and Gena's score if both players play optimally. -----Input----- The first line of input contains a single integer n (2 ≀ n ≀ 200 000)Β β€” the number of stickers, initially located on the wall. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10 000 ≀ a_{i} ≀ 10 000)Β β€” the numbers on stickers in order from left to right. -----Output----- Print one integerΒ β€” the difference between the Petya's score and Gena's score at the end of the game if both players play optimally. -----Examples----- Input 3 2 4 8 Output 14 Input 4 1 -7 -2 3 Output -3 -----Note----- In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0. In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value - 8. On the second move Gena will take the remaining two stickers. The Petya's score is 1 + ( - 7) + ( - 2) = - 8, Gena's score is ( - 8) + 3 = - 5, i.e. the score difference will be - 3.
def f(a): n = len(a) dp = [0] * n s = [0] * (n + 1) for i in range(0, n): s[i + 1] = s[i] + a[i] maxdiffyet = s[n] for i in range(n - 2, -1, -1): dp[i] = maxdiffyet maxdiffyet = max(maxdiffyet, s[i + 1] - dp[i]) return dp[0] n = int(input()) a = [int(x) for x in input().split(" ")] print(f(a))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. Initially, there are n stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first. One move happens as follows. Lets say there are m β‰₯ 2 stickers on the wall. The player, who makes the current move, picks some integer k from 2 to m and takes k leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move. Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent. Given the integer n and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya's and Gena's score if both players play optimally. -----Input----- The first line of input contains a single integer n (2 ≀ n ≀ 200 000)Β β€” the number of stickers, initially located on the wall. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10 000 ≀ a_{i} ≀ 10 000)Β β€” the numbers on stickers in order from left to right. -----Output----- Print one integerΒ β€” the difference between the Petya's score and Gena's score at the end of the game if both players play optimally. -----Examples----- Input 3 2 4 8 Output 14 Input 4 1 -7 -2 3 Output -3 -----Note----- In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0. In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value - 8. On the second move Gena will take the remaining two stickers. The Petya's score is 1 + ( - 7) + ( - 2) = - 8, Gena's score is ( - 8) + 3 = - 5, i.e. the score difference will be - 3.
n = int(input()) a = [0] * n a = list(map(int, input().split())) for i in range(1, len(a)): a[i] += a[i - 1] ans = a[-1] for i in range(n - 2, 0, -1): ans = max(ans, a[i] - ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. Initially, there are n stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first. One move happens as follows. Lets say there are m β‰₯ 2 stickers on the wall. The player, who makes the current move, picks some integer k from 2 to m and takes k leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move. Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent. Given the integer n and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya's and Gena's score if both players play optimally. -----Input----- The first line of input contains a single integer n (2 ≀ n ≀ 200 000)Β β€” the number of stickers, initially located on the wall. The second line contains n integers a_1, a_2, ..., a_{n} ( - 10 000 ≀ a_{i} ≀ 10 000)Β β€” the numbers on stickers in order from left to right. -----Output----- Print one integerΒ β€” the difference between the Petya's score and Gena's score at the end of the game if both players play optimally. -----Examples----- Input 3 2 4 8 Output 14 Input 4 1 -7 -2 3 Output -3 -----Note----- In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0. In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value - 8. On the second move Gena will take the remaining two stickers. The Petya's score is 1 + ( - 7) + ( - 2) = - 8, Gena's score is ( - 8) + 3 = - 5, i.e. the score difference will be - 3.
n = int(input()) raw = input().split() d = [] prev = 0 for i in range(n): di = int(raw[i]) di += prev d.append(di) prev = di i = n - 2 cur = d[n - 1] while i > 0: cur = max(cur, d[i] - cur) i -= 1 print(cur)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least $\frac{p_{i} - \epsilon}{2000}$, where Ξ΅ < 10^{ - 7}. To better prepare himself, he wants to know the answer for q different values of p_{i}. Since he is busy designing the battle strategy with Sam, he asks you for your help. -----Input----- First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer p_{i} (1 ≀ p_{i} ≀ 1000) β€” i-th query. -----Output----- Output q lines. On i-th of them output single integer β€” answer for i-th query. -----Examples----- Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2
import sys def main(): eps = 10**-7 k, q = readIntArr() def rowFactory(): row = [(0) for _ in range(k + 1)] return row dp = [rowFactory()] dp[0][0] = 1 while dp[-1][-1] <= 0.5: newRow = rowFactory() for j in range(1, k + 1): newRow[j] += dp[-1][j] * j / k + dp[-1][j - 1] * (k - (j - 1)) / k dp.append(newRow) n = len(dp) for _ in range(q): p = int(input()) b = n + 1 nTurns = 0 while b > 0: while nTurns + b < n and dp[nTurns + b][k] < p / 2000: nTurns += b b //= 2 nTurns += 1 print(nTurns) 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()] inf = float("inf") MOD = 10**9 + 7 main()
IMPORT FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER 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 ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least $\frac{p_{i} - \epsilon}{2000}$, where Ξ΅ < 10^{ - 7}. To better prepare himself, he wants to know the answer for q different values of p_{i}. Since he is busy designing the battle strategy with Sam, he asks you for your help. -----Input----- First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer p_{i} (1 ≀ p_{i} ≀ 1000) β€” i-th query. -----Output----- Output q lines. On i-th of them output single integer β€” answer for i-th query. -----Examples----- Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2
k, q = list(map(int, input().split())) t = [0] * (k + 1) t[1] = 1 d = [0] n = i = 1 while i < 1001: if 2000 * t[k] > i - 1e-07: d.append(n) i += 1 else: t = [0] + [((j * t[j] + (k - j + 1) * t[j - 1]) / k) for j in range(1, k + 1)] n += 1 for i in range(q): print(d[int(input())])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR
Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least $\frac{p_{i} - \epsilon}{2000}$, where Ξ΅ < 10^{ - 7}. To better prepare himself, he wants to know the answer for q different values of p_{i}. Since he is busy designing the battle strategy with Sam, he asks you for your help. -----Input----- First line consists of two space separated integers k, q (1 ≀ k, q ≀ 1000) β€” number of different kinds of orbs and number of queries respectively. Each of the next q lines contain a single integer p_{i} (1 ≀ p_{i} ≀ 1000) β€” i-th query. -----Output----- Output q lines. On i-th of them output single integer β€” answer for i-th query. -----Examples----- Input 1 1 1 Output 1 Input 2 2 1 2 Output 2 2
k, q = map(int, input().split()) dp = [[(0.0) for i in range(k + 1)] for j in range(10000)] dp[0][0] = 1.0 for i in range(1, 10000): for j in range(1, k + 1): dp[i][j] = dp[i - 1][j] * j / k + dp[i - 1][j - 1] * (k - j + 1) / k for t in range(q): p = int(input()) for i in range(10000): if p <= dp[i][k] * 2000: print(i) break
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 NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, s): if len(s) == 1 and s == "0": return 0 elif len(s) == 1 and s != "0": return 1 temp = {} for i in range(1, 27): temp["{}".format(i)] = True memo = {} def check(first, second): if second != "": if first + "#" + second in memo: return memo[first + "#" + second] if not second in temp: return 0 if second in temp and len(first) <= 1: if first == "": return 1 elif first in temp: return 1 else: return 0 val_1 = check(first[: len(first) - 2], first[len(first) - 2 :]) val_2 = check(first[: len(first) - 1], first[len(first) - 1 :]) memo[first + "#" + second] = val_1 + val_2 return val_1 + val_2 return check(s, "") % (10**9 + 7)
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR STRING RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR STRING RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR STRING IF BIN_OP BIN_OP VAR STRING VAR VAR RETURN VAR BIN_OP BIN_OP VAR STRING VAR IF VAR VAR RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR STRING RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR STRING BIN_OP BIN_OP NUMBER NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): mod = 1000000007 if str[0] == "0": return 0 prev0 = 1 prev1 = 1 for i in range(2, len(str) + 1): curr = 0 if str[i - 1] != "0": curr += prev1 if str[i - 2] == "1" or str[i - 2] == "2" and str[i - 1] <= "6": curr += prev0 prev0, prev1 = prev1, curr return prev1 % mod
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): memo = {x: (-1) for x in range(len(str) + 1)} mod = 1000000007 if str[0] == "0": return 0 def dp(i): if i == 1 or i == 0: return 1 if memo[i] != -1: return memo[i] count = 0 if str[i - 1] >= "1": count = count + dp(i - 1) % mod if "1" == str[i - 2] or str[i - 2] == "2" and str[i - 1] <= "6": count = (count + dp(i - 2) % mod) % mod memo[i] = count return memo[i] return dp(len(str))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF STRING VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): mod = 1000000007 dp = [] def rec(i, str, dp): if i == len(str): return 1 if dp[i] != -1: return dp[i] c1 = 0 c2 = 0 if str[i] >= "1" and str[i] <= "9": c1 = rec(i + 1, str, dp) part = str[i : i + 2] if i <= len(str) - 2 and part >= "10" and part <= "26": c2 = rec(i + 2, str, dp) dp[i] = (c1 + c2) % mod return dp[i] dp = [-1] * len(str) return rec(0, str, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, string): def dfs(i): if i >= len(string): return 1 if string[i] == "0": return 0 if i in mem: return mem[i] c = 0 c += dfs(i + 1) if string[i] == "1" and i + 1 < len(string): c += dfs(i + 2) if string[i] == "2" and i + 1 < len(string) and int(string[i + 1]) <= 6: c += dfs(i + 2) mem[i] = c return c % MOD MOD = 10**9 + 7 mem = {} return dfs(0)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR STRING RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT RETURN FUNC_CALL VAR NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): n = len(str) count = [0] * (n + 1) count[0] = 1 count[1] = 1 for i in range(2, n + 1): count[i] = 0 if str[i - 1] > "0": count[i] = count[i - 1] if str[i - 2] == "1" or str[i - 2] == "2" and str[i - 1] < "7": count[i] += count[i - 2] return count[n] % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): n = len(str) t = [(0) for i in range(n + 1)] if str[0] == "0": return 0 t[0] = 1 t[1] = 1 mod = 10**9 + 7 for i in range(1, n): if str[i] > "0": t[i + 1] = t[i] if str[i - 1] == "1" or str[i - 1] == "2" and int(str[i]) < 7: t[i + 1] += t[i - 1] elif str[i] == "0": return 0 t[i + 1] %= mod return t[n]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR STRING RETURN NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): ans = [(0) for i in range(len(str) + 1)] ans[0] = 1 ans[1] = 1 for i in range(2, len(str) + 1): if str[i - 1] > "0": ans[i] = ans[i - 1] if ( str[i - 2] == "1" or str[i - 2] == "2" and str[i - 1] >= "0" and str[i - 1] <= "6" ): ans[i] += ans[i - 2] return ans[len(str)] % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def func(self, str, n, dp): m = 1000000007 if n == 0 or n == 1: return 1 if dp[n] != -1: return dp[n] cnt = 0 if str[n - 1] >= "1": cnt = cnt + self.func(str, n - 1, dp) % m if str[n - 2] == "1" or str[n - 2] == "2" and str[n - 1] <= "6": cnt = (cnt + self.func(str, n - 2, dp) % m) % m dp[n] = cnt % m return dp[n] def CountWays(self, str): n = len(str) if str[0] == "0": return -1 dp = [-1] * (n + 1) return self.func(str, n, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): mp = {} return self.countWaysHelper(str, 0, mp) % (10**9 + 7) def countWaysHelper(self, str, i, mp): if i == len(str): return 1 if str[i] == "0": return 0 if i == len(str) - 1: return 1 if i in mp: return mp[i] if str[i] == "1" or str[i] == "2" and str[i + 1] >= "0" and str[i + 1] <= "6": res = self.countWaysHelper(str, i + 1, mp) + self.countWaysHelper( str, i + 2, mp ) else: res = self.countWaysHelper(str, i + 1, mp) mp[i] = res return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR STRING RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): mod = 10**9 + 7 n = len(str) prev1 = 1 prev2 = 1 for i in range(2, n + 1): curr = 0 if str[i - 1] != "0": curr = prev1 if str[i - 2] == "1" or str[i - 2] == "2" and str[i - 1] <= "6": curr = (curr + prev2) % mod prev2 = prev1 prev1 = curr return prev1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): if str[0] == "0": return 0 n = len(str) mod = 1000000007 dp = [(0) for i in range(n + 1)] dp[0] = dp[1] = 1 for i in range(1, n): if str[i] == "0" and str[i - 1] > "2": return 0 for i in range(2, n + 1): if str[i - 1] > "0": dp[i] = dp[i - 1] if str[i - 2] == "1" or str[i - 2] == "2" and str[i - 1] < "7": dp[i] += dp[i - 2] dp[i] %= mod return dp[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): def fun(i): if i == n: return 1 if i > n: return 0 if dp[i] != -1: return dp[i] ans = 0 if str[i] != "0": ans += fun(i + 1) if str[i] != "0" and int(str[i : i + 2]) < 27: ans += fun(i + 2) ans = ans % 1000000007 dp[i] = ans return dp[i] n = len(str) dp = [] for i in range(n): dp.append(-1) return fun(0)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): t = [(-1) for i in range(len(str) + 1)] mod = 10**9 + 7 def helper(idx, str, t): if idx == len(str): return 1 if str[idx] == "0": return 0 if t[idx] != -1: return t[idx] res = helper(idx + 1, str, t) if idx < len(str) - 1 and ( str[idx] == "1" or str[idx] == "2" and str[idx + 1] < "7" ): res = res + helper(idx + 2, str, t) t[idx] = res return t[idx] % mod return 0 if len(str) == 0 else helper(0, str, t) % mod
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR STRING RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER VAR VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def isValid(self, s, n): if s[0] == "0": return True for i in range(n - 1): if s[i] == s[i + 1] == "0": return True return False def CountWays(self, s): mod = 1000000000.0 + 7 n = len(s) flag = self.isValid(s, n) if flag: return 0 dp = [0] * (n + 1) dp[0] = dp[1] = 1 for i in range(2, n + 1): dp[i] = 0 if s[i - 1] > "0": dp[i] = dp[i - 1] if s[i - 2] == "1" or s[i - 2] == "2" and s[i - 1] < "7": dp[i] = int((dp[i] + dp[i - 2]) % mod) return dp[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER STRING RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): m = 1000000007 if str[-1] == "0": first = 0 second = 1 else: first = 1 second = 1 for i in range(len(str) - 1)[::-1]: if str[i] == "0": if first == 0: return 0 else: second = first first = 0 elif str[i] == "1" or str[i] == "2" and int(str[i + 1]) <= 6: sum = first + second second = first first = sum elif first == 0: return 0 else: second = first if first == 0: return 0 else: return first % m
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): if len(str) == 1: return 1 dp = [0] * len(str) if str[0] == "0": return 0 dp[0] = 1 if str[1] != "0": dp[1] = 1 if str[0:2] <= "26": dp[1] += 1 mod = 10**9 + 7 for i in range(2, len(str)): if str[i] == "0": if str[i - 1] == "0": return 0 if str[i - 1] == "1" or str[i - 1] == "2": dp[i] += dp[i - 2] % mod elif str[i - 1] == "0": dp[i] += dp[i - 1] % mod else: dp[i] += dp[i - 1] % mod if str[i - 1 : i + 1] <= "26": dp[i] += dp[i - 2] % mod return dp[len(str) - 1] % mod
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER STRING VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING RETURN NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER STRING VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): s = str hm = {} for i in range(1, 27): hm["{}".format(i)] = 1 dp = [(0) for i in range(len(s) + 1)] dp[0] = 1 for i in range(len(s)): if s[i] in hm: dp[i + 1] = dp[i] if i > 0 and s[i - 1] + s[i] in hm: dp[i + 1] += +dp[i - 1] return dp[-1] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, s): if not s: return 1 n = len(s) self.s = s self.cache = [None] * (n + 1) self.cache[n - 1 :] = int(s[-1] != "0"), 1 return self.DP(0) def DP(self, j): if self.cache[j]: return self.cache[j] if self.s[j] == "0": return 0 takeOne = self.DP(j + 1) twoDigit = int(self.s[j : j + 2]) takeTwo = 0 if twoDigit < 27: takeTwo = self.DP(j + 2) self.cache[j] = (takeOne + takeTwo) % 1000000007 return self.cache[j]
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER STRING NUMBER RETURN FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, s): n = len(s) if n == 0: return 1 if n == 1: return 0 if s[0] == "0" else 1 dp = [(0) for i in range(n)] dp[0] = 1 dp[1] = 1 if int(s[0] + s[1]) <= 26: dp[1] += 1 mod = 10**9 + 7 for i in range(2, n): dp[i] = dp[i - 1] if s[i] != "0" else 0 if int(s[i - 1] + s[i]) <= 26 and s[i - 1] != "0": dp[i] += dp[i - 2] dp[i] %= mod return dp[n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER STRING NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): if len(str) == 0: return 1 memory = {} return self.helper(0, str, memory) % (10**9 + 7) def helper(self, i, str, memory): if str[i:] in memory: return memory[str[i:]] if i >= len(str): return 1 elif str[i] == "0": return 0 elif i == len(str) - 1: return 1 elif ( str[i] == "1" or str[i] == "2" and (int(str[i + 1]) >= 0 and int(str[i + 1]) <= 6) ): count = self.helper(i + 1, str, memory) + self.helper(i + 2, str, memory) memory[str[i:]] = count return count else: count = self.helper(i + 1, str, memory) memory[str[i:]] = count return count
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT RETURN BIN_OP FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR STRING RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR STRING VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): def helper(str, k, memo): if k == 0: return 1 s = len(str) - k if str[s] == "0": return 0 if memo[k] != None: return memo[k] res = helper(str, k - 1, memo) if k >= 2 and int(str[s : s + 2]) <= 26: res += helper(str, k - 2, memo) memo[k] = res return memo[k] memo = [None] * (len(str) + 1) return helper(str, len(str), memo) % (10**9 + 7)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR STRING RETURN NUMBER IF VAR VAR NONE RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): s = str n = len(s) h = {(-1): 1, (0): 1} div = 10**9 + 7 if s[0] == 0: return 0 for i in range(1, n): if s[i] == "0": if s[i - 1] == "0": return 0 h[i] = h[i - 2] * int(int(s[i - 1] + s[i]) <= 26) % div elif s[i - 1] == "0": h[i] = h[i - 1] else: h[i] = (h[i - 1] + h[i - 2] * int(int(s[i - 1] + s[i]) <= 26)) % div return h[n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR BIN_OP VAR NUMBER STRING RETURN NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): md = 10**9 + 7 n = len(str) tem = [0] * (n + 1) tem[0] = 1 for i in range(n): if str[i] != "0": tem[i + 1] = tem[i + 1] + tem[i] if str[i - 1] != "0" and i - 1 >= 0 and int(str[i - 1 : i + 1]) <= 26: tem[i + 1] = (tem[i + 1] + tem[i - 1]) % md return tem[n]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): if str[0] == "0": return 0 dp = [0] * (len(str) + 1) dp[0] = dp[1] = 1 mod = 10**9 + 7 for i in range(2, len(str) + 1): one = int(str[i - 1]) two = int(str[i - 2 : i]) if 10 <= two <= 26: dp[i] += dp[i - 2] if 1 <= one <= 9: dp[i] += dp[i - 1] dp[i] %= mod return dp[-1] % mod
CLASS_DEF FUNC_DEF IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
import sys sys.setrecursionlimit(10**9) class Solution: mod = 10**9 + 7 arr = [] def fun(self, ind, st, n): if ind == n: return 1 if self.arr[ind] != -1: return self.arr[ind] take = 0 leave = 0 if st[ind] >= "1" and st[ind] <= "9": leave = self.fun(ind + 1, st, n) if ind < n - 1: tmp = st[ind : ind + 2] if "10" <= tmp <= "26": take = self.fun(ind + 2, st, n) t = (take + leave) % self.mod self.arr[ind] = t return t def CountWays(self, str): self.arr = [-1] * len(str) return self.fun(0, str, len(str))
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF STRING VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, s): if s[0] == "0": return 0 n = len(s) dp = [0] * (n + 1) dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): if s[i - 1] != "0": dp[i] += dp[i - 1] if s[i - 2 : i] >= "10" and s[i - 2 : i] <= "26": dp[i] += dp[i - 2] dp[i] %= 1000000007 return dp[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR STRING VAR BIN_OP VAR NUMBER VAR STRING VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): s = str mod = 10**9 + 7 dp = {len(s): 1} for i in range(len(s) - 1, -1, -1): if s[i] == "0": dp[i] = 0 else: dp[i] = dp[i + 1] if i + 1 < len(s) and ( s[i] == "1" or s[i] == "2" and s[i + 1] in "0123456" ): dp[i] += dp[i + 2] return dp[0] % mod
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, s): def dfs(i, n): nonlocal d if i in d: return d[i] if s[i] == "0": return 0 res = 0 res = (res + dfs(i + 1, n)) % (10**9 + 7) if i + 1 < n and (s[i] == "1" or s[i] == "2" and s[i + 1] in "0123456"): res = (res + dfs(i + 2, n)) % (10**9 + 7) d[i] = res % (10**9 + 7) return res % (10**9 + 7) n = len(s) d = {n: 1} return dfs(0, n)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR STRING RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def decode(self, digits, n, count, dp): if n == 0 or n == 1: return 1 if digits[0] == 0: return 0 if dp[n] != -1: return dp[n] if digits[n - 1] > "0": count = self.decode(digits, n - 1, count, dp) if digits[n - 2] == "1" or digits[n - 2] == "2" and digits[n - 1] < "7": count += self.decode(digits, n - 2, count, dp) dp[n] = count return count def CountWays(self, digits): count = 0 dp = [-1] * (len(digits) + 1) dp[0] = dp[1] = 1 count = self.decode(digits, len(str), count, dp) return count % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
A top secret message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 You are an FBI agent. You have to determine the total number of ways that message can be decoded, as the answer can be large return the answer modulo 10^{9} + 7. Note: An empty digit sequence is considered to have one decoding. It may be assumed that the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two or more consecutive 0s then it is an invalid string. Example 1: Input: str = "123" Output: 3 Explanation: "123" can be decoded as "ABC"(123), "LC"(12 3) and "AW"(1 23). Example 2: Input: str = "90" Output: 0 Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'. Your Task: You don't need to read or print anything. Your task is to complete the function CountWays() which takes the string as str as input parameter and returns the total number of ways the string can be decoded modulo 10^{9} + 7. Expected Time Complexity: O(n) Expected Space Complexity: O(n) where n = |str| Constraints: 1 <= |str| <= 10^{4}
class Solution: def CountWays(self, str): MOD = 10**9 + 7 n = len(str) dp = {n: 1} def f(i): if i in dp: return dp[i] if str[i] == "0": return 0 ans = f(i + 1) if i + 1 < n and ( str[i] == "1" and str[i + 1] in "0123456789" or str[i] == "2" and str[i + 1] in "0123456" ): ans += f(i + 2) dp[i] = ans return ans % MOD return f(0) % MOD
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR STRING RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR NUMBER VAR