description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def KMPSearch(pat, txt): M = len(pat) N = len(txt) lps = [0] * M j = 0 computeLPSArray(pat, M, lps) i = 0 while i < N: if pat[j] == txt[i]: i += 1 j += 1 if j == M: print("Found pattern at index " + str(i - j)) j = lps[j - 1] elif i < N and pat[j] != txt[i]: if j != 0: j = lps[j - 1] else: i += 1 def computeLPSArray(pat, M, lps): len = 0 lps[0] i = 1 while i < M: if pat[i] == pat[len]: len += 1 lps[i] = len i += 1 elif len != 0: len = lps[len - 1] else: lps[i] = 0 i += 1 return lps s = input() m = len(s) lps = [0] * m x = computeLPSArray(s, m, lps) x = [0] + x a = x[-1] b = x[a] ans = "" for i in range(len(x)): if x[i] == a and i != m: ans = s[0:a] if ans == "": if b != 0: ans = s[0:b] else: ans = "Just a legend" print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER EXPR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR IF VAR STRING IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s = input() n = len(s) p = [-1] * n mark = [False] * n q = -1 for i in range(1, n): while q >= 0 and s[i] != s[q + 1]: q = p[q] if s[i] == s[q + 1]: q += 1 p[i] = q if q >= 0 and i < n - 1: mark[q] = True q = p[n - 1] while q >= 0: if s[0 : q + 1] == s[n - q - 1 :] and mark[q]: print(s[0 : q + 1]) exit() q = p[q] print("Just a legend")
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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def z_func(s): n = len(s) z = [0] * n l, r = 0, 0 for i in range(1, n): if i <= r: z[i] = min(r - i + 1, z[i - l]) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: l = i r = i + z[i] - 1 return z def solve(s): z = z_func(s) n = len(z) max_substr = 0 max_suffix = 0 for i in range(1, n): max_substr = max(max_substr, min(z[i], n - i - 1)) for i in range(n - max_substr, n): if i + z[i] == n: max_suffix = max(max_suffix, z[i]) return max_suffix s = input() ans = solve(s) if ans > 0: print(s[:ans]) else: print("Just a legend")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
a = input() n = len(a) z = [0] * n l = 0 r = 0 for i in range(1, n): if i <= r: z[i] = min(r - i, z[i - l]) while i + z[i] < n and a[z[i]] == a[i + z[i]]: z[i] += 1 if i + z[i] - 1 >= r: r = i + z[i] - 1 l = i mx = 0 ans = 0 for i in range(n): if z[i] == n - i and mx >= z[i]: ans = z[i] break mx = max(z[i], mx) if ans == 0: print("Just a legend") else: print(a[:ans])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s = input() n = len(s) p = [0] * (n + 1) i = 0 j = 1 while j < n: if s[j] == s[i]: j += 1 i += 1 p[j] = i elif i: i = p[i] else: j += 1 a = p.pop() b = p[a] if a and a in p: print(s[:a]) elif b: print(s[:b]) else: print("Just a legend")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
from sys import stdin def Zarr(pat, text): s = pat + "/" + text z = [(0) for i in range(len(s))] L = 0 R = 0 n = len(s) for i in range(1, len(s)): if i > R: L = R = i while R < n and s[R - L] == s[R]: R += 1 z[i] = R - L R -= 1 elif z[i - L] + i <= R: z[i] = z[i - L] else: L = i while R < n and s[R - L] == s[R]: R += 1 z[i] = R - L R -= 1 dp = [(0) for i in range(1000006)] ans = -1 for i in range(len(pat) + 2, len(s)): dp[z[i]] += 1 for i in range(len(dp) - 2, -1, -1): dp[i] += dp[i + 1] for i in range(len(pat) + 2, len(s)): if i + z[i] == len(s) and dp[z[i]] > 1 and z[i] > ans: ans = z[i] if ans == -1: print("Just a legend") else: print(pat[0:ans]) text = stdin.readline().strip() Zarr(text, text)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s = input() i, n = 0, len(s) p = [-1] + [0] * n for j in range(1, n): while i + 1 and s[i] != s[j]: i = p[i] i += 1 p[j + 1] = i k = p.pop() print(s[:k] if k and k in p else s[: p[k]] if p[k] > 0 else "Just a legend")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def KMP(st): i = 0 j = 1 arr = [(0) for i in range(len(st))] while j < len(st): if s[j] == s[i]: arr[j] = i + 1 i += 1 else: while i > 0: i = arr[i - 1] if s[i] == s[j]: arr[j] = i + 1 i += 1 break j += 1 return arr s = input() arr = KMP(s) maxi = arr[-1] found = arr[maxi - 1] for i in arr[1:-1]: if i == maxi: found = maxi if found == 0: print("Just a legend") else: print(s[:found])
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
P = input() m = len(P) f = [0] * m j = 1 k = 0 while j < m: if P[j] == P[k]: f[j] = k + 1 j += 1 k += 1 elif k > 0: k = f[k - 1] else: j += 1 l = f.pop() if l: if l in f: print(P[:l]) elif f[l - 1]: print(P[: f[l - 1]]) else: print("Just a legend") else: print("Just a legend")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def kmp_preprocess(s, kmp): kmp[0] = 0 i = 1 j = 0 while i < len(s): if s[i] == s[j]: j += 1 kmp[i] = j i += 1 elif j != 0: j = kmp[j - 1] else: kmp[i] = 0 i += 1 s = input() kmp = [None] * len(s) kmp_preprocess(s, kmp) res = None for i in range(len(s) - 1): if kmp[i] == kmp[-1]: res = kmp[-1] break if not res: if kmp[kmp[-1] - 1] != 0: res = kmp[kmp[-1] - 1] if not res: print("Just a legend") else: print(s[:res])
FUNC_DEF ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
import sys I = lambda: int(input()) readline = lambda: sys.stdin.readline().strip() RM = readlist = lambda x=int: map(x, readline().split()) def prefix_function(s): n = len(s) pi = [0] * n for i in range(1, n): j = pi[i - 1] while j > 0 and s[i] != s[j]: j = pi[j - 1] if s[i] == s[j]: j += 1 pi[i] = j return pi s = readline() pi = prefix_function(s) pis = set(pi[:-1]) j = pi[-1] while j > 0 and not j in pis: j = pi[j - 1] print(s[:j] if j > 0 else "Just a legend") quit() s = readline() n = len(s) ppow, base, p, m = [0] * int(n + 5), 1, 31, int(1000000000.0 + 7) for i in range(n + 5): ppow[i], base = base, base * p % m hashp = [0] * n hashp[0] = ord(s[0]) - 96 for ind in range(1, n): hashp[ind] = (hashp[ind - 1] + (ord(s[ind]) - 96) * ppow[ind]) % m stop = False for l in range(n - 1, 0, -1): prefix_hash = hashp[l - 1] * ppow[n + 1] % m suffix_hash = (hashp[n - 1] - hashp[n - l - 1]) * ppow[l + 1] % m if suffix_hash != prefix_hash: continue for t in range(1, n - l): temp_hash = (hashp[t + l - 1] - hashp[t - 1]) * ppow[n - t + 1] % m if temp_hash == suffix_hash: print(s[t : t + l]) stop = True break if stop: break else: print("Just a legend")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
import sys Ri = lambda: [int(x) for x in sys.stdin.readline().split()] ri = lambda: sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**18 MOD = 10**8 def computeLPSArray(pat): val = 0 lps = [0] * len(pat) lps[0] = 0 i = 1 while i < len(pat): if pat[i] == pat[val]: val += 1 lps[i] = val i += 1 elif val != 0: val = lps[val - 1] else: lps[i] = 0 i += 1 return lps s = ri() dp = computeLPSArray(s) val = dp[len(s) - 1] ans = "" val3 = dp[dp[len(s) - 1] - 1] maxx = -1 for i in range(len(s) - 2, 0, -1): if dp[i] == val and val != 0: maxx = val elif dp[i] != 0: val2 = dp[i] if val2 == val3: maxx = max(maxx, val2) ans = s[:maxx] if maxx != -1: print(ans) else: print("Just a legend")
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
from sys import stdin def prefix_calculator(string): p = [0] * len(string) j = 0 for i in range(1, len(string)): while j > 0 and string[i] != string[j]: j = p[j - 1] if string[i] == string[j]: j += 1 p[i] = j return p string = stdin.readline().strip() if len(string) <= 2: print("Just a legend") exit() p = prefix_calculator(string) if p[-1] == 0: print("Just a legend") else: for i in range(1, len(p) - 1): if p[i] == p[-1]: print(string[0 : p[-1]]) break else: if p[p[-1] - 1] != 0: print(string[0 : p[p[-1] - 1]]) else: print("Just a legend")
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def prefix(lsa, n): index = 0 i = 1 while i < n: if s[i] == s[index]: lsa[i] = index + 1 i += 1 index += 1 elif index == 0: lsa[i] = 0 i += 1 else: index = lsa[index - 1] s = input() n = len(s) lsa = [0] * n prefix(lsa, n) if lsa[-1] == 0: print("Just a legend") else: for i in range(n - 1): if lsa[i] == lsa[-1]: print(s[: lsa[i]]) exit() if lsa[lsa[-1] - 1] == 0: print("Just a legend") else: print(s[: lsa[lsa[-1] - 1]])
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def kmpTable(s, n): res = [0] * n check_idx = 0 for i in range(1, n): while check_idx > 0 and s[check_idx] != s[i]: check_idx = res[check_idx - 1] if s[check_idx] == s[i]: check_idx += 1 res[i] = check_idx return res s = input() n = len(s) kmp_table = kmpTable(s, n) psize = kmp_table[n - 1] if psize > 0 and psize in kmp_table[:-1]: print(s[:psize]) else: psize = kmp_table[psize - 1] if psize > 0 and psize in kmp_table[:-1]: print(s[:psize]) else: print("Just a legend")
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def password(str): lst = [0] * len(str) n = 0 for i in range(1, len(str)): while n > 0 and str[n] != str[i]: n = lst[n - 1] if str[n] == str[i]: n += 1 lst[i] = n else: lst[i] = n return lst x = None in_c = input() lst = password(in_c) var = lst[len(in_c) - 1] if var != 0: for j in range(2): for i in range(1, len(in_c) - 1): if var == lst[i]: x = True break if not x: var = lst[var - 1] if var == 0: break else: break if not x or var == 0: print("Just a legend") else: print(in_c[0:var])
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s = input() count = -1 arr = [-1] for x in s: while count != -1 and s[count] != x: count = arr[count] count = count + 1 arr.append(count) le = arr[-1] if arr.count(le) < 2: le = arr[le] if le > 0: print(s[:le]) else: print("Just a legend")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s = input() n = len(s) lps = [0] * n i = 1 length = 0 while i < n: if s[i] == s[length]: length += 1 lps[i] = length i += 1 elif length == 0: i += 1 else: length = lps[length - 1] x = lps[-1] flag = 1 while x: for i in range(1, n - 1): if lps[i] == x: flag = 0 break else: flag = 1 if flag == 0: break x = lps[x - 1] print(s[:x]) if flag == 0 else print("Just a legend")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
from sys import stderr, stdin, stdout def lcp(string): l = len(string) lis = [0] * l j = 1 k = 0 while j < l: if string[j] == string[k]: lis[j] = k + 1 j += 1 k += 1 elif k > 0: k = lis[k - 1] else: j += 1 return lis string = input() lis = lcp(string) if lis[-1] == 0: print("Just a legend") exit() for i in range(len(string) - 1): if lis[i] != 0 and lis[i] == lis[-1]: print(string[: lis[-1]]) exit() l = lis[-1] if l == 0: print("Just a legend") exit() l = lis[lis[-1] - 1] if l: print(string[:l]) exit() print("Just a legend")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
from sys import stdin, stdout s = stdin.readline().rstrip() n = len(s) def compute_lps(s): lps = [(0) for i in range(n)] Len = 0 lps[0] = 0 i = 1 while i < n: if s[i] == s[Len]: Len += 1 lps[i] = Len i += 1 elif Len != 0: Len = lps[Len - 1] else: lps[i] = 0 i += 1 return lps def Longestsubstring(s): lps = compute_lps(s) if lps[n - 1] == 0: return 0 for i in range(0, n - 1): if lps[i] == lps[n - 1]: return s[0 : lps[i]] if lps[lps[n - 1] - 1] == 0: return 0 else: return s[0 : lps[lps[n - 1] - 1]] res = Longestsubstring(s) if res: stdout.write(res) else: stdout.write("Just a legend")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER RETURN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def lcp(text): length = len(text) lis = [0] * length j = 1 k = 0 while j < length: if text[j] == text[k]: lis[j] = k + 1 j += 1 k += 1 elif k > 0: k = lis[k - 1] else: j += 1 return lis text = input() length = len(text) lis = lcp(text) idf = 0 for i in range(length - 1): if lis[i] == lis[-1] and lis[i] != 0: print(text[0 : lis[i]]) idf = 1 break if idf == 0: if lis[-1] == 0: print("Just a legend") else: maximum = lis[lis[-1] - 1] if maximum == 0: print("Just a legend") else: print(text[0:maximum])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
from sys import stdin, stdout def z_function(s): z = [0] * len(s) l, r = 0, 0 for i in range(1, len(s)): z[i] = max(0, min(z[i - l], r - i)) while z[i] + i < len(s) and s[z[i]] == s[z[i] + i]: z[i] += 1 if i + z[i] - 1 > r: l, r = i, i + z[i] return z def prefix_function(s): v = [0] * len(s) for i in range(1, len(s)): k = v[i - 1] while k > 0 and s[k] != s[i]: k = v[k - 1] if s[k] == s[i]: k = k + 1 v[i] = k return v def build_hash(s): global hs, pw, q, p n = len(s) hs = [0] * (n + 1) pw = [1] * (n + 1) for i in range(n): hs[i + 1] = (hs[i] * q + ord(s[i])) % p pw[i + 1] = pw[i] * q % p def get_hash(a, b): return (hs[b] - hs[a] * pw[b - a]) % p s = input() p = prefix_function(s) if p.count(p[-1]) > 1 and p[-1] > 0: print(s[: p[-1]]) else: q = p[p[-1] - 1] if q > 0: print(s[:q]) else: print("Just a legend")
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
import sys input = sys.stdin.readline def swaparr(arr, a, b): temp = arr[a] arr[a] = arr[b] arr[b] = temp def gcd(a, b): if a == 0: return b return gcd(b % a, a) def nCr(n, k): if k > n - k: k = n - k res = 1 for i in range(k): res = res * (n - i) res = res / (i + 1) return res def upper_bound(a, x, lo=0): hi = len(a) while lo < hi: mid = (lo + hi) // 2 if a[mid] < x: lo = mid + 1 else: hi = mid return lo def primefs(n): primes = {} while n % 2 == 0: primes[2] = primes.get(2, 0) + 1 n = n // 2 for i in range(3, int(n**0.5) + 2, 2): while n % i == 0: primes[i] = primes.get(i, 0) + 1 n = n // i if n > 2: primes[n] = primes.get(n, 0) + 1 return primes def power(x, y, p): res = 1 x = x % p if x == 0: return 0 while y > 0: if y & 1 == 1: res = res * x % p y = y >> 1 x = x * x % p return res def swap(a, b): temp = a a = b b = temp return a, b def find(x, link): p = x while p != link[p]: p = link[p] while x != p: nex = link[x] link[x] = p x = nex return p def union(x, y, link, size): x = find(x, link) y = find(y, link) if size[x] < size[y]: x, y = swap(x, y) if x != y: size[x] += size[y] link[y] = x def sieve(n): prime = [(True) for i in range(n + 1)] p = 2 while p * p <= n: if prime[p] == True: for i in range(p * p, n + 1, p): prime[i] = False p += 1 return prime MAXN = int(1000000.0 + 5) def spf_sieve(): spf[1] = 1 for i in range(2, MAXN): spf[i] = i for i in range(4, MAXN, 2): spf[i] = 2 for i in range(3, ceil(MAXN**0.5), 2): if spf[i] == i: for j in range(i * i, MAXN, i): if spf[j] == j: spf[j] = i def factoriazation(x): ret = {} while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1 x = x // spf[x] return ret def int_array(): return list(map(int, input().strip().split())) def str_array(): return input().strip().split() MOD = int(1000000000.0) + 7 CMOD = 998244353 INF = float("inf") NINF = -float("inf") def ComputeLPSArray(pat): M = len(pat) lps = [0] * M length = 0 i = 1 while i < M: if pat[i] == pat[length]: lps[i] = length + 1 length += 1 i += 1 elif length != 0: length = lps[length - 1] else: lps[i] = 0 i += 1 return lps string = input().strip() lps = ComputeLPSArray(string) x, y = -1, -1 x = lps[lps[-1] - 1] if lps[-1] > 0 and lps.count(lps[-1]) > 1: y = lps[-1] k = max(x, y) if k > 0: print(string[:k]) else: print("Just a legend")
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR DICT WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s = input() longest_matching = [-1] for c in s[1:]: last_matching = longest_matching[-1] no_match = False while s[last_matching + 1] != c: if last_matching != -1: last_matching = longest_matching[last_matching] else: last_matching = last_matching - 1 break longest_matching.append(last_matching + 1) if longest_matching.count(longest_matching[-1]) < 2: ans = longest_matching[longest_matching[-1]] + 1 else: ans = longest_matching[-1] + 1 if ans > 0: print(s[:ans]) else: print("Just a legend")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def prefix_function(s): pi = [0] * len(s) for i in range(1, len(s)): j = pi[i - 1] while j > 0 and s[i] != s[j]: j = pi[j - 1] if s[i] == s[j]: j += 1 pi[i] = j return pi s = input() pi = prefix_function(s) n = len(s) ok = True if pi[n - 1] != 0 and pi.count(pi[n - 1]) > 1: print(s[n - pi[n - 1] : n]) elif pi[pi[n - 1] - 1] > 0: print(s[pi[n - 1] - pi[pi[n - 1] - 1] : pi[n - 1]]) else: print("Just a legend")
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def z_func(s): l = 0 r = 0 n = len(s) z = [0] * n z[0] = n for i in range(1, n): if i <= r: z[i] = min(r - i + 1, z[i - l]) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: l = i r = i + z[i] - 1 return z string = input() n = len(string) z = z_func(string) l = 0 for i in range(1, n): if z[i] == n - i: for j in range(1, i): if z[j] >= z[i]: l = z[i] break if l > 0: break if l > 0: print(string[0:l]) else: print("Just a legend")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s = input() def count_unique_substring(s): n = len(s) p = 31 m = 10**9 + 9 p_pow = [1] for i in range(1, n): p_pow.append(p_pow[i - 1] * p % m) hash = [0] count = 0 flag = 0 for i in range(n): hash.append((hash[i] + (ord(s[i]) - ord("a") + 1) * p_pow[i]) % m) for l in range(n, 0, -1): c_1 = (hash[l] + m - hash[0]) % m c_1 = c_1 * p_pow[n - 1] % m c_2 = (hash[n - l + l] + m - hash[n - l]) % m c_2 = c_2 * p_pow[l - 1] % m if c_1 == c_2: for i in range(1, n - l): c_h = (hash[i + l] + m - hash[i]) % m c_h = c_h * p_pow[n - i - 1] % m if c_h == c_2: return s[i : i + l] t = count_unique_substring(s) if t: print(t) else: print("Just a legend")
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def main(): t = 1 for z in range(t): s = input() s = s n = len(s) z = [0] * n l = 0 r = 0 for i in range(1, n): if r >= i: z[i] = min(z[i - l], r - i + 1) while z[i] + i < n and s[z[i]] == s[z[i] + i]: z[i] += 1 if i + z[i] - 1 > r: l = i r = i + z[i] - 1 ar = [0] * n for i in range(n): ar[z[i]] += 1 for i in range(n - 2, -1, -1): ar[i] += ar[i + 1] ans = -1 for i in range(1, n): if z[i] == n - i and ar[z[i]] > 1: ans = i break if ans == -1: print("Just a legend") else: print(s[ans:]) main()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def buildZ(S): L, R = 0, 0 n = len(S) Z = [0] * n for i in range(1, n): if i > R: L = R = i while R < n and S[R - L] == S[R]: R += 1 Z[i] = R - L R -= 1 else: k = i - L if Z[k] < R - i + 1: Z[i] = Z[k] else: L = i while R < n and S[R - L] == S[R]: R += 1 Z[i] = R - L R -= 1 return Z S = input().strip() Z = buildZ(S) maxz, res = 0, 0 n = len(S) for i in range(1, n): if Z[i] == n - i and maxz >= n - i: res = n - i break maxz = max(maxz, Z[i]) if res > 0: print(S[0:res]) else: print("Just a legend")
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
n = input() hm = [0] * (len(n) + 1) p = 31 m = len(n) mod = 10**9 + 9 pow = [0] * (m + 1) pow[0] = 1 for i in range(1, m): pow[i] = pow[i - 1] * p % mod hm[0] = 1 * (ord(n[0]) - ord("a") + 1) m = len(n) s = 0 e = 0 ans = [] a = 0 for i in range(1, m): x = ord(n[i]) - ord("a") + 1 hm[i] = (hm[i - 1] % mod + pow[i] * x % mod) % mod for j in range(0, m): a = hm[j] s = m - 1 - j e = m - 1 a = hm[j] * pow[s] % mod b = (hm[e] - hm[s - 1] + mod) % mod if a == b: ans.append(j) def check(idx): id = ans[idx] l = 1 r = id + 1 while l < m and r < m: a = hm[id] * pow[l] % mod b = (hm[r] - hm[l - 1] + mod) % mod if a == b and r != m - 1: return True l += 1 r += 1 return False lo = 0 hi = len(ans) while lo <= hi: mi = (lo + hi) // 2 if check(mi): lo = mi + 1 else: hi = mi - 1 if lo - 1 == -1: print("Just a legend") else: print(n[: ans[lo - 1] + 1])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def KMPS(pat, txt, lps): M = len(pat) N = len(txt) j = 0 i = 0 while i < N: if pat[j] == txt[i]: i += 1 j += 1 if j == M: return 1 j = lps[j - 1] elif i < N and pat[j] != txt[i]: if j != 0: j = lps[j - 1] else: i += 1 return 0 s = input() s = s.lower() n = len(s) pi = [0] * n m = 0 i = 1 while i < n: if s[i] == s[m]: m += 1 pi[i] = m i += 1 elif m: m = pi[m - 1] else: pi[i] = 0 i += 1 cr = pi[n - 1] if cr == 0: print("Just a legend") else: an = s[n - cr :] ix = 1 for i in range(n): if pi[i]: ix = i break if pi.count(len(an)) < 2: an = an[ix:] t = len(an) if an: if an == s[:t] and an == s[n - t :]: pi = [0] * t m = 0 i = 1 while i < t: if an[i] == an[m]: m += 1 pi[i] = m i += 1 elif m: m = pi[m - 1] else: pi[i] = 0 i += 1 if not KMPS(an, s[1 : n - 1], pi): an = "" else: an = "" if an: print(an) else: print("Just a legend")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR STRING ASSIGN VAR STRING IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
from sys import stdin def findpass(s): arr = suff_array(s) n = len(s) maxidx = arr[n - 1] valid = False for i in range(n - 1): if arr[i] == maxidx: valid = True break if not valid: maxidx = arr[maxidx - 1] if maxidx == 0: return "Just a legend" return s[:maxidx] def suff_array(s): n = len(s) table = [0] * n pos, i = 0, 1 while i < n: if s[pos] == s[i]: table[i] = pos + 1 pos += 1 i += 1 elif pos == 0: i += 1 else: pos = table[pos - 1] return table s = stdin.readline().strip() print(findpass(s))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN STRING RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def longestPrefixSuffix(s): global d n = len(s) lps = [0] * n l = 0 m = 0 i = 1 while i < n: if s[i] == s[l]: l = l + 1 lps[i] = l i = i + 1 elif l != 0: l = lps[l - 1] else: lps[i] = 0 i = i + 1 s = s[::-1] lps1 = [0] * n l = 0 i = 1 while i < n: if s[i] == s[l]: l = l + 1 lps1[i] = l i = i + 1 elif l != 0: l = lps1[l - 1] else: lps1[i] = 0 i = i + 1 ans = 0 for i in range(n - 1): if lps[i] == lps1[n - (i - lps[i] + 1) - 1]: ans = max(ans, lps[i]) return ans q = input() l = longestPrefixSuffix(q) if l == 0: print("Just a legend") else: print(q[:l])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s = input() n = len(s) p = [0] * n for i in range(1, n): cur = p[i - 1] while s[i] != s[cur] and cur > 0: cur = p[cur - 1] if s[i] == s[cur]: p[i] = cur + 1 if p[-1] == 0: print("Just a legend") else: sa = p[:-1] if p[-1] not in sa: if p[p[-1] - 1] == 0: print("Just a legend") else: print(s[: p[p[-1] - 1]]) else: print(s[: p[-1]])
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
pat = input() m = len(pat) lps = [(0) for i in range(m)] j, i = 0, 1 while i < m: if pat[i] == pat[j]: j += 1 lps[i] = j i += 1 elif j == 0: lps[i] = 0 i += 1 else: j = lps[j - 1] req = lps[-1] if lps[-1] != 0 and lps.count(req) > 1: print(pat[:req]) elif lps[-1] != 0 and lps[lps[-1] - 1] != 0: print(pat[: lps[lps[-1] - 1]]) else: print("Just a legend")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def knuthMorrisonPreprocess(pattern, m): fail = [0] * m i = 0 j = 1 fail[0] = 0 while j < m: if pattern[i] == pattern[j]: fail[j] = i + 1 i += 1 j += 1 elif i > 0: i = fail[i - 1] else: j += 1 return fail string = input() n = len(string) properPrefixArrayL = knuthMorrisonPreprocess(string, n) properPrefixArray = properPrefixArrayL[-1] if properPrefixArray == 0: print("Just a legend") quit() for i in range(0, n - 1): if properPrefixArrayL[i] == properPrefixArrayL[n - 1]: print(string[0 : properPrefixArrayL[i]]) quit() if properPrefixArrayL[properPrefixArrayL[n - 1] - 1] == 0: print("Just a legend") else: print(string[0 : properPrefixArrayL[properPrefixArrayL[n - 1] - 1]])
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s, k, l = input(), -1, [-1] for c in s: while k != -1 and s[k] != c: k = l[k] k += 1 l += [k] q = l[-1] if l.count(q) < 2: q = l[q] if q > 0: print(s[:q]) else: print("Just a legend")
ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER LIST NUMBER FOR VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR LIST VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
def prefix(s): p = [0] for i in s[1:]: j = p[-1] while j > 0 and s[j] != i: j = p[j - 1] p.append(j + (s[j] == i)) return p def kmp(s, p): s = p + "$" + s pre = prefix(s) o = [] for i in range(len(p), len(pre)): if pre[i] == len(p): o.append(i - 2 * len(p)) return o s = input() p = prefix(s) print( s[: p[-1]] if p[-1] and p.count(p[-1]) > 1 and len(s) > 2 else s[: p[p[-1] - 1]] if p[p[-1] - 1] else "Just a legend" )
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER STRING
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them. A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s. Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end. Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend
s = input() t = [0] i, length = 1, 0 while i < len(s): if s[i] == s[length]: length += 1 t.append(length) i += 1 elif length == 0: t.append(length) i += 1 else: length = t[length - 1] if t[-1] != 0: if len([a for a in t if a == t[-1]]) >= 2: print(s[: t[-1]]) else: q = t[t[-1] - 1] if q != 0: print(s[:q]) else: print("Just a legend") else: print("Just a legend")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are given a rooted tree of $2^n - 1$ vertices. Every vertex of this tree has either $0$ children, or $2$ children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree. The vertices of the tree are numbered in the following order: the root has index $1$; if a vertex has index $x$, then its left child has index $2x$, and its right child has index $2x+1$. Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex $x$ as $s_x$. Let the preorder string of some vertex $x$ be defined in the following way: if the vertex $x$ is a leaf, then the preorder string of $x$ be consisting of only one character $s_x$; otherwise, the preorder string of $x$ is $s_x + f(l_x) + f(r_x)$, where $+$ operator defines concatenation of strings, $f(l_x)$ is the preorder string of the left child of $x$, and $f(r_x)$ is the preorder string of the right child of $x$. The preorder string of the tree is the preorder string of its root. Now, for the problem itself... You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree: choose any non-leaf vertex $x$, and swap its children (so, the left child becomes the right one, and vice versa). -----Input----- The first line contains one integer $n$ ($2 \le n \le 18$). The second line contains a sequence of $2^n-1$ characters $s_1, s_2, \dots, s_{2^n-1}$. Each character is either A or B. The characters are not separated by spaces or anything else. -----Output----- Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo $998244353$. -----Examples----- Input 4 BAAAAAAAABBABAB Output 16 Input 2 BAA Output 1 Input 2 ABA Output 2 Input 2 AAB Output 2 Input 2 AAA Output 1 -----Note----- None
import sys N = int(sys.stdin.readline().strip()) s = sys.stdin.readline().strip() m = 1 << N mod = 998244353 def dfs(i): if i >= m: return 1, "" ln, ls = dfs(i * 2) rn, rs = dfs(i * 2 + 1) if ls < rs: return ln * rn * 2 % mod, ls + s[i - 1] + rs elif ls > rs: return ln * rn * 2 % mod, rs + s[i - 1] + ls else: return ln * rn % mod, ls + s[i - 1] + rs n, _ = dfs(1) print(n)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a rooted tree of $2^n - 1$ vertices. Every vertex of this tree has either $0$ children, or $2$ children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree. The vertices of the tree are numbered in the following order: the root has index $1$; if a vertex has index $x$, then its left child has index $2x$, and its right child has index $2x+1$. Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex $x$ as $s_x$. Let the preorder string of some vertex $x$ be defined in the following way: if the vertex $x$ is a leaf, then the preorder string of $x$ be consisting of only one character $s_x$; otherwise, the preorder string of $x$ is $s_x + f(l_x) + f(r_x)$, where $+$ operator defines concatenation of strings, $f(l_x)$ is the preorder string of the left child of $x$, and $f(r_x)$ is the preorder string of the right child of $x$. The preorder string of the tree is the preorder string of its root. Now, for the problem itself... You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree: choose any non-leaf vertex $x$, and swap its children (so, the left child becomes the right one, and vice versa). -----Input----- The first line contains one integer $n$ ($2 \le n \le 18$). The second line contains a sequence of $2^n-1$ characters $s_1, s_2, \dots, s_{2^n-1}$. Each character is either A or B. The characters are not separated by spaces or anything else. -----Output----- Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo $998244353$. -----Examples----- Input 4 BAAAAAAAABBABAB Output 16 Input 2 BAA Output 1 Input 2 ABA Output 2 Input 2 AAB Output 2 Input 2 AAA Output 1 -----Note----- None
N = int(input()) N = 2**N S = input() U = [0] * N cnt = 0 for i in range(N - 2, -1, -1): a = 2 * i + 1 b = 2 * i + 2 if b >= N: U[i] = ord(S[i]) continue if U[a] != U[b]: cnt += 1 U[i] = ( ord(S[i]) + 331 * min(U[a], U[b]) + 3331 * max(U[a], U[b]) + min(U[a], U[b]) ** 2 ) U[i] %= 2**104 print(pow(2, cnt, 998244353))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER
You are given a rooted tree of $2^n - 1$ vertices. Every vertex of this tree has either $0$ children, or $2$ children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree. The vertices of the tree are numbered in the following order: the root has index $1$; if a vertex has index $x$, then its left child has index $2x$, and its right child has index $2x+1$. Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex $x$ as $s_x$. Let the preorder string of some vertex $x$ be defined in the following way: if the vertex $x$ is a leaf, then the preorder string of $x$ be consisting of only one character $s_x$; otherwise, the preorder string of $x$ is $s_x + f(l_x) + f(r_x)$, where $+$ operator defines concatenation of strings, $f(l_x)$ is the preorder string of the left child of $x$, and $f(r_x)$ is the preorder string of the right child of $x$. The preorder string of the tree is the preorder string of its root. Now, for the problem itself... You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree: choose any non-leaf vertex $x$, and swap its children (so, the left child becomes the right one, and vice versa). -----Input----- The first line contains one integer $n$ ($2 \le n \le 18$). The second line contains a sequence of $2^n-1$ characters $s_1, s_2, \dots, s_{2^n-1}$. Each character is either A or B. The characters are not separated by spaces or anything else. -----Output----- Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo $998244353$. -----Examples----- Input 4 BAAAAAAAABBABAB Output 16 Input 2 BAA Output 1 Input 2 ABA Output 2 Input 2 AAB Output 2 Input 2 AAA Output 1 -----Note----- None
from sys import setrecursionlimit, stdin input = stdin.readline setrecursionlimit(10**6) inp = lambda: list(map(int, input().split())) mod = 998244353 def add(a, b): return (a % mod + b % mod) % mod def mul(a, b): return a % mod * (b % mod) % mod def solve(i): if i >= (1 << n) - 1: return ["", 1] left = solve(2 * i + 1) right = solve(2 * i + 2) if left[0] == right[0]: ans = mul(left[1], right[1]) else: ans = mul(2 * left[1], right[1]) st = a[i] + min(left[0], right[0]) + max(left[0], right[0]) return [st, ans] def answer(): ans = solve(0) return ans[1] for T in range(1): n = int(input()) a = input().strip() print(answer())
ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN LIST STRING NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN LIST VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a rooted tree of $2^n - 1$ vertices. Every vertex of this tree has either $0$ children, or $2$ children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree. The vertices of the tree are numbered in the following order: the root has index $1$; if a vertex has index $x$, then its left child has index $2x$, and its right child has index $2x+1$. Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex $x$ as $s_x$. Let the preorder string of some vertex $x$ be defined in the following way: if the vertex $x$ is a leaf, then the preorder string of $x$ be consisting of only one character $s_x$; otherwise, the preorder string of $x$ is $s_x + f(l_x) + f(r_x)$, where $+$ operator defines concatenation of strings, $f(l_x)$ is the preorder string of the left child of $x$, and $f(r_x)$ is the preorder string of the right child of $x$. The preorder string of the tree is the preorder string of its root. Now, for the problem itself... You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree: choose any non-leaf vertex $x$, and swap its children (so, the left child becomes the right one, and vice versa). -----Input----- The first line contains one integer $n$ ($2 \le n \le 18$). The second line contains a sequence of $2^n-1$ characters $s_1, s_2, \dots, s_{2^n-1}$. Each character is either A or B. The characters are not separated by spaces or anything else. -----Output----- Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo $998244353$. -----Examples----- Input 4 BAAAAAAAABBABAB Output 16 Input 2 BAA Output 1 Input 2 ABA Output 2 Input 2 AAB Output 2 Input 2 AAA Output 1 -----Note----- None
MOD = 998244353 n, s = int(input()), input() def calc(u: int) -> tuple: if u >= 1 << n: return 0, 0 t1, t2 = calc(u * 2), calc(u * 2 + 1) return t1[0] + t2[0] + (t1[1] != t2[1]), hash( (min(t1[1], t2[1]), max(t1[1], t2[1]), s[u - 1]) ) print(pow(2, calc(1)[0], MOD))
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR IF VAR BIN_OP NUMBER VAR RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER VAR
You are given a rooted tree of $2^n - 1$ vertices. Every vertex of this tree has either $0$ children, or $2$ children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree. The vertices of the tree are numbered in the following order: the root has index $1$; if a vertex has index $x$, then its left child has index $2x$, and its right child has index $2x+1$. Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex $x$ as $s_x$. Let the preorder string of some vertex $x$ be defined in the following way: if the vertex $x$ is a leaf, then the preorder string of $x$ be consisting of only one character $s_x$; otherwise, the preorder string of $x$ is $s_x + f(l_x) + f(r_x)$, where $+$ operator defines concatenation of strings, $f(l_x)$ is the preorder string of the left child of $x$, and $f(r_x)$ is the preorder string of the right child of $x$. The preorder string of the tree is the preorder string of its root. Now, for the problem itself... You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree: choose any non-leaf vertex $x$, and swap its children (so, the left child becomes the right one, and vice versa). -----Input----- The first line contains one integer $n$ ($2 \le n \le 18$). The second line contains a sequence of $2^n-1$ characters $s_1, s_2, \dots, s_{2^n-1}$. Each character is either A or B. The characters are not separated by spaces or anything else. -----Output----- Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo $998244353$. -----Examples----- Input 4 BAAAAAAAABBABAB Output 16 Input 2 BAA Output 1 Input 2 ABA Output 2 Input 2 AAB Output 2 Input 2 AAA Output 1 -----Note----- None
class Node: def __init__(self, v, left, right): self.v = v self.left = left self.right = right def f(self): if self.left is None: return 1, self.v lc, lp = self.left.f() rc, rp = self.right.f() return (1 + int(lp != rp)) * lc * rc % 998244353, self.v + "".join( sorted([lp, rp]) ) def construct(s, i): if i >= len(s): return None return Node(s[i], construct(s, i * 2 + 1), construct(s, i * 2 + 2)) n = int(input()) s = input().strip() print(construct(s, 0).f()[0])
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL STRING FUNC_CALL VAR LIST VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NONE RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER NUMBER
You are given a rooted tree of $2^n - 1$ vertices. Every vertex of this tree has either $0$ children, or $2$ children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree. The vertices of the tree are numbered in the following order: the root has index $1$; if a vertex has index $x$, then its left child has index $2x$, and its right child has index $2x+1$. Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex $x$ as $s_x$. Let the preorder string of some vertex $x$ be defined in the following way: if the vertex $x$ is a leaf, then the preorder string of $x$ be consisting of only one character $s_x$; otherwise, the preorder string of $x$ is $s_x + f(l_x) + f(r_x)$, where $+$ operator defines concatenation of strings, $f(l_x)$ is the preorder string of the left child of $x$, and $f(r_x)$ is the preorder string of the right child of $x$. The preorder string of the tree is the preorder string of its root. Now, for the problem itself... You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree: choose any non-leaf vertex $x$, and swap its children (so, the left child becomes the right one, and vice versa). -----Input----- The first line contains one integer $n$ ($2 \le n \le 18$). The second line contains a sequence of $2^n-1$ characters $s_1, s_2, \dots, s_{2^n-1}$. Each character is either A or B. The characters are not separated by spaces or anything else. -----Output----- Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo $998244353$. -----Examples----- Input 4 BAAAAAAAABBABAB Output 16 Input 2 BAA Output 1 Input 2 ABA Output 2 Input 2 AAB Output 2 Input 2 AAA Output 1 -----Note----- None
import sys MOD = 998244353 n = int(next(sys.stdin)) s = next(sys.stdin) m = len(s) - 1 h = [(0) for i in range(m)] f = [[0, 0] for i in range(m)] for i in range(m - 1, -1, -1): l = (i + 1) * 2 - 1 r = (i + 1) * 2 c = int(s[i] == "B") if l < m: h[i] = h[l] * 2 + 1 p1 = min(f[l][0], f[r][0]) p2 = max(f[l][0], f[r][0]) f[i][0] = (p1 << h[l]) + p2 + (c << h[i] - 1) f[i][1] = f[l][1] * f[r][1] % MOD if p1 != p2: f[i][1] = f[i][1] * 2 % MOD else: f[i][0] = c f[i][1] = 1 h[i] = 1 print(f[0][1])
IMPORT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR STRING IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
You are given a rooted tree of $2^n - 1$ vertices. Every vertex of this tree has either $0$ children, or $2$ children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree. The vertices of the tree are numbered in the following order: the root has index $1$; if a vertex has index $x$, then its left child has index $2x$, and its right child has index $2x+1$. Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex $x$ as $s_x$. Let the preorder string of some vertex $x$ be defined in the following way: if the vertex $x$ is a leaf, then the preorder string of $x$ be consisting of only one character $s_x$; otherwise, the preorder string of $x$ is $s_x + f(l_x) + f(r_x)$, where $+$ operator defines concatenation of strings, $f(l_x)$ is the preorder string of the left child of $x$, and $f(r_x)$ is the preorder string of the right child of $x$. The preorder string of the tree is the preorder string of its root. Now, for the problem itself... You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree: choose any non-leaf vertex $x$, and swap its children (so, the left child becomes the right one, and vice versa). -----Input----- The first line contains one integer $n$ ($2 \le n \le 18$). The second line contains a sequence of $2^n-1$ characters $s_1, s_2, \dots, s_{2^n-1}$. Each character is either A or B. The characters are not separated by spaces or anything else. -----Output----- Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo $998244353$. -----Examples----- Input 4 BAAAAAAAABBABAB Output 16 Input 2 BAA Output 1 Input 2 ABA Output 2 Input 2 AAB Output 2 Input 2 AAA Output 1 -----Note----- None
if True: def compute_hashes(v): if v < nodes_count: l, r = sorted([compute_hashes(2 * v), compute_hashes(2 * v + 1)]) b[v] = l + b[v] + r return b[v] return b[v] n = int(input()) nodes_count = 2 ** (n - 1) s = input() b = ["dum"] for c in s: b.append(c) compute_hashes(1) res = sum([(1) for i in range(1, nodes_count) if b[2 * i] != b[2 * i + 1]]) print(2**res % 998244353)
IF NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER
You are given a rooted tree of $2^n - 1$ vertices. Every vertex of this tree has either $0$ children, or $2$ children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree. The vertices of the tree are numbered in the following order: the root has index $1$; if a vertex has index $x$, then its left child has index $2x$, and its right child has index $2x+1$. Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex $x$ as $s_x$. Let the preorder string of some vertex $x$ be defined in the following way: if the vertex $x$ is a leaf, then the preorder string of $x$ be consisting of only one character $s_x$; otherwise, the preorder string of $x$ is $s_x + f(l_x) + f(r_x)$, where $+$ operator defines concatenation of strings, $f(l_x)$ is the preorder string of the left child of $x$, and $f(r_x)$ is the preorder string of the right child of $x$. The preorder string of the tree is the preorder string of its root. Now, for the problem itself... You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree: choose any non-leaf vertex $x$, and swap its children (so, the left child becomes the right one, and vice versa). -----Input----- The first line contains one integer $n$ ($2 \le n \le 18$). The second line contains a sequence of $2^n-1$ characters $s_1, s_2, \dots, s_{2^n-1}$. Each character is either A or B. The characters are not separated by spaces or anything else. -----Output----- Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo $998244353$. -----Examples----- Input 4 BAAAAAAAABBABAB Output 16 Input 2 BAA Output 1 Input 2 ABA Output 2 Input 2 AAB Output 2 Input 2 AAA Output 1 -----Note----- None
n = int(input()) s = input() a = [-1] for i in range(0, 2**n - 1): if s[i] == "A": a.append("A") else: a.append("B") res = 1 for i in range(2 ** (n - 1) - 1, 0, -1): if a[2 * i] == a[2 * i + 1]: a[i] = a[i] + a[2 * i] + a[2 * i + 1] elif a[2 * i] < a[2 * i + 1]: res = res * 2 % 998244353 a[i] = a[i] + a[2 * i] + a[2 * i + 1] else: res = res * 2 % 998244353 a[i] = a[i] + a[2 * i + 1] + a[2 * i] print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
for _ in range(int(input())): length = int(input()) array = list(map(int, input().split())) array.sort() cnt = [0] * (max(array) + 1) dp = [0] * (max(array) + 1) for x in array: cnt[x] += 1 for i in range(1, len(dp)): dp[i] += cnt[i] for j in range(i, len(dp), i): dp[j] = max(dp[j], dp[i]) print(length - max(dp))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
from sys import stdin input = stdin.buffer.readline t = int(input()) for i in range(t): n = int(input()) arr = [int(x) for x in input().split()] d = {} for i in arr: if i in d: d[i] = d[i] + 1 else: d[i] = 1 l = list(d.keys()) l.sort() m = max(l) for i in range(len(l)): d[l[i]] = i, d[l[i]] dp = [0] * len(l) for i in range(len(l)): dp[i] = dp[i] + d[l[i]][1] x = 2 * l[i] while x <= m: if x in d: dp[d[x][0]] = max(dp[i], dp[d[x][0]]) x = x + l[i] print(n - max(dp))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") inp = lambda: list(map(int, sys.stdin.readline().rstrip("\r\n").split())) mod = 10**9 + 7 Mod = 998244353 INF = float("inf") tc = 1 M = 2 * 10**5 + 1 cnt = [0] * (M + 1) dp = [0] * (M + 1) (tc,) = inp() for _ in range(tc): (n,) = inp() a = sorted(inp()) M = max(a) for i in range(M + 1): cnt[i] = 0 dp[i] = 0 for i in a: cnt[i] += 1 for i in range(M, 0, -1): for j in range(i, M + 1, i): dp[i] = max(dp[i], cnt[i] + dp[j]) print(n - dp[1])
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) count = [0] * 200001 for v in arr: count[v] += 1 dp = [0] * 200001 for i in range(1, 200001): if count[i]: dp[i] += count[i] for j in range(2 * i, 200001, i): dp[j] = max(dp[i], dp[j]) print(n - max(dp))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
def solve(): for _ in range(int(input())): n = int(input()) cnt = [0] * 200001 dp = [0] * 200001 best = 0 for a in map(int, input().split()): cnt[a] += 1 for a, c in enumerate(cnt): if c: c += dp[a] if best < c: best = c for i in range(2 * a, 200001, a): if dp[i] < c: dp[i] = c print(n - best) solve()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
import sys def solve(): input = sys.stdin.readline MAXN = int(200000.0) + 1 for t in range(int(input())): dp = [0] * MAXN n = int(input()) a = list(map(int, input().split())) a.sort() ans = 0 i = 0 while i < n: v = a[i] ii = i + 1 while ii < n: if a[ii] != v: break ii += 1 best = dp[v] + ii - i for j in range(2 * v, MAXN, v): dp[j] = max(dp[j], best) ans = max(ans, best) i = ii print(n - ans) solve()
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
import sys t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) m = max(arr) + 1 dp = [0] * m cnt = [0] * m for i in range(len(arr)): cnt[arr[i]] += 1 for i in range(1, m): dp[i] += cnt[i] for j in range(2 * i, m, i): dp[j] = max(dp[i], dp[j]) print(n - max(dp))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
import sys factorsMemo = [(-1) for _ in range(200001)] def getAllFactors(x): if factorsMemo[x] == -1: factors = [] for i in range(1, int(x**0.5) + 1): if x % i == 0: factors.append(i) if x // i != i: factors.append(x // i) factorsMemo[x] = factors return factorsMemo[x] def main(): t = int(input()) allAns = [] for _ in range(t): n = int(input()) a = readIntArr() a.sort() dp = [(-inf) for _ in range(200001)] maxSizeOfBeautifulArray = 0 for x in a: maxPrevCnts = 0 for f in getAllFactors(x): maxPrevCnts = max(maxPrevCnts, dp[f]) dp[x] = 1 + maxPrevCnts maxSizeOfBeautifulArray = max(maxSizeOfBeautifulArray, dp[x]) allAns.append(n - maxSizeOfBeautifulArray) multiLineArrayPrint(allAns) return input = sys.stdin.buffer.readline def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] inf = float("inf") MOD = 10**9 + 7 main()
IMPORT ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
import sys input = sys.stdin.readline for _ in range(int(input())): N = int(input()) a = list(map(int, input().split())) mx = max(a) table = [0] * (mx + 1) for x in a: table[x] += 1 dp = [0] * (mx + 1) for x in range(mx, 0, -1): if table[x] == 0: continue dp[x] = table[x] for y in range(x + x, mx + 1, x): dp[x] = max(dp[x], dp[y] + table[x]) print(N - max(dp))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) mx = max(a) b, c = [0] * (mx + 1), [0] * (mx + 1) for i in a: b[i] += 1 for k in range(1, mx + 1): c[k] += b[k] for j in range(2 * k, mx + 1, k): c[j] = max(c[j], c[k]) print(n - max(c))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
from sys import stdin, stdout def strange_beauty(n, a_a): mx = max(a_a) dp = [0] * (mx + 1) cnt = [0] * (mx + 1) r = 0 for a in a_a: cnt[a] += 1 for i in range(1, mx + 1): if cnt[i] == 0: continue dp[i] += cnt[i] j = i * 2 while j <= mx: dp[j] = max(dp[j], dp[i]) j += i for a in a_a: r = max(r, dp[a]) return n - r t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) a_a = list(map(int, stdin.readline().split())) r = strange_beauty(n, a_a) stdout.write(str(r) + "\n")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Polycarp found on the street an array $a$ of $n$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $a$ beautiful if at least one of the following conditions must be met for each different pair of indices $i \ne j$: $a_i$ is divisible by $a_j$; or $a_j$ is divisible by $a_i$. For example, if: $n=5$ and $a=[7, 9, 3, 14, 63]$, then the $a$ array is not beautiful (for $i=4$ and $j=2$, none of the conditions above is met); $n=3$ and $a=[2, 14, 42]$, then the $a$ array is beautiful; $n=4$ and $a=[45, 9, 3, 18]$, then the $a$ array is not beautiful (for $i=1$ and $j=4$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $a$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $a$ beautiful. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 10$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of the array $a$. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) — elements of the array $a$. -----Output----- For each test case output one integer — the minimum number of elements that must be removed to make the array $a$ beautiful. -----Examples----- Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Output 2 0 1 0 -----Note----- In the first test case, removing $7$ and $14$ will make array $a$ beautiful. In the second test case, the array $a$ is already beautiful. In the third test case, removing one of the elements $45$ or $18$ will make the array $a$ beautiful. In the fourth test case, the array $a$ is beautiful.
T = int(input()) for _ in range(T): n = int(input()) A = list(map(int, input().split())) mv = max(A) DP = [(0) for i in range(mv + 1)] G = {} for i in A: try: G[i] = G[i] + 1 except: G[i] = 1 for i in range(mv, 0, -1): if i in G: j = i while j <= mv: DP[i] = max(DP[i], DP[j] + G[i]) j = j + i print(n - max(DP))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between versions is the constraints on $n$ and $a_i$. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has $x$ candies. There are also $n$ enemies numbered with integers from $1$ to $n$. Enemy $i$ has $a_i$ candies. Yuzu is going to determine a permutation $P$. A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $\{2,3,1,5,4\}$ is a permutation, but $\{1,2,2\}$ is not a permutation ($2$ appears twice in the array) and $\{1,3,4\}$ is also not a permutation (because $n=3$ but there is the number $4$ in the array). After that, she will do $n$ duels with the enemies with the following rules: If Yuzu has equal or more number of candies than enemy $P_i$, she wins the duel and gets $1$ candy. Otherwise, she loses the duel and gets nothing. The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations $P$ exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define $f(x)$ as the number of valid permutations for the integer $x$. You are given $n$, $a$ and a prime number $p \le n$. Let's call a positive integer $x$ good, if the value $f(x)$ is not divisible by $p$. Find all good integers $x$. Your task is to solve this problem made by Akari. -----Input----- The first line contains two integers $n$, $p$ $(2 \le p \le n \le 10^5)$. It is guaranteed, that the number $p$ is prime (it has exactly two divisors $1$ and $p$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(1 \le a_i \le 10^9)$. -----Output----- In the first line, print the number of good integers $x$. In the second line, output all good integers $x$ in the ascending order. It is guaranteed that the number of good integers $x$ does not exceed $10^5$. -----Examples----- Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 -----Note----- In the first test, $p=2$. If $x \le 2$, there are no valid permutations for Yuzu. So $f(x)=0$ for all $x \le 2$. The number $0$ is divisible by $2$, so all integers $x \leq 2$ are not good. If $x = 3$, $\{1,2,3\}$ is the only valid permutation for Yuzu. So $f(3)=1$, so the number $3$ is good. If $x = 4$, $\{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\}$ are all valid permutations for Yuzu. So $f(4)=4$, so the number $4$ is not good. If $x \ge 5$, all $6$ permutations are valid for Yuzu. So $f(x)=6$ for all $x \ge 5$, so all integers $x \ge 5$ are not good. So, the only good number is $3$. In the third test, for all positive integers $x$ the value $f(x)$ is divisible by $p = 3$.
import sys input = sys.stdin.readline n, p = map(int, input().split()) a = list(map(int, input().split())) a.sort() mn = 0 mx = 2000000000000000 for i in range(n): d = a[i] - i mn = max(d, mn) if i >= p - 1: d2 = a[i] - i + p - 1 mx = min(mx, d2) print(max(mx - mn, 0)) for i in range(mn, mx): print(i, end=" ")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
This is the hard version of the problem. The difference between versions is the constraints on $n$ and $a_i$. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has $x$ candies. There are also $n$ enemies numbered with integers from $1$ to $n$. Enemy $i$ has $a_i$ candies. Yuzu is going to determine a permutation $P$. A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $\{2,3,1,5,4\}$ is a permutation, but $\{1,2,2\}$ is not a permutation ($2$ appears twice in the array) and $\{1,3,4\}$ is also not a permutation (because $n=3$ but there is the number $4$ in the array). After that, she will do $n$ duels with the enemies with the following rules: If Yuzu has equal or more number of candies than enemy $P_i$, she wins the duel and gets $1$ candy. Otherwise, she loses the duel and gets nothing. The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations $P$ exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define $f(x)$ as the number of valid permutations for the integer $x$. You are given $n$, $a$ and a prime number $p \le n$. Let's call a positive integer $x$ good, if the value $f(x)$ is not divisible by $p$. Find all good integers $x$. Your task is to solve this problem made by Akari. -----Input----- The first line contains two integers $n$, $p$ $(2 \le p \le n \le 10^5)$. It is guaranteed, that the number $p$ is prime (it has exactly two divisors $1$ and $p$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(1 \le a_i \le 10^9)$. -----Output----- In the first line, print the number of good integers $x$. In the second line, output all good integers $x$ in the ascending order. It is guaranteed that the number of good integers $x$ does not exceed $10^5$. -----Examples----- Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 -----Note----- In the first test, $p=2$. If $x \le 2$, there are no valid permutations for Yuzu. So $f(x)=0$ for all $x \le 2$. The number $0$ is divisible by $2$, so all integers $x \leq 2$ are not good. If $x = 3$, $\{1,2,3\}$ is the only valid permutation for Yuzu. So $f(3)=1$, so the number $3$ is good. If $x = 4$, $\{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\}$ are all valid permutations for Yuzu. So $f(4)=4$, so the number $4$ is not good. If $x \ge 5$, all $6$ permutations are valid for Yuzu. So $f(x)=6$ for all $x \ge 5$, so all integers $x \ge 5$ are not good. So, the only good number is $3$. In the third test, for all positive integers $x$ the value $f(x)$ is divisible by $p = 3$.
n, p = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() m = max(a[i] - i for i in range(n)) M = a[p - 1] - 1 ban = [(0) for i in range(p)] ans = [] query = [(a[i] - 1 - i, 1, -100) for i in range(n)] for x in range(m, M + 1): query.append((x, 0, -100)) for i in range(n): query.append((a[i], -1, i)) query.sort() for val, q, id in query: if q == -1: r = (val - id - 1) % p ban[r] -= 1 elif q == 0: if ban[val % p] == 0: ans.append(val) else: ban[val % p] += 1 ans.sort() print(len(ans)) print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between versions is the constraints on $n$ and $a_i$. You can make hacks only if all versions of the problem are solved. First, Aoi came up with the following idea for the competitive programming problem: Yuzu is a girl who collecting candies. Originally, she has $x$ candies. There are also $n$ enemies numbered with integers from $1$ to $n$. Enemy $i$ has $a_i$ candies. Yuzu is going to determine a permutation $P$. A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $\{2,3,1,5,4\}$ is a permutation, but $\{1,2,2\}$ is not a permutation ($2$ appears twice in the array) and $\{1,3,4\}$ is also not a permutation (because $n=3$ but there is the number $4$ in the array). After that, she will do $n$ duels with the enemies with the following rules: If Yuzu has equal or more number of candies than enemy $P_i$, she wins the duel and gets $1$ candy. Otherwise, she loses the duel and gets nothing. The candy which Yuzu gets will be used in the next duels. Yuzu wants to win all duels. How many valid permutations $P$ exist? This problem was easy and wasn't interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea: Let's define $f(x)$ as the number of valid permutations for the integer $x$. You are given $n$, $a$ and a prime number $p \le n$. Let's call a positive integer $x$ good, if the value $f(x)$ is not divisible by $p$. Find all good integers $x$. Your task is to solve this problem made by Akari. -----Input----- The first line contains two integers $n$, $p$ $(2 \le p \le n \le 10^5)$. It is guaranteed, that the number $p$ is prime (it has exactly two divisors $1$ and $p$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(1 \le a_i \le 10^9)$. -----Output----- In the first line, print the number of good integers $x$. In the second line, output all good integers $x$ in the ascending order. It is guaranteed that the number of good integers $x$ does not exceed $10^5$. -----Examples----- Input 3 2 3 4 5 Output 1 3 Input 4 3 2 3 5 6 Output 2 3 4 Input 4 3 9 1 1 1 Output 0 Input 3 2 1000000000 1 999999999 Output 1 999999998 -----Note----- In the first test, $p=2$. If $x \le 2$, there are no valid permutations for Yuzu. So $f(x)=0$ for all $x \le 2$. The number $0$ is divisible by $2$, so all integers $x \leq 2$ are not good. If $x = 3$, $\{1,2,3\}$ is the only valid permutation for Yuzu. So $f(3)=1$, so the number $3$ is good. If $x = 4$, $\{1,2,3\} , \{1,3,2\} , \{2,1,3\} , \{2,3,1\}$ are all valid permutations for Yuzu. So $f(4)=4$, so the number $4$ is not good. If $x \ge 5$, all $6$ permutations are valid for Yuzu. So $f(x)=6$ for all $x \ge 5$, so all integers $x \ge 5$ are not good. So, the only good number is $3$. In the third test, for all positive integers $x$ the value $f(x)$ is divisible by $p = 3$.
n, p = map(int, input().split()) arr = list(map(int, input().split())) l, r = 0, float("inf") arr.sort() for i in range(len(arr)): l = max(l, arr[i] - i) if i + 1 >= p: r = min(r, arr[i] - i + p - 1) print(max(0, r - l)) res = [] for i in range(l, r): res.append(i) print(*res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level. Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level. -----Input----- The first line of the input consists of two integers, the number of robots n (2 ≤ n ≤ 100 000) and the number of rap battles m ($1 \leq m \leq \operatorname{min}(100000, \frac{n(n - 1)}{2})$). The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}), indicating that robot u_{i} beat robot v_{i} in the i-th rap battle. No two rap battles involve the same pair of robots. It is guaranteed that at least one ordering of the robots satisfies all m relations. -----Output----- Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1. -----Examples----- Input 4 5 2 1 1 3 2 3 4 2 4 3 Output 4 Input 3 2 1 2 3 2 Output -1 -----Note----- In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles. In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles.
n, m = [int(i) for i in input().split()] n += 1 A = [[int(i) for i in input().split()] for j in range(m)] m += 1 def check(upper): p = [[] for i in range(n)] d = [0] * n for u, v in A[:upper]: p[u].append(v) d[v] += 1 if d.count(0) > 2: return False x = d.index(0, 1) while x: q, x = p[x], 0 for y in q: d[y] -= 1 if d[y] == 0: if x: return False x = y return True left, right = 1, m while left < right: mid = (left + right) // 2 if check(mid): right = mid else: left = mid + 1 if check(left): print(left) else: print(-1)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level. Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level. -----Input----- The first line of the input consists of two integers, the number of robots n (2 ≤ n ≤ 100 000) and the number of rap battles m ($1 \leq m \leq \operatorname{min}(100000, \frac{n(n - 1)}{2})$). The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}), indicating that robot u_{i} beat robot v_{i} in the i-th rap battle. No two rap battles involve the same pair of robots. It is guaranteed that at least one ordering of the robots satisfies all m relations. -----Output----- Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1. -----Examples----- Input 4 5 2 1 1 3 2 3 4 2 4 3 Output 4 Input 3 2 1 2 3 2 Output -1 -----Note----- In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles. In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles.
from sys import stdin def main(): def f(k): g, cnt = [[] for _ in range(n)], [0] * n for u, v in data[:k]: g[u].append(v) cnt[v] += 1 if cnt.count(0) > 1: return False w, u = cnt.index(0), -1 while u != w: u = w for v in g[u]: cnt[v] -= 1 if not cnt[v]: if u != w: return False w = v return True n, m = map(int, input().split()) data = stdin.read().splitlines() for i, s in enumerate(data): u, v = map(int, s.split()) data[i] = u - 1, v - 1 lo, hi = n - 1, m + 1 while lo < hi: mid = (lo + hi) // 2 if f(mid): hi = mid else: lo = mid + 1 print(-1 if hi > m else lo) main()
FUNC_DEF FUNC_DEF ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
def ans(m, n): q1, q2, k = m, 2, 0 while q1 <= n: q1 *= 2 k += q2 q2 *= 2 q2 //= 2 q1 //= 2 if n - q1 < q2: return k + n - q1 - q2 + 1 return k n, k = list(map(int, input().split())) if k == n: print(1) elif k == 1: print(n) else: l, r = 1, n // 2 + 1 while r - l > 1: m = (l + r) // 2 if ans(2 * m, n) >= k: l = m else: r = m l1, r1 = 1, n // 2 + 1 while r1 - l1 > 1: m = (l1 + r1) // 2 if ans(2 * m, n) >= k - 1: l1 = m else: r1 = m print(max(l1, 2 * l))
FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return list(map(int, minp().split())) def full(x, n): if n + 1 & n != 0: raise Exception("qwe") r = 1 while True: if r >= x: return n if n == 1: return None r *= 2 n -= 1 if r >= x: return n n //= 2 r += 1 def is_good(x, n): l = x r = x rp = x if x % 2 == 0: return None while l <= n: if l <= n and r > n: return None l *= 2 rp = r r = r * 2 + 1 return rp def full2(x, c, rp): r = 1 while True: if r >= x: return rp if rp == c: return None r *= 2 rp -= 1 if r >= x: return rp rp //= 2 r += 1 return None def full1(k, x, n): rp = is_good(x, n) if rp == None: r = None if f(x, n) >= k: r = x if 2 * x <= n: r1 = full1(k, 2 * x, n) if r1 != None and (r == None or r < r1): r = r1 if 2 * x + 1 <= n: r1 = full1(k, 2 * x + 1, n) if r1 != None and (r == None or r < r1): r = r1 return r else: return full2(k, x, rp) def fulls(k, n): r = 1 for i in range(1, n + 1): if f(i, n) >= k: r = i return r def f(x, n): r = 0 rp = is_good(x, n) if rp != None: r = 1 while rp != x: r = r * 2 + 1 rp //= 2 return r if x <= n: r += 1 if x % 2 == 0 and x + 1 <= n: r += f(x + 1, n) if 2 * x <= n: r += f(x * 2, n) return r def f1(x, n): r = 0 if x <= n: r += 1 if x % 2 == 0 and x + 1 <= n: r += f(x + 1, n) if 2 * x <= n: r += f(x * 2, n) return r n, k = mints() print(full1(k, 1, n))
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR RETURN VAR IF VAR NUMBER RETURN NONE VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NONE WHILE VAR VAR IF VAR VAR VAR VAR RETURN NONE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN NONE VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR VAR NUMBER VAR NUMBER RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR NONE IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR IF VAR NONE VAR NONE VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF VAR NONE VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
N, K = [int(x) for x in input().split(" ")] def f(x): ret, i = 0, 0 while x << i <= N: ret += min(N, (x << i) + (1 << i) - 1) - (x << i) + 1 i += 1 return ret lo, hi = 1, N // 2 while lo < hi: mi = (lo + hi + 1) // 2 if f(2 * mi) + f(2 * mi + 1) >= K: lo = mi else: hi = mi - 1 if f(2 * lo) + f(2 * lo + 1) < K: lo = 0 lo2, hi2 = 0, (N - 1) // 2 while lo2 != hi2: mi2 = (lo2 + hi2 + 1) // 2 if f(2 * mi2 + 1) >= K: lo2 = mi2 else: hi2 = mi2 - 1 print(max(2 * lo, 2 * lo2 + 1))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
def paths(x): c = 1 if x % 2 else 2 a = 0 m = 1 while m * x + c * m - 1 <= n: a += c * m m *= 2 a += max(-1, n - m * x) + 1 return a def search_odd(k, i, j): if i == j: return 2 * i + 1 if i + 1 == j: return 2 * j + 1 if paths(2 * j + 1) >= k else 2 * i + 1 m = i + j >> 1 if paths(2 * m + 1) >= k: return search_odd(k, m, j) else: return search_odd(k, i, m - 1) def search_even(k, i, j): if i == j: return 2 * i if i + 1 == j: return 2 * j if paths(2 * j) >= k else 2 * i m = i + j >> 1 if paths(2 * m) >= k: return search_even(k, m, j) else: return search_even(k, i, m - 1) def search(k): return max(search_even(k, 0, n // 2), search_odd(k, 0, (n - 1) // 2)) n, k = map(int, input().split()) print(search(k))
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = [int(x) for x in input().split()] def chg(n): lis = [] for i in range(100): if 2**i > n: break lis.append((n & 2**i) >> i) return lis def calc(x): cnt = 0 k = 1 if x % 2 == 0: x = x // 2 cnt = -1 while k * x <= n: cnt += min(k * x + k - 1, n) - k * x + 1 k *= 2 return cnt l = 1 r = n while l < r: mid = l + r + 1 >> 1 if calc(2 * mid - 1) >= k: l = mid else: r = mid - 1 ans = 2 * l - 1 l = 1 r = n while l < r: mid = l + r + 1 >> 1 if calc(2 * mid) >= k: l = mid else: r = mid - 1 if calc(2 * l) >= k: ans = max(ans, 2 * l) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
def gg(n, lol): ans = 0 cur = 1 lol2 = lol while 2 * lol + 1 <= n: cur *= 2 ans += cur lol = 2 * lol + 1 lol2 *= 2 if lol2 * 2 <= n: ans += n - lol2 * 2 + 1 return ans n, k = map(int, input().split()) low = 1 high = n // 2 res = 1 while low <= high: mid = (low + high) // 2 if gg(n, mid) >= k: res = mid low = mid + 1 else: high = mid - 1 if n == k: print(1) elif gg(n, res) - 1 - gg(n, res * 2) >= k: print(res * 2 + 1) else: print(res * 2)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
import sys input = sys.stdin.readline n, k = [int(item) for item in input().split()] possible = set() t = n while t > 1: possible.add(t) possible.add(t - 1) possible.add(t - 2) possible.add(t - 3) t //= 2 if 0 in possible: possible.remove(0) possible.add(1) possible = sorted(list(possible), reverse=True) for i in possible: ret = 0 l = i r = l + 1 + (l % 2 == 0) while l <= n: ret += min(r, n + 1) - l l <<= 1 r <<= 1 if ret >= k: print(i) exit()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = map(int, input().split()) left = 0 right = 10**18 + 2 p = [(2**i) for i in range(61)] while right - left > 2: mid = (right + left) // 2 mid -= mid % 2 l = 0 r = 60 if mid > n: right = mid continue while r - l > 1: m = (r + l) // 2 if mid * p[m] <= n: l = m else: r = m cur = 0 cur += p[l + 1] - 2 + min(n - p[l] * mid + 1, p[l + 1]) if cur < k: right = mid else: left = mid x = left left = 1 right = 10**18 + 1 while right - left > 2: mid = (right + left) // 2 if mid % 2 == 0: mid += 1 l = 0 r = 60 if mid > n: right = mid continue while r - l > 1: m = (r + l) // 2 if mid * p[m] <= n: l = m else: r = m cur = 0 cur += min(n - p[l] * mid + 1, p[l]) + p[l] - 1 if cur < k: right = mid else: left = mid print(max(left, x))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
import sys def cnt(x): c = 0 for i in range(65): if int(x) << i > n: break if x & 1: c += min(int(1) << i, n - (x << i) + 1) else: c += min(int(1) << i + 1, n - (x << i) + 1) return c n, k = [int(x) for x in input().split()] if k == 1: print(n) sys.exit(0) l = 1 r = n + 1 while r - l > 1: m = (r + l) // 2 if cnt(m * 2 - 1) >= k: l = m else: r = m ans1 = 2 * l - 1 l = 0 r = n + 1 while r - l > 1: m = (l + r) // 2 if cnt(m * 2) >= k: l = m else: r = m ans2 = 2 * l ans = max(ans1, ans2) print(ans) sys.exit(0)
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = map(int, input().split()) def c(m): a = b = m ans = 0 if m % 2 == 0: b += 1 while b <= n: ans += b - a + 1 a *= 2 b *= 2 b += 1 return ans + max(0, n - a + 1) if n < 100: ans = 1 for i in range(1, n + 1): if c(i) >= k: ans = i print(ans) return for i in range(n, n - 100, -1): if c(i) >= k: print(i) return ng = 0 ok = (n + 1) // 2 * 2 while ng + 2 != ok: mid = (ok + ng) // 4 * 2 if c(mid) < k: ok = mid else: ng = mid x = ng ng = -1 ok = (n + 1) // 2 * 2 - 1 while ng + 2 != ok: mid = (ok + ng) // 4 * 2 + 1 if c(mid) < k: ok = mid else: ng = mid print(max(ng, x))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
import sys def cnt(y, n): if y <= 1: return n if y > n: return 0 if y % 2 == 1: return 1 + cnt(2 * y, n) c = 0 p = 1 while p * y <= n: mx = min(n, p * y + 2 * p - 1) c += mx - p * y + 1 p *= 2 return c n, k = input().split() n, k = int(n), int(k) if k == 1: print(n) sys.exit(0) l, h = 1, n // 2 while l < h: m = (l + h) // 2 if cnt(2 * m, n) < k: h = m else: l = m + 1 mx_even = 2 * l - 2 l, h = 1, n // 2 while l < h: m = (l + h) // 2 if cnt(2 * m + 1, n) < k: h = m else: l = m + 1 mx_odd = 2 * l - 1 mx_heur = -1 i = 0 while i < 20 and n - i > 0: if cnt(n - i, n) >= k: mx_heur = n - i break i += 1 print(max(mx_even, max(mx_odd, mx_heur)))
IMPORT FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
import sys input = sys.stdin.readline N, K = map(int, input().split()) def search(odd_num): c = 0 upper = odd_num down = odd_num while upper <= N: c += upper - down + 1 upper = 2 * upper + 1 down = 2 * down if down <= N: c += N - down + 1 return c def binary_search(maximum, odd): l = 0 r = maximum + 2 while r - l > 1: m = (r + l) // 2 num = 2 * m + 1 if odd else 2 * m if odd: count = search(num) else: count = search(num // 2) - 1 if count >= K: l = m else: r = m return 2 * l + 1 if odd else 2 * l a1 = binary_search((N + 1) // 2, odd=True) a2 = binary_search((N + 1) // 2, odd=False) print(max(a1, a2))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = list(map(int, input().split())) s = bin(n)[2:] ans = 1 if k == 1: ans = n else: f = len(s) for d in range(1, f): rgt = int(s[-d:], 2) lft = int(s[:-d], 2) c = 2**d if rgt + c >= k: if rgt + c > k: ans = max(lft * 2, ans) else: ans = max(lft, ans) if 2 * c - 1 >= k: if 2 * c - 1 > k: ans = max((lft - 1) * 2, ans) else: ans = max(lft - 1, ans) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = [int(x) for x in input().split()] def DTB(num, x): if num > 1: DTB(num // 2, x) x.append(num % 2) nn = [] kk = [] DTB(n, nn) DTB(k, kk) if k == 1: print(n) elif k == 2: if n == 2: print(1) elif n % 2 == 0: print(n - 2) else: print(n - 1) elif k == 3: if n % 4 == 2: print(n // 2 - 1) else: print(n // 2) else: a = len(kk) - 2 m = len(nn) p = 2 ** (a + 1) - 1 + n % 2**a if nn[m - a - 1] == 1: p += 2**a if p >= k: print(int("".join([str(x) for x in nn[0 : m - a - 1]]) + "0", 2)) else: N = nn[0 : m - a - 1] if sum(N) > 1: if k == 2 ** (a + 2) - 1: if sum(nn[m - a - 1 : m]) == a + 1: print(int("".join([str(x) for x in nn[0 : m - a - 1]]), 2)) else: print(int("".join([str(x) for x in nn[0 : m - a - 2]]) + "0", 2)) else: N.reverse() ind = N.index(1) N[ind] = 0 for i in range(ind): N[i] = 1 N.reverse() N.append(0) print(int("".join([str(x) for x in N]), 2)) elif m - a - 1 == 1: print(1) elif k != 2 ** (a + 2) - 1: print(2 ** (m - a - 1) - 2) else: print(2 ** (m - a - 2))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER STRING NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER STRING NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = map(int, input().split()) def check(m): if m % 2 == 0: ret = check(m + 1) else: ret = 0 i = 0 while True: if (m + 1 << i) - 1 <= n: ret += 1 << i elif m << i > n: break else: ret += n - (m << i) + 1 break i += 1 return ret left = 0 right = n // 2 + 1 while right - left > 1: mid = (right + left) // 2 if check(2 * mid) >= k: left = mid else: right = mid ans = 2 * left left = 0 right = (n + 1) // 2 + 1 while right - left > 1: mid = (right + left) // 2 if check(2 * mid - 1) >= k: left = mid else: right = mid ans = max(ans, 2 * left - 1) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = map(int, input().split()) ans = 0 if n == k: ans = 1 elif k == 1: ans = n else: p = 2 s = 2 while s < k: p *= 2 s += p n1 = n n -= k ans = (n + p - 1) // p ans *= 2 flag = False for i in range(1, 64): if 2**i - 1 == k: flag = True flag1 = False if flag and n1 % p == p - 1: ans += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
a, b = input().split() a = int(a) b = int(b) if b == 1: print(a) elif b == 2: if a % 2 == 0: print(a // 2) else: print(a - 1) else: chopped_even = bin(b + 1)[3:] len_even = len(chopped_even) best_even = (a - int(chopped_even, 2)) // 2**len_even * 2 chopped_odd = bin(b)[2:] len_odd = len(chopped_odd) best_odd = (a - b) // 2**len_odd * 2 + 1 if best_even > best_odd: print(best_even) else: print(best_odd)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
count = 0 def f(t, n): count = 0 if t % 2 == 1: while t <= n: t *= 2 count += 1 if n - t // 2 + 1 >= pow(2, count - 1): return pow(2, count - 1) - 1 + pow(2, count - 1) else: return pow(2, count - 1) - 1 + (n - t // 2 + 1) else: while t <= n: t *= 2 count += 1 if n - t // 2 + 1 >= pow(2, count): return pow(2, count) - 2 + pow(2, count) else: return pow(2, count) - 2 + (n - t // 2 + 1) def search(low, high, k, n): if low > high: return 1 mid = (low + high) // 2 if mid % 2 == 0 and low % 2 == 1: mid -= 1 elif mid % 2 == 1 and low % 2 == 0: mid -= 1 x = f(mid, n) y = f(mid + 2, n) if x >= k and y < k: return mid elif x >= k: return search(mid + 2, high, k, n) elif x < k: return search(low, mid - 2, k, n) l = input().split() n = int(l[0]) k = int(l[1]) if n % 2 == 0: print(max(search(1, n - 1, k, n), search(2, n, k, n))) else: print(max(search(1, n, k, n), search(2, n - 1, k, n)))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
def c(x): global n a = b = x if x % 2 == 0: b += 1 res = b - a + 1 while 2 * b < n: a = 2 * a b = 2 * b + 1 res += b - a + 1 if 2 * a <= n: res += n - 2 * a + 1 return res n, k = map(int, input().split()) cnt = 0 l = 0 r = n // 2 + 1 while r - l > 1: m = (r + l) // 2 cnt = c(2 * m) if cnt >= k: l = m else: r = m maxl = 2 * l l = 0 r = (n + 1) // 2 while r - l > 1: m = (r + l) // 2 cnt = c(2 * m + 1) if cnt >= k: l = m else: r = m maxl = max(maxl, 2 * l + 1) print(maxl)
FUNC_DEF ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
def _calc(prefix, n): if prefix > n: return 0 prefix = bin(prefix)[2:] n = bin(n)[2:] plen = len(prefix) nlen = len(n) res = (1 << nlen - plen) - 1 nprefix = n[:plen] if nprefix == prefix: for i in range(plen, nlen): if n[i] == "1": res += 1 << nlen - i - 1 res += 1 elif nprefix > prefix: res += 1 << nlen - plen return res def calc(prefix, n): if prefix % 2 == 0: return _calc(prefix, n) + _calc(prefix + 1, n) return _calc(prefix, n) n, k = map(int, input().split()) if n == k: print(1) exit(0) ans = 1 l, r = 1, n >> 1 while l < r: mid = l + r + 1 >> 1 tot = calc(mid * 2, n) if tot >= k: l = mid else: r = mid - 1 l *= 2 if calc(l, n) >= k: ans = max(ans, l) l, r = 0, n - 1 >> 1 while l < r: mid = l + r + 1 >> 1 tot = calc(mid * 2 + 1, n) if tot >= k: l = mid else: r = mid - 1 l = l * 2 + 1 if calc(l, n) >= k: ans = max(ans, l) print(ans)
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = map(int, input().split()) def calc(z): global n if z > n: return False if z == 0: return True if z % 2 == 1: ans = 1 now = 2 * z co = 1 if k == 1: return True elif now > n: return False else: ans = 0 now = z co = 1 while True: if n <= now + 2 * co - 1: ans += max(0, n - now + 1) break else: now *= 2 co *= 2 ans += co return ans >= k realans = [] l = 1 r = n // 2 + 2 while l + 1 < r: y = l + (r - l) // 2 if calc(2 * y - 1): l = y else: r = y realans.append(2 * l - 1) l = 0 r = n // 2 + 2 while l + 1 < r: y = l + (r - l) // 2 if calc(2 * y): l = y else: r = y realans.append(2 * l) print(max(realans))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = map(int, input().split()) p = 1 w = 0 while p * 2 <= k: p *= 2 w += 1 kk = k - p odd = (n - kk) // 2**w if odd % 2 == 0: odd -= 1 p = 1 w = 0 while 2 * (2 * p - 1) < k: p *= 2 w += 1 poz = k - 2 * (p - 1) if poz == 0: even = (n - (2**w - 1)) // 2 ** (w - 1) if even % 2 == 1: even -= 1 else: if poz <= p * 4343: strzal = (n + 1 - poz) // p if strzal % 2 == 1: strzal -= 1 even = strzal print(max(even, odd))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
def check(md, n, k): s = 0 temp, i = md, 1 while temp <= n: s += min(n - temp + 1, i) temp *= 2 i *= 2 if md % 2 == 0: temp, i = md + 1, 1 while temp <= n: s += min(n - temp + 1, i) temp *= 2 i *= 2 if s >= k: return True return False n, k = map(int, input().split()) l, r = 0, n // 2 + 1 while r - l > 1: md = (l + r) // 2 if check(2 * md, n, k) == True: l = md else: r = md even = 2 * l l, r = 0, n // 2 + 1 while r - l > 1: md = (l + r) // 2 if check(2 * md + 1, n, k) == True: l = md else: r = md odd = 2 * l + 1 print(max(even, odd))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = map(int, input().split()) if n == k: print(1) exit(0) def calc(x): ans = 1 if x % 2 == 1: t = x while True: x *= 2 t = 2 * t + 1 if x > n: break ans += min(t, n) - x + 1 elif x + 1 <= n: t = x + 1 ans += 1 while True: x *= 2 t = 2 * t + 1 if x > n: break ans += min(t, n) - x + 1 return ans l = 1 ans = 0 if n % 2 == 0: r = n - 1 else: r = n while True: if l == r: ans = l break if l == r - 1: if l % 2 == 1: ans = l else: ans = r break m = (l + r) // 2 if m % 2 == 0: m += 1 tt = calc(m) if tt >= k: l = m else: r = m - 1 l = 2 if n % 2 == 0: r = n else: r = n - 1 while True: if l == r: anss = l break if l == r - 1: if l % 2 == 0: anss = l else: anss = r break m = (l + r) // 2 if m % 2 == 1: m += 1 tt = calc(m) if tt >= k: l = m else: r = m - 1 print(max(anss, ans))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
a, N, K = [0] + [int(i) for i in input().split()] d = lambda n: sum([min(N - (n << e) + 1, 1 << e) for e in range(99) if n << e <= N]) c = lambda n: d(n) + (d(n + 1), 0)[n & 1] for b in range(59, -1, -1): a += (0, 1 << b)[c(a + 2**b) >= K] print(a)
ASSIGN VAR VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
import sys reader = (map(int, line.split()) for line in sys.stdin) def count(num): ans = 0 if num % 2: add = 1 else: add = 2 while num + add - 1 <= n: ans += add add *= 2 num *= 2 if num <= n: ans += n - num + 1 return ans def func(n, k): if k == n: return 1 L = 0 R = n // 2 + n % 2 while L + 1 < R: m = (L + R) // 2 num = 2 * m + 1 if count(num) >= k: L = m else: R = m m_odd = 2 * L + 1 L = 1 R = n // 2 + 1 while L + 1 < R: m = (L + R) // 2 num = 2 * m if count(num) >= k: L = m else: R = m m_even = 2 * L return max(m_odd, m_even) n, k = next(reader) ans = func(n, k) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = map(int, input().split()) j = n odd = 3 even = 6 if j % 2 == 0: n1 = 1 n2 = 1 elif j % 2 != 0: n1 = 1 n2 = 2 preodd = 1 preeven = 2 if k == n: print(1) elif k == 1: print(j) elif k == 2: if j % 2 == 0: print(j - 2) else: print(j - 1) else: pre = n j = n // 2 while j >= 1: if j % 2 == 0: if pre % 2 == 0: if n1 + preodd + 1 >= k: ans = j break elif odd >= k: ans = j - 1 break elif even >= k and j > 4: ans = j - 2 break else: n1 = n1 + preodd + 1 n2 = odd elif n2 + preodd + 1 >= k: ans = j break elif odd >= k: ans = j - 1 break elif even >= k and j > 4: ans = j - 2 break else: n1 = n2 + preodd + 1 n2 = odd elif pre % 2 != 0: if n2 + 1 >= k: ans = j break elif preeven + n2 + 2 >= k: ans = j - 1 break elif odd >= k and j > 4: ans = j - 2 break elif even >= k and j > 5: ans = j - 3 break else: n1 = n2 + 1 n2 = preeven + n2 + 2 elif n1 + 1 >= k: ans = j break elif n1 + preeven + 2 >= k: ans = j - 1 break elif odd >= k and j > 5: ans = j - 2 break elif even >= k and j > 4: ans = j - 3 break else: n1 = n1 + 1 n2 = n1 + preeven + 2 preodd = odd preeven = even odd = even + 1 even = even + odd + 1 pre = j j = j // 2 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
def binary_search(lb, ub, criterion): ok = lb ng = ub while ng - ok > 1: mid = (ng + ok) // 2 if criterion(mid): ok = mid else: ng = mid return ok n, k = list(map(int, input().split())) odd_acc_count = 0 odd_count = 1 depth = 0 while odd_acc_count < k: odd_acc_count += odd_count odd_count *= 2 depth += 1 residual = k - (odd_acc_count - odd_count // 2) - 1 odd_max_idx = binary_search( 0, n + 2, lambda x: (2 * x + 1) * (1 << depth - 1) + residual <= n ) ans_odd = 2 * odd_max_idx + 1 even_acc_count = 0 even_count = 2 depth = 0 while even_acc_count < k: even_acc_count += even_count even_count *= 2 depth += 1 residual = k - (even_acc_count - even_count // 2) - 1 even_max_idx = binary_search( 0, n + 2, lambda x: 2 * x * (1 << depth - 1) + residual <= n ) ans_even = 2 * even_max_idx print(max(ans_odd, ans_even))
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
N, K = map(int, input().split()) def even(a): if a == 0: return True n = 2 * a res = 0 m = n.bit_length() M = N.bit_length() for i in range(60): if i + m < M: res += 2 ** (i + 1) elif i + m == M: if N >> i > n: res += 2**i elif N >> i == n: res += N - (n << i) + 1 if N >> i > n + 1: res += 2**i elif N >> i == n + 1: res += N - (n + 1 << i) + 1 break return res >= K def odd(a): n = 2 * a - 1 res = 0 m = n.bit_length() M = N.bit_length() for i in range(60): if i + m < M: res += 2**i elif i + m == M: if N >> i > n: res += 2**i elif N >> i == n: res += N - (n << i) + 1 break return res >= K start = 0 end = 10**18 while end - start > 1: t = (end + start) // 2 if even(t): start = t else: end = t test1 = -1 if even(end): test1 = 2 * end else: test1 = 2 * start start = 1 end = 10**18 while end - start > 1: t = (end + start) // 2 if odd(t): start = t else: end = t test2 = -1 if odd(end): test2 = 2 * end - 1 else: test2 = 2 * start - 1 print(max(test1, test2))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
import sys def f(n, mid): a, b, ans = 1, mid, 0 while 2 * mid + 1 <= n: a *= 2 ans += a mid = 2 * mid + 1 b *= 2 if 2 * b <= n: ans += n - 2 * b + 1 return ans n, k = map(int, input().split()) if n == k: print(1) sys.exit() low, high, m = 0, n // 2, 1 while low <= high: mid = low + (high - low) // 2 x = f(n, mid) if x >= k: m = mid low = mid + 1 else: high = mid - 1 if f(n, m) - 1 - f(n, 2 * m) >= k: print(2 * m + 1) else: print(2 * m)
IMPORT FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
import sys input = sys.stdin.readline n, k = map(int, input().split()) l = 0 r = n while r - l > 1: mid = (l + r) // 2 x = mid * 2 cnt = 0 if x <= n: cnt += 1 if x + 1 <= n: cnt += 1 v = 2 while v * x <= n: if v * x + 2 * v - 1 <= n: cnt += 2 * v v *= 2 else: cnt += n - v * x + 1 break if cnt >= k and x <= n: l = mid else: r = mid ans = l * 2 l = 1 r = n while r - l > 1: mid = (l + r) // 2 x = mid * 2 - 1 cnt = 0 if x <= n: cnt += 1 v = 2 while v * x <= n: if v * x + v - 1 <= n: cnt += v v *= 2 else: cnt += n - v * x + 1 break if cnt >= k and x <= n: l = mid else: r = mid ans = max(ans, 2 * l - 1) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
n, k = map(int, input().split()) def solve(n, num): ans = 0 keta_num = num.bit_length() keta_n = n.bit_length() keta_diff = keta_n - keta_num if keta_diff < 0: return ans ans += (1 << keta_diff) - 1 if n >> keta_diff > num: ans += 1 << keta_diff elif n >> keta_diff == num: ans += n & (1 << keta_diff) - 1 ans += 1 return ans ok = 0 ng = n + 10 while abs(ok - ng) > 1: mid = (ok + ng) // 2 val = mid * 2 + 1 if solve(n, val) >= k: ok = mid else: ng = mid ans1 = ok * 2 + 1 ok = 0 ng = n + 10 while abs(ok - ng) > 1: mid = (ok + ng) // 2 val = mid * 2 if solve(n, val) + solve(n, val + 1) >= k: ok = mid else: ng = mid ans2 = ok * 2 print(max(ans1, ans2))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
At first, let's define function $f(x)$ as follows: $$ \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} $$ We can see that if we choose some value $v$ and will apply function $f$ to it, then apply $f$ to $f(v)$, and so on, we'll eventually get $1$. Let's write down all values we get in this process in a list and denote this list as $path(v)$. For example, $path(1) = [1]$, $path(15) = [15, 14, 7, 6, 3, 2, 1]$, $path(32) = [32, 16, 8, 4, 2, 1]$. Let's write all lists $path(x)$ for every $x$ from $1$ to $n$. The question is next: what is the maximum value $y$ such that $y$ is contained in at least $k$ different lists $path(x)$? Formally speaking, you need to find maximum $y$ such that $\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 10^{18}$). -----Output----- Print the only integer — the maximum value that is contained in at least $k$ paths. -----Examples----- Input 11 3 Output 5 Input 11 6 Output 4 Input 20 20 Output 1 Input 14 5 Output 6 Input 1000000 100 Output 31248 -----Note----- In the first example, the answer is $5$, since $5$ occurs in $path(5)$, $path(10)$ and $path(11)$. In the second example, the answer is $4$, since $4$ occurs in $path(4)$, $path(5)$, $path(8)$, $path(9)$, $path(10)$ and $path(11)$. In the third example $n = k$, so the answer is $1$, since $1$ is the only number occuring in all paths for integers from $1$ to $20$.
from sys import stdin, stdout def getmaxval(n, k): if n == k: return 1 ol = 1 oh = n // 2 while ol < oh: om = (ol + oh + 1) // 2 cnt = counteven(om * 2, n) if cnt >= k: ol = om else: oh = om - 1 r1 = ol * 2 if r1 < n: cnt2 = countodd(r1 + 1, n) if cnt2 >= k: return r1 + 1 return r1 def counteven(b, n): res = 2 inc = 2 mv = b + 1 while mv <= n: inc *= 2 res += inc mv = b * inc // 2 + inc - 1 if mv - inc <= n < mv: res -= mv - n elif n < mv - inc: res -= inc return res def countodd(b, n): res = 1 inc = 1 mv = b while mv <= n: inc *= 2 mv = b * inc + inc - 1 res += inc if mv - inc <= n < mv: res -= mv - n elif n < mv - inc: res -= inc return res nk = list(map(int, stdin.readline().split())) n = nk[0] k = nk[1] res = getmaxval(n, k) stdout.write(str(res))
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: a ticket for one trip costs 20 byteland rubles, a ticket for 90 minutes costs 50 byteland rubles, a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip. -----Input----- The first line of input contains integer number n (1 ≤ n ≤ 10^5) — the number of trips made by passenger. Each of the following n lines contains the time of trip t_{i} (0 ≤ t_{i} ≤ 10^9), measured in minutes from the time of starting the system. All t_{i} are different, given in ascending order, i. e. t_{i} + 1 > t_{i} holds for all 1 ≤ i < n. -----Output----- Output n integers. For each trip, print the sum the passenger is charged after it. -----Examples----- Input 3 10 20 30 Output 20 20 10 Input 10 13 45 46 60 103 115 126 150 256 516 Output 20 20 10 0 20 0 0 20 20 10 -----Note----- In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.
n = int(input()) t = [int(input()) for _ in range(n)] INF = 10**12 ll = [1, 90, 1440] pp = [20, 50, 120] lowest_price = [0] * n p = pp[0] lowest_price[0] = p for i in range(1, n): u = lowest_price[i - 1] + pp[0] for j in range(1, 3): l = ll[j] p = pp[j] start = t[i] - l + 1 prev = 0 if t[0] < start: b = i a = max(0, b - l) while b - a > 1: m = (a + b) // 2 if t[m] < start: a = m else: b = m prev = lowest_price[a] if prev + p < u: u = prev + p lowest_price[i] = u to_print = [0] * n to_print[0] = lowest_price[0] for i in range(1, n): if lowest_price[i - 1] < lowest_price[i]: to_print[i] = lowest_price[i] - lowest_price[i - 1] print(*to_print, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: a ticket for one trip costs 20 byteland rubles, a ticket for 90 minutes costs 50 byteland rubles, a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip. -----Input----- The first line of input contains integer number n (1 ≤ n ≤ 10^5) — the number of trips made by passenger. Each of the following n lines contains the time of trip t_{i} (0 ≤ t_{i} ≤ 10^9), measured in minutes from the time of starting the system. All t_{i} are different, given in ascending order, i. e. t_{i} + 1 > t_{i} holds for all 1 ≤ i < n. -----Output----- Output n integers. For each trip, print the sum the passenger is charged after it. -----Examples----- Input 3 10 20 30 Output 20 20 10 Input 10 13 45 46 60 103 115 126 150 256 516 Output 20 20 10 0 20 0 0 20 20 10 -----Note----- In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.
n = int(input()) time = [] for i in range(n): time.append(int(input())) ptr90 = 0 ptrs = 0 dp = [0] * n dp[0] = 20 print(20) for i in range(1, n): s1 = dp[i - 1] + 20 if time[i] <= 89 + time[ptr90]: if ptr90 == 0: s2 = 50 else: s2 = dp[ptr90 - 1] + 50 else: while time[ptr90] + 89 < time[i]: ptr90 += 1 s2 = dp[ptr90 - 1] + 50 if time[i] <= 1439 + time[ptrs]: if ptrs == 0: s3 = 120 else: s3 = dp[ptrs - 1] + 120 else: while time[ptrs] + 1439 < time[i]: ptrs += 1 s3 = dp[ptrs - 1] + 120 dp[i] = min(s1, s2, s3) print(dp[i] - dp[i - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER
A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: a ticket for one trip costs 20 byteland rubles, a ticket for 90 minutes costs 50 byteland rubles, a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip. -----Input----- The first line of input contains integer number n (1 ≤ n ≤ 10^5) — the number of trips made by passenger. Each of the following n lines contains the time of trip t_{i} (0 ≤ t_{i} ≤ 10^9), measured in minutes from the time of starting the system. All t_{i} are different, given in ascending order, i. e. t_{i} + 1 > t_{i} holds for all 1 ≤ i < n. -----Output----- Output n integers. For each trip, print the sum the passenger is charged after it. -----Examples----- Input 3 10 20 30 Output 20 20 10 Input 10 13 45 46 60 103 115 126 150 256 516 Output 20 20 10 0 20 0 0 20 20 10 -----Note----- In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.
n = int(input()) travel_times = list() travel_pay = list() pay_ticket1 = 0 time_ticket1 = 0 pay_ticket2 = 0 time_ticket2 = 0 for travel_id in range(n): t = int(input()) travel_times.append(t) pay2 = 20 sum_time1 = t - travel_times[time_ticket1] sum_time2 = t - travel_times[time_ticket2] if sum_time1 >= 90: pay_ticket1 -= travel_pay[time_ticket1] for id2 in range(time_ticket1 + 1, travel_id + 1): if t - travel_times[id2] < 90: time_ticket1 = id2 break else: pay_ticket1 -= travel_pay[id2] sum_time1 = t - travel_times[time_ticket1] if sum_time2 >= 1440: pay_ticket2 -= travel_pay[time_ticket2] for id2 in range(time_ticket2 + 1, travel_id + 1): if t - travel_times[id2] < 1440: time_ticket2 = id2 break else: pay_ticket2 -= travel_pay[id2] sum_time2 = t - travel_times[time_ticket2] if pay_ticket1 + pay2 > 50: pay2 = 50 - pay_ticket1 if pay_ticket2 + pay2 > 120: pay2 = 120 - pay_ticket2 pay_ticket1 += pay2 pay_ticket2 += pay2 travel_pay.append(pay2) for pay in travel_pay: print(pay)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR