description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() answ = [] i = len(k) while True: j = i - 1 while int(k[j:i]) < n: j -= 1 if j < 0: j = 0 break while int(k[j:i]) >= n or j + 1 != i and k[j] == "0": j += 1 answ.append(k[j:i]) i = j if i <= 0: break res = 0 p = 1 for i in map(int, answ): res += i * p p *= n print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
import sys dpp = [([0] * 100) for i in range(100)] used = [([0] * 100) for i in range(100)] inf = 1 << 300 def dp(pos, i): if pos >= k: return "0" hh = n**i if pos == k - 1: return str(int(s[pos]) * hh) if used[pos][i]: return dpp[pos][i] used[pos][i] = 1 temp = s[pos] ans = inf best = "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" ctr = pos while int(temp) < n: if temp[0] == "0" and len(temp) > 1: if ctr == k - 1: break ctr += 1 temp = s[ctr] + temp continue tt = dp(ctr + 1, i + 1) gg = str(int(tt) + int(temp) * hh) if int(gg) < ans: ans = int(gg) best = gg if ctr == k - 1: break ctr += 1 temp = s[ctr] + temp dpp[pos][i] = best return best n = int(input()) s = input()[::-1] k = len(s) print(int(dp(0, 0)))
IMPORT ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN STRING ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = input() s = input() n = int(n) t1 = len(s) k = 1 sum = 0 while t1 > 0: m = 0 for i in range(0, t1): w = int(s[i:t1]) if w < n: m = i break while m < t1 - 1 and s[m] == "0": m = m + 1 sum += k * w k *= n t1 = m print(sum)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = input() l = len(n) n, k = int(n), input() K, d, ans = [], 1, 0 while k: ll = l while ll > len(k) or int(k[-ll:]) >= n or k[-ll] == "0": if ll > 1: ll -= 1 else: break K += [int(k[-ll:])] k = k[:-ll] for x in K: ans += x * d d = d * n print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER WHILE VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING IF VAR NUMBER VAR NUMBER VAR LIST FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = [int(i) for i in input()] sz = len(k) dp = [[int(1e19), -1] for i in range(sz)] dp.append([0, 1]) for i in range(sz - 1, -1, -1): if k[i] == 0: dp[i] = [dp[i + 1][0], dp[i + 1][1] * n] continue tp = 0 for j in range(i, sz): tp = tp * 10 + k[j] if tp >= n: break if dp[i][0] > tp * dp[j + 1][1] + dp[j + 1][0]: dp[i] = [tp * dp[j + 1][1] + dp[j + 1][0], dp[j + 1][1] * n] print(dp[0][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR LIST VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR IF VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
from _collections import defaultdict n = int(input()) s = input() l = len(s) ll = len(str(n)) d = defaultdict(int) for i in range(l): for j in range(1, ll + 1): if ( i - j + 1 >= 0 and (s[i - j + 1] != "0" or j == 1) and (d[i - j] or i - j == -1) ): ss = d[i - j] add = int(s[i - j + 1 : i + 1]) if add < n: ss = ss * n + add if not d[i]: d[i] = ss else: d[i] = min(ss, d[i]) print(d[l - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input().strip()) s = input().strip() ans = 0 j = 0 while len(s) > 0: i = 1 while i < len(s) and int(s[-i:]) < n: i += 1 if int(s[-i:]) >= n: i -= 1 while s[-i:][0] == "0" and len(s[-i:]) > 1: i -= 1 ans += int(s[-i:]) * pow(n, j) j += 1 s = s[:-i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() if int(k) < n: print(k) else: power = 0 digits = 0 ans = 0 while len(k) > 0: current = 0 while ( int(k[current:]) >= n and current < len(k) or int(k[current]) == 0 and len(k) > 1 and current < len(k) - 1 ): current += 1 if current == len(k): power += 1 else: ans += int(k[current:]) * n**power power += 1 k = str(k[:current]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
base = int(input()) num = input() a = 0 n = "" l = [] sk = len(str(base)) while num: nn = num[-sk:] j = 0 while int(nn) >= base: j += 1 nn = nn[1:] while len(nn) > 1 and nn[0] == "0": j += 1 nn = nn[1:] l.append(int(nn)) num = num[: -sk + j] p = 1 for n in l: a += p * n p *= base print(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
t, m = input(), input() n, d = int(t), len(t) s, p = 0, 1 j = len(m) while j: i = max(0, j - d) k = int(m[i:j]) if k >= n: i += 1 k = int(m[i:j]) while m[i] == "0" and i < j - 1: i += 1 j = i s += k * p p *= n print(s)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR STRING VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
def getint(a): return int("".join(map(str, a))) b = int(input()) s = input() s = list(map(int, list(s))) n = len(s) DP = [(-1, 0) for i in range(n + 1)] for i in range(n - 1, -1, -1): pass for j in range(i + 1, n + 1): a = getint(s[i:j]) pass if a >= b: break if a > 9: t = DP[j] a = a * b ** t[1] if t[0] != -1: a += t[0] p = a, t[1] + 1 if DP[i][0] == -1: DP[i] = p elif DP[i][0] > p[0]: DP[i] = p else: t = DP[j] a = a * b ** t[1] if t[0] != -1: a += t[0] p = a, t[1] + 1 if DP[i][0] == -1: DP[i] = p elif DP[i][0] > p[0]: DP[i] = p pass for i in range(n): pass print(DP[0][0])
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
inf = int(1e18) B = int(input()) s = input() power = [] power.append(1) LIMIT = 0 dp = [] def value(l, r): ans = 0 for i in range(l, r + 1): c = int(s[i]) - int("0") ans = ans * 10 + c return ans def min(a, b): if a < b: return a else: return b def f(l, r, i): if i >= LIMIT: return inf if dp[l][r][i] != -1: return dp[l][r][i] if l == 0: return value(l, r) * power[i] opt1 = inf opt2 = inf opt3 = inf temp = value(l - 1, r) if temp < B: opt1 = f(l - 1, r, i) if s[l] == "0": if l == r: opt2 = f(l - 1, r - 1, i + 1) dp[l][r][i] = min(opt1, opt2) return dp[l][r][i] opt3 = f(l - 1, l - 1, i + 1) + value(l, r) * power[i] ans = min(opt1, opt2) dp[l][r][i] = min(ans, opt3) return dp[l][r][i] for i in range(1, 61): power.append(power[i - 1] * B) if power[i] > inf: LIMIT = i break for i in range(0, 65): dp.append([]) for j in range(0, 65): dp[i].append([]) for k in range(0, 65): dp[i][j].append(-1) l = len(s) print(f(l - 1, l - 1, 0))
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() pos = len(k) ans = [] while pos > 0: tmp = 0 l = pos - 1 mark = l while l >= 0: a = int(k[l:pos]) if a >= n: break else: if a != tmp: mark = l tmp = a l -= 1 if tmp == 0: pos -= 1 ans.append(0) else: pos = mark ans.append(tmp) aans = 0 for i in ans[::-1]: aans = aans * n + i print(aans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
base = int(input()) number = input() n = i = len(number) - 1 ans = power = 0 while n >= 0: i = n while i >= 0 and int(number[i : n + 1]) < base: i -= 1 while i < n - 1 and number[i + 1] == "0": i += 1 ans += int(number[i + 1 : n + 1]) * base**power n = i power += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() z = 0 w = 0 j = len(k) - 1 i = j while j >= 0: r = j - 1 while r >= 0 and k[r] == "0": r -= 1 if j >= 0 and k[j] == "0" and (i - r + 1 > 10 or int(k[r : i + 1]) >= n): if j == i: w += 1 j -= 1 i -= 1 else: z += pow(n, w) * int(k[j + 1 : i + 1]) i = j w += 1 continue elif i - j + 1 >= 10 or int(k[j : i + 1]) >= n: z += pow(n, w) * int(k[j + 1 : i + 1]) i = j w += 1 continue else: j -= 1 continue z += pow(n, w) * int(k[j + 1 : i + 1]) print(z)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR STRING BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = 0 k = "" mem = [[(-1) for xx in range(66)] for yy in range(66)] def go(ind, po): global n global k global mem if ind >= len(k): return 0 if mem[ind][po] != -1: return mem[ind][po] for i in range(ind, len(k)): cur = int(k[ind : i + 1][::-1]) if cur >= n: break if k[i] == "0" and i != ind: continue if mem[ind][po] == -1: if go(i + 1, po + 1) != -1: mem[ind][po] = cur * pow(n, po) + go(i + 1, po + 1) elif go(i + 1, po + 1) != -1: mem[ind][po] = min(mem[ind][po], cur * pow(n, po) + go(i + 1, po + 1)) return mem[ind][po] n = int(input()) k = input() k = k[::-1] print(go(0, 0))
ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR IF VAR VAR STRING VAR VAR IF VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
def solve(n, k): dp = [420] * (len(k) + 1) dp[-1] = 0 idx = len(k) - 1 n_num = int(n) while idx >= 0: if k[idx] != "0": for shift in range(1, len(k) - idx + 1): ok = int(k[idx : idx + shift]) < n_num if ok: dp[idx] = min(dp[idx], 1 + dp[idx + shift]) else: break else: dp[idx] = 1 + dp[idx + 1] idx -= 1 digits = list() idx = 0 while idx < len(k): shift = 1 while idx + shift <= len(k): if dp[idx + shift] + 1 == dp[idx]: digits.append(int(k[idx : idx + shift])) break else: shift += 1 idx += shift tot_res, n_pow = 0, 1 for el in reversed(digits): tot_res += n_pow * el n_pow *= n_num return tot_res N = input() K = input() print(solve(N, K))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
INF = int(1e18) def reverse(s): for i in range(len(s) // 2): s[i], s[len(s) - i - 1] = s[len(s) - i - 1], s[i] return s dp = [] for i in range(61): dp += [[]] for j in range(61): dp[i] += [[]] for k in range(61): dp[i][j] += [INF] n = int(input()) ff = input() k = [] for i in range(len(ff)): k += ff[i] k = reverse(k) dp[0][0][0] = 0 for kk in range(1, len(k) + 1): for i in range(1, len(k) + 1): s = "" for j in range(1, i + 1): s += k[i - j] if k[i - j] == "0": if j == 1: mn = INF for jj in range(0, 61): mn = min(mn, dp[i - j][jj][kk - 1]) if mn != INF: dp[i][j][kk] = mn if s[0] == "0": continue d = int(s) if d < n: mn = INF for jj in range(0, 61): mn = min(mn, dp[i - j][jj][kk - 1]) if mn != INF: dp[i][j][kk] = mn + pow(n, kk - 1) * d ans = INF n = len(k) for k in range(1, 61): for j in range(0, 61): ans = min(ans, dp[n][j][k]) print(ans)
ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR LIST LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
import sys input = sys.stdin.readline n = int(input()) k = input().rstrip() dp = [10**70] * (len(k) + 1) dp[0] = 0 for i in range(1, len(k) + 1): for j in range(i): if int(k[j:i]) < n and (k[j] != "0" or j == i - 1): dp[i] = min(dp[i], dp[j] * n + int(k[j:i])) print(dp[-1])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = input() ans = 0 cs = "" cp = 1 def get_digit(s, n): for i in range(len(s)): if int(s[i:]) < n and s[i] != "0": return int(s[i:]), s[:i] return 0, s[:-1] while s: d, s = get_digit(s, n) ans += d * cp cp *= n print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
b = 0 value = 0 v = "" b = int(input("")) v = input("") vetor = [] aux = "" while len(v) > 0: n = v[-1] + aux if int(n) >= b: vetor.append(aux) if aux[0] == "0" and len(aux) > 1: i = 0 while i < len(aux) - 1: if aux[i] == "0": v += aux[i] else: break i += 1 aux = "" else: aux = v[-1] + aux v = v[:-1] vetor.append(aux) soma = 0 i = 0 for I in vetor: soma += int(I) * b**i i += 1 print(soma)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = [0] + list(map(int, list(input()))) def calc(l, r): if k[l] == 0 and l < r - 1: return -1 res = 0 for i in range(max(l, 0), r): res *= 10 res += k[i] return res z = [] last_index = len(k) index = len(k) - 1 while index >= -1: c = calc(index, last_index) while c >= n: li = index + 1 nw = calc(li, last_index) while nw < 0: li += 1 nw = calc(li, last_index) z.append(nw) last_index = li c = calc(index, last_index) index -= 1 li = index + 1 nw = calc(li, last_index) while nw < 0: li += 1 nw = calc(li, last_index) z.append(nw) last_index = li c = calc(index, last_index) z = z[::-1] ans = 0 for i in z: ans *= n ans += i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
import sys n = int(sys.stdin.readline()) s = sys.stdin.readline()[:-1] l = [] m = len(s) i = m - 1 dp = [(0) for x in range(m)] for i in range(m): zero = False fzero = -1 j = i while j >= 0: if s[j] == "0": zero = True if int(s[j : i + 1]) < n: last = j j -= 1 else: break if zero: while last < i: if s[last] == "0": last += 1 else: break if last == i and s[i] == 0: dp[i] = 0 continue else: dp[i] = int(s[last : i + 1]) continue else: dp[i] = int(s[last : i + 1]) i = m - 1 while i >= 0: cur = str(dp[i]) a = len(cur) l.append(cur) while a > 0: i -= 1 a -= 1 l.reverse() k = len(l) i = 0 res = 0 k -= 1 while k >= 0: res += int(l[i]) * n**k k -= 1 i += 1 print(res)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR WHILE VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = input() s = input() k = len(n) n = int(n) a = [] i = len(s) - 1 l = 0 while i - k + 1 >= 0: if int(s[i - k + 1 : i + 1]) < n: z = len(str(int(s[i - k + 1 : i + 1]))) a.append(int(s[i - z + 1 : i + 1])) i -= z else: z = len(str(int(s[i - k + 2 : i + 1]))) a.append(int(s[i - z + 1 : i + 1])) i -= z else: if i > -1 and int(s[0 : i + 1]) < n: a.append(int(s[0 : i + 1])) i -= k for i in range(len(a)): l += a[i] * n**i print(min(l, 10**18))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
k = int(input()) n = input() if n == "0": print(0) exit(0) cp = 1 ans = 0 cn = "" cz = 0 for i in range(len(str(n))): ls = n[-1] n = n[:-1] if ls == "0": cz += 1 else: sn = ls + "0" * cz + cn if int(sn) < k: cn = sn cz = 0 else: if cn != "": ans += cp * int(cn) cp *= k cn = ls + "0" * cz while int(cn) >= k: cn = cn[:-1] cp *= k cz = 0 ans += cp * int(cn) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP STRING VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR STRING VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP STRING VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = int(input()) arr = [] idx = 0 while k > 0: while 10**idx <= k and k % 10**idx < n: idx += 1 if k % 10**idx >= n: idx -= 1 while idx > 1 and k % 10**idx == k % 10 ** (idx - 1): idx -= 1 arr.append(k % 10**idx) k //= 10**idx idx = 0 mul = 1 sm = 0 for item in arr: sm += item * mul mul *= n print(sm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR NUMBER WHILE VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = int(input()) l = 1 t = n - 1 while True: t //= 10 if t <= 0: break l += 1 b = 10**l p = [] while k > 0: u = b m = k % u while m > 0: if m < n and 10 * m >= u: k //= u break u //= 10 m = k % u else: k //= 10 p.append(m) r = 0 for k, i in enumerate(p): r += n**k * i print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
dp = [float("inf") for i in range(100)] dp[0] = 0 n = int(input()) s = input() ln = len(s) s = "." + s for i in range(1, ln + 1): for j in range(1, i + 1): e = s[j : i + 1] e = int(e) if i - j + 1 > len(str(e)): continue vl = dp[j - 1] if e >= n: continue dp[i] = min(dp[i], dp[j - 1] * n + e) print(dp[ln])
ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = input() k = input() nums = [] intn = int(n) l = len(n) i = len(k) curl = l while True: if i - curl < 0: cur = k[:i] else: cur = k[i - curl : i] if len(cur) > 1 and cur[0] == "0": curl -= 1 continue if int(cur) < intn: nums.append(int(cur)) i = i - curl curl = l else: curl -= 1 if i <= 0: break r = 0 p = 1 for num in nums: r += num * p p *= intn print(r)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = list(map(int, list(input()))) m = len(k) rec = [0] * (m + 1) for i in range(m): u = rec[i] * n if k[i] > 0: d = 0 for j in range(i, m): d = d * 10 + k[j] if d >= n: break if rec[j + 1]: rec[j + 1] = min(rec[j + 1], u + d) else: rec[j + 1] = u + d elif rec[i + 1]: rec[i + 1] = min(rec[i + 1], u) else: rec[i + 1] = u print(rec[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
def init(dp): for i in range(70): for j in range(70): dp[i][j] = -1 def memo(cnt, power, N, K, dp): if cnt >= len(K): return 0 if dp[cnt][power] >= 0: return dp[cnt][power] res = 1 << 100 base = N**power if power < 0: return res if K[cnt] == "0": dp[cnt][power] = memo(cnt + 1, power - 1, N, K, dp) return dp[cnt][power] tmp = "" for i in range(cnt, len(K)): tmp += K[i] d = int(tmp) if d >= N: break res = min(res, memo(i + 1, power - 1, N, K, dp) + d * base) dp[cnt][power] = res return res N = int(input()) K = input() M = len(K) dp = [[(0) for i in range(70)] for j in range(70)] limit = 10**18 result = 1 << 100 for x in range(65, -1, -1): init(dp) result = min(result, memo(0, x, N, K, dp)) print(result)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
base = int(input()) k = input() k = k[::-1] n = len(k) dp = [[int(1e18) for i in range(0, 2 * n + 1)] for j in range(0, n + 1)] for i in range(0, n + 1): dp[n][i] = 0 for i in reversed(range(0, n)): for k1 in range(0, n): for j in range(i, n): if k[j] == "0" and i != j: continue num = int(k[j - n : i - n - 1 : -1]) if num >= base: break num *= base**k1 if dp[i][k1] > num + dp[j + 1][k1 + 1]: dp[i][k1] = num + dp[j + 1][k1 + 1] print(int(dp[0][0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
from sys import stdin, stdout base = stdin.readline().strip() num = stdin.readline().strip() ans = [] i = len(num) - len(base) while i >= 0: j = i while num[j] == "0" and j < i + len(base) - 1: j += 1 if int(num[j : i + len(base)]) >= int(base): j += 1 while num[j] == "0" and j < i + len(base) - 1: j += 1 ans.append(num[j : i + len(base)]) else: ans.append(num[j : i + len(base)]) i = j - len(base) i += len(base) if i: ans.append(num[:i]) cnt = 0 for i in range(len(ans)): cnt += int(base) ** i * int(ans[i]) stdout.write(str(cnt))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR STRING VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR STRING VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
def powr(a, r): if r == 0: return 1 tmp = powr(a, r >> 1) tmp = tmp * tmp if r & 1: tmp *= a return tmp n = int(input("")) k = str(input()) l = len(k) dp = [(1e20, 1e20) for i in range(l + 1)] dp[l] = 0, 0 dp[l - 1] = int(k[l - 1]), 1 for i in range(l - 2, -1, -1): if k[i] == "0": dp[i] = dp[i + 1][0], dp[i + 1][1] + 1 continue num = 0 for j in range(i, l): num = num * 10 + int(k[j]) if num >= n: break dp[i] = min( dp[i], (dp[j + 1][0] + num * powr(n, dp[j + 1][1]), dp[j + 1][1] + 1) ) print(dp[0][0])
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() l = len(k) ans = 0 b = 1 i = l - 1 while i >= 0: sum = 0 res = i for j in range(i, -1, -1): sum += (ord(k[j]) - ord("0")) * 10 ** (i - j) res = j if sum >= n: res = j + 1 break for j in range(res, i + 1): if k[j] == "0": res = j + 1 else: break res = min(res, i) sum = 0 for j in range(res, i + 1): sum = sum * 10 + ord(k[j]) - ord("0") ans += sum * b b *= n i = res - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
was = {} dp = {} def solve(id, cnt): if id == -1: return 0 if was.get((id, cnt)) != None: return dp[id, cnt] else: was[id, cnt] = True dp[id, cnt] = 10**18 for i in range(0, id + 1): cur = 0 for j in range(i, id + 1): cur = cur * 10 + ord(k[j]) - 48 if cur < n and k[i] != "0": dp[id, cnt] = min(dp[id, cnt], solve(i - 1, cnt + 1) + cur * n**cnt) if k[id] == "0": dp[id, cnt] = min(dp[id, cnt], solve(id - 1, cnt + 1)) return dp[id, cnt] n = input() k = input() n = int(n) print(solve(len(k) - 1, 0))
ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR NONE RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
def findMin(base, number, power): len_base = len(base) len_number = len(number) BASE = int(base) if int(number) < int(base): return int(number) * int(base) ** power else: for i in range(len_base, 0, -1): if number[-1 * i] == "0": continue elif int(number[len_number - i :]) < BASE: answer = int(number[len_number - i :]) * int(base) ** power number = number[: len_number - i] return answer + findMin(base, number, power + 1) number = number[: len_number - 1] return findMin(base, number, power + 1) base = input() number = input() print(findMin(base, number, 0))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP NUMBER VAR STRING IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
import sys k = int(input()) t = input() n = len(t) inf = 10**18 + 1 dp = [inf] * (n + 1) for i in range(n + 1): dp[i] = [inf] * (n + 1) t = t[::-1] dp[0][0] = 0 for i in range(n + 1): for j in range(n + 1): if dp[i][j] == inf: continue cur = 0 for len in range(0, n): if i + len >= n: break symb = ord(t[i + len]) - ord("0") if symb == 0 and len > 0: continue cur = cur + symb * 10**len if cur >= k: break neww = dp[i][j] + cur * k**j dp[i + len + 1][j + 1] = min(dp[i + len + 1][j + 1], neww) ans = -1 for i in range(1, n + 1): if ans == -1 or ans > dp[n][i]: ans = dp[n][i] print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
def check(i, new, s): d = "" while i >= 0 and not int(s[i]): d = s[i] + d i -= 1 if i >= 0: d = s[i] + d if int(d + new) >= n: return "" return d n = int(input()) s = input() m = [] i = len(s) - 1 while i >= 0: new = s[i] i -= 1 add = check(i, new, s) while i >= 0 and add: new = add + new i -= len(add) add = check(i, new, s) m.append(int(new)) l = len(m) for i in range(l // 2): m[i], m[l - i - 1] = m[l - i - 1], m[i] ans = 0 for i in range(l): ans += m[i] * n ** (l - i - 1) print(ans)
FUNC_DEF ASSIGN VAR STRING WHILE VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
N = input().strip() K = input().strip() al = [] while K != "": best = 0 for i in range(len(K)): if ( int(K[i : len(K)]) < int(N) and K[i] != "0" or K[i] == "0" and i == len(K) - 1 ): best = i break al.append(K[best : len(K)]) K = K[0:best] ans = 0 for i in range(len(al)): ans += int(al[i]) * int(N) ** i print(str(ans) + "\n")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
b = int(input()) s = input() n = len(s) pwr = [1] for _ in range(60): if pwr[-1] * b <= 10**18: pwr.append(pwr[-1] * b) else: break digit = len(pwr) ans = 10**18 dp = [[(10**20) for _ in range(digit + 10)] for _ in range(n)] for j in range(digit): dp[0][j] = int(s[0]) * pwr[j] for i in range(1, n): if int(s[: i + 1]) < b: dp[i][0] = int(s[: i + 1]) for j in range(digit): dp[i][j] = min(dp[i][j], dp[i - 1][j + 1] + int(s[i]) * pwr[j]) for k in range(i - 1, 0, -1): if s[k] != "0" and int(s[k : i + 1]) < b: dp[i][j] = min(dp[i][j], dp[k - 1][j + 1] + int(s[k : i + 1]) * pwr[j]) if int(s[: i + 1]) < b: dp[i][j] = min(dp[i][j], int(s[: i + 1]) * pwr[j]) print(dp[n - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
def main(): n = int(input()) k = int(input()) if k == 0: print(0) return ans = 0 np = 1 while k > 0: i = 10 ok = k p10 = 1 x = 0 while ok > 0: c = ok % 10 x += p10 * c p10 *= 10 if x >= n: break if c != 0: i = p10 ok //= 10 x = k % i k //= i ans += x * np np *= n print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) a = input() cn = 0 fn = 0 tp = 0 c10p = 0 maxi = -1 while len(a) != 0: maxi = -1 for i in range(1, len(a) + 1): ds = int(a[-i:]) if ds < n and int(a[-i]) != 0: maxi = i if maxi == -1 and int(a[-1]) == 0: a = a[:-1] tp += 1 else: fn += int(a[-maxi:]) * n**tp tp += 1 a = a[:-maxi] print(fn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
base = int(input()) num = input() result = 0 place_value = 1 end = len(num) while end > 0: begin = end - 1 good_begin = begin while begin >= 0: if int(num[begin:end]) >= base: break elif num[begin] != "0": good_begin = begin begin -= 1 begin = good_begin result += place_value * int(num[begin:end]) place_value *= base end = begin print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) d = input() l = len(d) dp = [[0, 0] for i in range(0, l + 1)] dp[l - 1] = [ord(d[l - 1]) - ord("0"), 1] for i in range(1, l): w = l - 1 - i m = (ord(d[w]) - ord("0")) * n**i + dp[w + 1][0] dp[w] = [m, i + 1] if d[w] == "0": dp[w][0] = dp[w + 1][0] dp[w][1] = dp[w + 1][1] + 1 continue for j in range(w, l): subs = int(d[w : j + 1]) u = dp[j + 1] if subs < n: re = subs * n ** u[1] + u[0] if re < dp[w][0] or re == dp[w][0] and 1 + u[1] < dp[w][1]: dp[w][0] = re dp[w][1] = 1 + u[1] else: break print(dp[0][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = list(map(int, input())) k = k[::-1] ans, cur, i = 0, 1, 0 while i < len(k): nxt, sm, temp, mul = i + 1, k[i], k[i], 10 for j in range(i + 1, len(k)): temp += mul * k[j] mul *= 10 if k[j] != 0 and temp < n: nxt = j + 1 sm = temp ans += sm * cur i = nxt cur *= n print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = input() dp = [0] for i in range(1, len(s) + 1): dp.append(1000000000000000000000000000000000000) for j in range(1, 10): if j <= i: can = s[i - j : i] tmp = int(can) if tmp < n and (can[0] != "0" or can == "0"): val = dp[i - j] * n + tmp if val < dp[i]: dp[i] = val if tmp >= n: break print(dp[len(s)])
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 FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
INF = 100000000000000000000 b = int(input()) s = input() n = len(s) dp = [INF for i in range(n + 1)] dp[0] = 0 for i in range(n): if s[i] == "0": dp[i + 1] = min(dp[i + 1], dp[i] * b) continue for j in range(i + 1, n): if int(s[i:j]) < b: dp[j] = min(dp[j], b * dp[i] + int(s[i:j])) else: break else: if int(s[i:]) < b: dp[n] = min(dp[n], b * dp[i] + int(s[i:])) print(dp[n])
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) x = input() l = len(x) dp = [[(1e18) for i in range(l + 1)] for i in range(l + 1)] for i in range(l + 1): dp[l][i] = 0 for i in range(l - 1, -1, -1): sum = 0 for j in range(i, l): sum = sum * 10 + (ord(x[j]) - ord("0")) if sum >= n: break for k in range(l): dp[i][k + 1] = min(dp[i][k + 1], dp[j + 1][k] + sum * n**k) if sum == 0: break ans = 10**18 for i in range(l + 1): ans = min(ans, dp[0][i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
inf = 10**18 + 1 n = int(input()) num = ["0"] + list(input())[::-1] T = [inf for i in range(len(num))] P = [inf for i in range(len(num))] T[0] = 0 P[0] = -1 for i in range(1, len(num)): if num[i] != "0": min_pow = inf for j in range(0, i): if n > int("".join(num[j + 1 : i + 1][::-1])): min_pow = min([min_pow, P[j]]) P[i] = min_pow + 1 for j in range(0, i): if P[j] == min_pow and n > int("".join(num[j + 1 : i + 1][::-1])): T[i] = min( [T[i], int("".join(num[j + 1 : i + 1][::-1])) * n ** P[i] + T[j]] ) else: P[i] = P[i - 1] + 1 T[i] = T[i - 1] print(T[len(num) - 1])
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = input() k = input() digit = len(n) lst = [] def recul(k): if len(k) == 0: return if k[-digit:] < n: lst.append(k[-digit:]) if k[-digit:][0:1] == "0" and len(k[-digit:]) != 1: recul(k[:-digit] + "0") else: recul(k[:-digit]) else: temp = -digit + 1 while k[temp : temp + 1] == "0" and temp < 0: temp += 1 if temp == 0: lst.append("0") recul(k[:-1]) return lst.append(k[temp:]) recul(k[:temp]) if digit == 1: print(int(k, int(n))) exit() recul(k) ans = 0 for x in range(len(lst)): ans += int(lst[x]) * int(n) ** x print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) K = input() m = len(K) inf = 10**100 dp = [inf] * (m + 1) dp[0] = 0 for i in range(m): if K[i] == "0": dp[i + 1] = min(dp[i + 1], dp[i] * n + int(K[i])) else: val = 0 for j in range(i, m): val = val * 10 + int(K[j]) if val >= n: break dp[j + 1] = min(dp[j + 1], dp[i] * n + val) print(dp[m])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
inf = 10**100 b = int(input()) s = input() n = len(s) dp = [] for i in range(n + 1): dp.append([inf] * (n + 2)) dp[n][0] = 0 for i in range(n - 1, -1, -1): for j in range(i + 1, min(i + 12, n + 1)): if j - i > 1 and s[i] == "0": continue cur = int(s[i:j]) if cur >= b: continue for p in range(n + 1): dp[i][p + 1] = min(dp[i][p + 1], dp[j][p] + cur) cur *= b res = inf for p in range(n + 2): res = min(res, dp[0][p]) print(res)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() r = len(k) a = [] i = len(k) - 1 while i >= 0: if k[i] != "0": if int(k[i:r]) >= n: a.append(int(k[i + 1 : r])) r = i + 1 i -= 1 else: i -= 1 else: j = i while j >= 0 and k[j] == "0": j -= 1 if int(k[j:r]) >= n: if i == r - 1: a.append(int(k[i:r])) r = i i -= 1 else: a.append(int(k[i + 1 : r])) r = i + 1 i -= 1 else: i = j - 1 ans = 0 a.append(int(k[0:r])) for i in range(len(a)): ans += a[i] * n**i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR STRING VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) m = int(input()) ln = len(str(n)) ans = 0 i = 0 while m != 0: dgt = m % 10**ln if dgt < n and len(str(dgt)) == ln: ans += dgt * n**i i += 1 m = m // 10**ln ln = len(str(n)) else: ln -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = input() dp = [0] * (len(s) + 1) pn = [0] * (len(s) + 1) pn[len(pn) - 1] = 1 for k in range(len(s) - 1, -1, -1): num = 0 dp[k] = -1 if s[k] == "0": dp[k] = pn[k + 1] * num + dp[k + 1] pn[k] = pn[k + 1] * n continue for i in range(k, len(s)): num = num * 10 + int(s[i]) if num >= n: break if dp[k] == -1 or pn[i + 1] * num + dp[i + 1] < dp[k]: dp[k] = pn[i + 1] * num + dp[i + 1] pn[k] = pn[i + 1] * n print(dp[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = int(input()) out = 0 base = 1 while k != 0: big = 10000000000 while k % big >= n and big > 10: big //= 10 out += k % big * base base *= n cpy = big while k % big < cpy // 10 and cpy > 10: cpy //= 10 big = cpy k //= big print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() INF = 10**100 dp = [([INF] * 100) for i in range(len(k) + 1)] dp[len(k)][0] = 0 dp[len(k) - 1][0] = int(k[len(k) - 1]) deg = [1] * 1000 for i in range(1, 100): deg[i] = deg[i - 1] * n for i in range(len(k) - 1, -1, -1): if k[i] == "0": for cur_deg in range(1, 61): dp[i][cur_deg] = dp[i + 1][cur_deg - 1] continue if int(k[i:]) < n: dp[i][0] = int(k[i:]) for cur_deg in range(1, 61): j = i cur_num = 0 while j < len(k) and cur_num < n: cur_num = cur_num * 10 + int(k[j]) if dp[j + 1][cur_deg - 1] == INF or cur_num >= n: j += 1 continue dp[i][cur_deg] = min( dp[i][cur_deg], dp[j + 1][cur_deg - 1] + cur_num * deg[cur_deg] ) j += 1 print(min(dp[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = input() dp = [([10**100] * len(s)) for _ in range(len(s))] for i in range(len(s) - 1, -1, -1): for j in range(i, len(s)): a = int(s[i : j + 1]) if len(str(a)) == j - i + 1 and a < n: if j == len(s) - 1: dp[i][0] = min(dp[i][0], a) else: for k in range(1, len(s)): dp[i][k] = min(dp[i][k], a * n**k + dp[j + 1][k - 1]) print(min(dp[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = input() i = len(s) j = i - 1 tot = 0 mul = 1 while i > 0 and j >= 0: while j >= 0: _num = int(s[j:i]) if _num >= n: j += 1 while s[j] == "0" and i - j > 1: j += 1 break num = _num j -= 1 tot += mul * num mul *= n j -= 1 i = j + 1 print(tot)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR STRING BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
def convertTo(act, param): return int(act) * param def rmLeading(act): while act[0] == "0": act = act[1:] return act def pureZero(act): for i in act: if i != "0": return False return True def getTheAct(act, i, n, k): while i >= 0 and int(k[i] + act) < int(n): act = k[i] + act i = i - 1 if pureZero(act): return "0" act = rmLeading(act) return act n = input() k = input() act = "" rpta = 0 mult = 1 i = len(k) - 1 while i >= 0: act = getTheAct("", i, n, k) i = i - len(act) rpta = rpta + convertTo(act, mult) mult = mult * int(n) print(rpta)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF WHILE VAR NUMBER STRING ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR VAR IF VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF WHILE VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
import sys def debug(*args, **kwargs): print(*args, **kwargs, file=sys.stderr) n = int(input()) s = input() h = len(s) k = int(s) dec = [0] * (h + 1) if k < n: print(k) else: dec[0] = 0 for i in range(1, h + 1): dec[i] = dec[i - 1] * n + int(s[i - 1]) for j in range(2, i + 1): if s[i - j] != "0": lastdig = int(s[i - j : i]) if lastdig >= n: break newdec = dec[i - j] * n + lastdig dec[i] = min(dec[i], newdec) print(dec[h])
IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = str(input()) m = len(s) x = m - 1 inf = int(1e19) dp = [[inf for p in range(m + 1)] for z in range(m + 1)] pows = [1] for i in range(m + 1): pows.append(pows[-1] * n) for j in range(m - 1, -1, -1): if j == m - 1: dp[j][0] = int(s[j]) continue if s[j] == "0": for x in range(m): dp[j][x + 1] = dp[j + 1][x] continue r = "" for k in range(j, m): r = r + s[k] y = int(r) if y >= n: break if k == m - 1: dp[j][0] = y else: for x in range(m): dp[j][x + 1] = min(dp[j][x + 1], dp[k + 1][x] + pows[x + 1] * y) ans = inf for i in range(m + 1): ans = min(ans, dp[0][i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
a = int(input()) b = input() la = len(str(a)) ans = 0 c = 0 k = 0 i = 0 ch = 0 while True: if len(b) < la: if len(b) == 0: break else: ans += int(b) * a**i i += 1 break c = int(b[len(b) - la : len(b)]) k = la while b[len(b) - la + ch] == "0" and la - ch != 1: ch += 1 c = int(b[len(b) - la + ch : len(b)]) k = la - ch while c >= a: ch += 1 if b[len(b) - la + ch] != "0" or la - ch == 1: c = int(b[len(b) - la + ch : len(b)]) k = la - ch ch = 0 ans += c * a**i i += 1 b = b[0 : len(b) - k] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR STRING BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = input() prr = [] while len(s) > 0: k = 0 for i in range(len(s) - 1, -1, -1): if s[i] == "0": continue if int(s[i:]) >= n or len(s[i:]) > len(str(n)): k = i + 1 while k < len(s) and s[k] == "0": k += 1 if k == len(s): k = len(s) - 1 break prr.append(int(s[k:])) s = s[:k] sum = 0 for i in range(len(prr) - 1, -1, -1): sum *= n sum += prr[i] print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
def ri(): return map(int, input().split()) n = int(input()) k = input() ans = 0 p = 0 while True: kk = k[:] for i in range(len(kk)): if int(kk[i]) == 0 and len(kk[i:]) != 1: continue if n > int(kk[i:]): ans += int(kk[i:]) * n**p p += 1 if i == 0: print(ans) exit() k = kk[:i] break
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = input() s = "".join(j for j in [i for i in reversed(s)]) cnt = 0 t = n while t != 0: cnt += 1 t //= 10 dp = [[(-1) for i in range(100)] for j in range(100)] dp[0][0] = ord(s[0]) - ord("0") len = len(s) for i in range(1, len): for j in range(len): for k in range(i, -1, -1): if j == 0: if s[i] == "0": continue else: get = int("".join(i for i in reversed(s[0 : i + 1]))) if get < n and i + 1 <= cnt: dp[i][j] = get elif s[i] == "0": dp[i][j] = dp[i - 1][j - 1] else: get = int("".join(i for i in reversed(s[k : i + 1]))) if get < n and i - k + 1 <= cnt: if dp[k - 1][j - 1] != -1: new_val = get * n**j + dp[k - 1][j - 1] if dp[i][j] == -1 or dp[i][j] > new_val: dp[i][j] = new_val ans = 2e18 for i in range(len): if dp[len - 1][i] != -1: ans = min(ans, dp[len - 1][i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() ans = 0 p = 1 i = len(k) - 1 j = len(k) while i >= 0: if k[i] != "0" or j - i == 1: v = int(k[i:j]) if v < n: x = i else: ans += p * int(k[x:j]) p *= n i = j = x i -= 1 if j > 0: ans += p * int(k[0:j]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF VAR VAR STRING BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = input() l = len(s) dp = [([10**20] * l) for i in range(l)] for i in range(l - 1, -1, -1): for j in range(l): for k in range(i, l): if s[i] == "0" and i != k: continue x = int(s[i : k + 1]) if x < n: if j == 0 and k == l - 1: dp[i][j] = min(dp[i][j], x) elif j != 0 and k < l - 1: dp[i][j] = min(dp[i][j], x * n**j + dp[k + 1][j - 1]) print(min(dp[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
m = int(input()) s = input() n = len(s) s = s[::-1] inf = 10**18 + 213 f = [0] * (n + 1) for i in range(n + 1): f[i] = [inf] * (n + 1) f[0][0] = 0 for prefix in range(n + 1): for base in range(n + 1): if f[prefix][base] == inf: continue for len in range(1, n + 1): if prefix + len > n: break if s[prefix + len - 1] == "0" and len > 1: continue val = 0 for k in range(len): val *= 10 val += int(s[prefix + len - k - 1]) if val < m: val *= m**base f[prefix + len][base + 1] = min( f[prefix + len][base + 1], f[prefix][base] + val ) ans = inf for i in range(n + 1): ans = min(ans, f[n][i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() a = 0 p = 1 while len(k) > 0: i = len(k) - 1 while i >= 0 and int(k[i:]) < n: i -= 1 i += 1 r = int(k[i:]) k = k[0 : len(k) - len(str(r))] a += r * p p *= n print(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) s = input() pw = [1] last = 1 for i in range(70): if last > 1e19: break pw.append(last * n) last = last * n dp = [1e19] * 100 for i in range(100): dp[i] = [1e19] * 100 dp[len(s)][0] = 0 for i in range(len(s), -1, -1): for power in range(0, len(pw)): cur = "" for j in range(i - 1, -1, -1): cur = s[j] + cur if int(cur) > n or int(cur) * pw[power] > 1e19: break if (cur[0] != "0" or len(cur) == 1) and int(cur) < n: dp[j][power + 1] = min( dp[j][power + 1], dp[i][power] + int(cur) * pw[power] ) print(min(dp[0]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input() dp = [10**18] * len(k) + [0] st = [100] * len(k) + [-1] for l in range(len(k) - 1, -1, -1): for r in range(l + 1, len(k) + 1): if int(k[l:r]) >= n or k[l] == "0" and r > l + 1: break dp[l], st[l] = min(dp[l], dp[r] + int(k[l:r]) * n ** (st[r] + 1)), min( st[l], st[r] + 1 ) print(dp[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·16^2 + 13·16^1 + 11·16^0). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k. -----Input----- The first line contains the integer n (2 ≤ n ≤ 10^9). The second line contains the integer k (0 ≤ k < 10^60), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n. Alexander guarantees that the answer exists and does not exceed 10^18. The number k doesn't contain leading zeros. -----Output----- Print the number x (0 ≤ x ≤ 10^18) — the answer to the problem. -----Examples----- Input 13 12 Output 12 Input 16 11311 Output 475 Input 20 999 Output 3789 Input 17 2016 Output 594 -----Note----- In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13^0 or 15 = 1·13^1 + 2·13^0.
n = int(input()) k = input().strip() power = [1] for i in range(20): power.append(power[-1] * 10) dp = [10**18] * len(k) for i in range(len(k)): for j in range(min(10, i + 1)): if k[i - j] == "0" and j > 0: continue s = int(k[i - j : i + 1]) if s >= n: break if i - j > 0: dp[i] = min(dp[i], dp[i - j - 1] * n + s) else: dp[i] = min(dp[i], s) print(dp[len(k) - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER
The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it? The discotheque can be seen as an infinite xy-plane, in which there are a total of n dancers. Once someone starts moving around, they will move only inside their own movement range, which is a circular area C_{i} described by a center (x_{i}, y_{i}) and a radius r_{i}. No two ranges' borders have more than one common point, that is for every pair (i, j) (1 ≤ i < j ≤ n) either ranges C_{i} and C_{j} are disjoint, or one of them is a subset of the other. Note that it's possible that two ranges' borders share a single common point, but no two dancers have exactly the same ranges. Tsukihi, being one of them, defines the spaciousness to be the area covered by an odd number of movement ranges of dancers who are moving. An example is shown below, with shaded regions representing the spaciousness if everyone moves at the same time. [Image] But no one keeps moving for the whole night after all, so the whole night's time is divided into two halves — before midnight and after midnight. Every dancer moves around in one half, while sitting down with friends in the other. The spaciousness of two halves are calculated separately and their sum should, of course, be as large as possible. The following figure shows an optimal solution to the example above. [Image] By different plans of who dances in the first half and who does in the other, different sums of spaciousness over two halves are achieved. You are to find the largest achievable value of this sum. -----Input----- The first line of input contains a positive integer n (1 ≤ n ≤ 1 000) — the number of dancers. The following n lines each describes a dancer: the i-th line among them contains three space-separated integers x_{i}, y_{i} and r_{i} ( - 10^6 ≤ x_{i}, y_{i} ≤ 10^6, 1 ≤ r_{i} ≤ 10^6), describing a circular movement range centered at (x_{i}, y_{i}) with radius r_{i}. -----Output----- Output one decimal number — the largest achievable sum of spaciousness over two halves of the night. The output is considered correct if it has a relative or absolute error of at most 10^{ - 9}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if $\frac{|a - b|}{\operatorname{max}(1,|b|)} \leq 10^{-9}$. -----Examples----- Input 5 2 1 6 0 4 1 2 -1 3 1 -2 1 4 -1 1 Output 138.23007676 Input 8 0 0 1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0 8 Output 289.02652413 -----Note----- The first sample corresponds to the illustrations in the legend.
n = int(input()) d = [1] * n p = [[] for i in range(n)] def f(): x, y, r = map(int, input().split()) return r * r, x, y t = sorted(f() for i in range(n)) for i in range(n): r, x, y = t[i] for j in range(i + 1, n): s, a, b = t[j] if (a - x) ** 2 + (b - y) ** 2 < s: p[j].append(i) d[i] = 0 break def f(i): s = t[i][0] q = [(1, j) for j in p[i]] while q: d, i = q.pop() s += d * t[i][0] q += [(-d, j) for j in p[i]] return s print(3.1415926536 * sum(f(i) for i in range(n) if d[i]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it? The discotheque can be seen as an infinite xy-plane, in which there are a total of n dancers. Once someone starts moving around, they will move only inside their own movement range, which is a circular area C_{i} described by a center (x_{i}, y_{i}) and a radius r_{i}. No two ranges' borders have more than one common point, that is for every pair (i, j) (1 ≤ i < j ≤ n) either ranges C_{i} and C_{j} are disjoint, or one of them is a subset of the other. Note that it's possible that two ranges' borders share a single common point, but no two dancers have exactly the same ranges. Tsukihi, being one of them, defines the spaciousness to be the area covered by an odd number of movement ranges of dancers who are moving. An example is shown below, with shaded regions representing the spaciousness if everyone moves at the same time. [Image] But no one keeps moving for the whole night after all, so the whole night's time is divided into two halves — before midnight and after midnight. Every dancer moves around in one half, while sitting down with friends in the other. The spaciousness of two halves are calculated separately and their sum should, of course, be as large as possible. The following figure shows an optimal solution to the example above. [Image] By different plans of who dances in the first half and who does in the other, different sums of spaciousness over two halves are achieved. You are to find the largest achievable value of this sum. -----Input----- The first line of input contains a positive integer n (1 ≤ n ≤ 1 000) — the number of dancers. The following n lines each describes a dancer: the i-th line among them contains three space-separated integers x_{i}, y_{i} and r_{i} ( - 10^6 ≤ x_{i}, y_{i} ≤ 10^6, 1 ≤ r_{i} ≤ 10^6), describing a circular movement range centered at (x_{i}, y_{i}) with radius r_{i}. -----Output----- Output one decimal number — the largest achievable sum of spaciousness over two halves of the night. The output is considered correct if it has a relative or absolute error of at most 10^{ - 9}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if $\frac{|a - b|}{\operatorname{max}(1,|b|)} \leq 10^{-9}$. -----Examples----- Input 5 2 1 6 0 4 1 2 -1 3 1 -2 1 4 -1 1 Output 138.23007676 Input 8 0 0 1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0 8 Output 289.02652413 -----Note----- The first sample corresponds to the illustrations in the legend.
import sys def inside(a, b): return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 < (a[2] + b[2]) ** 2 def main(): pi = 3.141592653589793 n = int(sys.stdin.readline()) a = [] p = [-1] * n for i in range(n): x, y, r = map(int, sys.stdin.readline().split()) a.append([x, y, r]) for i in range(n): for j in range(n): if i == j: continue if inside(a[i], a[j]): if a[i][2] < a[j][2]: if p[i] == -1: p[i] = j elif a[p[i]][2] > a[j][2]: p[i] = j elif p[j] == -1: p[j] = i elif a[p[j]][2] > a[i][2]: p[j] = i q = [] for i in range(n): if p[i] == -1: q.append((i, True)) s = len(q) ans = 0.0 for i in range(s): c, b = q[i] for j in range(n): if p[j] == c: q.append((j, True)) ans += pi * a[c][2] * a[c][2] q = q[s:] while len(q) != 0: c, b = q.pop() for j in range(n): if p[j] == c: q.append((j, not b)) if b: ans += pi * a[c][2] * a[c][2] else: ans -= pi * a[c][2] * a[c][2] print(ans) main()
IMPORT FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it? The discotheque can be seen as an infinite xy-plane, in which there are a total of n dancers. Once someone starts moving around, they will move only inside their own movement range, which is a circular area C_{i} described by a center (x_{i}, y_{i}) and a radius r_{i}. No two ranges' borders have more than one common point, that is for every pair (i, j) (1 ≤ i < j ≤ n) either ranges C_{i} and C_{j} are disjoint, or one of them is a subset of the other. Note that it's possible that two ranges' borders share a single common point, but no two dancers have exactly the same ranges. Tsukihi, being one of them, defines the spaciousness to be the area covered by an odd number of movement ranges of dancers who are moving. An example is shown below, with shaded regions representing the spaciousness if everyone moves at the same time. [Image] But no one keeps moving for the whole night after all, so the whole night's time is divided into two halves — before midnight and after midnight. Every dancer moves around in one half, while sitting down with friends in the other. The spaciousness of two halves are calculated separately and their sum should, of course, be as large as possible. The following figure shows an optimal solution to the example above. [Image] By different plans of who dances in the first half and who does in the other, different sums of spaciousness over two halves are achieved. You are to find the largest achievable value of this sum. -----Input----- The first line of input contains a positive integer n (1 ≤ n ≤ 1 000) — the number of dancers. The following n lines each describes a dancer: the i-th line among them contains three space-separated integers x_{i}, y_{i} and r_{i} ( - 10^6 ≤ x_{i}, y_{i} ≤ 10^6, 1 ≤ r_{i} ≤ 10^6), describing a circular movement range centered at (x_{i}, y_{i}) with radius r_{i}. -----Output----- Output one decimal number — the largest achievable sum of spaciousness over two halves of the night. The output is considered correct if it has a relative or absolute error of at most 10^{ - 9}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if $\frac{|a - b|}{\operatorname{max}(1,|b|)} \leq 10^{-9}$. -----Examples----- Input 5 2 1 6 0 4 1 2 -1 3 1 -2 1 4 -1 1 Output 138.23007676 Input 8 0 0 1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0 8 Output 289.02652413 -----Note----- The first sample corresponds to the illustrations in the legend.
s = 0 t = [list(map(int, input().split())) for i in range(int(input()))] f = ( lambda b: a[2] < b[2] and (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 <= (a[2] - b[2]) ** 2 ) for a in t: k = sum(f(b) for b in t) s += (-1, 1)[(k < 1) + k & 1] * a[2] ** 2 print(3.1415926536 * s)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() z = 0 o = 0 for i in s: if i == "1": o += 1 else: z += 1 for i in range(2**o, 2**n - 2**z + 1 + 1): print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
import sys input = sys.stdin.readline def readList(): return list(map(int, input().split())) def readInt(): return int(input()) def readInts(): return map(int, input().split()) def readStr(): return input().strip() def solve(): n = readInt() s = readStr() cnt = 0 for i in range(n): cnt += int(s[i]) ans = [i for i in range(pow(2, cnt), pow(2, n) - (pow(2, n - cnt) - 1) + 1)] return ans print(*solve())
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() for i in range(pow(2, s.count("1")), pow(2, n) - pow(2, s.count("0")) + 2): print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR STRING BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() start = 1 end = 2**n ones = s.count("1") zeros = s.count("0") start += 2**ones - 1 end -= 2**zeros - 1 for val in range(start, end + 1): print(val, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
t = int(input()) arr = input() i = 1 j = 1 r = 1 l = 1 for y in range(t): r *= 2 for x in arr: if x == "1": l += i i *= 2 if x == "0": r -= j j *= 2 for x in range(l, r + 1): print(x, end=" ")
ASSIGN VAR FUNC_CALL 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 VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR VAR NUMBER IF VAR STRING VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) t = 2**n s = input() cnt = 0 for i in range(len(s)): cnt += int(s[i]) if cnt == 0: print(1) else: left = 2**cnt ans = [] for i in range(1, t + 1): if i < left: continue sm = i - left bg = t - left - sm rem = cnt while rem > 0: if bg >= sm: sm = 0 break sm -= bg sm //= 2 rem -= 1 if sm == 0: ans.append(str(i)) print(" ".join(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) l = 1 r = int(2**n) s = input() nl = 0 nr = 0 for ch in s: if ch == "0": r = r - int(2**nr) nr += 1 else: l = l + int(2**nl) nl += 1 for i in range(l, r + 1): if i != r: print(i, end=" ") else: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
t = int(input()) a = input() w = a.count("1") y = len(a) - w for i in range(2**w, 2**t - 2**y + 2): print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
roll = int(input()) bin = input() bin = bin[::-1] working = [] if bin[0] == "1": working = [2, 2] else: working = [1, 1] for i in range(1, len(bin)): if bin[i] == "1": working = [2 * working[0], 2**i + working[1]] else: working = [working[0], 2 * working[1] - 1] blank = "" for i in range(working[0], working[1]): blank += str(i) + " " blank += str(working[1]) print(blank)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER STRING ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR LIST BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() arr = list(range(1, 2**n + 1)) l = 0 r = len(arr) - 1 r_change = 1 l_change = 1 for i in range(len(s)): if s[i] == "0": r -= r_change r_change *= 2 else: l += l_change l_change *= 2 print(*arr[l : r + 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() init = 2 ** s.count("1") total = 2**n fin = total - 2 ** s.count("0") arr = [] for i in range(init, fin + 2): arr.append(i) print(*arr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() l = [s.count("0"), s.count("1")] zeros = l[0] ones = l[1] start = 2**ones ending = 2**n - 2**zeros + 1 for i in range(start, ending + 1): print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() z = s.count("0") o = n - z res = list(range(pow(2, o), pow(2, n) + 2 - pow(2, z))) print(*res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() lo = 1 hi = 2**n count0 = 1 count1 = 1 for x in s: if x == "0": hi -= count0 count0 <<= 1 else: lo += count1 count1 <<= 1 for i in range(lo, hi + 1): print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() nz = 0 for j in range(0, n): if s[j] == "0": nz = nz + 1 n1 = n - nz x = pow(2, n1) y = pow(2, n) - pow(2, nz) + 1 for j in range(x, y + 1): print(j, end=" ") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) st = input() s = 0 for i in range(n): if st[i] == "1": s += 1 l = 2**s r = 2**n - 2 ** (n - s) + 2 out = "" for i in range(l, r): out += str(i) + " " print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
k = int(input()) max = 2**k s = str(input()) arr = [int(x) for x in s] sum = 0 for i in arr: sum += i start = 2**sum end = max - 2 ** (k - sum) + 1 m = start ans = [] while m <= end: ans.append(m) m += 1 result = " ".join(str(item) for item in ans) print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() ans = [] zer = s.count("0") one = s.count("1") c = 2**one d = 2**zer - 1 for i in range(1, 2**n + 1): if i >= c and i <= 2**n - d: ans.append(i) for j in ans: print(j, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() zs = s.count("0") os = s.count("1") [print(i, end=" ") for i in range(1, 2**n + 1) if 2**os - 1 < i <= 2**n - (2**zs - 1)]
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() a = list(map(int, s)) x = a.count(1) y = a.count(0) ans = [i for i in range(2**x, 2**n - 2**y + 2)] print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) a = input() k = a.count("1") t = pow(2, n) if k == 0: print(1) elif k == n: print(t) else: m1 = pow(2, k) min = 1 oraliq = 1 max = t for x in a: if x == "1": min += oraliq * t // 2 else: max -= oraliq oraliq *= 2 t //= 2 for x in range(m1, max + 1): print(x, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) string = input() c0 = 0 c1 = 0 for i in range(n): if string[i] == "0": c0 += 1 else: c1 += 1 a = 2**c1 - 1 b = 2**n - (2**c0 - 1) for j in range(a + 1, b + 1): print(j, end=" ") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
$2^n$ teams participate in a playoff tournament. The tournament consists of $2^n - 1$ games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team $1$ plays against team $2$, team $3$ plays against team $4$, and so on (so, $2^{n-1}$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $2^{n-1}$ teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where $2^{n-2}$ games are played: in the first one of them, the winner of the game "$1$ vs $2$" plays against the winner of the game "$3$ vs $4$", then the winner of the game "$5$ vs $6$" plays against the winner of the game "$7$ vs $8$", and so on. This process repeats until only one team remains. The skill level of the $i$-th team is $p_i$, where $p$ is a permutation of integers $1$, $2$, ..., $2^n$ (a permutation is an array where each element from $1$ to $2^n$ occurs exactly once). You are given a string $s$ which consists of $n$ characters. These characters denote the results of games in each phase of the tournament as follows: if $s_i$ is equal to 0, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the lower skill level wins; if $s_i$ is equal to 1, then during the $i$-th phase (the phase with $2^{n-i}$ games), in each match, the team with the higher skill level wins. Let's say that an integer $x$ is winning if it is possible to find a permutation $p$ such that the team with skill $x$ wins the tournament. Find all winning integers. -----Input----- The first line contains one integer $n$ ($1 \le n \le 18$). The second line contains the string $s$ of length $n$ consisting of the characters 0 and/or 1. -----Output----- Print all the winning integers $x$ in ascending order. -----Examples----- Input 3 101 Output 4 5 6 7 Input 1 1 Output 2 Input 2 01 Output 2 3 -----Note----- None
n = int(input()) s = input() k = s.count("1") if k == n: print(pow(2, n)) exit(0) kk = s.count("0") be = 1 << k f = [0] * 22 f[0] = 1 for i in range(1, 22): f[i] = f[i - 1] * 2 for i in range(1, n): f[i] = f[i - 1] + f[i] en = pow(2, n) - f[kk - 1] for i in range(be, en + 1): print(i, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING