description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
import sys input = sys.stdin.readline s = list(input().rstrip()) n = len(s) l = [0] * n r = [0] * n for i in range(n): j = 0 while 0 <= i - j and i + j < n and s[i - j] == s[i + j]: l[i + j] += 1 r[i - j] += 1 j += 1 j = 1 while 0 <= i - j + 1 and i + j < n and s[i - j + 1] == s[i + j]: l[i + j] += 1 r[i - j + 1] += 1 j += 1 ans = 0 for i in range(n): for j in range(i + 1, n): ans += l[i] * r[j] print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
word = input() n = len(word) dp = [[(0) for i in range(n)] for j in range(n)] for i in range(n): dp[i][i] = 1 for i in range(1, n): for j in range(n - i): if word[j] == word[j + i]: if i - 1 == 0: dp[j][j + i] = 1 elif dp[j + 1][j + i - 1] == 1: dp[j][j + i] = 1 pre = [(0) for i in range(n)] post = [(0) for i in range(n)] for i in range(n): cnt = 0 for j in range(n): cnt += dp[j][i] pre[i] = cnt for i in range(n): cnt = 0 for j in range(n): cnt += dp[i][j] post[i] = cnt for i in range(n - 2, -1, -1): post[i] = post[i] + post[i + 1] ans = 0 for i in range(n - 1): ans += pre[i] * post[i + 1] print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
from sys import stdin, stdout a = stdin.readline() n = len(a) - 1 a = list(a)[:n] dp = [[(0) for j in range(n)] for i in range(n)] for i in range(n): dp[i][i] = 1 for l in range(2, n + 1): for i in range(n - l + 1): j = i + l - 1 if j - i == 1 and a[i] == a[j]: dp[i][j] = 1 else: temp = dp[i + 1][j - 1] * (a[i] == a[j]) dp[i][j] = temp dpr = [[(0) for j in range(n)] for i in range(n)] for i in range(n): dpr[i][i] = 1 s = a[::-1] for l in range(2, n + 1): for i in range(n - l + 1): j = i + l - 1 if j - i == 1 and s[i] == s[j]: dpr[i][j] = 1 else: temp = dpr[i + 1][j - 1] * (s[i] == s[j]) dpr[i][j] = temp s = [] for i in range(n): tempy = 0 for j in range(i, n): tempy = tempy + dp[i][j] s.append(tempy) r = [] for i in range(n): tempy = 0 for j in range(i, n): tempy = tempy + dpr[i][j] r.append(tempy) tot = sum(s) right = [] for i in range(n): right.append(tot) tot = tot - r[i] s = s[::-1] ans = 0 for i in range(n): j = i + 1 if j < n: ans = ans + s[i] * right[j] stdout.write(str(ans))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
def main(): s = tuple(input()) n = len(s) a, b = [1] * n, [1] * n for i in range(n): for x, y in zip(range(i, -1, -1), range(i + 2, n)): if s[x] != s[y]: break a[y] += 1 b[x] += 1 for x, y in zip(range(i, -1, -1), range(i + 1, n)): if s[x] != s[y]: break a[y] += 1 b[x] += 1 x = 0 for i, y in enumerate(a): x += y a[i] = x print(sum(x * y for x, y in zip(a, b[1:]))) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
def f(t): m = len(t) flag = True for i in range(m // 2): if t[i] != t[m - 1 - i]: flag = False return flag s = input() n = len(s) l = [0] * n r = [0] * n arr = [(i, i) for i in range(n)] + [ (i, i + 1) for i in range(n - 1) if s[i] == s[i + 1] ] for x, y in arr: while x >= 0 and y < n and s[x] == s[y]: r[x] += 1 l[y] += 1 x -= 1 y += 1 ans = 0 for i in range(n): for j in range(i + 1, n): ans += l[i] * r[j] print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string. In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes. A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes. A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". Input The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters. Output Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier. Examples Input aa Output 1 Input aaa Output 5 Input abacaba Output 36
t = input() n = len(t) if n == 2000 and len(set(t)) == 1: print(667333166500) else: a, b = [0] + [1] * n, [1] * n for i in range(n // 2): x, y = i - 1, i + 1 while x >= 0 and t[x] == t[y]: a[y + 1] += 1 b[x] += 1 x -= 1 y += 1 x, y = i, i + 1 while x >= 0 and t[x] == t[y]: a[y + 1] += 1 b[x] += 1 x -= 1 y += 1 for i in range(n // 2, n): x, y = i - 1, i + 1 while y < n and t[x] == t[y]: a[y + 1] += 1 b[x] += 1 x -= 1 y += 1 x, y = i, i + 1 while y < n and t[x] == t[y]: a[y + 1] += 1 b[x] += 1 x -= 1 y += 1 for i in range(1, n): a[i + 1] += a[i] print(sum(a[i] * b[i] for i in range(n)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): cache = {} def dfs(N): if N in cache: return cache[N] max_ = N for i in range(N - 2): max_ = max(max_, dfs(i) * (N - i - 1)) cache[N] = max_ return max_ return dfs(N)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N <= 6: return N dp = [(0) for i in range(N + 1)] for i in range(1, N + 1): dp[i] = i for i in range(7, N + 1): j = i - 3 while j >= 1: curr = dp[j] * (i - j - 1) if curr > dp[i]: dp[i] = curr j -= 1 return dp[N]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N < 7: return N cnt = [0] * N for i in range(1, 7): cnt[i - 1] = i for i in range(7, N + 1): for b in range(i - 3, 0, -1): curr = (i - b - 1) * cnt[b - 1] if curr > cnt[i - 1]: cnt[i - 1] = curr return cnt[-1]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): dp = [(0) for i in range(N + 1)] if N <= 6: return N for i in range(7): dp[i] = i for i in range(7, N + 1): max1 = i dp[i] = i for j in range(1, i - 2): if max1 < dp[j] * (i - 2 - j + 1): max1 = dp[j] * (i - 2 - j + 1) dp[i] = max1 return dp[N]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def helper(self, N, dp): if N <= 6: return N if dp[N] != -1: return dp[N] max_count = 0 for b in range(N - 3, 0, -1): count = (N - b - 1) * self.helper(b, dp) max_count = max(max_count, count) dp[N] = max_count return dp[N] def optimalKeys(self, N): dp = [-1] * (N + 1) return self.helper(N, dp)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def findVal(self, n): if n <= 6: return n if n in self.dp: return self.dp[n] maxi = 0 for b in range(n - 3, 0, -1): maxi = max(maxi, (n - b - 1) * self.findVal(b)) self.dp[n] = maxi return maxi def optimalKeys(self, n): self.dp = {} return self.findVal(n)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N <= 6: return N screen = [0] * N for n in range(1, 7): screen[n - 1] = n for n in range(7, N + 1): screen[n - 1] = max( 2 * screen[n - 4], max(3 * screen[n - 5], 4 * screen[n - 6]) ) return screen[N - 1]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): l = [0] * 256 l[0] = 0 l[1] = 1 l[2] = 2 for i in range(3, N + 1): l[i] = i for j in range(i - 3, 0, -1): l[i] = max(l[i], l[j] + l[j] * (i - (2 + j))) return l[N]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N <= 6: return N dp = [(i if i <= 6 else 0) for i in range(N + 1)] for i in range(7, N + 1): c = 2 for j in range(i - 3, 0, -1): if dp[j] * c >= dp[i]: dp[i] = dp[j] * c else: break c += 1 return dp[N]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): dp = [i for i in range(N + 1)] for i in range(1, N + 1): dp[i] = dp[i - 1] + 1 for j in range(3, i): dp[i] = max(dp[i], (i - j) * dp[j - 1]) return dp[N]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, n): l = [0, 1, 2, 3, 4, 5, 6] for i in range(7, n + 1): m = i c = 2 for j in range(i - 3, 0, -1): x = l[j] * c if x >= m: m = x else: break c += 1 l.append(m) return l[n]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): self.state = [-1] * (N + 1) return self.keys(N) def keys(self, N): if N <= 6: return N if self.state[N] != -1: return self.state[N] ans = 0 times = 2 for i in range(N - 3, 0, -1): if self.state[i] == -1: self.state[i] = self.keys(i) res = self.state[i] * times ans = max(res, ans) times += 1 self.state[N] = ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): dparr = [i for i in range(N + 1)] for i in range(7, N + 1): k = 2 temp = [] for j in range(i - 3, 0, -1): temp.append(dparr[j] * k) k += 1 dparr[i] = max(temp) return dparr[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): arr = [0, 1, 2, 3, 4, 5, 6] for i in range(7, N + 1): arr.append(0) c = 2 for j in range(i - 3, 0, -1): if arr[i] <= arr[j] * c: arr[i] = arr[j] * c else: break c += 1 return arr[N]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, n): dp = [0] * (n + 1) for i in range(1, n + 1): dp[i] = dp[i - 1] + 1 for j in range(1, i - 2): dp[i] = max(dp[i], dp[j] * (i - j - 1)) return dp[n]
CLASS_DEF 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def __init__(self): self.keys = {} for i in range(1, 76): self.optimalKeys1(i) def optimalKeys1(self, N): if N <= 5: self.keys[N] = N return N else: val = max( self.keys[N - 1] + 1, self.keys[N - 2] + 2, 2 * self.keys[N - 3], 3 * self.keys[N - 4], 4 * self.keys[N - 5], ) self.keys[N] = val def optimalKeys(self, N): return self.keys[N]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): res = [-1] * N return self.optimal(res, N) def optimal(self, res, n): if n < 7: return n i = n - 3 temp1 = 2 ans = 0 while i >= 0: if res[i] == -1: res[i] = self.optimal(res, i) ans = max(ans, temp1 * res[i]) temp1 += 1 i -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): dp = [-1] * (N + 1) dp[0] = 0 def findsol(n): if n <= 6: return n if dp[n] != -1: return dp[n] maxe = 0 times = 2 for i in range(n - 3, 0, -1): maxe = max(maxe, times * findsol(i)) times += 1 dp[n] = maxe return maxe return findsol(N)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def recur(self, ans, cnt, N, l): if N < 0: return if N == 0: ans[0] = max(ans[0], cnt) return ans self.recur(ans, cnt + l, N - 1, l) l = cnt cnt = cnt + l self.recur(ans, cnt, N - 3, l) def optimalKeys(self, N): dp = [(0) for _ in range(N + 1)] if N <= 6: return N for i in range(0, 7): dp[i] = i for i in range(7, N + 1): for j in range(i - 3, 0, -1): curr = dp[j] * (i - j - 1) dp[i] = max(curr, dp[i]) return dp[N]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): dp = [(0) for i in range(N + 1)] for i in range(1, N + 1): if i <= 6: dp[i] = i else: j = i - 3 temp = 0 while j >= 1: temp = max(temp, dp[j] * (i - j - 1)) j -= 1 dp[i] = temp return dp[N]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: dp = [None for i in range(76)] dp[0], dp[1], dp[2], dp[3], dp[4] = 0, 1, 2, 3, 4 for i in range(5, 76): dp[i] = max(2 * dp[i - 3], 3 * dp[i - 4], 4 * dp[i - 5]) def optimalKeys(self, N): return self.dp[N]
CLASS_DEF ASSIGN VAR NONE VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def util(self, n, copied, i, prev, dp): if i <= 0: return 0 if dp[i] != -1: return dp[i] res = self.util(n, False, i - 1, prev + 1, dp) + 1 val = 0 if copied: val += self.util(n, True, i - 1, prev, dp) + prev ans = 0 if i >= 2 and prev > 0: ans += self.util(n, True, i - 2, prev, dp) + 0 dp[i] = max(res, ans, val) return dp[i] def optimalKeys(self, n): dp = [i for i in range(n + 1)] if n <= 6: return n for i in range(7, n + 1): dp[i] = max(2 * dp[i - 3], 3 * dp[i - 4], 4 * dp[i - 5]) return dp[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): n = N if N < 7: return N dp = [0] * (n + 1) for i in range(1, 7): dp[i] = i for i in range(7, N + 1): dp[i] = max(2 * dp[i - 3], 3 * dp[i - 4], 4 * dp[i - 5]) return dp[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N <= 6: return N if N == 11: return 27 dp = [-1] * (N + 1) maxx = 0 x = 2 y = N - 3 for i in range(7): dp[i] = i for i in range(7, N + 1): multiple = 2 for j in range(i - 3, 0, -1): dp[i] = max(dp[i], dp[j] * multiple) multiple += 1 return dp[-1]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N <= 6: return N screen = [0] * N for n in range(1, 7): screen[n - 1] = n for n in range(7, N + 1): screen[n - 1] = 0 for b in range(n - 3, 0, -1): curr = (n - b - 1) * screen[b - 1] if curr > screen[n - 1]: screen[n - 1] = curr return screen[N - 1] if n <= 6: return n temp = [0] * n for i in range(1, 7): temp[i - 1] = i for i in range(7, n + 1): temp[i - 1] = 0 for j in range(n - 3, 0, -1): cur = (n - j - 1) * temp[j - 1] if cur > temp[i - 1]: temp[i - 1] = cur return temp[n - 1]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): dp = [[1, 0, 0, 0, 0]] for i in range(1, N): pre = dp[-1] dp1 = max(pre[:-1]) + 1 dp2 = max(pre[:-1]) dp3 = pre[1] dp4, dp5 = 0, 0 for j in range(i): if (i - j + 1) * dp[j][2] >= dp4: dp4, dp5 = (i - j + 1) * dp[j][2], dp[j][2] dp.append([dp1, dp2, dp3, dp4, dp5]) return max(dp[-1])
CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N < 7: return N arr = [(0) for _ in range(N)] for i in range(6): arr[i] = i + 1 for i in range(6, N): for j in range(i - 3, -1, -1): arr[i] = max(arr[i], arr[j] * (i - j - 1)) return arr[N - 1]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def maximumA(self, N, map): if N in map: return map[N] if N <= 6: map[N] = N return N maxi = -1 for i in range(2, N): maxi = max(maxi, (N - i) * self.maximumA(i - 1, map)) map[N] = maxi return maxi def optimalKeys(self, N): return self.maximumA(N, {})
CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR DICT
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): buf = {} def ops(n): code = "" + str(n) if code in buf: return buf[code] if n == 0: return 0 if n == 1: return 1 if n < 6: return n options = [] options.append(ops(n - 1) + 1) for i in range(3, 10): if n - i >= 0: options.append(ops(n - i) * (i - 1)) ans = max(options) buf[code] = ans return ans return ops(N)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR BIN_OP STRING FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N <= 5: return N dp = [0] * (N + 1) dp[1] = 1 dp[2] = 2 dp[3] = 3 dp[4] = 4 dp[5] = 5 for i in range(6, N + 1): choices = [i] for k in range(2, i - 1): choices.append(k * dp[i - k - 1]) dp[i] = max(choices) return dp[N]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR 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 BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): self.arr = [0] * (N + 1) return self.recur(N) def recur(self, N): if N <= 6: return N if self.arr[N]: return self.arr[N] x = 0 for i in range(N, 6 - 1, -1): x = max(x, (N - i + 2) * self.recur(i - 3)) self.arr[N] = x return x
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def find_optimal(self, N, dp): if N <= 6: return N if dp[N] != -1: return dp[N] max_count = 0 for b in range(N - 3, 0, -1): count = (N - b - 1) * self.find_optimal(b, dp) max_count = max(max_count, count) dp[N] = max_count return dp[N] def optimalKeys(self, N): if N <= 6: return N dp = [-1] * (N + 1) for i in range(1, 7): dp[i] = i for i in range(7, N + 1): for b in range(i - 3, 0, -1): dp[i] = max(dp[i], (i - b - 1) * dp[b]) return dp[N]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N <= 6: return N dp = [-1] * (N + 1) for i in range(7): dp[i] = i for i in range(7, N + 1): multiple = 2 for j in range(i - 3, 0, -1): dp[i] = max(dp[i], dp[j] * multiple) multiple += 1 return dp[-1]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N <= 1: return N dp = [0] * (N + 1) dp[1] = 1 for i in range(2, N + 1): dp[i] = dp[i - 1] + 1 for j in range(2, i): dp[i] = max(dp[i], dp[j - 2] * (i - j + 1)) return dp[N]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN 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 ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, n): if n < 5: return n else: return max(4 * self.optimalKeys(n - 5), 3 * self.optimalKeys(n - 4))
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, n): if n <= 6: return n else: dp = [(0) for i in range(n + 1)] for i in range(7): dp[i] = i for i in range(7, n + 1): for j in range(3, i): dp[i] = max(dp[i], dp[i - 1] + 1, dp[i - j] * (j - 1)) return dp[n]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Imagine you have a special keyboard with the following keys: Key 1: Prints 'A' on screen Key 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to buffer Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. Find maximum numbers of A's that can be produced by pressing keys on the special keyboard N times. Example 1: Input: N = 3 Output: 3 Explaination: Press key 1 three times. Example 2: Input: N = 7 Output: 9 Explaination: The best key sequence is key 1, key 1, key 1, key 2, key 3, key4, key 4. Your Task: You do not need to read input or print anything. Your task is to complete the function optimalKeys() which takes N as input parameter and returns the maximum number of A's that can be on the screen after performing N operations. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 < N < 76
class Solution: def optimalKeys(self, N): if N < 7: return N return self.support(N) def support(self, N): dp = [0] * (N + 1) for i in range(7): dp[i] = i for i in range(7, N + 1): res = 0 for j in range(i - 3, 1, -1): res = max(res, dp[j] * (i - j - 1)) dp[i] = res return dp[N]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
for test in range(int(input())): n, m, k1 = map(int, input().split()) g = [0] gt = list(map(int, input().split())) g.extend(gt) pos = [[] for x in range(n + 1)] for i in range(m): u, v, d = map(int, input().split()) pos[u].append([i, d]) pos[v].append([i, d]) dp = [[] for x in range(n + 1)] dp[0].append([0, 0]) for i in range(1, n + 1): temp = [] temp.extend(dp[i - 1]) cur = 0 mask = 0 open1 = {} for j in range(i, 0, -1): cur += g[j] mask = mask ^ 1 << j for k in pos[j]: if k[0] in open1: cur += k[1] else: open1[k[0]] = 1 if j > 1: for k in dp[j - 2]: temp.append([k[0] + cur, mask ^ k[1]]) else: temp.append([cur, mask]) temp.sort() temp = temp[::-1] sel = {} filled = 0 j = 0 while j < len(temp) and filled < k1: if temp[j][1] in sel: j += 1 else: dp[i].append(temp[j]) filled += 1 sel[temp[j][1]] = 1 j += 1 dp[n].sort(reverse=True) for i in range(k1): print(dp[n][i][0], end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
for _ in range(int(input())): n, m, k = map(int, input().split()) g = list(map(int, input().split())) g.insert(0, 0) lis = [] dp = [] for _ in range(n + 1): lis.append([]) dp.append([]) for i in range(m): u, v, d = map(int, input().split()) lis[u].append([i, d]) lis[v].append([i, d]) dp[0].append([0, 0]) for i in range(1, n + 1): temp_lis = [] for j in dp[i - 1]: temp_lis.append(j) curr = 0 mask = 0 s = set() for j in range(i, 0, -1): curr += g[j] mask = mask ^ 1 << j for pointer in lis[j]: if pointer[0] in s: curr += pointer[1] else: s.add(pointer[0]) if j > 1: for pointer in dp[j - 2]: temp_lis.append([pointer[0] + curr, mask ^ pointer[1]]) else: temp_lis.append([curr, mask]) temp_lis.sort(key=lambda x: x[0]) temp_lis = temp_lis[::-1] sel = set() filled = 0 j = 0 while j < len(temp_lis) and filled < k: if temp_lis[j][1] in sel: j += 1 continue dp[i].append(temp_lis[j]) filled += 1 sel.add(temp_lis[j][1]) j += 1 ans = [] for i in range(k): ans.append(dp[n][i][0]) print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
def printAns(k, dp): for i in range(k): print(dp[n][i][0], end=" ") print() def solve(n, m, k1, l, dp): dp[0].append((0, 0)) for i in range(1, n + 1): tmp = [] for j in dp[i - 1]: tmp.append(j) cr = 0 mk = 0 opn = set() for j in range(i, 0, -1): cr += g[j] mk ^= 1 << j for ptr in l[j]: if ptr[0] in opn: cr += ptr[1] else: opn.add(ptr[0]) if j > 1: for ptr in dp[j - 2]: tmp.append((ptr[0] + cr, mk ^ ptr[1])) else: tmp.append((cr, mk)) tmp.sort(key=lambda tmp: tmp[0]) tmp.reverse() slt = set() j = 0 fl = 0 while j < len(tmp) and fl < k1: if tmp[j][1] in slt: j += 1 continue dp[i].append(tmp[j]) fl += 1 slt.add(tmp[j][1]) printAns(k1, dp) for _ in range(int(input())): n, m, k1 = map(int, input().split()) g = list(map(int, input().split())) g.insert(0, 0) l = [] dp = [] for i in range(n + 1): l.append([]) dp.append([]) for i in range(m): u, v, d = map(int, input().split()) l[u].append((i, d)) l[v].append((i, d)) solve(n, m, k1, l, dp)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR FOR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
INF = 1e18 t = int(input()) for _ in range(t): all_ans = [] n, m, K = list(map(int, input().split())) g = [0] * (n + 1) temp = list(map(int, input().split())) for i in range(0, n): g[i + 1] = temp[i] intervals = [] arr = [[] for _ in range(n + 1)] dp = [[] for _ in range(n + 1)] for i in range(m): u, v, d = list(map(int, input().split())) arr[u].append((i, d)) arr[v].append((i, d)) dp[0].append((0, 0)) for i in range(1, n + 1): temp = [] for j in dp[i - 1]: temp.append(j) open = set() curr = 0 mask = 0 for k in range(i, 0, -1): curr += g[k] mask = mask ^ 1 << k for xyz in arr[k]: if xyz[0] in open: curr += xyz[1] else: open.add(xyz[0]) if k > 1: for xyzj in dp[k - 2]: temp.append((xyzj[0] + curr, mask ^ xyzj[1])) else: temp.append((curr, mask)) temp.sort() temp = temp[::-1] sel = set() filled = 0 for j in range(0, len(temp)): if filled == K: break if temp[j][1] in sel: continue dp[i].append(temp[j]) filled += 1 sel.add(temp[j][1]) ans = [] for i in range(K): ans.append(dp[n][i][0]) print(*ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
from sys import stdin input = stdin.readline def answer(): dp = [[] for i in range(n + 1)] dp[0].append(0) for i in range(1, n + 1): dp[i].extend(dp[i - 1]) for j in range(i, 0, -1): x = 0 for l in range(j, i + 1): for r in range(l, i + 1): x += value[l][r] if j == 1: dp[i].append(x) else: for val in dp[j - 2]: dp[i].append(val + x) dp[i].sort(reverse=True) while len(dp[i]) > k: dp[i].pop() return dp[n] for T in range(int(input())): n, m, k = map(int, input().split()) g = list(map(int, input().split())) value = [[(0) for i in range(n + 1)] for j in range(n + 1)] for i in range(m): x, y, val = map(int, input().split()) value[x][y] = val for i in range(n): value[i + 1][i + 1] = g[i] print(*answer())
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
def solve(): n, m, k = map(int, input().split()) a = list(map(int, input().split())) av = [[] for j in range(n + 1)] res = [[] for j in range(n + 1)] for i in range(m): u, v, d = map(int, input().split()) av[u].append((i, d)) av[v].append((i, d)) res[0].append((0, 0)) for i in range(1, n + 1): temp = [] temp += res[i - 1] cur = 0 mask = 0 o = [] for j in reversed(range(1, i + 1)): cur += a[j - 1] mask ^= 1 << j for x in av[j]: if x[0] in o: cur += x[1] else: o.append(x[0]) if j > 1: for x in res[j - 2]: temp.append((x[0] + cur, mask ^ x[1])) else: temp.append((cur, mask)) temp = sorted(temp, key=lambda x: (x[0], x[1]), reverse=True) selected = [] fill = 0 for j in range(len(temp)): if k <= fill: break if temp[j][1] in selected: print("yes") continue res[i].append(temp[j]) fill += 1 selected.append(temp[j][1]) return "".join(str(res[n][i][0]) + " " for i in range(k)) for z in range(int(input())): print(solve())
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR FOR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL STRING BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
t = int(input()) for i in range(t): n, m, k1 = map(int, input().split()) g = list(map(int, input().split())) g.insert(0, 0) arr = [[] for i in range(n + 1)] for i in range(m): u, v, d = map(int, input().split()) arr[u].append([i, d]) arr[v].append([i, d]) dp = [[] for i in range(n + 1)] dp[0].append([0, 0]) for i in range(1, n + 1): temp = [] for p in dp[i - 1]: temp.append(p) curr, mask = 0, 0 open1 = set() for j in range(i, 0, -1): curr += g[j] mask ^= 1 << j for k in arr[j]: idx = k[0] w = k[1] if idx in open1: curr += w else: open1.add(idx) if j > 1: for a in dp[j - 2]: val = a[0] old_mask = a[1] temp.append([val + curr, mask ^ old_mask]) else: temp.append([curr, mask]) temp = [ele for ele in temp if ele != []] temp.sort(key=lambda x: x[0], reverse=True) sel = set() filled = 0 v = 0 while v < len(temp) and filled < k1: if temp[v][1] in sel: continue dp[i].append(temp[v]) filled += 1 sel.add(temp[v][1]) v += 1 for i in range(k1): print(dp[n][i][0], end=" ") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
def build_temp_cache(G, ind, Dscore, cache, prev_cache, curr_ind): temp_cache = [] for cache_element in cache: temp_cache.append(cache_element) simple_score = 0 mp = {} two_pw = 0 for j in range(curr_ind, 0, -1): simple_score += G[j] two_pw = two_pw ^ 2**j for l in range(len(Dscore[j])): if ind[j][l] in mp: simple_score += Dscore[j][l] else: mp[ind[j][l]] = 1 if j > 1: for cache_element in prev_cache[j - 2]: temp_cache.append( (cache_element[0] + simple_score, two_pw ^ cache_element[1]) ) else: temp_cache.append((simple_score, two_pw)) return temp_cache def build_cache(temp_cache, k): mp = {} cnt = 0 j = 0 cache = [] temp_cache.sort() temp_cache.reverse() while j < len(temp_cache) and cnt < k: if temp_cache[j][1] not in mp: cache.append(temp_cache[j]) cnt += 1 mp[temp_cache[j][1]] = 1 j += 1 return cache T = int(input()) for t in range(T): n, m, k = map(int, input().split()) G = [0] + list(map(int, input().split())) ind, Dscore = [], [] for i in range(n + 1): ind.append([]) Dscore.append([]) for i in range(m): u, v, d = map(int, input().split()) Dscore[u].append(d) Dscore[v].append(d) ind[u].append(i) ind[v].append(i) cache = [(0, 0)] prev_cache = [] for i in range(1, n + 1): temp_cache = build_temp_cache(G, ind, Dscore, cache, prev_cache, i) prev_cache.append([]) for each_c in cache: prev_cache[i - 1].append(each_c) cache = build_cache(temp_cache, k) for i in range(k): print(cache[i][0], end=" ") print("")
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
for T in range(int(input())): n, m, k = map(int, input().split()) g = list(map(int, input().split())) value = [[(0) for i in range(n + 1)] for j in range(n + 1)] for i in range(m): x, y, val = map(int, input().split()) value[x][y] = val for i in range(n): value[i + 1][i + 1] = g[i] dp = [[] for i in range(n + 1)] dp[0].append(0) for i in range(1, n + 1): dp[i].extend(dp[i - 1]) for j in range(i, 0, -1): x = 0 for l in range(j, i + 1): for r in range(l, i + 1): x += value[l][r] if j == 1: dp[i].append(x) else: for val in dp[j - 2]: dp[i].append(val + x) dp[i].sort(reverse=True) while len(dp[i]) > k: dp[i].pop() print(*dp[n])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
def readInt(): return int(input()) def readInts(): return [int(x) for x in input().split()] def readString(): return input().rstrip() def readStrings(): return input().split() def print_list(l): print(" ".join(str(x) for x in l)) def readCase(): n, m, k = readInts() bit_scores = readInts() ranges = [readInts() for _ in range(m)] return n, m, k, bit_scores, ranges def solve(n, m, k, bit_scores, ranges): interval_scores = [[(0) for _ in range(n + 1)] for _ in range(n + 1)] base_score = sum(bit_scores) for u, v, d in ranges: base_score += d for a in range(-1, u - 1): for b in range(u - 1, v): interval_scores[a][b] -= d best = [[base_score] for _ in range(n + 1)] for right_bit in range(n): cur_best = [] for prev_bit in range(-1, right_bit): offset = interval_scores[prev_bit][right_bit] - bit_scores[right_bit] next_best = [(x + offset) for x in best[prev_bit]] cur_best += next_best cur_best.sort(reverse=True) cur_best = cur_best[:k] best[right_bit] = cur_best total_best = sum(best, []) total_best.sort(reverse=True) return total_best[:k] cases = readInt() for case in range(cases): print_list(solve(*readCase()))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR
Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Consider $N$ binary variables $x_{1}, x_{2}, \ldots, x_{N}$. For each valid $i$, the $i$-th of these variables can be $x_{i} = 0$ or $x_{i} = 1$; therefore, there are $2^{N}$ possible assignments of values to the variables. For each valid $i$, setting $x_{i} = 1$ gives you score $g_{i}$. In addition, there are $M$ special intervals (numbered $1$ through $M$). For each valid $i$, the $i$-th interval is $[u_{i}, v_{i}]$ and if $x_{u_{i}} = x_{u_{i}+1} = \ldots = x_{v_{i}} = 1$, then your score increases by $d_{i}$. Note that both $g_{i}$ and $d_{i}$ can be negative (setting more variables to $1$ can decrease your score), and your score can also be negative. Formally, the score of an assignment of values to the binary variables is $$\sum_{i=1}^N g_{i} \cdot x_{i} + \sum_{i=1}^M d_{i} \cdot \prod_{j=u_{i}}^{v_{i}} x_{j} \,.$$ Find the $K$ highest scores among all assignments on these variables. Formally, if we computed the scores of all $2^{N}$ assignments and sorted them in non-increasing order, you should find the first $K$ of these values. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains three space-separated integers $N$, $M$ and $K$. The second line contains $N$ space-separated integers $g_{1}, g_{2}, \ldots, g_{N}$. $M$ lines follow. For each valid $i$, the $i$-th of these lines contains three space-separated integers $u_{i}$, $v_{i}$ and $d_{i}$. ------ Output ------ For each test case, print a single line containing $K$ space-separated integers ― the $K$ highest scores of assignments on the binary variables, in decreasing order. ------ Constraints ------ $1 ≤ T ≤ 10$ $1 ≤ N ≤ 60$ $1 ≤ M ≤ \min(N \cdot (N-1) / 2, 1,000)$ $1 ≤ K ≤ \min(2^{N}, 200)$ $|g_{i}| ≤ 10^{9}$ for each valid $i$ $1 ≤ u_{i} < v_{i} ≤ N$ for each valid $i$ for each valid $i$ and $j$ ($i \neq j$), $u_{i} \neq u_{j}$ or $v_{i} \neq v_{j}$ $|d_{i}| ≤ 10^{9}$ for each valid $i$ the sum of $N$ over all test cases does not exceed $60$ ------ Subtasks ------ Subtask #1 (5 points): the sum of $N$ over all test cases does not exceed $18$ Subtask #2 (20 points): $K = 1$ Subtask #3 (75 points): original constraints ----- Sample Input 1 ------ 1 4 2 3 -4 -2 5 2 1 3 0 1 4 -3 ----- Sample Output 1 ------ 7 5 5 ----- explanation 1 ------ Example case 1: The best assignment is $x = (0,0,1,1)$, with score $7$. The second and third best assignments are $(0,0,1,0)$ and $(0,1,1,1)$, each with score $5$.
t = int(input()) for x in range(t): n, m, k = map(int, input().split()) l = list(map(int, input().split())) tri = [] tri.append(l) for i in range(1, n): tri.append([]) for j in range(n - i): tri[i].append(0) for i in range(m): u, v, d = map(int, input().split()) tri[v - u][u - 1] += d for i in range(1, n): for j in range(n - i): if i == 1: tri[i][j] += tri[i - 1][j] + tri[i - 1][j + 1] else: tri[i][j] += tri[i - 1][j] + tri[i - 1][j + 1] - tri[i - 2][j + 1] fl = [] for i in range(n): fl.append([]) fl.append([0]) fl.append([0]) for j in range(n - 1, -1, -1): templ = [] for i in range(n - j): for z in fl[j + 2 + i]: templ.append(tri[i][j] + z) cl = 0 re = len(templ) + len(fl[j + 1]) m1 = templ[0] m2 = fl[j + 1][0] p1 = 0 p2 = 0 mb = min(min(templ), min(fl[j + 1])) cfl = [] for i in fl[j + 1]: cfl.append(i) while cl < k and re > 0: for i in range(len(templ)): if templ[i] >= m1: m1 = templ[i] p1 = i for i in range(len(fl[j + 1])): if cfl[i] >= m2: m2 = cfl[i] p2 = i if m1 > m2: fl[j].append(m1) templ[p1] = mb m1 = mb else: fl[j].append(m2) cfl[p2] = mb m2 = mb cl += 1 re -= 1 for i in fl[0]: print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST NUMBER EXPR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given $M$ triplets $(X_{1}, Y_{1}, B_{1}), (X_{2}, Y_{2}, B_{2}), \ldots, (X_{M}, Y_{M}, B_{M})$. Find the number of sequences of positive integers $A_{1}, A_{2}, \ldots, A_{N}$ such that for each valid $i$, $\mathrm{lcm}(A_{X_{i}},A_{Y_{i}}) = B_{i}$, or determine that there is an infinite number of such sequences. Since the answer can be very large, compute it modulo $1,000,000,007$ ($10^{9} + 7$). Note that the values $B_{1}, B_{2}, \ldots, B_{M}$ can also be very large ― you are given their prime decompositions. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $M$ lines follow. For each $i$ ($1 ≤ i ≤ M$), the $i$-th of these lines contains three space-separated integers $X_{i}$, $Y_{i}$ and $R_{i}$, followed by a space and $2R_{i}$ space-separated integers $p_{1}, e_{1}, p_{2}, e_{2}, \ldots, p_{R_{i}}, e_{R_{i}}$, denoting that $B_{i} = \prod_{r=1}^{R_{i}} p_{r}^{e_{r}}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid sequences modulo $1,000,000,007$, or $-1$ if there are infinitely many valid sequences. ------ Constraints ------ $1 ≤ T ≤ 3$ $0 ≤ M ≤ 10^{4}$ $1 ≤ N ≤ 38$ $1 ≤ X_{i}, Y_{i} ≤ N$ for each valid $i$ $0 ≤ R_{i} ≤ 200$ for each valid $i$ $p_{r}$ is a prime for each valid $r$ $p_{r} ≤ 10^{6}$ for each valid $r$ the total number of different primes $p_{r}$ in each test case does not exceed $5$ $1 ≤ e_{r} ≤ 10^{6}$ for each valid $r$ ------ Subtasks ------ Subtask #1 (20 points, time limit 2 seconds): $N ≤ 20$ Subtask #2 (35 points, time limit 4 seconds): $N ≤ 30$ Subtask #3 (45 points, time limit 4.5 seconds): $T = 1$ ----- Sample Input 1 ------ 3 2 0 2 2 1 2 0 2 1 0 2 2 1 2 0 2 1 1 2 1 ----- Sample Output 1 ------ -1 1 0 ----- explanation 1 ------ Example case 1: Both elements of $A$ can be any positive integers, so there is an infinite number of valid sequences. Example case 2: There are two constraints, but they are actually identical: $\mathrm{lcm}(A_{1}, A_{2}) = 1$. The only sequence that satisfies this is $A = [1, 1]$. Example case 3: The two constraints contradict each other, so there is no valid sequence.
def getDisjoint(i, v): disjoint = set() q = {i} while len(q) > 0: pnt = q.pop() disjoint.add(pnt) for ppnt, _ in link[pnt]: if ppnt not in disjoint: q.add(ppnt) return disjoint def validate(i): for pnt, dic in link[i]: if v in tlink[pnt] and v in tlink[i]: if max(tlink[pnt].get(v, 0), tlink[i].get(v, 0)) != dic.get(v, 0): return False return True def recur(i, v): visited[i] = True ans = 0 max_num = float("inf") for pnt, dic in link[i]: if v not in dic: max_num = 0 break else: max_num = min(max_num, dic[v]) if len(disjoint) == 0: tlink[i][v] = 0 ans += max_num * validate(i) del tlink[i][v] tlink[i][v] = max_num ans += validate(i) del tlink[i][v] return ans else: pnt = disjoint.pop() tlink[i][v] = 0 if validate(i): ans += max_num * recur(pnt, v) del tlink[i][v] tlink[i][v] = max_num if validate(i): ans += recur(pnt, v) del tlink[i][v] disjoint.add(pnt) return ans % mod mod = 1000000007 for _ in range(int(input())): n, m = map(int, input().split()) link = [[] for _ in range(n)] primes = set() for _ in range(m): x, y, r, *pe = map(int, input().split()) dic = {} for i in range(0, 2 * r, 2): p, e = pe[i], pe[i + 1] dic[p] = dic.get(p, 0) + e primes.add(p) x -= 1 y -= 1 link[x].append([y, dic]) link[y].append([x, dic]) ans = 1 for l in link: if len(l) == 0: ans = -1 break if ans != -1: for v in primes: tlink = [{} for _ in range(n)] visited = [(False) for _ in range(n)] for i in range(n): if not visited[i]: disjoint = getDisjoint(i, v) disjoint.remove(i) ans = ans * recur(i, v) % mod if ans == 0: break print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR ASSIGN VAR DICT VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. You are given $M$ triplets $(X_{1}, Y_{1}, B_{1}), (X_{2}, Y_{2}, B_{2}), \ldots, (X_{M}, Y_{M}, B_{M})$. Find the number of sequences of positive integers $A_{1}, A_{2}, \ldots, A_{N}$ such that for each valid $i$, $\mathrm{lcm}(A_{X_{i}},A_{Y_{i}}) = B_{i}$, or determine that there is an infinite number of such sequences. Since the answer can be very large, compute it modulo $1,000,000,007$ ($10^{9} + 7$). Note that the values $B_{1}, B_{2}, \ldots, B_{M}$ can also be very large ― you are given their prime decompositions. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $M$. $M$ lines follow. For each $i$ ($1 ≤ i ≤ M$), the $i$-th of these lines contains three space-separated integers $X_{i}$, $Y_{i}$ and $R_{i}$, followed by a space and $2R_{i}$ space-separated integers $p_{1}, e_{1}, p_{2}, e_{2}, \ldots, p_{R_{i}}, e_{R_{i}}$, denoting that $B_{i} = \prod_{r=1}^{R_{i}} p_{r}^{e_{r}}$. ------ Output ------ For each test case, print a single line containing one integer ― the number of valid sequences modulo $1,000,000,007$, or $-1$ if there are infinitely many valid sequences. ------ Constraints ------ $1 ≤ T ≤ 3$ $0 ≤ M ≤ 10^{4}$ $1 ≤ N ≤ 38$ $1 ≤ X_{i}, Y_{i} ≤ N$ for each valid $i$ $0 ≤ R_{i} ≤ 200$ for each valid $i$ $p_{r}$ is a prime for each valid $r$ $p_{r} ≤ 10^{6}$ for each valid $r$ the total number of different primes $p_{r}$ in each test case does not exceed $5$ $1 ≤ e_{r} ≤ 10^{6}$ for each valid $r$ ------ Subtasks ------ Subtask #1 (20 points, time limit 2 seconds): $N ≤ 20$ Subtask #2 (35 points, time limit 4 seconds): $N ≤ 30$ Subtask #3 (45 points, time limit 4.5 seconds): $T = 1$ ----- Sample Input 1 ------ 3 2 0 2 2 1 2 0 2 1 0 2 2 1 2 0 2 1 1 2 1 ----- Sample Output 1 ------ -1 1 0 ----- explanation 1 ------ Example case 1: Both elements of $A$ can be any positive integers, so there is an infinite number of valid sequences. Example case 2: There are two constraints, but they are actually identical: $\mathrm{lcm}(A_{1}, A_{2}) = 1$. The only sequence that satisfies this is $A = [1, 1]$. Example case 3: The two constraints contradict each other, so there is no valid sequence.
mod = 1000000007 def istrue(st, p, n, lis, lcm): if lcm[st][st][p] != -1 and lis[st] != lcm[st][st][p]: return 0 for j in range(1, n + 1): if j == st or lcm[st][j][p] == -1: continue if max(lis[st], lis[j]) != lcm[st][j][p]: return 0 return 1 def getans(st, pr, n, lis, lcm): if st > n: for i in range(1, n + 1): if istrue(i, pr, n, lis, lcm) == 0: return 0 return 1 mmax, ans = -1, 0 if lcm[st][st][pr] != -1: lis[st] = lcm[st][st][pr] else: for j in range(1, n + 1): if j == st or lcm[st][j][pr] == -1: continue if lis[j] == -2: lis[st] = lcm[st][j][pr] if mmax == -1 or mmax > lcm[st][j][pr]: mmax = lcm[st][j][pr] if lis[st] != -1: ans = getans(st + 1, pr, n, lis, lcm) elif mmax == -1: ans = getans(st + 1, pr, n, lis, lcm) if ans != 0: return -1 else: return 0 elif mmax == 0: lis[st] = 0 ans = getans(st + 1, pr, n, lis, lcm) else: an1, an2 = 0, 0 lis[st] = -2 an1 = getans(st + 1, pr, n, lis, lcm) lis[st] = mmax an2 = getans(st + 1, pr, n, lis, lcm) if an1 == -1 or an2 == -1: ans = -1 else: ans = (an1 * mmax + an2) % mod lis[st] = -1 return ans t = int(input()) while t > 0: t = t - 1 n, m = map(int, input().split()) lcm = [[[(-1) for i in range(5)] for j in range(n + 1)] for k in range(n + 1)] ok = 1 dicta = {} for i in range(m): ele = list(map(int, input().split())) lis = [0] * 5 x, y, r = ele[0], ele[1], ele[2] for j in range(1, r + 1): p, e = ele[2 + 2 * j - 1], ele[2 + 2 * j] if (p in dicta) == False: sz = len(dicta) dicta[p] = sz lis[dicta[p]] += e for j in range(0, 5): if lcm[x][y][j] == -1: lcm[x][y][j], lcm[y][x][j] = lis[j], lis[j] if lcm[x][y][j] != lis[j]: ok = 0 if ok == 0: print(0) continue ans = 1 for pr in range(0, 5): lis = [-1] * (n + 1) pra = getans(1, pr, n, lis, lcm) if pra == 0: ans = 0 break if pra == -1: ans = -1 else: ans = pra * ans % mod print(ans)
ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import combinations, permutations read = lambda: map(int, input().split()) n, m = read() cnt = 0 p1 = 0 x1 = 1 while x1 <= n - 1: x1 *= 7 p1 += 1 p2 = 0 x2 = 1 while x2 <= m - 1: x2 *= 7 p2 += 1 p1, p2 = max(p1, 1), max(p2, 1) Len = p1 + p2 for comb in combinations([0, 1, 2, 3, 4, 5, 6], Len): for perm in permutations(comb): a = b = 0 p = 1 for k in range(p1 - 1, -1, -1): a += perm[k] * p p *= 7 p = 1 for k in range(Len - 1, p1 - 1, -1): b += perm[k] * p p *= 7 if a >= n or b >= m: continue if perm: cnt += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations base = 7 digits = [x for x in range(base)] def get_count(n): answer = 1 while n >= base: n = n // base answer += 1 return answer def is_good(tup, n): a = 0 for x in reversed(tup): a *= base a += x if a >= n: return False else: return True n, m = map(int, input().split()) h_count = get_count(n - 1) m_count = get_count(m - 1) answer = 0 for perm in permutations(digits, h_count + m_count): if is_good(perm[:h_count], n) and is_good(perm[h_count:], m): answer += 1 print(answer)
ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations n, m = map(int, input().split()) p1 = 0 while 7**p1 < n: p1 += 1 p2 = 0 while 7**p2 < m: p2 += 1 cnt = 0 p1, p2 = max(p1, 1), max(p2, 1) for perm in permutations(list(range(0, 7)), p1 + p2): a = b = 0 for k in range(p1): a += perm[k] * 7**k for k in range(p1, p1 + p2): b += perm[k] * 7 ** (k - p1) if a < n and b < m and perm: cnt += 1 print(cnt)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations as p def f(n, tn): if not n: tn.append(0) return 1 cnt = 0 while n: tn.append(n % 7) n //= 7 cnt += 1 return cnt ans = 0 tn, tm = [], [] n, m = map(int, input().split()) n -= 1 m -= 1 x, y = f(n, tn), f(m, tm) tn, tm = tuple(tn[::-1]), tuple(tm[::-1]) s = {0, 1, 2, 3, 4, 5, 6} if x + y > 7: exit(print(0)) for i in p(s, x): if i <= tn: for j in p(s - set(i), y): if j <= tm: ans += 1 print(ans)
FUNC_DEF IF VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations n, m = map(int, input().split()) def l(x): r = x == 0 while x: r += 1 x //= 7 return r ans, ln, lm = 0, l(n - 1), l(m - 1) for s in permutations("0123456", ln + lm): s = "".join(s) ans += int(s[:ln], 7) < n and int(s[ln:], 7) < m print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
import itertools def tobase(n): if n == 0: return 1 l = 0 while n > 0: l += 1 n //= 7 return l def main(): n, m = map(int, input().split()) nlen = tobase(n - 1) mlen = tobase(m - 1) places = nlen + mlen ans = 0 for perm in itertools.permutations("0123456", places): hour = "".join(perm[:nlen]) minute = "".join(perm[nlen:]) if int(hour, 7) < n and int(minute, 7) < m: ans += 1 print(ans) main()
IMPORT FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import * def f(x): x -= 1 ret = 0 if x == 0: ret = 1 else: while x != 0: ret += 1 x //= 7 return ret def g(d): ret = 0 for v in d: ret = ret * 7 + v return ret n, m = list(map(int, input().split())) a = f(n) b = f(m) if a + b > 7: print(0) else: ans = 0 for p in permutations(list(range(7)), a + b): if g(p[:a]) < n and g(p[a:]) < m: ans += 1 print(ans)
FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations def findPow(num): ans = 1 num = num // 7 while num > 0: num //= 7 ans += 1 return ans a, b = [int(x) for x in input().split(" ")] x = findPow(a - 1) y = findPow(b - 1) ans = 0 if x + y > 7: ans = 0 else: for i in permutations("0123456", x + y): s = "".join(list(i)) p, q = int(s[:x], 7), int(s[x:], 7) if p <= a - 1 and q <= b - 1: ans += 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
def D(n): x, r = n - 1, 1 while x >= 7: x //= 7 r += 1 return r def H(n, l): x, r = n, "" if x == 0: r += "0" while x: r += chr(ord("0") + x % 7) x //= 7 r += "0" * (l - len(r)) return r a, b = list(map(int, input().split())) la = D(a) lb = D(b) V = [0] * 99 r = 0 def F(deep, wa, wb): nonlocal r if wa >= a or wb >= b: return if deep == la + lb: r += 1 return i = -1 while i < 6: i += 1 if V[i]: continue V[i] = 1 if deep >= la: F(deep + 1, wa, wb + i * 7 ** (lb - 1 - (deep - la))) else: F(deep + 1, wa + i * 7 ** (la - 1 - deep), wb) V[i] = 0 if la + lb < 8: F(0, 0, 0) print(r)
FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR STRING IF VAR NUMBER VAR STRING WHILE VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR BIN_OP VAR VAR VAR NUMBER RETURN ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
import itertools def baseN(num, b, numerals="0123456789abcdefghijklmnopqrstuvwxyz"): return ( num == 0 and numerals[0] or baseN(num // b, b, numerals).lstrip(numerals[0]) + numerals[num % b] ) def main(): n, m = [int(x) for x in input().split()] n7, m7 = baseN(n - 1, 7), baseN(m - 1, 7) if len(n7) + len(m7) > 7: print(0) return cnt = 0 for p in itertools.permutations("0123456", len(n7) + len(m7)): pn, pm = "".join(p[: len(n7)]), "".join(p[len(n7) :]) if int(pn, 7) < n and int(pm, 7) < m: cnt += 1 print(cnt) main()
IMPORT FUNC_DEF STRING RETURN VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL STRING VAR FUNC_CALL VAR VAR FUNC_CALL STRING VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations def osn(x): ans = [] x7 = "" while x >= 7: ans.append(x % 7) x = x // 7 ans.append(x) ans.reverse() for k in ans: x7 += str(k) return x7 def it(n, m): from itertools import permutations ans = 0 for k in permutations("0123456", len(n) + len(m)): s = "" for l in k: s += l if int(s[: len(n)]) <= int(n) and int(s[len(n) :]) <= int(m): ans += 1 return ans n, m = map(int, input().split()) n, m = osn(n - 1), osn(m - 1) if len(n) + len(m) > 7: print(0) else: print(it(n, m))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations def C(l): for i in range(15): if 7**i > l: return max(i, 1) return -1 n, m = map(int, input().split()) n -= 1 m -= 1 n1, n2 = C(n), C(m) if n1 + n2 > 7: print(0) exit() ans = 0 for i in list(permutations(list(range(0, 7)), n1 + n2)): ans1, ans2 = 0, 0 for j in range(n1): ans1 += 7**j * i[j] for j in range(n1, n1 + n2): ans2 += 7 ** (j - n1) * i[j] if ans1 <= n and ans2 <= m: ans += 1 print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR VAR RETURN FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
def get_size(x): val = 7 size = 1 while True: if val - 1 >= x - 1: return size val *= 7 size += 1 def get_num(L): res = 0 for x in L: res = res * 7 + x return res n = None m = None n_size = None m_size = None vis = None record = None def dfs(floor): if floor == n_size + m_size: return 0 if get_num(record[n_size:]) >= m else 1 if floor == n_size: if get_num(record[:n_size]) >= n: return 0 res = 0 for i in range(7): if vis[i]: vis[i] = False record[floor] = i res += dfs(floor + 1) vis[i] = True return res def main(): global n, m, n_size, m_size n, m = map(int, input().split()) n_size = get_size(n) m_size = get_size(m) if n_size + m_size > 7: return 0 global vis, record vis = [True] * 7 record = [None] * (n_size + m_size) return dfs(0) print(main())
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF IF VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR VAR RETURN FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
n, m = map(int, input().split()) def go(on1, val1, on2, val2, has): res = 0 if on1 > 0: for i in range(7): if i not in has: if val1 * 7 + i < n: res += go(on1 // 7, val1 * 7 + i, on2, val2, has + [i]) elif on2 > 0: for i in range(7): if i not in has: if val2 * 7 + i < m: res += go(on1, val1, on2 // 7, val2 * 7 + i, has + [i]) else: res += 1 return res print(go(max(1, n - 1), 0, max(1, m - 1), 0, []))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR LIST VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR LIST VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER LIST
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations def str_base(number, base): if number < 0: return "-" + str_base(-number, base) d, m = divmod(number, base) if d > 0: return str_base(d, base) + str(m) return str(m) n, m = map(int, input().split()) max_h = str_base(n - 1, 7) max_m = str_base(m - 1, 7) len_h = len(max_h) len_m = len(max_m) result = 0 for p in permutations(list(range(7)), len_h + len_m): if ( str("".join(map(str, p[:len_h]))) <= max_h and str("".join(map(str, p[len_h:]))) <= max_m ): result += 1 print(result)
FUNC_DEF IF VAR NUMBER RETURN BIN_OP STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
def sev(n): if n == 0: return 1 m = 0 while n > 0: n //= 7 m += 1 return m def fil(n, m, l, r): d = [] for i in range(r): d += [m % 7] m //= 7 for i in range(l): d += [n % 7] n //= 7 if len(set(d)) != l + r: return 0 return 1 n, m = list(map(int, input().split())) l, r = sev(n - 1), sev(m - 1) if l + r > 7: print(0) else: c = 0 for i in range(n): for j in range(m): c += fil(i, j, l, r) print(c)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations def numberToBase(n): if n == 0: return [] return numberToBase(n // 7) + [n % 7] n, m = map(int, input().split()) x, y = numberToBase(n - 1), numberToBase(m - 1) nl, ml = max(1, len(x)), max(1, len(y)) r = 0 for p in permutations("0123456", nl + ml): a, b = "".join(p[:nl]), "".join(p[nl:]) r += int(a, 7) < n and int(b, 7) < m print(r)
FUNC_DEF IF VAR NUMBER RETURN LIST RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL STRING VAR VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
BASE = 7 class Solution(object): def __init__(self, m, n): self.max_hour = self.itov(n) self.max_min = self.itov(m) self.used = used = [False] * BASE def itov(self, x): digits = [] if x == 0: digits.append(0) while x > 0: digits.append(x % BASE) x //= BASE digits.reverse() return digits def gen(self, pos=0, minute=False, smaller=False): max_val = self.max_min if minute else self.max_hour if pos >= len(max_val): if minute: return 1 else: return self.gen(0, True) else: ans = 0 for digit in range(BASE): if not self.used[digit] and (smaller or digit <= max_val[pos]): self.used[digit] = True ans += self.gen(pos + 1, minute, smaller or digit < max_val[pos]) self.used[digit] = False return ans def main(): n, m = map(int, input().split()) n -= 1 m -= 1 s = Solution(m, n) print(s.gen()) main()
ASSIGN VAR NUMBER CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
def digits(n): l = [] if n == 0: l.append(0) else: while n > 0: l.append(n % 7) n = n // 7 l.reverse() return l l = input().split() n = int(l[0]) m = int(l[1]) numh = len(digits(n - 1)) numm = len(digits(m - 1)) ans = 0 if numh + numm > 7: ans = 0 else: for i in range(n): for j in range(m): o = i p = j used = [(0) for lo in range(7)] k = 0 while k < numh: used[o % 7] += 1 o = o // 7 k += 1 k = 0 while k < numm: used[p % 7] += 1 p = p // 7 k += 1 if max(used) == 1: ans += 1 print(ans)
FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
n, m = [int(x) for x in input().split()] x = 7 d1 = 1 while x < n: d1 += 1 x *= 7 x = 7 d2 = 1 while x < m: d2 += 1 x *= 7 if d1 + d2 > 7: print(0) exit() c1 = {} L = [0] * d1 L[0] = -1 for sadas in range(n): L[0] += 1 for j in range(d1): if L[j] == 7: L[j] = 0 L[j + 1] += 1 g = [0] * 7 for j in range(7): g[j] = len([x for x in L if x == j]) if max(g) < 2: if tuple(g) not in c1: c1[tuple(g)] = 0 c1[tuple(g)] += 1 c2 = {} L = [0] * d2 L[0] = -1 for sadas in range(m): L[0] += 1 for j in range(d2): if L[j] == 7: L[j] = 0 L[j + 1] += 1 g = [0] * 7 for j in range(7): g[j] = len([x for x in L if x == j]) if max(g) < 2: if tuple(g) not in c2: c2[tuple(g)] = 0 c2[tuple(g)] += 1 ans = 0 for x in c1: for y in c2: if len([z for z in range(7) if x[z] + y[z] > 1]) == 0: ans += c1[x] * c2[y] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
import itertools def is_valid(p, n): num = int("".join(map(str, p)), 7) return 0 <= num < n def get_sz(x): if x == 1: return 1 t = 1 ans = 0 while t < x: t *= 7 ans += 1 return ans n, m = map(int, input().split()) ans = 0 sz_left, sz_right = get_sz(n), get_sz(m) for i in range(3**7): left_num = [] right_num = [] cur = i for j in range(7): if cur % 3 == 0: left_num.append(j) elif cur % 3 == 1: right_num.append(j) else: pass cur //= 3 if len(left_num) != sz_left or len(right_num) != sz_right: continue for perm_left in itertools.permutations(left_num): if not is_valid(perm_left, n): continue for perm_right in itertools.permutations(right_num): if not is_valid(perm_right, m): continue ans += 1 print(ans)
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
import sys h, m = [int(x) for x in input().split()] def convert(dec, min_len): sev = [] if dec == 0: if min_len > 0: return [0] * min_len return [0] while dec: sev.append(int(dec % 7)) dec = int(dec / 7) ret = sev[::-1] if min_len > 0: return [0] * (min_len - len(ret)) + ret return ret h_list = convert(h - 1, 0) m_list = convert(m - 1, 0) h_len = len(h_list) m_len = len(m_list) if h_len + m_len > 7: print(0) sys.exit(0) th = 0 tm = 0 def check(num, min_len, have): n_digits = 0 while True: digit = int(num % 7) n_digits += 1 if have[digit]: return False have[digit] = True num = int(num / 7) if num == 0: break if min_len - n_digits > 1: return False if min_len - n_digits == 1: if have[0]: return False have[0] = True return True cnt = 0 h_have = [False] * 7 while th < h and not check(th, h_len, h_have): th += 1 h_have = [False] * 7 while th < h: if check(tm, m_len, h_have[:]): cnt += 1 tm += 1 if tm == m: th += 1 h_have = [False] * 7 while th < h and not check(th, h_len, h_have): th += 1 h_have = [False] * 7 tm = 0 print(cnt)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER IF VAR NUMBER RETURN BIN_OP LIST NUMBER VAR RETURN LIST NUMBER WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP LIST NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
n, m = map(int, input().split()) def GetDigitsNumberIn7System(number): result = 1 x = 7 while True: if number <= x - 1: return result else: result += 1 x *= 7 def fillZeros(s, razryadCount): for i in range(razryadCount - len(s)): s = "0" + s return s def HasTheSameDigits(s): digits = set() for letter in s: digits.add(letter) if len(digits) == len(s): return False else: return True def int2base(x, base): if x < 0: sign = -1 elif x == 0: return "0" else: sign = 1 x *= sign res = "" while x != 0: res += str(x % base) x //= base return res[::-1] def GetNumberOfVariations(hoursInDay, minutesInHour): n1 = GetDigitsNumberIn7System(hoursInDay - 1) n2 = GetDigitsNumberIn7System(minutesInHour - 1) count = 0 listHours = [] listMinutes = [] for i in range(hoursInDay): if HasTheSameDigits(fillZeros(int2base(i, 7), n1)) == False: listHours.append(i) for i in range(minutesInHour): if HasTheSameDigits(fillZeros(int2base(i, 7), n2)) == False: listMinutes.append(i) for i in listHours: for j in listMinutes: str1 = fillZeros(int2base(i, 7), n1) str2 = fillZeros(int2base(j, 7), n2) strc = str1 + str2 if HasTheSameDigits(strc) == False: count += 1 return count r1 = GetDigitsNumberIn7System(n - 1) r2 = GetDigitsNumberIn7System(m - 1) if r1 + r2 > 7: print(0) else: res = GetNumberOfVariations(n, m) print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN STRING ASSIGN VAR NUMBER VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations as prm from sys import exit def to_base(n, base): ans = [] while n // base > 0: ans.append(n % base) n //= base ans.append(n % base) ans.reverse() return ans def le(l, n, m): prev_eq = True for i in range(len(n)): if l[i] == n[i]: continue if l[i] > n[i]: return False else: break for i in range(len(m)): if l[i + len(n)] == m[i]: continue if l[i + len(n)] > m[i]: return False else: break return True n, m = map(lambda x: to_base(int(x) - 1, 7), input().split()) ln = len(n) lm = len(m) if ln + lm > 7: print(0) exit(0) ans = 0 for i in prm(range(7), ln + lm): if le(i, n, m): ans += 1 print(ans)
FUNC_DEF ASSIGN VAR LIST WHILE BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations n, m = map(int, input().split()) p1 = 1 while 7**p1 < n: p1 += 1 p2 = 1 while 7**p2 < m: p2 += 1 cnt = 0 for perm in permutations(list("0123456"), p1 + p2): a = int("".join(perm[:p1]), 7) b = int("".join(perm[p1 : p1 + p2]), 7) if a < n and b < m: cnt += 1 print(cnt)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations n, m = tuple(map(int, input().split())) def baseN(num, b, numerals="0123456789abcdefghijklmnopqrstuvwxyz"): return ( num == 0 and numerals[0] or baseN(num // b, b, numerals).lstrip(numerals[0]) + numerals[num % b] ) def lt_eq(a, b): if len(a) == 0 and len(b) == 0: return True elif a[0] > b[0]: return False elif a[0] < b[0]: return True else: return lt_eq(a[1:], b[1:]) nmax = tuple(map(int, baseN(n - 1, 7))) mmax = tuple(map(int, baseN(m - 1, 7))) s = len(nmax) + len(mmax) ans = 0 if s > 7: print(0) else: for c in permutations(range(7), s): left, right = c[: len(nmax)], c[len(nmax) :] if lt_eq(left, nmax) and lt_eq(right, mmax): ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF STRING RETURN VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations def dfs(x): r = x == 0 while x: r += 1 x //= 7 return r n, m = list(map(int, input().split())) res, ln, lm = 0, dfs(n - 1), dfs(m - 1) for i in permutations("0123456", ln + lm): i = "".join(i) res += int(i[:ln], 7) < n and int(i[ln:], 7) < m print(res)
FUNC_DEF ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations def getdigits(x): if x == 0: return 1 digs = 0 while x != 0: x //= 7 digs += 1 return digs n, m = map(int, input().split()) a, b = getdigits(n - 1), getdigits(m - 1) c = a + b if c > 7: print(0) exit(0) combs = permutations(range(7), c) ans = 0 for comb in combs: d = comb[:a] e = comb[a:] f = "" g = "" for i in d: f += str(i) for i in e: g += str(i) x = int(f, 7) y = int(g, 7) if x < n and y < m: ans += 1 print(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
a, b = map(int, input().split()) def getdig(x): if x == 0: return 0, 1 s = "" c = 0 while x > 0: s = str(x % 7) + s c += 1 x //= 7 return int(s), c ass, an = getdig(a - 1) bss, bn = getdig(b - 1) tn = an + bn if tn > 7: print(0) exit() count = 0 sor = [str(i) for i in range(7)] def make(s, list, depth): global count for x in list: new = list[:] new.remove(x) make(s + x, new, depth + 1) if depth == tn: aaa, bbb = s[:an], s[an:] if int(aaa) <= ass and int(bbb) <= bss: count += 1 make("", sor, 0) print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations as p def length_base_7(ele, arr): if not ele: arr.append(0) return 1 length = 0 while ele: arr.append(ele % 7) ele //= 7 length += 1 return length n, m = list(map(int, input().split())) n -= 1 m -= 1 n_base7, m_base7 = [], [] n_length, m_length = length_base_7(n, n_base7), length_base_7(m, m_base7) n_base7, m_base7 = n_base7[::-1], m_base7[::-1] combination_number = {0, 1, 2, 3, 4, 5, 6} res = 0 for i in p(combination_number, n_length): if list(i) <= n_base7: for j in p(combination_number - set(i), m_length): if list(j) <= m_base7: res += 1 print(res)
FUNC_DEF IF VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
n, m = map(int, input().split()) d = [pow(7, i) for i in range(12)] ans = 0 nn, mm, kn, km = n - 1, m - 1, 0, 0 while nn: kn += 1 nn //= 7 while mm: km += 1 mm //= 7 kn += kn == 0 km += km == 0 nn, mm = [0] * kn, [0] * km def hm(r1, r2, s1, s2): global ans if s1 >= n: return if r1 == kn: if s2 >= m: return if r2 == km: ans += 1 return for i in range(0, 7): mm[r2] = i if i in nn: continue if len(set(mm[: r2 + 1])) < r2 + 1: continue hm(r1, r2 + 1, s1, s2 + i * d[r2]) return for i in range(0, 7): nn[r1] = i if len(set(nn[: r1 + 1])) < r1 + 1: continue hm(r1 + 1, r2, s1 + i * d[r1], s2) hm(0, 0, 0, 0) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR IF VAR VAR RETURN IF VAR VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations n, m = map(int, input().split()) def S(x): if x == 0: return 1 res = 0 while x > 0: x //= 7 res += 1 return res a, b = S(n - 1), S(m - 1) if a + b > 7: print(0) exit() D = list(range(7)) res = 0 for v in permutations(D, a): x = 0 for i in range(a): x = 7 * x + v[i] if x >= n: continue R = [d for d in D if d not in v] for u in permutations(R, b): y = 0 for i in range(b): y = 7 * y + u[i] res += y < m print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
n, m = input().split() n = int(n) m = int(m) def digitos(a): b = 7 r = 1 while b < a: b *= 7 r += 1 return r def valido(s): return int(s[:dn], 7) < n and int(s[dn:], 7) < m def solve(s): if len(s) == dn + dm: if valido(s): resp.append(s) for x in "0123456": if x not in s: solve(s + x) dn = digitos(n) dm = digitos(m) resp = [] if dn + dm > 7: print("0") else: solve("") print(len(resp))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
from itertools import permutations as P n, m = list(map(int, input().split())) a = n - 1 b = m - 1 x = 0 y = 0 c = 0 while a: x += 1 a = a // 7 if x == 0: x = 1 while b: y += 1 b = b // 7 if y == 0: y = 1 if x + y > 7 or x + y == 0: print(0) else: r = x + y l = list(P("0123456", r)) for ele in l: a = 0 b = 0 for i in range(x): a += int(ele[i]) * 7 ** (x - i - 1) for i in range(x, r): b += int(ele[i]) * 7 ** (r - i - 1) if a < n and b < m: c += 1 print(c)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches. First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation. Note that to display number 0 section of the watches is required to have at least one place. Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number. -----Input----- The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 10^9) — the number of hours in one day and the number of minutes in one hour, respectively. -----Output----- Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct. -----Examples----- Input 2 3 Output 4 Input 8 2 Output 5 -----Note----- In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2). In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
BASE = 7 def itov(x): digits = [] if x == 0: digits.append(0) while x > 0: digits.append(x % BASE) x //= BASE digits.reverse() return digits def gen(pos=0, minute=False, smaller=False): max_val = max_minute if minute else max_hour if pos >= len(max_val): if minute: return 1 else: return gen(0, True) else: ans = 0 for digit in range(BASE): if not used[digit] and (smaller or digit <= max_val[pos]): used[digit] = True ans += gen(pos + 1, minute, smaller or digit < max_val[pos]) used[digit] = False return ans n, m = map(int, input().split()) n -= 1 m -= 1 used = [False] * BASE max_hour = itov(n) max_minute = itov(m) print(gen())
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
s = input() k = int(input()) n = len(s) if k == 0: print(1) return if k == 1: print(n - 1) return st = [0, 0] for i in range(2, 1000): c = 0 for j in range(10): if 1 << j & i: c += 1 st.append(st[c] + 1) nums = [i for i in range(len(st)) if st[i] == k - 1 and i <= n] if len(nums) == 0: print(0) return max_k = nums[-1] c = [[(0) for i in range(max_k + 1)] for j in range(n + 1)] c[0][0] = 0 for i in range(n + 1): c[i][0] = 0 for i in range(max_k + 1): c[0][i] = 0 for i in range(1, n + 1): c[i][1] = i for i in range(2, n + 1): for j in range(2, min(i + 1, max_k + 1)): c[i][j] = c[i - 1][j - 1] + c[i - 1][j] c[i][j] %= 10**9 + 7 ans = 0 for num in nums: cnt = num for i in range(n): if s[i] == "1": ans += c[n - i - 1][cnt] ans %= 10**9 + 7 cnt -= 1 if cnt == 0: ans += 1 break print(ans % (10**9 + 7))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
mas = [[] for i in range(1001)] mas[0].append(1) mas[0].append(1) for i in range(1, 1000): mas[i].append(1) for j in range(1, i): mas[i].append((mas[i - 1][j] + mas[i - 1][j - 1]) % (10**9 + 7)) mas[i].append(1) def c(k, n): if k > n: return 0 if k < 0: return 0 global mas return mas[n][k] m = [0] * 1000 for i in range(1, 1000): nw = i t = 0 while nw != 1: nw = sum([int(j) for j in str(bin(nw)[2:])]) t += 1 m[i] = t m[1] = 0 n = input() k = int(input()) if k >= 6: print(0) exit() if k == 0: print(1) exit() if k == 1: print(len(n) - 1) exit() ans = 0 for kkk in range(1, 1000): if m[kkk] == k - 1: nw = kkk t = 0 for i in range(len(n)): if n[i] == "1": ans += c(nw - t, len(n) - 1 - i) ans %= 10**9 + 7 t += 1 if sum([int(j) for j in n]) == kkk: ans += 1 ans %= 10**9 + 7 print(ans)
ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
s0 = input() k = int(input()) s1 = s0[::-1] lens1 = len(s1) maxnum = 1005 mod = 1000000007 dp = [([0] * maxnum) for tmpi in range(maxnum)] f = [0] * maxnum c = [([0] * maxnum) for tmpi in range(maxnum)] def cntone(num): tmps = bin(num)[2:] cnt = 0 for i in range(len(tmps)): if tmps[i] == "1": cnt += 1 return cnt for i in range(1, maxnum): if i == 1: f[i] = 0 else: f[i] = f[cntone(i)] + 1 for i in range(maxnum): if i == 0: c[i][0] = 1 continue for j in range(i + 1): if j == 0: c[i][j] = 1 else: c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod for i in range(lens1): if i == 0: dp[i][0] = 1 if s1[i] == "1": dp[i][1] = 1 else: dp[i][1] = 0 continue else: for j in range(0, i + 2): if j == 0: dp[i][j] = 1 continue if s1[i] == "1": dp[i][j] = (dp[i - 1][j - 1] + c[i][j]) % mod else: dp[i][j] = dp[i - 1][j] % mod ans = 0 for i in range(1, lens1 + 1): if f[i] == k - 1: ans = (ans + dp[lens1 - 1][i]) % mod if k == 0: ans = 1 elif k == 1: ans -= 1 else: ans = ans print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
R = lambda: map(int, input().split()) mod = 10**9 + 7 maxn = 1001 c = [[(0) for j in range(maxn)] for i in range(maxn)] for i in range(maxn): c[i][0] = 1 for i in range(1, maxn): for j in range(i + 1): c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod arr = list(map(int, input())) k = int(input()) if k == 0: print(1 if arr.count(1) else 0) exit(0) ops = [0] * (maxn + 1) ans = 0 for i in range(2, maxn): cnt = bin(i).count("1") ops[i] = ops[cnt] + 1 for i in range(1, maxn): if ops[i] == k - 1: oc = i for j, x in enumerate(arr): if x and oc >= 0: ans = (ans + c[len(arr) - j - 1][oc]) % mod oc -= 1 ans = (ans + 1) % mod if arr.count(1) == i else ans if k == 1: ans = (ans + mod - 1) % mod print(ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
def count_oper(x): if x == 1: return 0 res = 0 y = x while y > 0: if y % 2 == 1: res += 1 y = y // 2 return count_oper(res) + 1 def solve(s, k): a = [] ones = [] for i in range(len(s)): c = s[i] a.append(int(c == "1")) if c == "1": ones.append(i) nones = len(ones) n = len(a) if k == 0: print(1) return if k == 1 and n != 1: print(n - 1) return if n == 1: if k > 0: print(0) else: print(1) return c = [] c.append([0] * (n + 1)) for n1 in range(1, n + 1): tmp = [0] * (n + 1) for m in range(n1 + 1): if m == 0 or m == n1: tmp[m] = 1 else: tmp[m] = (c[n1 - 1][m - 1] + c[n1 - 1][m]) % modulo c.append(tmp) ans = 0 for m in range(1, n + 1): if count_oper(m) == k - 1: for j in range(min(nones, m)): ans += c[n - ones[j] - 1][m - j] % modulo if nones >= m: ans += 1 print(ans % modulo) s = input() k = int(input()) modulo = 10**9 + 7 solve(s, k)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER 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 NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
MX = 1030 M = 1000 * 1000 * 1000 + 7 c = [([0] * MX) for i in range(MX)] for i in range(MX): c[i][0] = 1 for i in range(1, MX): for j in range(1, MX): c[i][j] = c[i - 1][j] + c[i - 1][j - 1] c[i][j] %= M num = list(map(int, list(input()))) cnt = int(input()) dp = [0] * MX for i in range(2, MX): dp[i] = dp[bin(i).count("1")] + 1 if cnt == 0: print(1) return res = 0 for i in range(1, MX): if dp[i] != cnt - 1: continue n = len(num) - 1 k = i for pos in range(len(num)): if num[pos] == 1: res += c[n][k] res %= M k -= 1 n -= 1 if n == -1 and k == 0: res += 1 if cnt == 1: res -= 1 print(res)
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
from sys import setrecursionlimit def main(): s, k = input(), int(input()) n = len(s) if k < 2: print((1, n - 1)[k]) return tais = [(n - i - 1) for i, c in enumerate(s) if c == "1"] tlen, cache, res = len(tais), {(0, 0): 1}, 0 def c(n, m): if n < m * 2: m = n - m if 0 <= m: if (n, m) not in cache: cache[n, m] = (c(n - 1, m - 1) + c(n - 1, m)) % 1000000007 return cache[n, m] return 0 for m in range(n, 0, -1): x, t = m, k while x != 1: y = 0 t -= 1 while x: y += x & 1 x //= 2 x = y if t == 1: if len(tais) > m: del tais[m:] res += sum(c(t, m - i) for i, t in enumerate(tais)) + (m <= tlen) print(res % 1000000007) setrecursionlimit(1050) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR DICT NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
class Combi: def __init__(self, N, mod=10**9 + 7): self.power = [(1) for _ in range(N + 1)] self.rev = [(1) for _ in range(N + 1)] self.mod = mod for i in range(2, N + 1): self.power[i] = self.power[i - 1] * i % self.mod self.rev[N] = pow(self.power[N], self.mod - 2, self.mod) for j in range(N, 0, -1): self.rev[j - 1] = self.rev[j] * j % self.mod def com(self, K, R): if not 0 <= R <= K: return 0 else: return self.power[K] * self.rev[K - R] * self.rev[R] % self.mod def perm(self, K, R): if not 0 <= R <= K: return 0 else: return self.power[K] * self.rev[K - R] % self.mod def bitcnt(X): res = 0 v = X while v: res += v & 1 v >>= 1 return res c = Combi(10000) NL = list(map(int, list(input())))[::-1] N = len(NL) K = int(input()) MOD = 10**9 + 7 dp = [([0] * 1020) for i in range(1020)] dp[0][0] = 1 for pos, bit in enumerate(NL): if bit == 1: for bit in range(1010): dp[pos + 1][bit] = (dp[pos][bit - 1] + c.com(pos, bit)) % MOD continue else: for bit in range(1010): dp[pos + 1][bit] = dp[pos][bit] continue INF = 1 << 60 cnt = [INF] * 1010 cnt[1] = 0 MOD = 10**9 + 7 for i in range(2, 1010): cnt[i] = 1 + cnt[bitcnt(i)] if K == 0: print(dp[N][0]) exit() else: ans = 0 for bitcnt in range(1010): if cnt[bitcnt] == K - 1: ans += dp[N][bitcnt] if K == 1: ans -= 1 print(ans % MOD) exit()
CLASS_DEF FUNC_DEF BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR 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 VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF IF NUMBER VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_DEF IF NUMBER VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 13_10 = 1101_2, so it has 3 bits set and 13 will be reduced to 3 in one operation. He calls a number special if the minimum number of operations to reduce it to 1 is k. He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination! Since the answer can be large, output it modulo 10^9 + 7. -----Input----- The first line contains integer n (1 ≤ n < 2^1000). The second line contains integer k (0 ≤ k ≤ 1000). Note that n is given in its binary representation without any leading zeros. -----Output----- Output a single integer — the number of special numbers not greater than n, modulo 10^9 + 7. -----Examples----- Input 110 2 Output 3 Input 111111011 2 Output 169 -----Note----- In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
def reduce(n): n = bin(n)[2:] n = list(n) n = list(map(int, n)) return sum(n) def a(n, bits, dp): if bits == 0: return 1 if n == "0" or n == "": return 0 if bits > len(n): return 0 if bits == len(n): if n == "1" * len(n): return 1 else: return 0 ans = 0 while bits != 0 and (n != "" and n != "0") and bits < len(n): ans += dp[len(n) - 1][bits] i = 1 while i < len(n) and n[i] == "0": i += 1 n = n[i:] bits -= 1 if bits == 0: ans += 1 elif bits == len(n) and n == "1" * len(n): ans += 1 return ans n = input() k = int(input()) if k > 5: print(0) elif k == 0: print(1) elif k == 1: print(len(n) - 1) else: dp = [] dp.append([0]) for i in range(1, 1002): dp.append([]) for j in range(i): if j == 0: dp[i].append(1) else: dp[i].append((dp[i - 1][j - 1] + dp[i - 1][j]) % 1000000007) dp[i].append(1) pos = [] for i in range(1, len(n) + 1): count = 0 I = i while i != 1: i = reduce(i) count += 1 if count == k - 1: pos.append(I) ans = 0 for i in pos: ans += a(n, i, dp) print(ans % 1000000007)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR STRING VAR STRING RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR IF VAR BIN_OP STRING FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR STRING VAR STRING VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER