description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): maxx = float("-inf") val = 0 new_str = "" start = 0 dic = {} for i, (x_, b_) in enumerate(zip(x, b)): dic[x_] = b_ for i, c in enumerate(w): if c in dic: val = val + dic[c] else: val = val + ord(c) if val > maxx: maxx = val new_str = w[start : i + 1] if val < 0: val = 0 start = i + 1 return new_str
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): asc = {} for i in range(len(w)): asc[w[i]] = ord(w[i]) for i in range(n): asc[x[i]] = b[i] maxsum = currsum = 0 maxres = res = "" for i in range(len(w)): if currsum + asc[w[i]] >= 0: currsum += asc[w[i]] res += w[i] else: currsum = 0 res = "" if currsum > maxsum: maxsum = currsum maxres = res if maxres == "": maxres = w[0] for i in range(1, len(w)): if asc[maxres] < asc[w[i]]: maxres = w[i] return maxres
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): max_so_far = -10000000 max_ending_here = 0 s = "" for i in w: if i in x: c = x.index(i) max_ending_here += b[c] s += i else: max_ending_here += ord(i) s += i if max_so_far < max_ending_here: max_so_far = max_ending_here t = "" elif t == "": t = s[:-1] if max_ending_here < 0: max_ending_here = 0 s = "" if max_so_far > max_ending_here: return t elif max_so_far < 0: return w else: return s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING IF VAR STRING ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR VAR RETURN VAR IF VAR NUMBER RETURN VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): s = "" dict = {a: b for a, b in zip(x, b)} max_ending_here, max_so_far = -float("inf"), -float("inf") curr, res = "", "" for i in w: val = dict[i] if i in dict else ord(i) if max_ending_here < 0: curr = i max_ending_here = val else: max_ending_here += val curr += i if max_ending_here > max_so_far: max_so_far = max_ending_here res = curr return res
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR STRING STRING FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): d = dict(zip(x, b)) size = len(w) maxi = -1000000000.0 cur = 0 curStr = "" ans = "" for i in range(size): if w[i] in d.keys(): cur += d[w[i]] else: cur += ord(w[i]) curStr += w[i] if maxi < cur: maxi = cur ans = curStr if cur < 0: cur = 0 curStr = "" return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): if len(w) <= 1: return w dic = {} max_till_now = 0 max_so_far = -99999999999999999 ans = "" ans2 = "" for i in range(len(w)): if w[i] in x: index = x.index(w[i]) max_till_now = max_till_now + b[index] else: max_till_now = max_till_now + ord(w[i]) ans += w[i] if max_so_far < max_till_now: max_so_far = max_till_now ans2 = ans if max_till_now < 0: max_till_now = 0 ans = "" return ans2
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): arr = [(b[x.index(i)] if i in x else ord(i)) for i in w] max1 = -9999 ends = 0 sub1 = "" sub2 = "" for i in range(len(w)): ends += arr[i] sub1 += w[i] if ends > max1: max1 = ends sub2 = sub1 if ends < 0: ends = 0 sub1 = "" return sub2
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): s = set(w) asci = {} for i in s: if i in x: asci[i] = b[x.index(i)] else: asci[i] = ord(i) curr_max = 0 max_so_far = float("-inf") N = len(w) s = 0 e = 0 start = 0 for i in range(N): curr_max += asci[w[i]] if max_so_far < curr_max: max_so_far = curr_max s = start e = i if curr_max < 0: curr_max = 0 if i + 1 < N: start = i + 1 return w[s : e + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): d = dict() n = len(w) for i in range(ord("A"), ord("Z") + 1): d[chr(i)] = i for i in range(ord("a"), ord("z") + 1): d[chr(i)] = i for i, j in zip(x, b): d[i] = j anss = -1 an = -99999999 anse = -1 curs = -1 cure = -1 cu = 0 for i in range(n): cu += d[w[i]] if curs == -1: curs = i cure = i else: cure = i if cu > an: anss = curs anse = cure an = cu if cu < 0: cu = 0 curs = -1 cure = -1 return w[anss : anse + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): asc = {} for i in range(n): asc[x[i]] = b[i] res = [[0, 1], self.asc_value(w[0], asc)] cur = [[0, 1], self.asc_value(w[0], asc)] for i in range(1, len(w)): ch = w[i] val = self.asc_value(ch, asc) if cur[1] + val < val: cur = [[i, i + 1], val] elif cur[1] + val > 0: cur[0][1] = i + 1 cur[1] += val else: cur = [[i, i + 1], val] if res[1] <= cur[1]: res = [[cur[0][0], cur[0][1]], cur[1]] return w[res[0][0] : res[0][1]] def asc_value(self, ch, asc): if ch in asc: return asc[ch] asc[ch] = ord(ch) return asc[ch]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST LIST NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST LIST VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR LIST LIST VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): dic = {} for i in range(n): dic[x[i]] = b[i] ans = 0 s = "" fin = -999 fin_str = "" for i in w: if i not in dic: s += i ans += ord(i) else: s += i ans += dic[i] if ans > fin: fin = ans fin_str = s if ans < 0: ans = 0 s = "" return fin_str
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): for i in range(n): h = dict(zip(x, b)) s = 0 t = "" ans = "" mx = -1000000000.0 a = 0 for i in range(len(w)): if w[i] in h: a = h[w[i]] else: a = ord(w[i]) s += a t += w[i] if s > mx: mx = s ans = t if s < 0: s = 0 t = "" return ans
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): val = [] for i in w: if i not in x: val.append(ord(i)) else: val.append(b[x.index(i)]) maxyet = 0 maxcur = 0 start = 0 s = 0 e = 0 for i in range(len(val)): maxcur = maxcur + val[i] if maxcur > maxyet: maxyet = maxcur start = s e = i elif maxcur < 0: maxcur = 0 s = i + 1 return w[start : e + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): sum_val = 0 max_val = ord(w[0]) max_string = w[0] my_dict = {} for _ in range(len(x)): my_dict[x[_]] = b[_] final_string = w[0] counter = 0 for _ in w: if counter != 0: max_string += _ if _ in my_dict.keys(): sum_val += my_dict[_] else: sum_val += ord(_) if max_val <= sum_val: final_string = max_string max_val = max(max_val, sum_val) if sum_val < 0: sum_val = 0 max_string = "" counter = 1 return final_string
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): dp = {} for i in range(n): dp[x[i]] = b[i] sum = 0 maxStart = 0 maxEnd = 0 start = 0 end = 0 if dp.get(w[0]) == None: maxSum = ord(w[0]) else: maxSum = dp[w[0]] for i in w: if dp.get(w[end]) == None: sum += ord(w[end]) else: sum += dp[w[end]] if sum > maxSum: maxSum = sum maxStart = start maxEnd = end if sum < 0: start = end + 1 end = end sum = 0 end += 1 return w[maxStart : maxEnd + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NONE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): dic = dict(zip(x, b)) currsum = globalSum = 0 start = end = s = 0 for i in range(len(w)): k = ord(w[i]) if dic.get(w[i]) is None: currsum += k else: currsum += dic[w[i]] if currsum > globalSum: globalSum = currsum start = s end = i if currsum < 0: currsum = 0 s = i + 1 return w[start : end + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NONE VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def __init__(self): self.mp = dict() def asc(self, letr): if self.mp.get(letr) == None: return ord(letr) return self.mp[letr] def maxSum(self, w, x, b, n): self.mp.clear() for i in range(n): self.mp[x[i]] = b[i] curr_sum = self.asc(w[0]) max_sum = self.asc(w[0]) left_max = 0 right_max = 0 left = 0 right = 0 for i in range(1, len(w)): letr = w[i] val = self.asc(w[i]) if curr_sum + val <= val: curr_sum = val left = i right = i else: curr_sum += val right = i if max_sum < curr_sum: max_sum = curr_sum left_max = left right_max = right return w[left_max : right_max + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NONE RETURN FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): if len(w) == 1: return w arr = {} for i in w: if i in x: arr[i] = b[x.index(i)] else: arr[i] = ord(i) max_so_far = 0 max_end = 0 s = 0 end = 0 for i in range(len(w)): max_end += arr[w[i]] if max_end > max_so_far: max_so_far = max_end start = s end = i if max_end < 0: max_end = 0 s = i + 1 return w[start : end + 1]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): ump = {} for i in range(n): ump[x[i]] = b[i] start, stop, s = 0, 0, 0 res, sum = 0, 0 for i in range(len(w)): if w[i] in ump: sum += ump[w[i]] else: sum += ord(w[i]) if sum < 0: sum = 0 s = i + 1 if sum > res: res = sum start = s stop = i return w[start : stop + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
from sys import maxsize class Solution: def maxSum(self, w, x, b, n): dict = {} for ele in w: if ele not in dict: dict[ele] = ord(ele) for i in range(len(x)): if x[i] in dict: dict[x[i]] = b[i] start = 0 end = 0 max_sum = 0 cur_sum = 0 s = 0 i = 0 for i in range(len(w)): cur_sum += dict[w[i]] if cur_sum < 0: cur_sum = 0 s = i + 1 if max_sum < cur_sum: max_sum = cur_sum start = s end = i return w[start : end + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): eval_char = lambda a: b[x.index(a)] if a in x else ord(a) len_w = len(w) max_i = i = 0 max_j = j = 0 max_sum = summ = eval_char(w[j]) while j < len_w - 1: if summ < 0: j += 1 i = j summ = eval_char(w[i]) else: j += 1 summ += eval_char(w[j]) if summ > max_sum: max_sum = summ max_i = i max_j = j return w[max_i : max_j + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
get_ascii = lambda c, asciidict: asciidict.get(c, ord(c)) class Solution: def maxSum(self, w, x, b, n): asciidict = {x[i]: b[i] for i in range(n)} s_i, e_i, cur_s = 0, 0, 0 max_sum = 0 cursum = 0 for idx, c in enumerate(w): cursum = cursum + get_ascii(c, asciidict) if cursum < 0: cur_s = idx + 1 cursum = 0 elif cursum < max_sum: continue elif cursum >= max_sum: max_sum = cursum s_i = cur_s e_i = idx return w[s_i : e_i + 1]
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): score = {} for i in range(97, 97 + 26): score[chr(i)] = i for i in range(65, 65 + 26): score[chr(i)] = i for i in range(n): score[x[i]] = b[i] n = len(w) s = 0 m = [0, 0] ans = [0, 1, score[w[0]]] for i in range(n): s += score[w[i]] if s - m[0] > ans[2]: ans[0] = m[1] ans[1] = i + 1 ans[2] = s - m[0] if s < m[0]: m = [s, i + 1] return w[ans[0] : ans[1]]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): res = "" raji = "" curr = 0 maxi = -float("infinity") for a, i in enumerate(w): p = ord(i) if i in x: p = b[x.index(i)] curr += p raji += i if curr > maxi: maxi = curr res = raji if curr < 0: curr = 0 raji = "" return res
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): s = 0 e = 0 ans = "" l = [] start = s sum = 0 max = -1 for ele in w: if ele in x: l.append(b[x.index(ele)]) else: l.append(ord(ele)) for i in range(len(w)): sum = sum + l[i] if sum > max: max = sum start = s e = i if sum < 0: sum = 0 s = i + 1 for j in range(start, e + 1): ans = ans + w[j] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): all_negative = 1 max_position = -1 max_value = -1000000 for i in range(0, len(w)): if w[i] in x: check = b[x.index(w[i])] else: check = ord(w[i]) if check > 0: all_negative = 0 break if check > max_value: max_value = check max_position = i if all_negative == 1: return w[max_position] cs = 0 ms = 0 l = -1 result = "" for i in range(0, len(w)): if w[i] in x: cs = cs + b[x.index(w[i])] else: cs = cs + ord(w[i]) if cs < 0: cs = 0 l = i if cs > ms: result = w[l + 1 : i + 1] ms = cs return result
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
from sys import maxsize class Solution: def maxSum(self, w, x, b, n): ans_str = "" ans_ascii = -maxsize - 1 curr = 0 curr_str = "" w = list(w) for i in range(len(w)): curr_str += w[i] if w[i] in x: for j in range(n): if x[j] == w[i]: curr += b[j] else: curr += ord(w[i]) if ans_ascii < curr: ans_ascii = curr ans_str = curr_str if curr < 0: curr = 0 curr_str = "" return ans_str
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): start, stop, s = 0, 0, 0 su = 0 res = 0 for i in range(len(w)): if w[i] not in x: su += ord(w[i]) else: su += b[x.index(w[i])] if su < 0: su = 0 s = i + 1 if res < su: res = su start = s stop = i return w[start : stop + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
import sys class Solution: def maxSum(self, w, x, b, n): res = [] for i in w: if i in x: res.append(b[x.index(i)]) else: res.append(ord(i)) maxsum = -1 * sys.maxsize f = 0 l = 0 c = 0 p = [0, 0] for i in range(len(res)): c += res[i] if maxsum < c: maxsum = c p[:] = [f, i] if c < 0: c = 0 f = i + 1 res = "" for i in range(p[0], p[1] + 1): res = res + w[i] return res
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): map = {} for i in range(len(x)): map[x[i]] = b[i] res = -1e19 result = "" curr = 0 s = "" for i in w: if curr < 0: curr = 0 s = "" if i in map: curr += map[i] else: curr += ord(i) s += i if res < curr: res = curr result = s return result
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): result = -float("inf") temp = 0 u, v = 0, 0 i, j = 0, 0 for c in range(len(w)): for k in range(n): if w[c] == x[k]: temp += b[k] break else: temp += ord(w[c]) j += 1 if temp >= result: u, v = i, j result = temp if temp < 0: temp = 0 i = c + 1 j = c + 1 return w[u:v]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): A = [] if len(w) == 1: return w mp = {} for i in range(n): mp[x[i]] = b[i] curr_sum = 0 max_sum = -100000 curr_s = "" max_s = "" for i in range(len(w)): if w[i] in mp: x = mp[w[i]] else: x = ord(w[i]) curr_sum = curr_sum + x if curr_sum < 0: curr_sum = 0 curr_s = "" else: curr_s = curr_s + w[i] if max_sum < curr_sum: max_sum = curr_sum max_s = curr_s return max_s
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): max_sum_here = 0 max_sum_whole = max_sum_here start = end = s = 0 for i in range(0, len(w)): if w[i] not in x: if ord(w[i]) + max_sum_here >= 0: max_sum_here += ord(w[i]) if max_sum_here >= max_sum_whole: max_sum_whole = max_sum_here start = s end = i elif b[x.index(w[i])] + max_sum_here >= 0: max_sum_here += b[x.index(w[i])] if max_sum_here >= max_sum_whole: max_sum_whole = max_sum_here start = s end = i else: s = i + 1 max_sum_here = 0 return w[start : end + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): res = 0 res1 = 0 string_res = "" string_res1 = "" store = {} value = {} if len(w) == 1: return w[0] for i in range(n): value[x[i]] = b[i] curent = 0 maxtilnow = 0 rangeindex = 0 startindex = 0 endindex = 0 answer = "" storkeys = [a for a in store.keys()] storvalue = list(b for b in store.values()) for i in range(len(w)): curent += ord(w[i]) if w[i] not in value else value[w[i]] if curent < 0: curent = 0 rangeindex = i + 1 elif curent > maxtilnow: maxtilnow = curent startindex = rangeindex endindex = i for i in range(startindex, endindex + 1): answer += w[i] return answer
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): res = -float("infinity") resString = "" tempStrig = "" sum = 0 mydict = dict() for i in range(len(x)): mydict[x[i]] = b[i] for i in w: val = mydict[i] if i in mydict else ord(i) sum += val tempStrig += i if sum > res: res = sum resString = tempStrig if sum < 0: tempStrig = "" sum = 0 return resString
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): map = {} for i in range(n): map[x[i]] = b[i] maxi = float("-inf") ans = "" temp = "" sum = 0 i = 0 while i < len(w): if w[i] in map: sum = sum + map[w[i]] else: sum = sum + ord(w[i]) temp = temp + w[i] i += 1 if sum > maxi: maxi = sum ans = temp if sum < 0: sum = 0 temp = "" return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, W, X, B, n): cs = 0 ms = -1000000000 res = "" start = end = s = 0 if len(W) == 1: res = W return res for i in range(len(W)): if W[i] in X: ind = X.index(W[i]) x = B[ind] else: x = ord(W[i]) cs += x if cs > ms: ms = cs start = s end = i if cs < 0: cs = 0 s = i + 1 for i in range(start, end + 1): res += W[i] return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def maxSum(self, w, x, b, n): asci = {} for i in "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM": asci[i] = ord(i) for i in range(n): asci[x[i]] = b[i] arr = [] for i in w: arr.append(asci[i]) i = 0 j = 0 sm = 0 ans = -10000 ansi = 0 ansj = 0 while i < len(w): sm += arr[i] if sm > ans: ans = sm ansi = i ansj = j if sm < 0: sm = 0 j = i + 1 i += 1 return w[ansj : ansi + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
class Solution: def fillasciivalue(self, x, b, n, lower, upper): for i in range(n): value = ord(x[i]) if value >= 65 and value <= 90: upper[value - 65] = b[i] else: lower[value - 97] = b[i] def maxSum(self, w, x, b, n): lower = [i for i in range(97, 123)] upper = [i for i in range(65, 91)] self.fillasciivalue(x, b, n, lower, upper) maxvalue = 0 max_till_now = 0 ans = "" tmp = "" least_negative_chr = "" negative = -(10**9) for alpha in w: value = ord(alpha) sm = upper[value - 65] if value >= 65 and value <= 90 else lower[value - 97] maxvalue += sm tmp += alpha if maxvalue < 0: maxvalue = 0 tmp = "" if sm < 0 and sm > negative: negative = sm least_negative_chr = alpha if max_till_now < maxvalue: ans = tmp max_till_now = maxvalue return ans if ans != "" else least_negative_chr
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR STRING VAR VAR
Given a string w, integer array b, character array x and an integer n. n is the size of array b and array x. Find a substring which has the sum of the ASCII values of its every character, as maximum. ASCII values of some characters are redefined. Note: Uppercase & lowercase both will be present in the string w. Array b and Array x contain corresponding redefined ASCII values. For each i, b[i] contain redefined ASCII value of character x[i]. Example 1: Input: W = abcde N = 1 X[] = { 'c' } B[] = { -1000 } Output: de Explanation: Substring "de" has the maximum sum of ascii value, including c decreases the sum value Example 2: Input: W = dbfbsdbf N = 2 X[] = { 'b','s' } B[] = { -100, 45 } Output: dbfbsdbf Explanation: Substring "dbfbsdbf" has the maximum sum of ascii value. Your Task: You don't need to read input or print anything. Your task is to complete the function maxSum() which takes string W, character array X, integer array B, integer N as length of array X and B as input parameters and returns the substring with the maximum sum of ascii value. Expected Time Complexity: O(|W|) Expected Auxiliary Space: O(1) Constraints: 1<=|W|<=100000 1<=|X|<=52 -1000<=B_{i}<=1000 Each character of W and A will be from a-z, A-Z.
from sys import maxsize class Solution: def maxSum(self, w, x, b, n): ts = 0 strt = 0 end = 0 max = -maxsize - 1 sum = 0 for i in range(0, len(w)): if w[i] in x: sum = sum + b[x.index(w[i])] else: sum = sum + ord(w[i]) if max < sum: max = sum strt = ts end = i if sum < 0: sum = 0 ts = i + 1 return w[strt : end + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
T = int(input()) cache = {} def compute(width): if width < 4: cache[width] = 1 return 1 else: l, r = width - 1, width - 4 cache[l] = cache.get(l, None) or compute(l) cache[r] = cache.get(r, None) or compute(r) return cache[l] + cache[r] def count_primes(n): A = [False] * 2 + [True] * (n - 2) for number, is_prime in enumerate(A): if is_prime: for x in range(number * number, n, number): A[x] = False return sum([(1) for x in A if x == True]) while T > 0: print(count_primes(compute(int(input())) + 1)) T -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NONE FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NONE FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
import itertools def erat2(): D = {} yield 2 for q in itertools.islice(itertools.count(3), 0, None, 2): p = D.pop(q, None) if p is None: D[q * q] = q yield q else: x = p + q while x in D or not x & 1: x += p D[x] = p def get_primes_erat(n): return list(itertools.takewhile(lambda p: p < n, erat2())) def numberofways(N): if N < 4: return 1 return numberofways(N - 1) + numberofways(N - 4) for case in range(int(input())): N = int(input()) print(len(get_primes_erat(numberofways(N) + 1)))
IMPORT FUNC_DEF ASSIGN VAR DICT EXPR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NONE NUMBER ASSIGN VAR FUNC_CALL VAR VAR NONE IF VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR EXPR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def sieve(n): np1 = n + 1 s = list(range(np1)) s[1] = 0 sqrtn = int(round(n**0.5)) for i in range(2, sqrtn + 1): if s[i]: s[i * i : np1 : i] = [0] * len(range(i * i, np1, i)) return len(list(filter(None, s))) inp = [] for _ in range(int(input())): inp.append(int(input())) m = max(inp) a = [1, 1, 1, 2] for i in range(4, m): a.append(a[i - 1] + a[i - 4]) for i in inp: print(sieve(a[i - 1]))
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NONE VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
T = int(input()) for t in range(T): N = int(input()) dp = [1, 1, 1, 1] for i in range(4, 1 + N): dp.append(dp[i - 1] + dp[i - 4]) x = dp[N] prime = [True] * (1 + x) prime[0] = prime[1] = False p = 2 while p * p <= x: if prime[p]: for g in range(2 * p, x + 1, p): prime[g] = False p += 1 ans = 0 for g in prime: if g: ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def findConfig(N, configs): if len(configs) == 0: configs.append(0) configs.append(1) configs.append(1) configs.append(1) configs.append(2) index = 5 else: index = len(configs) for i in range(index, N + 1): numWays = configs[i - 4] + configs[i - 1] configs.append(numWays) return configs[N] def populatePrimes(N, primes, base): if len(primes) < N + 1: for i in range(len(primes), N + 1): primes.append(True) while base <= N // 2: value = base * 2 while value <= N: primes[value] = False value += base base += 1 return primes numInputs = int(input()) configs = [] primes = [] base = 2 for i in range(numInputs): N = int(input()) numConfigs = findConfig(N, configs) populatePrimes(numConfigs, primes, base) count = 0 for i in range(2, numConfigs + 1): if primes[i]: count += 1 print(count)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
T = int(input()) for _ in range(T): N = int(input()) DP = [1] * (N + 1) for i in range(4, N + 1): DP[i] = DP[i - 1] + DP[i - 4] Num = DP[N] sieve = [True] * (Num + 1) p = 2 primes = 0 while p <= Num: primes += 1 for k in range(p + p, Num + 1, p): sieve[k] = False p += 1 while p <= Num and not sieve[p]: p += 1 print(primes)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
_MAX_VALUE = 250000 def _is_prime(n): if n <= 3: return n >= 2 if n % 2 == 0 or n % 3 == 0: return False for i in range(5, int(n**0.5) + 1, 6): if n % i == 0 or n % (i + 2) == 0: return False return True def _calc_primes(_MAX_VALUE): nprimes = [(0) for _ in range(_MAX_VALUE)] for i in range(1, _MAX_VALUE): nprimes[i] = nprimes[i - 1] + _is_prime(i) return nprimes def _num_combinations(n): variants = [(0) for _ in range(n + 1)] for i in range(n + 1): if i == 0: continue elif i == 5: break variants[i] = 1 for i in range(4, n + 1): variants[i] += variants[i - 1] + variants[i - 4] return variants[-1] def num_primes(n, primes): return primes[_num_combinations(n)] primes = _calc_primes(_MAX_VALUE) t = int(input()) for _ in range(t): n = int(input()) print(num_primes(n, primes))
ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER FUNC_DEF RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def nchoosek(n, k): p = 1 for i in range(k): p = p * (n - i) // (i + 1) return p def countm(n): s = 0 for nh in range(n // 4 + 1): nv = n - 4 * nh s += nchoosek(nh + nv, nh) return s def genIsPrime(pmax): pend = pmax + 1 is_prime = pend * [True] is_prime[0] = is_prime[1] = False for i in range(2, pend): if is_prime[i]: for j in range(i * i, pend, i): is_prime[j] = False return is_prime t = int(input()) for _ in range(t): n = int(input()) m = countm(n) print(sum(genIsPrime(m)))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def primes(n): if n <= 2: return 0 sieve = [True] * n for i in range(3, int(n**0.5) + 1, 2): if sieve[i]: sieve[i * i :: 2 * i] = [False] * int((n - i * i - 1) / (2 * i) + 1) return len([i for i in range(3, n, 2) if sieve[i]]) + 1 def find_configs(N): if N == 0: return 1 elif N < 0: return 0 return find_configs(N - 1) + find_configs(N - 4) T = int(input()) for i in range(T): print(primes(find_configs(int(input())) + 1))
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
_brick_cache = {} def brick_combinations(N): if N < 0: return 0 if N < 4: return 1 if N in _brick_cache: return _brick_cache[N] answer = brick_combinations(N - 1) + brick_combinations(N - 4) _brick_cache[N] = answer return answer def primes(n): sieve = [True] * n for i in range(3, int(n**0.5) + 1, 2): if sieve[i]: sieve[i * i :: 2 * i] = [False] * ((n - i * i - 1) // (2 * i) + 1) return [2] + [i for i in range(3, n, 2) if sieve[i]] def primes_less_than(M): if M > 20: ps = primes(M + 1) else: ps = [2, 3, 5, 7, 11, 13, 17, 19] check = [p for p in ps if p <= M] return len(check) t = int(input()) for _ in range(t): n = int(input()) comb = brick_combinations(n) print(primes_less_than(comb))
ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP LIST NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER RETURN BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def seive(n): primes = [True] * n l = 0 primes[0] = primes[1] = False for i in range(2, n): if primes[i]: l += 1 primes[2 * i :: i] = [False] * ((n - 1) // i - 1) return l def tile(n): m = [1] * (n + 1) for i in range(4, n + 1): m[i] = m[i - 1] + m[i - 4] return seive(m[n] + 1) t = int(input()) for i in range(t): print(tile(int(input())))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP LIST NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF 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 BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def isPrime(n): if n % 2 == 0: return False i = 3 while i * i <= n: if n % i == 0: return False i += 2 return True def primes(n): if n == 1: return 0 i = 3 cnt = 1 while i <= n: if isPrime(i): cnt += 1 i += 2 return cnt def pascal_triangle(n): pascal = [] pascal.append([1, 0]) for i in range(1, n + 1): pascal.append([]) pascal[i] = [0] * (i + 2) pascal[i][0] = 1 for j in range(1, i + 1): pascal[i][j] = pascal[i - 1][j] + pascal[i - 1][j - 1] return pascal def combs(n, pascal): c = 1 segs = n // 4 for i in range(1, segs + 1): c += pascal[n - i * 4 + i][i] return c pascal = pascal_triangle(40) for test in range(int(input().strip())): N = int(input().strip()) print(primes(combs(N, pascal)))
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
cache = {} def sols(n): if n in cache: return cache[n] else: res = 1 for i in range(n - 3): res += sols(i) cache[n] = res return cache[n] primes = [1] * 1000000 primes[0] = 0 primes[1] = 0 for i in range(2, len(primes)): if primes[i] == 1: for k in range(2, (len(primes) - 1) // i + 1): primes[k * i] = 0 psums = [0] for i in range(1, len(primes)): psums.append(psums[-1]) if primes[i]: psums[-1] += 1 t = int(input()) for _ in range(t): n = int(input()) m = sols(n) print(psums[m])
ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
T = int(input()) def primes(n): ls = [True] * n limit = int(n**0.5) + 1 for i in range(2, limit): if ls[i]: j, k = i**2, 1 while j < n: ls[j] = False j = i**2 + k * i k += 1 return sum([a for a in ls[2:] if a]) for t in range(T): N = int(input()) if N < 4: print(0) continue dp = [0] * (N + 1) for i in range(4): dp[i] = 1 for i in range(4, N + 1): dp[i] = dp[i - 1] + dp[i - 4] print(primes(dp[N] + 1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def isprime(n): if n == 1: return False for i in range(2, int(n**0.5 + 1)): if n % i == 0: return False return True def calprime(n): c = 0 for i in range(2, n + 1): if isprime(i): c = c + 1 return c def calno(n): a = [1, 1, 1, 1] for i in range(4, n + 1): a.append(a[i - 1] + a[i - 4]) return calprime(a[n]) t = int(input()) ar = [] for i in range(t): ar.append(int(input())) for i in range(t): print(calno(ar[i]))
FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def getPrimes(n): primeList = [] for i in range(2, n + 1): if isPrime(i): primeList.append(i) return len(primeList) def isPrime(n): for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True t = int(input()) for i in range(t): n = int(input()) combBricks = [0] * n for i in range(n): if i < 3: combBricks[i] = 1 elif i == 3: combBricks[i] = 2 else: combBricks[i] = combBricks[i - 1] + combBricks[i - 4] limit = combBricks[n - 1] result = getPrimes(limit) print(result)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def countprimes3(x): if x == 1: return [] else: a = [False, False] + [(True) for i in range(2, x + 1)] i = 2 while i <= x**0.5: if a[i] is True: k = 0 while i**2 + k * i <= x: a[i**2 + k * i] = False k += 1 i += 1 return a def bricks(n): if n in range(0, 4): return 1 else: return bricks(n - 1) + bricks(n - 4) i = int(input()) for _ in range(i): j = int(input()) z = bricks(j) print(countprimes3(z).count(True))
FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR NUMBER NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def is_prime(n): if n <= 3: return n >= 2 if n % 2 == 0 or n % 3 == 0: return False for i in range(5, int(n**0.5) + 1, 6): if n % i == 0 or n % (i + 2) == 0: return False return True def nCr(n, r): res = 1.0 for i in range(1, r + 1): res *= (n + 1 - i) / i return res def calc(n): fours = int(n / 4) res = 1.0 for i in range(1, fours + 1): res += nCr(n - i * 4 + i, i) if res < 2: return 0 p = 1 for i in range(1, int(res - 2), 2): p += 1 if is_prime(i + 2) else 0 p += 1 if res % 2 != 0 and is_prime(res) else 0 return p for i in range(int(input())): print(calc(int(input())))
FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def memoize(func): pool = {} def wrapper(*arg): if arg not in pool: pool[arg] = func(*arg) return pool[arg] return wrapper def get_primes(max): sieve = [True] * max sieve[0] = False sieve[1] = False for i in range(int(max**0.5)): if sieve[i]: for composite in range(i * i, max, i): sieve[composite] = False return [index for index, is_prime in enumerate(sieve) if is_prime] primes = get_primes(217286 + 1) T = int(input()) for cases in range(T): N = int(input()) @memoize def get_ways(n): if n < 0: return 0 elif n == 0: return 1 else: return get_ways(n - 1) + get_ways(n - 4) print(sum(1 for i in primes if i <= get_ways(N)))
FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
nt = int(input()) np = [ 0, 0, 0, 0, 1, 2, 2, 3, 4, 4, 6, 8, 9, 11, 15, 19, 24, 32, 42, 53, 68, 91, 119, 155, 204, 269, 354, 462, 615, 816, 1077, 1432, 1912, 2543, 3385, 4522, 6048, 8078, 10794, 14475, 19385, ] for i in range(nt): print(np[int(input())])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
T = int(input()) A = [1] limit = 220000 for i in range(1, 41): A.append(A[i - 1] * i) isP = [True] * limit isP[0] = isP[1] = False for i in range(2, limit): if isP[i]: for j in range(i + i, limit, i): isP[j] = False C = [0] * limit acc = 0 for i in range(2, limit): if isP[i]: acc += 1 C[i] = acc for _ in range(T): N = int(input()) L = N // 4 answer = 0 for i in range(0, L + 1): M = N - i * 4 answer += A[M + i] // A[M] // A[i] print(C[answer])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR 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 FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def primes(n): ps = [2] for i in range(3, n + 1): for j in ps: if j * j > i: ps.append(i) break if i % j == 0: break return ps T = int(input()) vals = [] maxVal = 0 for i in range(T): vals.append(int(input())) if vals[i] > maxVal: maxVal = vals[i] N = [1, 1, 1, 2] for i in range(4, maxVal): N.append(N[i - 1] + N[i - 4]) for i in vals: if i <= 3: print(0) else: print(len(primes(N[i - 1])))
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
import sys C = 4 def generate_primes(n): if n == 2: return [2] elif n < 2: return [] sieve = list(range(3, n + 1, 2)) root_n = n**0.5 mid = (n + 1) // 2 - 1 i = 0 m = 3 while m <= root_n: if sieve[i]: j = (m * m - 3) // 2 sieve[j] = 0 while j < mid: sieve[j] = 0 j += m i += 1 m = 2 * i + 3 return [2] + [p for p in sieve if p] def choose(n, k): if k < 0 or k > n: return 0 else: p, q = 1, 1 for i in range(1, min(k, n - k) + 1): p *= n q *= i n -= 1 return p // q T = int(sys.stdin.readline()) for _ in range(T): N = int(sys.stdin.readline()) count, n = 1, 1 if N % C == 0: count += 1 while N - n * C > 0: count += choose(N - (C - 1) * n, n) n += 1 print(len(generate_primes(count)))
IMPORT ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER IF VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP LIST NUMBER VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
num_ways = [(-1) for x in range(40)] for i in range(3): num_ways[i] = 1 num_ways[3] = 2 for i in range(4, 40): num_ways[i] = num_ways[i - 1] + num_ways[i - 4] isprime = [(-1) for x in range(num_ways[39])] isprime[0] = 1 i = 2 while i < num_ways[39] + 1: if isprime[i - 1] == 0: i += 1 continue isprime[i - 1] = 1 j = 2 while i * j <= num_ways[39]: isprime[i * j - 1] = 0 j += 1 i += 1 num_primes = [(-1) for x in range(40)] num_primes[0] = 0 for i in range(1, 40): new_primes = 0 for j in range(num_ways[i - 1] + 1, num_ways[i] + 1): if isprime[j - 1] == 1: new_primes += 1 num_primes[i] = new_primes + num_primes[i - 1] T = int(input()) for i in range(T): N = int(input()) print(num_primes[N - 1])
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
vals = [ 1, 1, 1, 2, 3, 4, 5, 7, 10, 14, 19, 26, 36, 50, 69, 95, 131, 181, 250, 345, 476, 657, 907, 1252, 1728, 2385, 3292, 4544, 6272, 8657, 11949, 16493, 22765, 31422, 43371, 59864, 82629, 114051, 157422, 217286, 299915, 413966, ] primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, ] def isprime(val): if val == 2: return True if val % 2 == 0 or val == 1: return False i = 0 limit = int(val**0.5) + 1 while primes[i] < limit: if val % primes[i] == 0: return False else: i += 1 return True cases = int(input()) for i in range(cases): case = int(input()) case = vals[case - 1] i = 0 if case <= primes[-2]: while primes[i] <= case: i += 1 elif case == primes[-1]: i = len(primes) else: j = primes[-1] + 1 i = len(primes) while case >= primes[-1]: if isprime(j): primes.append(j) i += 1 j += 1 i = i - 1 print(i)
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
import sys def main(): numinputs = int(input()) dp = [1, 1, 1, 1] inputs = [int(val) for val in sys.stdin] for i in range(4, 41): dp.append(dp[i - 1] + dp[i - 4]) n = max(dp) primes = find_primes(n + 1) primes = count_primes(primes, n + 1) for x in inputs: print(primes[dp[x]]) def find_primes(n): primes = [val for val in range(2, n + 1)] primes = [0, 0] + primes i = 2 while i * i <= n: for j in range(2 * i, n + 1, i): primes[j] = 0 for temp in range(i + 1, n + 1): if primes[temp] != 0: i = temp break return primes def count_primes(primes, n): count = 0 for i in range(n + 1): if primes[i] != 0: count += 1 primes[i] = count return primes main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def sieve(n): if n < 3: return [] s = list(range(n // 2)) for i in s: if i: k = i - ~i for j in range(k * k, n, 2 * k): s[j // 2] = 0 primes = [2] primes += [(2 * s[prime] + 1) for prime in range(len(s)) if s[prime] > 0] return primes def f(n): if n < 4: return 1 else: return f(n - 1) + f(n - 4) for t in [1] * int(input()): countWays = f(int(input())) siev = sieve(countWays + 1) print(len(siev))
FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
values = [] values.append(1) for i in range(1, 41): value = values[i - 1] if i - 4 >= 0: value = value + values[i - 4] values.append(value) primes = [True] * (values[-1] + 1) primes[0] = False primes[1] = False for i in range(2, len(primes)): if primes[i]: for j in range(i * i, len(primes), i): primes[j] = False primes_count = [0] * (values[-1] + 1) for i in range(1, len(primes)): primes_count[i] = primes_count[i - 1] if primes[i]: primes_count[i] = primes_count[i] + 1 test_count = int(input()) for i in range(0, test_count): print(primes_count[values[int(input())]])
ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def walls(laid, height): if laid > height: return 0 if laid == height: return 1 return walls(laid + 1, height) + walls(laid + 4, height) def primes_less_than(M): sieve = [True] * (M + 1) sieve[0] = sieve[1] = False prime = 2 while prime**2 <= M: if sieve[prime]: for i in range(prime**2, M + 1, prime): sieve[i] = False prime += 1 return len([i for i in range(2, M + 1) if sieve[i]]) T = int(input()) for i in range(T): N = int(input()) arrangements = walls(0, N) print(primes_less_than(arrangements))
FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def confs(n): if n < 4: return 1 elif n == 4: return 2 else: return confs(n - 1) + confs(n - 4) def sieve(m): arr = [1] * m arr[0] = arr[1] = 0 primes = 0 for i in range(2, m): if arr[i] == 1: primes += 1 for j in range(i * 2, m, i): arr[j] = 0 return primes for _ in range(int(input())): n = int(input()) m = confs(n) p = sieve(m + 1) print(p)
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def primes(x): ar = [True] * (x + 1) ar[0] = False ar[1] = False for i in range(2, int(x ** (1 / 2)) + 1): if ar[i] == True: for j in range(2 * i, x + 1, i): ar[j] = False return ar.count(True) t = int(input()) for _ in range(t): n = int(input()) ar = [0] * (n + 1) ar[0] = 1 for i in range(1, n + 1): if i - 1 >= 0: ar[i] += ar[i - 1] if i - 4 >= 0: ar[i] += ar[i - 4] print(primes(ar[n]))
FUNC_DEF 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 FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
import sys n = int(input()) ts = [] for i in range(n): ts.append(int(input())) maxt = max(ts) ms = [1, 1, 1, 2] for i in range(4, maxt): ms.append(ms[i - 1] + ms[i - 4]) maxm = ms[-1] isprime = [True] * (maxm - 1) i = 2 while i <= maxm: if not isprime[i - 2]: i = i + 1 continue j = i + i while j <= maxm: isprime[j - 2] = False j += i i = i + 1 ps = [0] for a in isprime: if a: ps.append(ps[-1] + 1) else: ps.append(ps[-1]) for t in ts: print(ps[ms[t - 1] - 1])
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
T = int(input()) wall_widths = [int(input()) for _ in range(T)] max_wall_width = max(*wall_widths) width_to_num_formats = [1] for w in range(1, max_wall_width + 1): num_formats = width_to_num_formats[w - 1] if w >= 4: num_formats += width_to_num_formats[w - 4] width_to_num_formats.append(num_formats) primes_up_to = {(0): 0, (1): 0} composites = set() for i in range(2, width_to_num_formats[max_wall_width] + 1): primes_up_to[i] = primes_up_to.get(i - 1, 0) if i in composites: continue primes_up_to[i] += 1 mult = 2 while True: product = mult * i if product > width_to_num_formats[max_wall_width]: break composites.add(product) mult += 1 for w in wall_widths: print(primes_up_to[width_to_num_formats[w]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def wall(n): if n < 4: return 1 else: return wall(n - 1) + wall(n - 4) def isPrime(n): for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True def primeCounter(n): primeCount = 0 if n > 1: for num in range(2, n + 1): if isPrime(num): primeCount += 1 return primeCount def main(): T = int(input()) for i in range(T): N = int(input()) configCount = wall(N) primeCount = primeCounter(configCount) print(primeCount) main()
FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
__author__ = "said" def numberOfP(m): counter = 0 zahlen = [True] * (m + 1) zahlen[0] = False zahlen[1] = False p = 2 while p * p <= m: if zahlen[p] == True: for k in range(p * p, m + 1, p): zahlen[k] = False p = p + 1 for i, v in enumerate(zahlen): if v == True: counter += 1 return counter def possibilities(n): if n < 4: return 1 else: sum = 1 for j in range(0, n - 3): sum += possibilities(n - (j + 4)) return sum numberofInputIntegers = int(input()) for w in range(0, numberofInputIntegers): m = possibilities(int(input())) print(numberOfP(m))
ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
sols = {} sols[1] = 1 sols[2] = 1 sols[3] = 1 sols[4] = 2 def config(n): if n <= 0: return 0 sum = 0 if n in sols.keys(): return sols[n] else: sum = config(n - 1) + config(n - 4) sols[n] = sum return sum def Prime(n): n = n + 1 arr = list(range(0, n)) for i in range(2, n // 2 + 1): index = 2 while i * index < n: arr[i * index] = 0 index += 1 i += 1 arr[0] = 0 arr[1] = 0 primes = [] for i in arr: if i != 0: primes.append(i) return len(primes) t = int(input()) while t > 0: n = int(input()) s = config(n) print(Prime(s)) t = t - 1
ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def primes235(limit): if limit >= 2: yield 2 if limit >= 3: yield 3 if limit >= 5: yield 5 if limit < 7: return modPrms = [7, 11, 13, 17, 19, 23, 29, 31] gaps = [4, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2, 4, 6, 2, 6] ndxs = [ 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 7, 7, ] lmtbf = (limit + 23) // 30 * 8 - 1 lmtsqrt = int(limit**0.5) - 7 lmtsqrt = lmtsqrt // 30 * 8 + ndxs[lmtsqrt % 30] buf = [True] * (lmtbf + 1) for i in range(lmtsqrt + 1): if buf[i]: ci = i & 7 p = 30 * (i >> 3) + modPrms[ci] s = p * p - 7 p8 = p << 3 for j in range(8): c = s // 30 * 8 + ndxs[s % 30] buf[c::p8] = [False] * ((lmtbf - c) // p8 + 1) s += p * gaps[ci] ci += 1 for i in range(lmtbf - 6 + ndxs[(limit - 7) % 30]): if buf[i]: yield 30 * (i >> 3) + modPrms[i & 7] for t in range(int(input())): N = int(input()) A = [1, 1, 1, 1] for i in range(4, N + 1): A.append(A[i - 1] + A[i - 4]) print(len(list(primes235(A[N]))))
FUNC_DEF IF VAR NUMBER EXPR NUMBER IF VAR NUMBER EXPR NUMBER IF VAR NUMBER EXPR NUMBER IF VAR NUMBER RETURN ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP LIST NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def is_prime(n): if n <= 3: return True for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True li = [0] * 50 li[0] = 1 li[1] = 1 li[2] = 1 li[3] = 1 li[4] = 2 li[5] = 3 for i in range(6, 50): li[i] = li[i - 1] + li[i - 4] t = int(input()) for _ in range(t): n = int(input()) cnt = li[n] ans = 0 for i in range(2, cnt + 1): if is_prime(i): ans += 1 print(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
t = int(input()) def configurations(n): memo = {} if n < 4: return 1 else: x = configurations(n - 1) + configurations(n - 4) memo[n] = x return x def sieve(n): np1 = n + 1 s = list(range(np1)) s[1] = 0 sqrtn = int(round(n**0.5)) for i in range(2, sqrtn + 1): if s[i]: s[i * i : np1 : i] = [0] * len(range(i * i, np1, i)) return filter(None, s) for i in range(t): n = int(input()) x = configurations(n) print(len(list(sieve(x))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
T = int(input()) for t in range(T): m = int(input()) a = [0] * max(m + 1, 4) a[0] = a[1] = a[2] = a[3] = 1 for i in range(4, m + 1): a[i] = a[i - 1] + a[i - 4] maxPrime = a[m] isPrime = [True] * (maxPrime + 1) for i in range(2, maxPrime + 1): curr = 2 * i while curr <= maxPrime: isPrime[curr] = False curr += i nbPrimes = 0 for i in range(2, maxPrime + 1): if isPrime[i]: nbPrimes += 1 print(nbPrimes)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
nbCombMem = [], [] def nbComb(N): if N in nbCombMem[0]: return nbCombMem[1][nbCombMem[0].index(N)] if N < 0: return 0 c = 1 for i in range(0, N - 3): c += nbComb(N - 4 - i) nbCombMem[0].append(N) nbCombMem[1].append(c) return c def getPrimesUnder(n): r = [2] n2 = n // 2 l = list(True for i in range(0, n2)) l[0] = False for i in range(1, n2): if l[i]: r.append(2 * i + 1) for m in range(2 * i * (i + 1), n2, 2 * i + 1): l[m] = False return r T = int(input()) Cs = [nbComb(int(input())) for t in range(0, T)] Ps = getPrimesUnder(max(Cs) + 1) for t in range(0, T): c = 0 while c < len(Ps) and Ps[c] <= Cs[t]: c += 1 print(c)
ASSIGN VAR LIST LIST FUNC_DEF IF VAR VAR NUMBER RETURN VAR NUMBER FUNC_CALL VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def findPrimes(primes, n): if len(primes) < n + 1: primes = primes + [(1) for i in range(len(primes), n + 1)] num = int(n**0.5) primes[0] = primes[1] = 0 for i in range(2, num + 1): if primes[i] == 1: for j in range(2 * i, n + 1, i): primes[j] = 0 return primes def findWays(wayList, n): next_index = len(wayList) while next_index <= n: wayList.append(wayList[next_index - 1] + wayList[next_index - 4]) next_index += 1 else: return wayList[n] T = int(input()) wList = [1, 1, 1, 1] primes = [0, 0] for i in range(T): N = int(input()) ways = findWays(wList, N) primes = findPrimes(primes, ways) P = primes[: ways + 1].count(1) print(P)
FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
import sys def numConfigs(n): if n < 0: return 0 if n == 1 or n == 0: return 1 return numConfigs(n - 1) + numConfigs(n - 4) def numPrimes(n): numPrimes = 0 for i in range(2, n + 1): foundPrime = False j = 2 while j * j <= i and not foundPrime: if i % j == 0: foundPrime = True j += 1 if not foundPrime: numPrimes += 1 return numPrimes numCases = int(sys.stdin.readline().strip()) for i in range(numCases): bricks = int(sys.stdin.readline().strip()) configs = numConfigs(bricks) print(numPrimes(configs))
IMPORT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
dp = [1] + [0] * 43 for i in range(40): dp[i + 1] += dp[i] dp[i + 4] += dp[i] sieve = [1] * (dp[40] * 2) primes = [] for i in range(2, len(sieve)): if sieve[i]: primes.append(i) for j in range(2, dp[40] * 2 // i): sieve[i * j] = 0 primes.append(1000000000) for _ in range(int(input().strip())): N = dp[int(input().strip())] i = 0 while N >= primes[i]: i += 1 print(i)
ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
case_count = int(input()) cases = [int(input()) for _ in range(case_count)] def prime_number_count(maximum): prime_count = 0 vec = [(True) for _ in range(maximum + 1)] for i in range(2, maximum + 1): if vec[i]: prime_count += 1 for j in range(2, maximum + 1): if i * j > maximum: break vec[i * j] = False return prime_count for case in cases: solution_count = [(0) for _ in range(case + 1)] solution_count[0] = 1 for i in range(1, case + 1): if i >= 4: solution_count[i] = solution_count[i - 1] + solution_count[i - 4] else: solution_count[i] = solution_count[i - 1] final_count = solution_count[case] print(prime_number_count(final_count))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
dp = [ 0, 0, 0, 0, 1, 2, 2, 3, 4, 4, 6, 8, 9, 11, 15, 19, 24, 32, 42, 53, 68, 91, 119, 155, 204, 269, 354, 462, 615, 816, 1077, 1432, 1912, 2543, 3385, 4522, 6048, 8078, 10794, 14475, 19385, ] T = int(input()) for i in range(0, T): N = int(input()) print(dp[N])
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
res = [1] * 4 for i in range(4, 41): res.append(res[-1] + res[-4]) mx = res[-1] l = [False] * 2 + [True] * (mx - 1) z = [False] * (mx // 2) for i in range(2, int(mx**0.5) + 1): if l[i]: l[i * i :: i] = z[: mx // i - i + 1] for i in range(int(input())): print(sum(l[: res[int(input())] + 1]))
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER 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 FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
def is_prime(n): if n % 3 == 0: return False r = int(n**0.5) f = 5 while f <= r: if n % f == 0: return False if n % (f + 2) == 0: return False f += 6 return True def ma(i, j): r = 1 for u in range(i - j): r = r * i i = i - 1 return r * j def facd(n): u = 2 if n < 2: return 0 elif n == 2: return 1 elif n == 3: return 2 else: for i in range(3, n + 1, 6): if i + 2 <= n: if is_prime(i + 2): u = u + 1 if i + 4 <= n: if is_prime(i + 4): u = u + 1 return u def fac(n, r): if r == n or r == 0 or n == 0: return 1 else: return int(ma(n, r + 1) / ma(n - r, 1)) a = int(input()) for i in range(a): d = 1 b = int(input()) c = b if b < 4: print(0) elif b == 4: print(1) else: y = 0 while c >= 4: y = y + 1 c = c - 4 d = d + fac(c + y, c) e = facd(d) print(e)
FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
import sys def num_ways(N, values): if N == 0: return 1 if N < 0: return 0 result = 0 for i, v in enumerate(values): result += num_ways(N - v, values) return result def sieve(N): ledger = [True] * (N + 1) ledger[0] = False ledger[1] = False primes = [] while True: try: next_prime = next(i for i, v in enumerate(ledger) if v) primes.append(next_prime) for i in range(next_prime, N + 1, next_prime): ledger[i] = False except StopIteration: break return primes num_primes = { (1): 0, (2): 0, (3): 0, (4): 1, (5): 2, (6): 2, (7): 3, (8): 4, (9): 4, (10): 6, (11): 8, (12): 9, (13): 11, (14): 15, (15): 19, (16): 24, (17): 32, (18): 42, (19): 53, (20): 68, (21): 91, (22): 119, (23): 155, (24): 204, (25): 269, (26): 354, (27): 462, (28): 615, (29): 816, (30): 1077, (31): 1432, (32): 1912, (33): 2543, (34): 3385, (35): 4522, (36): 6048, (37): 8078, (38): 10794, (39): 14475, (40): 19385, } T = int(sys.stdin.readline()) for case in range(T): N = int(sys.stdin.readline()) print(num_primes[N])
IMPORT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
t = int(input()) for testCase in range(t): n = int(input()) m = 1 for i in range(4, n + 1, 4): a = i // 4 b = n - i + 1 currNum = 1 for j in range(a): currNum *= b + j currNum //= j + 1 m += currNum if m < 2: print(0) else: primes = [2] for i in range(3, m + 1, 2): j = 0 while primes[j] * primes[j] < i: if i % primes[j] == 0: break j += 1 if primes[j] * primes[j] > i: primes += [i] print(len(primes))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
arr = [0] * (40 + 1) arr[0] = 1 for i in range(1, 40 + 1): arr[i] = arr[i - 1] if i >= 4: arr[i] += arr[i - 4] MAX = 217286 prime = [True] * (MAX + 1) prime[0] = False prime[1] = False count = [0] * (MAX + 1) for i in range(2, MAX + 1): count[i] = count[i - 1] if prime[i]: count[i] += 1 t = 2 while t * i <= MAX: prime[t * i] = False t += 1 t = eval(input()) while t > 0: t -= 1 print(count[arr[eval(input())]])
ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
t = int(input()) dp = [1] + [0] * 50 for i in range(1, 41): dp[i] = dp[i - 1] + (dp[i - 4] if i - 4 >= 0 else 0) def isPrime(p): i = 2 while i * i <= p: if p % i == 0: return False i += 1 return True np, cnt = {}, 0 np[1] = 0 for i in range(2, max(dp) + 1): if isPrime(i): cnt += 1 if i in dp: np[i] = cnt for _ in range(t): n = int(input()) print(np[dp[n]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR DICT NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows. There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where $1\leq n\leq4$: There is one more step to the puzzle. Call the number of possible arrangements $\mbox{M}$. Patrick must calculate the number of prime numbers $\mbox{P}$ in the inclusive range $\textbf{0}-M$. As an example, assume $n=3$. From the diagram above, we determine that there is only one configuration that will cover the wall properly. $\mbox{I}$ is not a prime number, so $P=0$. A more complex example is $n=5$. The bricks can be oriented in $3$ total configurations that cover the wall. The two primes $2$ and $3$ are less than or equal to $3$, so $P=2$. Function Description Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer. redJohn has the following parameter(s): n: an integer that denotes the length of the wall Input Format The first line contains the integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ lines contains an integer $n$, the length of the $4\times n$ wall. Constraints $1\leq t\leq20$ $1\leq n\leq40$ Output Format Print the integer $\mbox{P}$ on a separate line for each test case. Sample Input 2 1 7 Sample Output 0 3 Explanation For $n=1$, the brick can be laid in 1 format only: vertically. The number of primes $\leq1$ is $\mbox{o}$. For $unrecognized$, one of the ways in which we can lay the bricks is There are $5$ ways of arranging the bricks for $n=7$ and there are $3$ primes $\leq5$.
tmp = [1] * 41 for i in range(4, 41): tmp[i] = tmp[i - 1] + tmp[i - 4] primes = [1] * (tmp[40] + 2) primes[0], primes[1] = 0, 0 for i in range(4, len(primes), 2): primes[i] = 0 for i in range(3, len(primes), 2): if primes[i] == 1: for j in range(2 * i, len(primes), i): primes[j] = 0 T = int(input()) for t in range(T): M = tmp[int(input())] print(sum(primes[0 : M + 1]))
ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
<image> To monitor cryptocurrency exchange rates trader William invented a wonderful device consisting of n lights arranged in a row. The device functions in the following way: Initially, all lights on William's device are turned off. At the beginning of a new iteration the device randomly, with a uniform distribution, picks a light that is turned off and turns it on, telling William which cryptocurrency he should invest in. After this iteration if any k consecutive lights contain more than one turned on light, then the device finishes working. William doesn't like uncertainty, so he wants you to calculate the expected value of the number of lights that are turned on in the device after it finishes working. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10). Description of the test cases follows. The only line for each test case contains two integers n and k (2 ≀ k ≀ n ≀ 10^5), which are the total number of lights and the length of subsegment of lights that are being checked, respectively. Output For each test case print the answer, modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≑ 0 \pmod{M}. Output the integer equal to p β‹… q^{-1} mod M. In other words, output such an integer x that 0 ≀ x < M and x β‹… q ≑ p \pmod{M}. Example Input 3 3 2 15 2 40 15 Output 333333338 141946947 329622137 Note Explanation of the first sample test case: Let's write out all possible sequences of light toggles, which will make the device complete its operation: 1. (1, 2) β€” 2 lights are turned on 2. (1, 3, 2) β€” 3 lights are turned on 3. (2, 1) β€” 2 lights are turned on 4. (2, 3) β€” 2 lights are turned on 5. (3, 2) β€” 2 lights are turned on 6. (3, 1, 2) β€” 3 lights are turned on Then the final expected value will be equal to 2/6 + 3/6 + 2/6 + 2/6 + 2/6 + 3/6 = 14/6 = 7/3. Then the required output will be 333333338, since 333333338 β‹… 3 ≑ 7 \pmod{10^9+7}.
import sys input = sys.stdin.readline MOD = int(1000000000.0 + 7) G = [0] * int(100000.0 + 2) GI = [0] * int(100000.0 + 2) G[0] = 1 for i in range(1, len(G)): G[i] = G[i - 1] * i % MOD GI[-1] = pow(G[-1], MOD - 2, MOD) for i in range(len(GI) - 2, -1, -1): GI[i] = GI[i + 1] * (i + 1) % MOD def A(n, V): if n - V < 0: return 0 return G[n] * GI[n - V] % MOD def solve(): n, V = map(int, input().split()) a = [0] * (n + 1) a[0] = 1 for i in range(1, n): a[i] = A(n - (i - 1) * (V - 1), i) a1 = 0 for i in range(n): a1 = (a1 + (a[i] * (n - i) - a[i + 1]) * (i + 1) * G[n - i - 1] * GI[n]) % MOD print(a1) for i in range(int(input())): solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
<image> To monitor cryptocurrency exchange rates trader William invented a wonderful device consisting of n lights arranged in a row. The device functions in the following way: Initially, all lights on William's device are turned off. At the beginning of a new iteration the device randomly, with a uniform distribution, picks a light that is turned off and turns it on, telling William which cryptocurrency he should invest in. After this iteration if any k consecutive lights contain more than one turned on light, then the device finishes working. William doesn't like uncertainty, so he wants you to calculate the expected value of the number of lights that are turned on in the device after it finishes working. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10). Description of the test cases follows. The only line for each test case contains two integers n and k (2 ≀ k ≀ n ≀ 10^5), which are the total number of lights and the length of subsegment of lights that are being checked, respectively. Output For each test case print the answer, modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≑ 0 \pmod{M}. Output the integer equal to p β‹… q^{-1} mod M. In other words, output such an integer x that 0 ≀ x < M and x β‹… q ≑ p \pmod{M}. Example Input 3 3 2 15 2 40 15 Output 333333338 141946947 329622137 Note Explanation of the first sample test case: Let's write out all possible sequences of light toggles, which will make the device complete its operation: 1. (1, 2) β€” 2 lights are turned on 2. (1, 3, 2) β€” 3 lights are turned on 3. (2, 1) β€” 2 lights are turned on 4. (2, 3) β€” 2 lights are turned on 5. (3, 2) β€” 2 lights are turned on 6. (3, 1, 2) β€” 3 lights are turned on Then the final expected value will be equal to 2/6 + 3/6 + 2/6 + 2/6 + 2/6 + 3/6 = 14/6 = 7/3. Then the required output will be 333333338, since 333333338 β‹… 3 ≑ 7 \pmod{10^9+7}.
import sys input = sys.stdin.buffer.readline mod = 10**9 + 7 n = 10**5 fac = [0] * (n + 1) inv_fac = [0] * (n + 1) fac[0] = 1 for i in range(1, n + 1): fac[i] = fac[i - 1] * i % mod inv_fac[n] = pow(fac[n], mod - 2, mod) for i in range(n - 1, -1, -1): inv_fac[i] = inv_fac[i + 1] * (i + 1) % mod def choose(n, k): if k > n: return 0 return fac[n] * inv_fac[k] * inv_fac[n - k] % mod for _ in range(int(input())): n, k = map(int, input().split()) ans = 0 not_finished = [0] + [choose(n - (m - 1) * (k - 1), m) for m in range(1, n + 1)] for m in range(2, n + 1): num_finished = ((n - (m - 1)) * not_finished[m - 1] - m * not_finished[m]) % mod ans = (ans + num_finished * fac[m - 1] * fac[n - m] * inv_fac[n] * m) % mod print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
<image> To monitor cryptocurrency exchange rates trader William invented a wonderful device consisting of n lights arranged in a row. The device functions in the following way: Initially, all lights on William's device are turned off. At the beginning of a new iteration the device randomly, with a uniform distribution, picks a light that is turned off and turns it on, telling William which cryptocurrency he should invest in. After this iteration if any k consecutive lights contain more than one turned on light, then the device finishes working. William doesn't like uncertainty, so he wants you to calculate the expected value of the number of lights that are turned on in the device after it finishes working. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10). Description of the test cases follows. The only line for each test case contains two integers n and k (2 ≀ k ≀ n ≀ 10^5), which are the total number of lights and the length of subsegment of lights that are being checked, respectively. Output For each test case print the answer, modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≑ 0 \pmod{M}. Output the integer equal to p β‹… q^{-1} mod M. In other words, output such an integer x that 0 ≀ x < M and x β‹… q ≑ p \pmod{M}. Example Input 3 3 2 15 2 40 15 Output 333333338 141946947 329622137 Note Explanation of the first sample test case: Let's write out all possible sequences of light toggles, which will make the device complete its operation: 1. (1, 2) β€” 2 lights are turned on 2. (1, 3, 2) β€” 3 lights are turned on 3. (2, 1) β€” 2 lights are turned on 4. (2, 3) β€” 2 lights are turned on 5. (3, 2) β€” 2 lights are turned on 6. (3, 1, 2) β€” 3 lights are turned on Then the final expected value will be equal to 2/6 + 3/6 + 2/6 + 2/6 + 2/6 + 3/6 = 14/6 = 7/3. Then the required output will be 333333338, since 333333338 β‹… 3 ≑ 7 \pmod{10^9+7}.
import sys from sys import stdin def modfac(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv return factorials, invs def modnCr(n, r): return fac[n] * inv[n - r] * inv[r] % mod mod = 10**9 + 7 fac, inv = modfac(200000, mod) tt = int(stdin.readline()) for loop in range(tt): n, k = map(int, stdin.readline().split()) ans = 0 for p in range(1, n): free = n - (p - 1) * k - 1 if free < 0: break patt = modnCr(free + p, p) prob = fac[p] * fac[n - p] * inv[n] ans += patt * prob print((ans + 1) % mod)
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
<image> To monitor cryptocurrency exchange rates trader William invented a wonderful device consisting of n lights arranged in a row. The device functions in the following way: Initially, all lights on William's device are turned off. At the beginning of a new iteration the device randomly, with a uniform distribution, picks a light that is turned off and turns it on, telling William which cryptocurrency he should invest in. After this iteration if any k consecutive lights contain more than one turned on light, then the device finishes working. William doesn't like uncertainty, so he wants you to calculate the expected value of the number of lights that are turned on in the device after it finishes working. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10). Description of the test cases follows. The only line for each test case contains two integers n and k (2 ≀ k ≀ n ≀ 10^5), which are the total number of lights and the length of subsegment of lights that are being checked, respectively. Output For each test case print the answer, modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≑ 0 \pmod{M}. Output the integer equal to p β‹… q^{-1} mod M. In other words, output such an integer x that 0 ≀ x < M and x β‹… q ≑ p \pmod{M}. Example Input 3 3 2 15 2 40 15 Output 333333338 141946947 329622137 Note Explanation of the first sample test case: Let's write out all possible sequences of light toggles, which will make the device complete its operation: 1. (1, 2) β€” 2 lights are turned on 2. (1, 3, 2) β€” 3 lights are turned on 3. (2, 1) β€” 2 lights are turned on 4. (2, 3) β€” 2 lights are turned on 5. (3, 2) β€” 2 lights are turned on 6. (3, 1, 2) β€” 3 lights are turned on Then the final expected value will be equal to 2/6 + 3/6 + 2/6 + 2/6 + 2/6 + 3/6 = 14/6 = 7/3. Then the required output will be 333333338, since 333333338 β‹… 3 ≑ 7 \pmod{10^9+7}.
import sys input = sys.stdin.readline max_n = 2 * 10**5 fact, inv_fact = [0] * (max_n + 1), [0] * (max_n + 1) fact[0] = 1 mod = 10**9 + 7 def make_nCr_mod(): global fact global inv_fact for i in range(max_n): fact[i + 1] = fact[i] * (i + 1) % mod inv_fact[-1] = pow(fact[-1], mod - 2, mod) for i in reversed(range(max_n)): inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod make_nCr_mod() def nCr_mod(n, r): global fact global inv_fact res = 1 while n or r: a, b = n % mod, r % mod if a < b: return 0 res = res * fact[a] % mod * inv_fact[b] % mod * inv_fact[a - b] % mod n //= mod r //= mod return res mod = 10**9 + 7 t = int(input()) for _ in range(t): n, k = map(int, input().split()) ans = 1 for i in range(1, n // (k - 1) + 2): ans = ( ans + nCr_mod(n - (i - 1) * (k - 1), i) * pow(nCr_mod(n, i), mod - 2, mod) ) % mod print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
<image> To monitor cryptocurrency exchange rates trader William invented a wonderful device consisting of n lights arranged in a row. The device functions in the following way: Initially, all lights on William's device are turned off. At the beginning of a new iteration the device randomly, with a uniform distribution, picks a light that is turned off and turns it on, telling William which cryptocurrency he should invest in. After this iteration if any k consecutive lights contain more than one turned on light, then the device finishes working. William doesn't like uncertainty, so he wants you to calculate the expected value of the number of lights that are turned on in the device after it finishes working. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10). Description of the test cases follows. The only line for each test case contains two integers n and k (2 ≀ k ≀ n ≀ 10^5), which are the total number of lights and the length of subsegment of lights that are being checked, respectively. Output For each test case print the answer, modulo 10^9+7. Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≑ 0 \pmod{M}. Output the integer equal to p β‹… q^{-1} mod M. In other words, output such an integer x that 0 ≀ x < M and x β‹… q ≑ p \pmod{M}. Example Input 3 3 2 15 2 40 15 Output 333333338 141946947 329622137 Note Explanation of the first sample test case: Let's write out all possible sequences of light toggles, which will make the device complete its operation: 1. (1, 2) β€” 2 lights are turned on 2. (1, 3, 2) β€” 3 lights are turned on 3. (2, 1) β€” 2 lights are turned on 4. (2, 3) β€” 2 lights are turned on 5. (3, 2) β€” 2 lights are turned on 6. (3, 1, 2) β€” 3 lights are turned on Then the final expected value will be equal to 2/6 + 3/6 + 2/6 + 2/6 + 2/6 + 3/6 = 14/6 = 7/3. Then the required output will be 333333338, since 333333338 β‹… 3 ≑ 7 \pmod{10^9+7}.
import sys input = sys.stdin.readline mod = 10**9 + 7 N = 10**5 F, iF = [0] * (N + 1), [0] * (N + 1) F[0] = 1 for i in range(1, N + 1): F[i] = F[i - 1] * i % mod iF[-1] = pow(F[-1], mod - 2, mod) for i in range(N - 1, -1, -1): iF[i] = iF[i + 1] * (i + 1) % mod def cal(n, k): if k < 0 or k > n: return 0 return F[n] * iF[k] * iF[n - k] % mod for _ in range(int(input())): n, k = map(int, input().split()) ans = 1 x = 1 while n - (k - 1) * (x - 1) >= x: ans = (ans + cal(n - (k - 1) * (x - 1), x) * pow(cal(n, x), mod - 2, mod)) % mod x += 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired and came up with the tree of his own which he called a k-tree. A k-tree is an infinite rooted tree where: each vertex has exactly k children; each edge has some weight; if we look at the edges that goes from some vertex to its children (exactly k edges), then their weights will equal 1, 2, 3, ..., k. The picture below shows a part of a 3-tree. [Image] As soon as Dima, a good friend of Lesha, found out about the tree, he immediately wondered: "How many paths of total weight n (the sum of all weights of the edges in the path) are there, starting from the root of a k-tree and also containing at least one edge of weight at least d?". Help Dima find an answer to his question. As the number of ways can be rather large, print it modulo 1000000007 (10^9 + 7). -----Input----- A single line contains three space-separated integers: n, k and d (1 ≀ n, k ≀ 100; 1 ≀ d ≀ k). -----Output----- Print a single integer β€” the answer to the problem modulo 1000000007 (10^9 + 7). -----Examples----- Input 3 3 2 Output 3 Input 3 3 3 Output 1 Input 4 3 2 Output 6 Input 4 5 2 Output 7
mod = int(1000000000.0 + 7) n, k, d = map(int, input().split()) dp = [[(-1) for _ in " " * 2] for _ in range(101)] def ktree(sum, ch): if sum > n: return 0 if sum == n: if ch: return 1 else: return 0 if dp[sum][ch] != -1: return dp[sum][ch] ans = 0 for i in range(1, k + 1): if i < d: ans += ktree(sum + i, ch) % mod else: ans += ktree(sum + i, 1) dp[sum][ch] = ans % mod return ans % mod print(ktree(0, 0))
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR BIN_OP STRING NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR IF VAR RETURN NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired and came up with the tree of his own which he called a k-tree. A k-tree is an infinite rooted tree where: each vertex has exactly k children; each edge has some weight; if we look at the edges that goes from some vertex to its children (exactly k edges), then their weights will equal 1, 2, 3, ..., k. The picture below shows a part of a 3-tree. [Image] As soon as Dima, a good friend of Lesha, found out about the tree, he immediately wondered: "How many paths of total weight n (the sum of all weights of the edges in the path) are there, starting from the root of a k-tree and also containing at least one edge of weight at least d?". Help Dima find an answer to his question. As the number of ways can be rather large, print it modulo 1000000007 (10^9 + 7). -----Input----- A single line contains three space-separated integers: n, k and d (1 ≀ n, k ≀ 100; 1 ≀ d ≀ k). -----Output----- Print a single integer β€” the answer to the problem modulo 1000000007 (10^9 + 7). -----Examples----- Input 3 3 2 Output 3 Input 3 3 3 Output 1 Input 4 3 2 Output 6 Input 4 5 2 Output 7
from sys import stdin def calculate_main(n, k, mod): dp = [0] * (n + 1) dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): for j in range(i - 1, max(-1, i - k - 1), -1): dp[i] += dp[j] dp[i] %= mod return dp n, k, d = list(map(int, stdin.readline().split())) mod = 10**9 + 7 dp = calculate_main(n, k, mod) dp2 = [0] * (n + 1) for i in range(d, n + 1): for j in range(1, min(k + 1, i + 1)): if j < d: dp2[i] += dp2[i - j] dp2[i] %= mod else: dp2[i] += dp[i - j] dp2[i] %= mod print(dp2[n] % mod)
FUNC_DEF 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR